OfficeToPDF

Sample Java code for using Apryse 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. Learn more about our Android SDK and Office Document Conversion Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import android.content.Context;
9
10import com.pdftron.android.pdfnetsdksamples.OutputListener;
11import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
12import com.pdftron.android.pdfnetsdksamples.R;
13import com.pdftron.android.pdfnetsdksamples.util.Utils;
14import com.pdftron.common.PDFNetException;
15import com.pdftron.pdf.Convert;
16import com.pdftron.pdf.DocumentConversion;
17import com.pdftron.pdf.OfficeToPDFOptions;
18import com.pdftron.pdf.PDFDoc;
19import com.pdftron.pdf.PDFNet;
20import com.pdftron.sdf.SDFDoc;
21
22import java.io.File;
23import java.util.ArrayList;
24
25/**
26 * The following sample illustrates how to use the PDF.Convert utility class to convert
27 * .docx files to PDF
28 * <p>
29 * This conversion is performed entirely within the PDFNet and has *no* external or
30 * system dependencies dependencies -- Conversion results will be the sam whether
31 * on Windows, Linux or Android.
32 * <p>
33 * Please contact us if you have any questions.
34 */
35public class OfficeToPDFTest extends PDFNetSample {
36
37 private static OutputListener mOutputListener;
38
39 private static ArrayList<String> mFileList = new ArrayList<>();
40
41 private static String sLayoutSmartPluginPath;
42
43 public OfficeToPDFTest(Context context) {
44 try {
45 String layoutPluginPath = Utils.copyResourceToTempFolder(context, R.raw.pdftron_layout_resources, false, "pdftron_layout_resources.plugin");
46 PDFNet.addResourceSearchPath(layoutPluginPath);
47 sLayoutSmartPluginPath = Utils.copyResourceToTempFolder(context, R.raw.pdftron_smart_substitution, false, "pdftron_smart_substitution.plugin");
48 PDFNet.addResourceSearchPath(sLayoutSmartPluginPath);
49 } catch (Exception e) {
50 mOutputListener.printError(e.getStackTrace());
51 }
52
53 setTitle(R.string.sample_officetopdf_title);
54 setDescription(R.string.sample_officetopdf_description);
55 }
56
57 @Override
58 public void run(OutputListener outputListener) {
59 super.run(outputListener);
60 mOutputListener = outputListener;
61 mFileList.clear();
62 printHeader(outputListener);
63
64
65 // first the one-line conversion interface
66 simpleDocxConvert("Fishermen.docx", "Fishermen.pdf");
67
68 // then the more flexible line-by-line interface
69 flexibleDocxConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf");
70
71 // conversion of RTL content
72 flexibleDocxConvert("factsheet_Arabic.docx", "factsheet_Arabic.pdf");
73
74 for (String file : mFileList) {
75 addToFileList(file);
76 }
77 printFooter(outputListener);
78 }
79
80
81 public static void simpleDocxConvert(String inputFilename, String outputFilename) {
82 try (PDFDoc doc = new PDFDoc()) {
83
84 // perform the conversion with no optional parameters
85 PDFDoc pdfdoc = new PDFDoc();
86 Convert.officeToPdf(pdfdoc, Utils.getAssetTempFile(INPUT_PATH + inputFilename).getAbsolutePath(), null);
87
88 // save the result
89 pdfdoc.save(Utils.createExternalFile(outputFilename, mFileList).getAbsolutePath(), SDFDoc.SaveMode.INCREMENTAL, null);
90
91 // And we're done!
92 mOutputListener.println("Done conversion " + Utils.createExternalFile(outputFilename, mFileList).getAbsolutePath());
93 } catch (PDFNetException e) {
94 mOutputListener.println("Unable to convert MS Office document, error:");
95 mOutputListener.printError(e.getStackTrace());
96 mOutputListener.printError(e.getStackTrace());
97 }
98 }
99
100 public static void flexibleDocxConvert(String inputFilename, String outputFilename) {
101 try {
102 OfficeToPDFOptions options = new OfficeToPDFOptions();
103 options.setSmartSubstitutionPluginPath(sLayoutSmartPluginPath);
104
105 // create a conversion object -- this sets things up but does not yet
106 // perform any conversion logic.
107 // in a multithreaded environment, this object can be used to monitor
108 // the conversion progress and potentially cancel it as well
109 DocumentConversion conversion = Convert.streamingPdfConversion(
110 Utils.getAssetTempFile(INPUT_PATH + inputFilename).getAbsolutePath(), options);
111
112 mOutputListener.println(inputFilename + ": " + Math.round(conversion.getProgress() * 100.0)
113 + "% " + conversion.getProgressLabel());
114
115 // actually perform the conversion
116 while (conversion.getConversionStatus() == DocumentConversion.e_incomplete) {
117 conversion.convertNextPage();
118 mOutputListener.println(inputFilename + ": " + Math.round(conversion.getProgress() * 100.0)
119 + "% " + conversion.getProgressLabel());
120 }
121
122 if (conversion.tryConvert() == DocumentConversion.e_success) {
123 int num_warnings = conversion.getNumWarnings();
124
125 // print information about the conversion
126 for (int i = 0; i < num_warnings; ++i) {
127 mOutputListener.println("Warning: " + conversion.getWarningString(i));
128 }
129
130 // save the result
131 try (PDFDoc doc = conversion.getDoc()) {
132 doc.save(Utils.createExternalFile(outputFilename, mFileList).getAbsolutePath(), SDFDoc.SaveMode.INCREMENTAL, null);
133 }
134
135 // done
136 mOutputListener.println("Done conversion " + Utils.createExternalFile(outputFilename, mFileList).getAbsolutePath());
137 } else {
138 mOutputListener.println("Encountered an error during conversion: " + conversion.getErrorString());
139 }
140 } catch (PDFNetException e) {
141 mOutputListener.println("Unable to convert MS Office document, error:");
142 mOutputListener.printError(e.getStackTrace());
143 mOutputListener.printError(e.getStackTrace());
144 }
145 }
146
147}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales