Convert File Types with Virtual Printer on Windows - Java Sample Code

Sample code to convert to PDF with virtual printer on Windows; provided in Python, C++, C#, Java, Node.js (JavaScript), Go and VB. It supports several input formats like docx, xlsx, rtf, txt, html, pub, emf, etc.

1//
2// Copyright (c) 2001-2023 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 convert to PDF with virtual printer on Windows.
14// It supports several input formats like docx, xlsx, rtf, txt, html, pub, emf, etc. For more details, visit
15// https://docs.apryse.com/windows/guides/features/conversion/convert-other/
16//
17// To check if ToPDF (or ToXPS) require that PDFNet printer is installed use Convert::RequiresPrinter(filename).
18// The installing application must be run as administrator. The manifest for this sample
19// specifies appropriate the UAC elevation.
20//
21// Note: the PDFNet printer is a virtual XPS printer supported on Vista SP1 and Windows 7.
22// For Windows XP SP2 or higher, or Vista SP0 you need to install the XPS Essentials Pack (or
23// equivalent redistributables). You can download the XPS Essentials Pack from:
24// http://www.microsoft.com/downloads/details.aspx?FamilyId=B8DCFFDD-E3A5-44CC-8021-7649FD37FFEE&displaylang=en
25// Windows XP Sp2 will also need the Microsoft Core XML Services (MSXML) 6.0:
26// http://www.microsoft.com/downloads/details.aspx?familyid=993C0BCF-3BCF-4009-BE21-27E85E1857B1&displaylang=en
27//
28// Note: Convert.fromEmf and Convert.toEmf will only work on Windows and require GDI+.
29//
30// Please contact us if you have any questions.
31//---------------------------------------------------------------------------------------
32class Testfile
33{
34 public String inputFile, outputFile;
35 public Testfile(String inFile, String outFile)
36 {
37 inputFile = inFile;
38 outputFile = outFile;
39 }
40}
41
42public class ConvertPrintTest
43{
44 // Relative path to the folder containing test files.
45 static String inputPath = "../../TestFiles/";
46 static String outputPath = "../../TestFiles/Output/";
47
48 static boolean ConvertSpecificFormats()
49 {
50 //////////////////////////////////////////////////////////////////////////
51 boolean err = false;
52 try (PDFDoc pdfdoc = new PDFDoc())
53 {
54 System.out.println("Converting from EMF");
55 Convert.fromEmf(pdfdoc, inputPath + "simple-emf.emf");
56 pdfdoc.save(outputPath + "emf2pdf v2.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
57 System.out.println("Saved emf2pdf v2.pdf");
58 }
59 catch (PDFNetException e)
60 {
61 System.out.println(e);
62 err = true;
63 }
64
65
66 //////////////////////////////////////////////////////////////////////////
67 try
68 {
69 // Convert MSWord document to XPS
70 System.out.println("Converting DOCX to XPS");
71 Convert.toXps(inputPath + "simple-word_2007.docx", outputPath + "simple-word_2007.xps");
72 System.out.println("Saved simple-word_2007.xps");
73 }
74 catch (PDFNetException e)
75 {
76 System.out.println(e);
77 err = true;
78 }
79
80 return err;
81 }
82
83 static boolean ConvertToPdfFromFile()
84 {
85 ArrayList<Testfile> testfiles = new ArrayList<Testfile>();
86 testfiles.add(new Testfile("simple-word_2007.docx", "docx2pdf.pdf"));
87 testfiles.add(new Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf"));
88 testfiles.add(new Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf"));
89 testfiles.add(new Testfile("simple-publisher.pub", "pub2pdf.pdf"));
90 testfiles.add(new Testfile("simple-text.txt", "txt2pdf.pdf"));
91 testfiles.add(new Testfile("simple-rtf.rtf", "rtf2pdf.pdf"));
92 testfiles.add(new Testfile("simple-emf.emf", "emf2pdf.pdf"));
93 testfiles.add(new Testfile("simple-webpage.mht", "mht2pdf.pdf"));
94 testfiles.add(new Testfile("simple-webpage.html", "html2pdf.pdf"));
95
96 boolean err = false;
97 try{
98 if (ConvertPrinter.isInstalled("PDFTron PDFNet"))
99 {
100 ConvertPrinter.setPrinterName("PDFTron PDFNet");
101 }
102 else if (!ConvertPrinter.isInstalled())
103 {
104 try
105 {
106 System.out.println("Installing printer (requires Windows platform and administrator)");
107 ConvertPrinter.install();
108 System.out.println("Installed printer " + ConvertPrinter.getPrinterName());
109 // the function ConvertToXpsFromFile may require the printer so leave it installed
110 // uninstallPrinterWhenDone = true;
111 }
112 catch (PDFNetException e)
113 {
114 System.out.println("ERROR: Unable to install printer.");
115 System.out.println(e);
116 err = true;
117 }
118 catch (Exception e)
119 {
120 System.out.println("ERROR: Unable to install printer. Make sure that the package's bitness matches your operating system's bitness and that you are running with administrator privileges.");
121 }
122 }
123 }
124 catch (PDFNetException e)
125 {
126 System.out.println("ERROR: Unable to install printer.");
127 System.out.println(e);
128 err = true;
129 }
130
131 for (Testfile file : testfiles)
132 {
133 try (PDFDoc pdfdoc = new PDFDoc())
134 {
135 if (Convert.requiresPrinter(inputPath + file.inputFile))
136 {
137 System.out.println("Using PDFNet printer to convert file " + file.inputFile);
138 }
139 Convert.toPdf(pdfdoc, inputPath + file.inputFile);
140 pdfdoc.save(outputPath + file.outputFile, SDFDoc.SaveMode.LINEARIZED, null);
141 System.out.println("Converted file: " + file.inputFile);
142 System.out.println("to: " + file.outputFile);
143 }
144 catch (PDFNetException e)
145 {
146 System.out.println("ERROR: on input file " + file.inputFile);
147 System.out.println(e);
148 err = true;
149 }
150 }
151
152 return err;
153 }
154
155 /// <summary>
156 /// The main entry point for the application.
157 /// </summary>
158 public static void main(String[] args)
159 {
160 if (System.getProperty("os.name").startsWith("Windows")) {
161
162 PDFNet.initialize(PDFTronLicense.Key());
163 boolean err = false;
164
165 err = ConvertToPdfFromFile();
166 if (err)
167 {
168 System.out.println("ConvertFile failed");
169 }
170 else
171 {
172 System.out.println("ConvertFile succeeded");
173 }
174
175 err = ConvertSpecificFormats();
176 if (err)
177 {
178 System.out.println("ConvertSpecificFormats failed");
179 }
180 else
181 {
182 System.out.println("ConvertSpecificFormats succeeded");
183 }
184
185 try
186 {
187 System.out.println("Uninstalling printer (requires Windows platform and administrator)");
188 ConvertPrinter.uninstall();
189 System.out.println("Uninstalled printer " + ConvertPrinter.getPrinterName());
190 }
191 catch (Exception e)
192 {
193 System.out.println("Unable to uninstall printer");
194 err = true;
195 }
196
197 System.out.println("Done.");
198
199 PDFNet.terminate();
200 }
201 else {
202 System.out.println("ConvertPrintTest only available on Windows");
203 }
204 }
205
206}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales