Convert

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales