Convert Print (Windows only)

Sample C# code to convert to PDF with virtual printer on Windows. It supports several input formats like docx, xlsx, rtf, txt, html, pub, emf, etc.

1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using System.Drawing;
7using System.Drawing.Drawing2D;
8
9using pdftron;
10using pdftron.Common;
11using pdftron.Filters;
12using pdftron.SDF;
13using pdftron.PDF;
14
15namespace ConvertPrintTestCS
16{
17 /// <summary>
18 // The following sample illustrates how to convert to PDF with virtual printer on Windows.
19 // It supports several input formats like docx, xlsx, rtf, txt, html, pub, emf, etc. For more details, visit
20 // https://docs.apryse.com/windows/guides/features/conversion/convert-other/
21 //
22 // To check if ToPDF (or ToXPS) require that PDFNet printer is installed use Convert::RequiresPrinter(filename).
23 // The installing application must be run as administrator. The manifest for this sample
24 // specifies appropriate the UAC elevation.
25 //
26 // Note: the PDFNet printer is a virtual XPS printer supported on Vista SP1 and Windows 7, or .NET Framework
27 // 3.x or higher. For older versions of .NET Framework running on Windows XP or Vista SP0 you need to install
28 // the XPS Essentials Pack (or equivalent redistributables).
29 //
30 // Also note that conversion under ASP.NET can be tricky to configure. Please see the following document for advice:
31 // http://www.pdftron.com/pdfnet/faq_files/Converting_Documents_in_Windows_Service_or_ASP.NET_Application_using_PDFNet.pdf
32 /// </summary>
33 class Testfile
34 {
35 public string inputFile, outputFile;
36 public Testfile(string inFile, string outFile)
37 {
38 inputFile = inFile;
39 outputFile = outFile;
40 }
41 };
42
43 class Class1
44 {
45 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
46 static Class1() {}
47
48 // Relative path to the folder containing test files.
49 const string inputPath = "../../../../TestFiles/";
50 const string outputPath = "../../../../TestFiles/Output/";
51
52 static bool ConvertSpecificFormats()
53 {
54 //////////////////////////////////////////////////////////////////////////
55 bool err = false;
56 try
57 {
58 // Convert MSWord document to XPS
59 Console.WriteLine("Converting DOCX to XPS");
60 pdftron.PDF.Convert.ToXps(inputPath + "simple-word_2007.docx", outputPath + "simple-word_2007.xps");
61 Console.WriteLine("Saved simple-word_2007.xps");
62 }
63 catch (PDFNetException e)
64 {
65 Console.WriteLine(e.Message);
66 err = true;
67 }
68
69
70 //////////////////////////////////////////////////////////////////////////
71 try
72 {
73 using (PDFDoc pdfdoc = new PDFDoc())
74 {
75 Console.WriteLine("Converting from EMF");
76 pdftron.PDF.Convert.FromEmf(pdfdoc, inputPath + "simple-emf.emf");
77 pdfdoc.Save(outputPath + "emf2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
78 Console.WriteLine("Saved emf2pdf v2.pdf");
79 }
80 }
81 catch (PDFNetException e)
82 {
83 Console.WriteLine(e.Message);
84 err = true;
85 }
86
87 return err;
88 }
89
90 static Boolean ConvertToPdfFromFile()
91 {
92 System.Collections.ArrayList testfiles = new System.Collections.ArrayList();
93 testfiles.Add(new ConvertPrintTestCS.Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf"));
94 testfiles.Add(new ConvertPrintTestCS.Testfile("simple-word_2007.docx", "docx2pdf.pdf"));
95 testfiles.Add(new ConvertPrintTestCS.Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf"));
96 testfiles.Add(new ConvertPrintTestCS.Testfile("simple-publisher.pub", "pub2pdf.pdf"));
97 testfiles.Add(new ConvertPrintTestCS.Testfile("simple-text.txt", "txt2pdf.pdf"));
98 testfiles.Add(new ConvertPrintTestCS.Testfile("simple-rtf.rtf", "rtf2pdf.pdf"));
99 testfiles.Add(new ConvertPrintTestCS.Testfile("simple-emf.emf", "emf2pdf.pdf"));
100 testfiles.Add(new ConvertPrintTestCS.Testfile("simple-webpage.mht", "mht2pdf.pdf"));
101 testfiles.Add(new ConvertPrintTestCS.Testfile("simple-webpage.html", "html2pdf.pdf"));
102
103 bool err = false;
104 try
105 {
106 if (pdftron.PDF.Convert.Printer.IsInstalled("PDFTron PDFNet"))
107 {
108 pdftron.PDF.Convert.Printer.SetPrinterName("PDFTron PDFNet");
109 }
110 else if (!pdftron.PDF.Convert.Printer.IsInstalled())
111 {
112 try
113 {
114 Console.WriteLine("Installing printer (requires Windows platform and administrator)");
115 pdftron.PDF.Convert.Printer.Install();
116 Console.WriteLine("Installed printer " + pdftron.PDF.Convert.Printer.GetPrinterName());
117 // the function ConvertToXpsFromFile may require the printer so leave it installed
118 // uninstallPrinterWhenDone = true;
119 }
120 catch (PDFNetException e)
121 {
122 Console.WriteLine("ERROR: Unable to install printer.");
123 Console.WriteLine(e.Message);
124 err = true;
125 }
126 catch
127 {
128 Console.WriteLine("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.");
129 }
130 }
131 }
132 catch (PDFNetException e)
133 {
134 Console.WriteLine("ERROR: Unable to install printer.");
135 Console.WriteLine(e.Message);
136 err = true;
137 }
138
139 foreach (Testfile file in testfiles)
140 {
141 try
142 {
143 using (pdftron.PDF.PDFDoc pdfdoc = new PDFDoc())
144 {
145
146 if (pdftron.PDF.Convert.RequiresPrinter(inputPath + file.inputFile))
147 {
148 Console.WriteLine("Using PDFNet printer to convert file " + file.inputFile);
149 }
150 pdftron.PDF.Convert.ToPdf(pdfdoc, inputPath + file.inputFile);
151 pdfdoc.Save(outputPath + file.outputFile, SDFDoc.SaveOptions.e_linearized);
152 Console.WriteLine("Converted file: " + file.inputFile);
153 Console.WriteLine("to: " + file.outputFile);
154 }
155 }
156 catch (PDFNetException e)
157 {
158 Console.WriteLine("ERROR: on input file " + file.inputFile);
159 Console.WriteLine(e.Message);
160 err = true;
161 }
162 }
163
164 return err;
165 }
166
167
168 /// <summary>
169 /// The main entry point for the application.
170 /// </summary>
171 [STAThread]
172 static void Main(string[] args)
173 {
174 if (Environment.OSVersion.Platform == PlatformID.Win32NT)
175 {
176 PDFNet.Initialize(PDFTronLicense.Key);
177 bool err = false;
178
179 err = ConvertToPdfFromFile();
180 if (err)
181 {
182 Console.WriteLine("ConvertFile failed");
183 }
184 else
185 {
186 Console.WriteLine("ConvertFile succeeded");
187 }
188
189 err = ConvertSpecificFormats();
190 if (err)
191 {
192 Console.WriteLine("ConvertSpecificFormats failed");
193 }
194 else
195 {
196 Console.WriteLine("ConvertSpecificFormats succeeded");
197 }
198
199
200 if (pdftron.PDF.Convert.Printer.IsInstalled())
201 {
202 try
203 {
204 Console.WriteLine("Uninstalling printer (requires Windows platform and administrator)");
205 pdftron.PDF.Convert.Printer.Uninstall();
206 Console.WriteLine("Uninstalled Printer " + pdftron.PDF.Convert.Printer.GetPrinterName());
207 }
208 catch
209 {
210 Console.WriteLine("Unable to uninstall printer");
211 }
212 }
213
214 PDFNet.Terminate();
215 Console.WriteLine("Done.");
216 }
217 else
218 {
219 Console.WriteLine("ConvertPrintTest only available on Windows");
220 }
221 }
222 }
223}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales