Sample Java code to use Apryse SDK for direct, high-quality conversion between PDF, XPS, SVG, TIFF, PNG, JPEG, and other image formats ('pdftron.PDF.Convert' namespace). The sample also shows how to convert MS Office files using our built in conversion. Learn more about our Android SDK and PDF 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 com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.common.PDFNetException;
13import com.pdftron.pdf.Convert;
14import com.pdftron.pdf.PDFDoc;
15import com.pdftron.sdf.Obj;
16import com.pdftron.sdf.ObjSet;
17import com.pdftron.sdf.SDFDoc;
18
19import java.util.ArrayList;
20
21public class ConvertTest extends PDFNetSample {
22
23 private static OutputListener mOutputListener;
24
25 private static ArrayList<String> mFileList = new ArrayList<>();
26
27 public ConvertTest() {
28 setTitle(R.string.sample_convert_title);
29 setDescription(R.string.sample_convert_description);
30 }
31
32 @Override
33 public void run(OutputListener outputListener) {
34 super.run(outputListener);
35 mOutputListener = outputListener;
36 mFileList.clear();
37 printHeader(outputListener);
38
39 String outputFile;
40
41 mOutputListener.println("-------------------------------------------------");
42
43 try {
44 mOutputListener.println("Converting Text to PDF with options");
45 ObjSet objset = new ObjSet();
46 Obj options = objset.createDict();
47 options.putNumber("FontSize", 15);
48 options.putBool("UseSourceCodeFormatting", true);
49 options.putNumber("PageWidth", 12);
50 options.putNumber("PageHeight", 6);
51 PDFDoc doc = new PDFDoc();
52 outputFile = Utils.createExternalFile("simple-text.pdf", mFileList).getAbsolutePath();
53 Convert.fromText(doc, Utils.getAssetTempFile(INPUT_PATH + "simple-text.txt").getAbsolutePath(), options);
54 doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null);
55 mOutputListener.println("Result saved in " + outputFile);
56 doc.close();
57 } catch (PDFNetException e) {
58 mOutputListener.println("Unable to convert Plain Text document to PDF, error:");
59 mOutputListener.printError(e.getStackTrace());
60 }
61
62 // Convert the XPS document to PDF
63 try {
64 mOutputListener.println("Converting XPS document to PDF");
65 PDFDoc doc = new PDFDoc();
66 Convert.fromXps(doc, Utils.getAssetTempFile(INPUT_PATH + "simple-xps.xps").getAbsolutePath());
67 outputFile = Utils.createExternalFile("xps2pdf_Java.pdf", mFileList).getAbsolutePath();
68 doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null);
69 mOutputListener.println("Result saved in " + outputFile);
70 doc.close();
71 } catch (PDFNetException e) {
72 mOutputListener.println("Unable to convert XPS document to PDF, error:");
73 mOutputListener.printError(e.getStackTrace());
74 }
75
76 // Convert the PDF document to SVG
77 try {
78 mOutputListener.println("Converting XPS document to SVG");
79 PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "tiger.pdf").getAbsolutePath());
80 Convert.fromXps(doc, Utils.getAssetTempFile(INPUT_PATH + "simple-xps.xps").getAbsolutePath());
81 mOutputListener.println("Converting PDF document to SVG");
82 outputFile = Utils.createExternalFile("pdf2svg_Java.svg", mFileList).getAbsolutePath();
83 Convert.toSvg(doc, outputFile);
84 mOutputListener.println("Result saved in " + outputFile);
85 doc.close();
86 } catch (PDFNetException e) {
87 mOutputListener.println("Unable to convert PDF document to SVG, error:");
88 mOutputListener.printError(e.getStackTrace());
89 }
90
91 // Convert the PDF document to XPS
92 try {
93 mOutputListener.println("Converting PDF document to XPS");
94 PDFDoc doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath());
95 outputFile = Utils.createExternalFile("pdf2xps_Java.xps", mFileList).getAbsolutePath();
96 Convert.toXps(doc, outputFile);
97 mOutputListener.println("Result saved in " + outputFile);
98 doc.close();
99 } catch (PDFNetException e) {
100 mOutputListener.println("Unable to convert PDF document to SVG, error:");
101 mOutputListener.printError(e.getStackTrace());
102 }
103
104 // Convert an image to PDF using internal converter
105 try {
106 mOutputListener.println("Converting PNG image to PDF");
107 PDFDoc doc = new PDFDoc();
108 Convert.toPdf(doc, Utils.getAssetTempFile(INPUT_PATH + "butterfly.png").getAbsolutePath());
109 outputFile = Utils.createExternalFile("png2pdf_Java.pdf", mFileList).getAbsolutePath();
110 doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null);
111 mOutputListener.println("Result saved in " + outputFile);
112 doc.close();
113 } catch (PDFNetException e) {
114 mOutputListener.println("Unable to convert PNG image to XPS, error:");
115 mOutputListener.printError(e.getStackTrace());
116 }
117
118 // Convert an image to XPS using internal converter
119 try {
120 mOutputListener.println("Converting PNG image to XPS");
121 outputFile = Utils.createExternalFile("buttefly_Java.xps", mFileList).getAbsolutePath();
122 Convert.toXps(Utils.getAssetTempFile(INPUT_PATH + "butterfly.png").getAbsolutePath(), outputFile);
123 mOutputListener.println("Result saved in " + outputFile);
124 } catch (PDFNetException e) {
125 mOutputListener.println("Unable to convert PNG image to XPS, error:");
126 mOutputListener.printError(e.getStackTrace());
127 }
128
129 // Convert a PDF document directly to XPS
130 try {
131 mOutputListener.println("Converting PDF to XPS");
132 outputFile = Utils.createExternalFile("newsletter.xps", mFileList).getAbsolutePath();
133 Convert.toXps(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath(), outputFile);
134 mOutputListener.println("Result saved in " + outputFile);
135 } catch (PDFNetException e) {
136 mOutputListener.println("Unable to convert PDF document to XPS, error:");
137 mOutputListener.printError(e.getStackTrace());
138 }
139
140 // Convert a PDF document to HTML
141 try {
142 mOutputListener.println("Converting PDF to HTML");
143 outputFile = Utils.createExternalFile("newsletter", mFileList).getAbsolutePath();
144 Convert.toHtml(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath(), outputFile);
145 mOutputListener.println("Result saved in " + outputFile);
146 } catch (PDFNetException e) {
147 mOutputListener.println("Unable to convert PDF document to HTML, error:");
148 mOutputListener.printError(e.getStackTrace());
149 }
150
151 // Convert a PDF document to EPUB
152 try {
153 mOutputListener.println("Converting PDF to EPUB");
154 outputFile = Utils.createExternalFile("newsletter.epub", mFileList).getAbsolutePath();
155 Convert.toEpub(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath(), outputFile);
156 mOutputListener.println("Result saved in " + outputFile);
157 } catch (PDFNetException e) {
158 mOutputListener.println("Unable to convert PDF document to EPUB, error:");
159 mOutputListener.printError(e.getStackTrace());
160 }
161
162 // Convert a PDF document to multipage TIFF
163 try {
164 mOutputListener.println("Converting PDF to multipage TIFF");
165 outputFile = Utils.createExternalFile("newsletter.tiff", mFileList).getAbsolutePath();
166 Convert.TiffOutputOptions tiff_options = new Convert.TiffOutputOptions();
167 tiff_options.setDPI(200);
168 tiff_options.setMono(true);
169 tiff_options.setDither(true);
170 Convert.toTiff(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath(), outputFile, tiff_options);
171 mOutputListener.println("Result saved in " + outputFile);
172 } catch (PDFNetException e) {
173 mOutputListener.println("Unable to convert PDF document to TIFF, error:");
174 mOutputListener.printError(e.getStackTrace());
175 }
176
177 mOutputListener.println("Done.");
178
179 for (String file : mFileList) {
180 addToFileList(file);
181 }
182 printFooter(outputListener);
183 }
184}
1package com.pdftron.android.pdfnetsdksamples.samples
2
3import com.pdftron.android.pdfnetsdksamples.OutputListener
4import com.pdftron.android.pdfnetsdksamples.PDFNetSample
5import com.pdftron.android.pdfnetsdksamples.R
6import com.pdftron.android.pdfnetsdksamples.util.Utils
7import com.pdftron.common.PDFNetException
8import com.pdftron.pdf.Convert
9import com.pdftron.pdf.PDFDoc
10import com.pdftron.sdf.ObjSet
11import com.pdftron.sdf.SDFDoc
12import java.util.ArrayList
13
14class ConvertTest : PDFNetSample() {
15 init {
16 setTitle(R.string.sample_convert_title)
17 setDescription(R.string.sample_convert_description)
18 }
19
20 override fun run(outputListener: OutputListener?) {
21 super.run(outputListener)
22 mOutputListener = outputListener
23 mFileList.clear()
24 printHeader(outputListener!!)
25
26 var outputFile: String
27
28 mOutputListener!!.println("-------------------------------------------------")
29
30 try {
31 mOutputListener!!.println("Converting Text to PDF with options")
32 val objset = ObjSet()
33 val options = objset.createDict()
34 options.putNumber("FontSize", 15.0)
35 options.putBool("UseSourceCodeFormatting", true)
36 options.putNumber("PageWidth", 12.0)
37 options.putNumber("PageHeight", 6.0)
38 val doc = PDFDoc()
39 outputFile = Utils.createExternalFile("simple-text.pdf", mFileList).absolutePath
40 Convert.fromText(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "simple-text.txt")!!.absolutePath, options)
41 doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null)
42 mOutputListener!!.println("Result saved in $outputFile")
43 doc.close()
44 } catch (e: PDFNetException) {
45 mOutputListener!!.println("Unable to convert Plain Text document to PDF, error:")
46 mOutputListener!!.printError(e.stackTrace)
47 }
48
49 // Convert the XPS document to PDF
50 try {
51 mOutputListener!!.println("Converting XPS document to PDF")
52 val doc = PDFDoc()
53 Convert.fromXps(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "simple-xps.xps")!!.absolutePath)
54 outputFile = Utils.createExternalFile("xps2pdf_Java.pdf", mFileList).absolutePath
55 doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null)
56 mOutputListener!!.println("Result saved in $outputFile")
57 doc.close()
58 } catch (e: PDFNetException) {
59 mOutputListener!!.println("Unable to convert XPS document to PDF, error:")
60 mOutputListener!!.printError(e.stackTrace)
61 }
62
63 // Convert the PDF document to SVG
64 try {
65 mOutputListener!!.println("Converting XPS document to SVG")
66 val doc = PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "tiger.pdf")!!.absolutePath)
67 Convert.fromXps(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "simple-xps.xps")!!.absolutePath)
68 mOutputListener!!.println("Converting PDF document to SVG")
69 outputFile = Utils.createExternalFile("pdf2svg_Java.svg", mFileList).absolutePath
70 Convert.toSvg(doc, outputFile)
71 mOutputListener!!.println("Result saved in $outputFile")
72 doc.close()
73 } catch (e: PDFNetException) {
74 mOutputListener!!.println("Unable to convert PDF document to SVG, error:")
75 mOutputListener!!.printError(e.stackTrace)
76 }
77
78 // Convert the PDF document to XPS
79 try {
80 mOutputListener!!.println("Converting PDF document to XPS")
81 val doc = PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf")!!.absolutePath)
82 outputFile = Utils.createExternalFile("pdf2xps_Java.xps", mFileList).absolutePath
83 Convert.toXps(doc, outputFile)
84 mOutputListener!!.println("Result saved in $outputFile")
85 doc.close()
86 } catch (e: PDFNetException) {
87 mOutputListener!!.println("Unable to convert PDF document to SVG, error:")
88 mOutputListener!!.printError(e.stackTrace)
89 }
90
91 // Convert an image to PDF using internal converter
92 try {
93 mOutputListener!!.println("Converting PNG image to PDF")
94 val doc = PDFDoc()
95 Convert.toPdf(doc, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "butterfly.png")!!.absolutePath)
96 outputFile = Utils.createExternalFile("png2pdf_Java.pdf", mFileList).absolutePath
97 doc.save(outputFile, SDFDoc.SaveMode.LINEARIZED, null)
98 mOutputListener!!.println("Result saved in $outputFile")
99 doc.close()
100 } catch (e: PDFNetException) {
101 mOutputListener!!.println("Unable to convert PNG image to XPS, error:")
102 mOutputListener!!.printError(e.stackTrace)
103 }
104
105 // Convert an image to XPS using internal converter
106 try {
107 mOutputListener!!.println("Converting PNG image to XPS")
108 outputFile = Utils.createExternalFile("buttefly_Java.xps", mFileList).absolutePath
109 Convert.toXps(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "butterfly.png")!!.absolutePath, outputFile)
110 mOutputListener!!.println("Result saved in $outputFile")
111 } catch (e: PDFNetException) {
112 mOutputListener!!.println("Unable to convert PNG image to XPS, error:")
113 mOutputListener!!.printError(e.stackTrace)
114 }
115
116 // Convert a PDF document directly to XPS
117 try {
118 mOutputListener!!.println("Converting PDF to XPS")
119 outputFile = Utils.createExternalFile("newsletter.xps", mFileList).absolutePath
120 Convert.toXps(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath, outputFile)
121 mOutputListener!!.println("Result saved in $outputFile")
122 } catch (e: PDFNetException) {
123 mOutputListener!!.println("Unable to convert PDF document to XPS, error:")
124 mOutputListener!!.printError(e.stackTrace)
125 }
126
127 // Convert a PDF document to HTML
128 try {
129 mOutputListener!!.println("Converting PDF to HTML")
130 outputFile = Utils.createExternalFile("newsletter", mFileList).absolutePath
131 Convert.toHtml(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath, outputFile)
132 mOutputListener!!.println("Result saved in $outputFile")
133 } catch (e: PDFNetException) {
134 mOutputListener!!.println("Unable to convert PDF document to HTML, error:")
135 mOutputListener!!.printError(e.stackTrace)
136 }
137
138 // Convert a PDF document to EPUB
139 try {
140 mOutputListener!!.println("Converting PDF to EPUB")
141 outputFile = Utils.createExternalFile("newsletter.epub", mFileList).absolutePath
142 Convert.toEpub(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath, outputFile)
143 mOutputListener!!.println("Result saved in $outputFile")
144 } catch (e: PDFNetException) {
145 mOutputListener!!.println("Unable to convert PDF document to EPUB, error:")
146 mOutputListener!!.printError(e.stackTrace)
147 }
148
149 // Convert a PDF document to multipage TIFF
150 try {
151 mOutputListener!!.println("Converting PDF to multipage TIFF")
152 outputFile = Utils.createExternalFile("newsletter.tiff", mFileList).absolutePath
153 val tiff_options = Convert.TiffOutputOptions()
154 tiff_options.setDPI(200.0)
155 tiff_options.setMono(true)
156 tiff_options.setDither(true)
157 Convert.toTiff(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath, outputFile, tiff_options)
158 mOutputListener!!.println("Result saved in $outputFile")
159 } catch (e: PDFNetException) {
160 mOutputListener!!.println("Unable to convert PDF document to TIFF, error:")
161 mOutputListener!!.printError(e.stackTrace)
162 }
163
164 mOutputListener!!.println("Done.")
165
166 for (file in mFileList) {
167 addToFileList(file)
168 }
169 printFooter(outputListener)
170 }
171
172 companion object {
173
174 private var mOutputListener: OutputListener? = null
175
176 private val mFileList = ArrayList<String>()
177 }
178}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales