DOCX, XSLX to PDF Conversion - OfficeToPDF - Java Sample Code

Sample code for using Apryse Server SDK to convert Office documents to PDF (including Word, Excel, PowerPoint and Publisher) without needing any external dependencies or MS Office licenses. Office to PDF conversion can be performed on a Linux or Windows server to automate Office-centric workflows, or entirely in the user's client (web browser, mobile device). The conversion functionality can be combined with our Viewer to display or annotate Office files (docx, xlsx, pptx) on all major platforms, including Web, Android, iOS, Xamarin, UWP, and Windows. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB.

Learn more about our Server SDK and Office Document Conversion Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.common.PDFNetException;
7import com.pdftron.pdf.Convert;
8import com.pdftron.pdf.DocumentConversion;
9import com.pdftron.pdf.PDFDoc;
10import com.pdftron.pdf.PDFNet;
11import com.pdftron.pdf.OfficeToPDFOptions;
12import com.pdftron.sdf.SDFDoc;
13
14//---------------------------------------------------------------------------------------
15// The following sample illustrates how to use the PDF.Convert utility class to convert
16// MS Office files to PDF
17//
18// This conversion is performed entirely within the PDFNet and has *no* external or
19// system dependencies dependencies -- Conversion results will be the same whether
20// on Windows, Linux or Android.
21//
22// Please contact us if you have any questions.
23//---------------------------------------------------------------------------------------
24public class OfficeToPDFTest {
25
26 static String input_path = "../../TestFiles/";
27 static String output_path = "../../TestFiles/Output/";
28
29 public static void main(String[] args) {
30 PDFNet.initialize(PDFTronLicense.Key());
31 PDFNet.setResourcesPath("../../../Resources");
32
33 // first the one-line conversion interface
34 simpleDocxConvert("Fishermen.docx", "Fishermen.pdf");
35
36 // then the more flexible line-by-line interface
37 flexibleDocxConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf");
38
39 // conversion of RTL content
40 flexibleDocxConvert("factsheet_Arabic.docx", "factsheet_Arabic.pdf");
41
42 PDFNet.terminate();
43 }
44
45 public static void simpleDocxConvert(String inputFilename, String outputFilename) {
46 try (PDFDoc pdfdoc = new PDFDoc()) {
47
48 // perform the conversion with no optional parameters
49 Convert.officeToPdf(pdfdoc, input_path + inputFilename, null);
50
51 // save the result
52 pdfdoc.save(output_path + outputFilename, SDFDoc.SaveMode.INCREMENTAL, null);
53 // output PDF pdfdoc
54
55 // And we're done!
56 System.out.println("Done conversion " + output_path + outputFilename);
57 } catch (PDFNetException e) {
58 System.out.println("Unable to convert MS Office document, error:");
59 e.printStackTrace();
60 System.out.println(e);
61 }
62 }
63
64 public static void flexibleDocxConvert(String inputFilename, String outputFilename) {
65 try {
66 OfficeToPDFOptions options = new OfficeToPDFOptions();
67 options.setSmartSubstitutionPluginPath(input_path);
68
69 // create a conversion object -- this sets things up but does not yet
70 // perform any conversion logic.
71 // in a multithreaded environment, this object can be used to monitor
72 // the conversion progress and potentially cancel it as well
73 DocumentConversion conversion = Convert.streamingPdfConversion(
74 input_path + inputFilename, options);
75
76 System.out.println(inputFilename + ": " + Math.round(conversion.getProgress() * 100.0)
77 + "% " + conversion.getProgressLabel());
78
79 // actually perform the conversion
80 while (conversion.getConversionStatus() == DocumentConversion.e_incomplete) {
81 conversion.convertNextPage();
82 System.out.println(inputFilename + ": " + Math.round(conversion.getProgress() * 100.0)
83 + "% " + conversion.getProgressLabel());
84 }
85
86 if (conversion.tryConvert() == DocumentConversion.e_success) {
87 int num_warnings = conversion.getNumWarnings();
88
89 // print information about the conversion
90 for (int i = 0; i < num_warnings; ++i) {
91 System.out.println("Warning: " + conversion.getWarningString(i));
92 }
93
94 // save the result
95 try (PDFDoc doc = conversion.getDoc()) {
96 doc.save(output_path + outputFilename, SDFDoc.SaveMode.INCREMENTAL, null);
97 }
98
99 // done
100 System.out.println("Done conversion " + output_path + outputFilename);
101 } else {
102 System.out.println("Encountered an error during conversion: " + conversion.getErrorString());
103 }
104 } catch (PDFNetException e) {
105 System.out.println("Unable to convert MS Office document, error:");
106 e.printStackTrace();
107 System.out.println(e);
108 }
109 }
110
111}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales