Convert

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 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//
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 ConvertTestCS
16{
17 /// <summary>
18 // The following sample illustrates how to use the PDF::Convert utility class to convert
19 // documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
20 // using our built in conversion.
21 //
22 // Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
23 // converted to PDF or XPS.
24 //
25 // Also note that conversion under ASP.NET can be tricky to configure. Please see the following document for advice:
26 // http://www.pdftron.com/pdfnet/faq_files/Converting_Documents_in_Windows_Service_or_ASP.NET_Application_using_PDFNet.pdf
27 /// </summary>
28 class Testfile
29 {
30 public string inputFile, outputFile;
31 public Testfile(string inFile, string outFile)
32 {
33 inputFile = inFile;
34 outputFile = outFile;
35 }
36 };
37
38 class Class1
39 {
40 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
41 static Class1() { }
42
43 // Relative path to the folder containing test files.
44 const string inputPath = "../../../../TestFiles/";
45 const string outputPath = "../../../../TestFiles/Output/";
46
47 static bool ConvertSpecificFormats()
48 {
49 //////////////////////////////////////////////////////////////////////////
50 bool err = false;
51 try
52 {
53 using (PDFDoc pdfdoc = new PDFDoc())
54 {
55 Console.WriteLine("Converting from XPS");
56
57 pdftron.PDF.Convert.FromXps(pdfdoc, inputPath + "simple-xps.xps");
58 pdfdoc.Save(outputPath + "xps2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
59 Console.WriteLine("Saved xps2pdf v2.pdf");
60 }
61 }
62 catch (PDFNetException e)
63 {
64 Console.WriteLine(e.Message);
65 err = true;
66 }
67
68 //////////////////////////////////////////////////////////////////////////
69 try
70 {
71 using (PDFDoc pdfdoc = new PDFDoc())
72 {
73 // Add a dictionary
74 ObjSet set = new ObjSet();
75 Obj options = set.CreateDict();
76
77 // Put options
78 options.PutNumber("FontSize", 15);
79 options.PutBool("UseSourceCodeFormatting", true);
80 options.PutNumber("PageWidth", 12);
81 options.PutNumber("PageHeight", 6);
82
83 // Convert from .txt file
84 Console.WriteLine("Converting from txt");
85 pdftron.PDF.Convert.FromText(pdfdoc, inputPath + "simple-text.txt", options);
86 pdfdoc.Save(outputPath + "simple-text.pdf", SDFDoc.SaveOptions.e_remove_unused);
87 Console.WriteLine("Saved simple-text.pdf");
88 }
89 }
90 catch (PDFNetException e)
91 {
92 Console.WriteLine(e.Message);
93 err = true;
94 }
95
96 //////////////////////////////////////////////////////////////////////////
97 try
98 {
99 using (PDFDoc pdfdoc = new PDFDoc(inputPath + "newsletter.pdf"))
100 {
101 // Convert PDF document to SVG
102 Console.WriteLine("Converting pdfdoc to SVG");
103 pdftron.PDF.Convert.ToSvg(pdfdoc, outputPath + "pdf2svg v2.svg");
104 Console.WriteLine("Saved pdf2svg v2.svg");
105 }
106 }
107 catch (PDFNetException e)
108 {
109 Console.WriteLine(e.Message);
110 err = true;
111 }
112
113 //////////////////////////////////////////////////////////////////////////
114 try
115 {
116 // Convert PNG image to XPS
117 Console.WriteLine("Converting PNG to XPS");
118 pdftron.PDF.Convert.ToXps(inputPath + "butterfly.png", outputPath + "butterfly.xps");
119 Console.WriteLine("Saved butterfly.xps");
120 }
121 catch (PDFNetException e)
122 {
123 Console.WriteLine(e.Message);
124 err = true;
125 }
126
127
128 //////////////////////////////////////////////////////////////////////////
129 try
130 {
131 // Convert PDF document to XPS
132 Console.WriteLine("Converting PDF to XPS");
133 pdftron.PDF.Convert.ToXps(inputPath + "newsletter.pdf", outputPath + "newsletter.xps");
134 Console.WriteLine("Saved newsletter.xps");
135 }
136 catch (PDFNetException e)
137 {
138 Console.WriteLine(e.Message);
139 err = true;
140 }
141
142 //////////////////////////////////////////////////////////////////////////
143 try
144 {
145 // Convert PDF document to HTML
146 Console.WriteLine("Converting PDF to HTML");
147 pdftron.PDF.Convert.ToHtml(inputPath + "newsletter.pdf", outputPath + "newsletter");
148 Console.WriteLine("Saved newsletter as HTML");
149 }
150 catch (PDFNetException e)
151 {
152 Console.WriteLine(e.Message);
153 err = true;
154 }
155
156 //////////////////////////////////////////////////////////////////////////
157 try
158 {
159 // Convert PDF document to EPUB
160 Console.WriteLine("Converting PDF to EPUB");
161 pdftron.PDF.Convert.ToEpub(inputPath + "newsletter.pdf", outputPath + "newsletter.epub");
162 Console.WriteLine("Saved newsletter.epub");
163 }
164 catch (PDFNetException e)
165 {
166 Console.WriteLine(e.Message);
167 err = true;
168 }
169
170 //////////////////////////////////////////////////////////////////////////
171 try
172 {
173 // Convert PDF document to multipage TIFF
174 Console.WriteLine("Converting PDF to multipage TIFF");
175 pdftron.PDF.Convert.TiffOutputOptions tiff_options = new pdftron.PDF.Convert.TiffOutputOptions();
176 tiff_options.SetDPI(200);
177 tiff_options.SetDither(true);
178 tiff_options.SetMono(true);
179 pdftron.PDF.Convert.ToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options);
180 Console.WriteLine("Saved newsletter.tiff");
181 }
182 catch (PDFNetException e)
183 {
184 Console.WriteLine(e.Message);
185 err = true;
186 }
187
188 //////////////////////////////////////////////////////////////////////////
189 try
190 {
191 using (PDFDoc pdfdoc = new PDFDoc())
192 {
193 // Convert SVG file to PDF
194 Console.WriteLine("Converting SVG to PDF");
195
196 pdftron.PDF.Convert.FromSVG(pdfdoc, inputPath + "tiger.svg", null);
197 pdfdoc.Save(outputPath + "svg2pdf.pdf", SDFDoc.SaveOptions.e_remove_unused);
198
199 Console.WriteLine("Saved svg2pdf.pdf");
200 }
201 }
202 catch (PDFNetException e)
203 {
204 Console.WriteLine(e.Message);
205 err = true;
206 }
207
208 return err;
209 }
210
211 static Boolean ConvertToPdfFromFile()
212 {
213 System.Collections.ArrayList testfiles = new System.Collections.ArrayList();
214 testfiles.Add(new ConvertTestCS.Testfile("simple-word_2007.docx", "docx2pdf.pdf"));
215 testfiles.Add(new ConvertTestCS.Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf"));
216 testfiles.Add(new ConvertTestCS.Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf"));
217 testfiles.Add(new ConvertTestCS.Testfile("simple-text.txt", "txt2pdf.pdf"));
218 testfiles.Add(new ConvertTestCS.Testfile("butterfly.png", "png2pdf.pdf"));
219 testfiles.Add(new ConvertTestCS.Testfile("simple-xps.xps", "xps2pdf.pdf"));
220
221 bool err = false;
222
223 foreach (Testfile file in testfiles)
224 {
225 try
226 {
227 using (pdftron.PDF.PDFDoc pdfdoc = new PDFDoc())
228 {
229 pdftron.PDF.Convert.Printer.SetMode(pdftron.PDF.Convert.Printer.Mode.e_prefer_builtin_converter);
230 pdftron.PDF.Convert.ToPdf(pdfdoc, inputPath + file.inputFile);
231 pdfdoc.Save(outputPath + file.outputFile, SDFDoc.SaveOptions.e_linearized);
232 Console.WriteLine("Converted file: " + file.inputFile);
233 Console.WriteLine("to: " + file.outputFile);
234 }
235 }
236 catch (PDFNetException e)
237 {
238 Console.WriteLine("ERROR: on input file " + file.inputFile);
239 Console.WriteLine(e.Message);
240 err = true;
241 }
242 }
243
244 return err;
245 }
246
247
248 /// <summary>
249 /// The main entry point for the application.
250 /// </summary>
251 [STAThread]
252 static void Main(string[] args)
253 {
254 PDFNet.Initialize(PDFTronLicense.Key);
255 bool err = false;
256
257 err = ConvertToPdfFromFile();
258 if (err)
259 {
260 Console.WriteLine("ConvertFile failed");
261 }
262 else
263 {
264 Console.WriteLine("ConvertFile succeeded");
265 }
266
267 err = ConvertSpecificFormats();
268 if (err)
269 {
270 Console.WriteLine("ConvertSpecificFormats failed");
271 }
272 else
273 {
274 Console.WriteLine("ConvertSpecificFormats succeeded");
275 }
276
277 PDFNet.Terminate();
278 Console.WriteLine("Done.");
279 }
280 }
281}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales