Convert

Sample C# 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 UWP SDK and PDF Conversion Library.

1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.Collections.Generic;
7using System.IO;
8using System.Threading.Tasks;
9using Windows.Foundation;
10
11using pdftron.PDF;
12using pdftron.SDF;
13
14using PDFNetUniversalSamples.ViewModels;
15
16namespace PDFNetSamples
17{
18 public sealed class ConvertTest : Sample
19 {
20 public ConvertTest() :
21 base("Convert", "This sample shows how to use PDFNet Convert Add-on (i.e. 'pdftron.PDF.Convert' namespace) for direct, high-quality conversion between PDF, XPS, EMF, SVG, TIFF, PNG, JPEG, and other image formats.")
22 {
23 }
24
25 public override IAsyncAction RunAsync()
26 {
27 return Task.Run(new System.Action(async () => {
28 WriteLine("--------------------------------");
29 WriteLine("Starting Convert Test...");
30 WriteLine("--------------------------------\n");
31
32 bool err = false;
33
34 err = await ConvertToPdfFromFile();
35 if (err)
36 {
37 WriteLine("ConvertFile failed");
38 }
39 else
40 {
41 WriteLine("ConvertFile succeeded");
42 }
43
44 err = await ConvertSpecificFormats();
45 if (err)
46 {
47 WriteLine("ConvertSpecificFormats failed");
48 }
49 else
50 {
51 WriteLine("ConvertSpecificFormats succeeded");
52 }
53
54 err = await ConvertToXpsFromFile().ConfigureAwait(false);
55 if (err)
56 {
57 WriteLine("ConvertToXpsFromFile failed");
58 }
59 else
60 {
61 WriteLine("ConvertToXpsFromFile succeeded");
62 }
63
64 WriteLine("\n--------------------------------");
65 WriteLine("Done Convert Test.");
66 WriteLine("--------------------------------\n");
67 })).AsAsyncAction();
68 }
69
70 async Task<bool> ConvertSpecificFormats()
71 {
72 bool err = false;
73 try
74 {
75 PDFDoc pdfdoc = new PDFDoc();
76 String input_file_path = Path.Combine(InputPath, "simple-xps.xps");
77 String output_file_path = Path.Combine(OutputPath, "ConvertTest_fromXps.pdf");
78 pdftron.PDF.Convert.FromXps(pdfdoc, input_file_path);
79 await pdfdoc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
80 pdfdoc.Destroy();
81 WriteLine("Done. Result saved in " + output_file_path);
82 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
83 }
84 catch (Exception e)
85 {
86 WriteLine(GetExceptionMessage(e));
87 err = true;
88 }
89 try
90 {
91 String input_file_path = Path.Combine(InputPath, "simple-word_2007.docx");
92 String output_file_path = Path.Combine(OutputPath, "ConvertTest_WordToPDF.pdf");
93 DocumentConversion conversion = pdftron.PDF.Convert.WordToPDFConversion(input_file_path, null);
94 conversion.Convert();
95 uint num_warnings = conversion.GetNumWarnings();
96 if (num_warnings > 0)
97 {
98 WriteLine("WordToPDF conversion warnings: ");
99 for (uint i = 0; i < num_warnings; ++i)
100 {
101 WriteLine(conversion.GetWarningString(i));
102 }
103 }
104 PDFDoc pdfdoc = conversion.GetDoc();
105 await pdfdoc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
106 pdfdoc.Destroy();
107 WriteLine("Done. Result saved in " + output_file_path);
108 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
109 }
110 catch (Exception e)
111 {
112 WriteLine(GetExceptionMessage(e));
113 err = true;
114 }
115 return err;
116 }
117
118 private struct TestFile
119 {
120 public TestFile(String inputFile, String outputFile)
121 {
122 this.InputFile = Path.Combine(InputPath, inputFile);
123 this.OutputFile = Path.Combine(OutputPath, outputFile);
124 }
125 public String InputFile;
126 public String OutputFile;
127 }
128
129 async Task<bool> ConvertToPdfFromFile()
130 {
131 List<TestFile> testfiles = new List<TestFile>(
132 new TestFile[] {
133 new TestFile("butterfly.png", "ConvertTest_png2pdf.pdf"),
134 new TestFile("pdftron.bmp", "ConvertTest_bmp2pdf.pdf"),
135 new TestFile("simple-xps.xps", "ConvertTest_xps2pdf.pdf")
136 }
137 );
138
139 bool err = false;
140
141 foreach (var testfile in testfiles)
142 {
143 try
144 {
145 pdftron.PDF.PDFDoc pdfdoc = new PDFDoc();
146 pdftron.PDF.Convert.ToPdf(pdfdoc, testfile.InputFile);
147 await pdfdoc.SaveAsync(testfile.OutputFile, SDFDocSaveOptions.e_linearized);
148 pdfdoc.Destroy();
149 pdfdoc = null;
150 WriteLine("Converted file: " + testfile.InputFile);
151 WriteLine(" to pdf: " + testfile.OutputFile);
152 await AddFileToOutputList(testfile.OutputFile).ConfigureAwait(false);
153 }
154 catch (Exception e)
155 {
156 WriteLine("ERROR: on input file " + testfile.InputFile);
157 WriteLine(GetExceptionMessage(e));
158 err = true;
159 }
160 }
161 return err;
162 }
163
164 async Task<bool> ConvertToXpsFromFile()
165 {
166 List<TestFile> testfiles = new List<TestFile>(
167 new TestFile[] {
168 new TestFile("butterfly.png", "butterfly.xps"),
169 new TestFile("newsletter.pdf", "newsletter.xps")
170 }
171 );
172
173 bool err = false;
174
175 foreach (var testfile in testfiles)
176 {
177 try
178 {
179 pdftron.PDF.Convert.ToXps(testfile.InputFile, testfile.OutputFile);
180 WriteLine("Converted file: " + testfile.InputFile);
181 WriteLine(" to xps: " + testfile.OutputFile);
182 await AddFileToOutputList(testfile.OutputFile).ConfigureAwait(false);
183 }
184 catch (Exception e)
185 {
186 WriteLine(GetExceptionMessage(e));
187 err = true;
188 }
189 }
190 return err;
191 }
192
193 }
194}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales