Convert Between File Types - PDF, SVG, XPS, SVG, TIFF, PNG, JPEG

Sample code to use Apryse Server SDK for direct, high-quality conversion between PDF, XPS, SVG, TIFF, PNG, JPEG, and other image formats ('pdftron.PDF.Convert' namespace); provided in Python, C++, C#, Java, JavaScript, PHP, Ruby, Go and VB. The sample also shows how to convert MS Office files using our built in conversion. Learn more about our Server SDK and PDF 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.*;
8import com.pdftron.sdf.Obj;
9import com.pdftron.sdf.ObjSet;
10import com.pdftron.sdf.SDFDoc;
11import java.util.ArrayList;
12//---------------------------------------------------------------------------------------
13// The following sample illustrates how to use the PDF::Convert utility class to convert
14// documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
15// using our built in conversion.
16//
17// Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
18// converted to PDF or XPS.
19//
20// Please contact us if you have any questions.
21//---------------------------------------------------------------------------------------
22class Testfile
23{
24 public String inputFile, outputFile;
25 public Testfile(String inFile, String outFile)
26 {
27 inputFile = inFile;
28 outputFile = outFile;
29 }
30}
31
32public class ConvertTest
33{
34 // Relative path to the folder containing test files.
35 static String inputPath = "../../TestFiles/";
36 static String outputPath = "../../TestFiles/Output/";
37
38 static boolean ConvertSpecificFormats()
39 {
40 //////////////////////////////////////////////////////////////////////////
41 boolean err = false;
42 try (PDFDoc pdfdoc = new PDFDoc())
43 {
44
45 System.out.println("Converting from XPS");
46
47 Convert.fromXps(pdfdoc, inputPath + "simple-xps.xps");
48 pdfdoc.save(outputPath + "xps2pdf v2.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
49 System.out.println("Saved xps2pdf v2.pdf");
50 }
51 catch (PDFNetException e)
52 {
53 System.out.println(e);
54 err = true;
55 }
56
57 //////////////////////////////////////////////////////////////////////////
58 try (PDFDoc pdfdoc = new PDFDoc())
59 {
60 // add a dictionary
61 ObjSet set = new ObjSet();
62 Obj options = set.createDict();
63
64 // Put options
65 options.putNumber("FontSize", 15);
66 options.putBool("UseSourceCodeFormatting", true);
67 options.putNumber("PageWidth", 12);
68 options.putNumber("PageHeight", 6);
69
70 // Convert from .txt file
71 System.out.println("Converting from txt");
72 Convert.fromText(pdfdoc, inputPath + "simple-text.txt", options);
73 pdfdoc.save(outputPath + "simple-text.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
74 System.out.println("Saved simple-text.pdf");
75 }
76 catch (PDFNetException e)
77 {
78 System.out.println(e);
79 err = true;
80 }
81
82 //////////////////////////////////////////////////////////////////////////
83 try (PDFDoc pdfdoc = new PDFDoc(inputPath + "newsletter.pdf"))
84 {
85 // Convert PDF document to SVG
86 System.out.println("Converting pdfdoc to SVG");
87 Convert.toSvg(pdfdoc, outputPath + "pdf2svg v2.svg");
88 System.out.println("Saved pdf2svg v2.svg");
89 }
90 catch (PDFNetException e)
91 {
92 System.out.println(e);
93 err = true;
94 }
95
96 //////////////////////////////////////////////////////////////////////////
97 try
98 {
99 // Convert PNG image to XPS
100 System.out.println("Converting PNG to XPS");
101 Convert.toXps(inputPath + "butterfly.png", outputPath + "butterfly.xps");
102 System.out.println("Saved butterfly.xps");
103 }
104 catch (PDFNetException e)
105 {
106 System.out.println(e);
107 err = true;
108 }
109
110 //////////////////////////////////////////////////////////////////////////
111 try
112 {
113 // Convert PDF document to XPS
114 System.out.println("Converting PDF to XPS");
115 Convert.toXps(inputPath + "newsletter.pdf", outputPath + "newsletter.xps");
116 System.out.println("Saved newsletter.xps");
117 }
118 catch (PDFNetException e)
119 {
120 System.out.println(e);
121 err = true;
122 }
123
124 //////////////////////////////////////////////////////////////////////////
125 try
126 {
127 // Convert PDF document to HTML
128 System.out.println("Converting PDF to HTML");
129 Convert.toHtml(inputPath + "newsletter.pdf", outputPath + "newsletter");
130 System.out.println("Saved newsletter as HTML");
131 }
132 catch (PDFNetException e)
133 {
134 System.out.println(e);
135 err = true;
136 }
137
138 //////////////////////////////////////////////////////////////////////////
139 try
140 {
141 // Convert PDF document to EPUB
142 System.out.println("Converting PDF to EPUB");
143 Convert.toEpub(inputPath + "newsletter.pdf", outputPath + "newsletter.epub");
144 System.out.println("Saved newsletter.epub");
145 }
146 catch (PDFNetException e)
147 {
148 System.out.println(e);
149 err = true;
150 }
151
152 //////////////////////////////////////////////////////////////////////////
153 try
154 {
155 // Convert PDF document to multipage TIFF
156 System.out.println("Converting PDF to multipage TIFF");
157 Convert.TiffOutputOptions tiff_options = new Convert.TiffOutputOptions();
158 tiff_options.setDPI(200);
159 tiff_options.setDither(true);
160 tiff_options.setMono(true);
161 Convert.toTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options);
162 System.out.println("Saved newsletter.tiff");
163 }
164 catch (PDFNetException e)
165 {
166 System.out.println(e);
167 err = true;
168 }
169
170 //////////////////////////////////////////////////////////////////////////
171 try (PDFDoc pdfdoc = new PDFDoc())
172 {
173 // Convert SVG file to PDF
174 System.out.println("Converting SVG to PDF");
175 Convert.fromSVG(pdfdoc, inputPath + "tiger.svg", null);
176 pdfdoc.save(outputPath + "svg2pdf.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
177
178 System.out.println("Saved svg2pdf.pdf");
179 }
180 catch (PDFNetException e)
181 {
182 System.out.println(e);
183 err = true;
184 }
185
186 return err;
187 }
188
189 static boolean ConvertToPdfFromFile()
190 {
191 ArrayList<Testfile> testfiles = new ArrayList<Testfile>();
192 testfiles.add(new Testfile("simple-word_2007.docx", "docx2pdf.pdf"));
193 testfiles.add(new Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf"));
194 testfiles.add(new Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf"));
195 testfiles.add(new Testfile("simple-text.txt", "txt2pdf.pdf"));
196 testfiles.add(new Testfile("butterfly.png", "png2pdf.pdf"));
197 testfiles.add(new Testfile("simple-xps.xps", "xps2pdf.pdf"));
198
199 boolean err = false;
200
201 for (Testfile file : testfiles)
202 {
203 try (PDFDoc pdfdoc = new PDFDoc())
204 {
205 //use built in converter
206 ConvertPrinter.setMode(ConvertPrinter.e_convert_printer_prefer_builtin_converter);
207 Convert.toPdf(pdfdoc, inputPath + file.inputFile);
208 pdfdoc.save(outputPath + file.outputFile, SDFDoc.SaveMode.LINEARIZED, null);
209 System.out.println("Converted file: " + file.inputFile);
210 System.out.println("to: " + file.outputFile);
211 }
212 catch (PDFNetException e)
213 {
214 System.out.println("ERROR: on input file " + file.inputFile);
215 System.out.println(e);
216 err = true;
217 }
218 }
219
220 return err;
221 }
222
223 /// <summary>
224 /// The main entry point for the application.
225 /// </summary>
226 public static void main(String[] args)
227 {
228 PDFNet.initialize(PDFTronLicense.Key());
229 boolean err = false;
230
231 err = ConvertToPdfFromFile();
232 if (err)
233 {
234 System.out.println("ConvertFile failed");
235 }
236 else
237 {
238 System.out.println("ConvertFile succeeded");
239 }
240
241 err = ConvertSpecificFormats();
242 if (err)
243 {
244 System.out.println("ConvertSpecificFormats failed");
245 }
246 else
247 {
248 System.out.println("ConvertSpecificFormats succeeded");
249 }
250
251 System.out.println("Done.");
252
253 PDFNet.terminate();
254 }
255
256}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales