Convert Between File Types - C++ Sample Code

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
6#include <iostream>
7#include <sstream>
8#include <PDF/PDFNet.h>
9#include <PDF/Convert.h>
10#include "../../LicenseKey/CPP/LicenseKey.h"
11
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. The sample also shows how to convert MS Office files
15// using our built in conversion.
16//
17// Certain file formats such as XPS, 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//---------------------------------------------------------------------------------------
22
23using namespace pdftron;
24using namespace PDF;
25using namespace std;
26
27UString inputPath("../../TestFiles/");
28UString outputPath("../../TestFiles/Output/");
29
30typedef struct
31{
32 UString inputFile, outputFile;
33}
34Testfile;
35
36Testfile testfiles[] =
37{
38 { "simple-word_2007.docx", "docx2pdf.pdf"},
39 { "simple-powerpoint_2007.pptx", "pptx2pdf.pdf"},
40 { "simple-excel_2007.xlsx", "xlsx2pdf.pdf"},
41 { "simple-text.txt", "txt2pdf.pdf"},
42 { "butterfly.png", "png2pdf.pdf"},
43 { "simple-xps.xps", "xps2pdf.pdf"}
44};
45
46int ConvertSpecificFormats(); // convert to/from PDF, XPS, SVG
47int ConvertToPdfFromFile(); // convert from a file to PDF automatically
48
49int main(int argc, char *argv[])
50{
51 // The first step in every application using PDFNet is to initialize the
52 // library. The library is usually initialized only once, but calling
53 // Initialize() multiple times is also fine.
54 int err = 0;
55
56 PDFNet::Initialize(LicenseKey);
57
58 // Demonstrate Convert::ToPdf
59 err = ConvertToPdfFromFile();
60 if (err)
61 {
62 cout << "ConvertFile failed" << endl;
63 }
64 else
65 {
66 cout << "ConvertFile succeeded" << endl;
67 }
68
69 // Demonstrate Convert::[FromXps, ToSVG, ToXPS]
70 err = ConvertSpecificFormats();
71 if (err)
72 {
73 cout << "ConvertSpecificFormats failed" << endl;
74 }
75 else
76 {
77 cout << "ConvertSpecificFormats succeeded" << endl;
78 }
79
80 PDFNet::Terminate();
81 cout << "Done.\n";
82 return err;
83}
84
85int ConvertToPdfFromFile()
86{
87 int ret = 0;
88
89 unsigned int ceTestfiles = sizeof (testfiles) / sizeof (Testfile);
90
91 for (unsigned int i = 0; i < ceTestfiles; i++)
92 {
93
94 try
95 {
96 PDFDoc pdfdoc;
97 UString inputFile = inputPath + testfiles[i].inputFile;
98 UString outputFile = outputPath + testfiles[i].outputFile;
99
100 Convert::Printer::SetMode(Convert::Printer::e_prefer_builtin_converter);
101 Convert::ToPdf(pdfdoc, inputFile);
102 pdfdoc.Save(outputFile, SDF::SDFDoc::e_linearized, NULL);
103 cout << "Converted file: " << testfiles[i].inputFile << endl << "to: " << testfiles[i].outputFile << endl;
104 }
105 catch (Common::Exception& e)
106 {
107 cout << "Unable to convert file " << testfiles[i].inputFile.ConvertToAscii().c_str() << endl;
108 cout << e << endl;
109 ret = 1;
110 }
111 catch (...)
112 {
113 cout << "Unknown Exception" << endl;
114 ret = 1;
115 }
116 }
117
118 return ret;
119}
120
121int ConvertSpecificFormats()
122{
123 //////////////////////////////////////////////////////////////////////////
124 int ret = 0;
125 try
126 {
127 PDFDoc pdfdoc;
128
129 cout << "Converting from XPS" << endl;
130 Convert::FromXps(pdfdoc, inputPath + "simple-xps.xps");
131 pdfdoc.Save(outputPath + "xps2pdf v2.pdf", SDF::SDFDoc::e_remove_unused, NULL);
132 cout << "Saved xps2pdf v2.pdf" << endl;
133 }
134 catch (Common::Exception& e)
135 {
136 cout << e << endl;
137 ret = 1;
138 }
139 catch (...)
140 {
141 cout << "Unknown Exception" << endl;
142 ret = 1;
143 }
144
145 //////////////////////////////////////////////////////////////////////////
146 try
147 {
148 PDFDoc pdfdoc;
149
150 // Add a dictionary
151 SDF::ObjSet set;
152 SDF::Obj options = set.CreateDict();
153
154 // Put options
155 options.PutNumber("FontSize", 15);
156 options.PutBool("UseSourceCodeFormatting", true);
157 options.PutNumber("PageWidth", 12);
158 options.PutNumber("PageHeight", 6);
159
160 // Convert from .txt file
161 cout << "Converting from txt" << endl;
162 Convert::FromText(pdfdoc, inputPath + "simple-text.txt", options);
163 pdfdoc.Save(outputPath + "simple-text.pdf", SDF::SDFDoc::e_remove_unused, NULL);
164 cout << "Saved simple-text.pdf" << endl;
165 }
166 catch (Common::Exception& e)
167 {
168 cout << e << endl;
169 ret = 1;
170 }
171 catch (...)
172 {
173 cout << "Unknown Exception" << endl;
174 ret = 1;
175 }
176
177 //////////////////////////////////////////////////////////////////////////
178 try
179 {
180 PDFDoc pdfdoc(inputPath + "newsletter.pdf");
181
182 // Convert PDF document to SVG
183 cout << "Converting pdfdoc to SVG" << endl;
184 Convert::ToSvg(pdfdoc, outputPath + "pdf2svg v2.svg");
185 cout << "Saved pdf2svg v2.svg" << endl;
186 }
187 catch (Common::Exception& e)
188 {
189 cout << e << endl;
190 ret = 1;
191 }
192 catch (...)
193 {
194 cout << "Unknown Exception" << endl;
195 ret = 1;
196 }
197
198 //////////////////////////////////////////////////////////////////////////
199 try
200 {
201 // Convert PNG image to XPS
202 cout << "Converting PNG to XPS" << endl;
203 Convert::ToXps(inputPath + "butterfly.png", outputPath + "butterfly.xps");
204 cout << "Saved butterfly.xps" << endl;
205 }
206 catch (Common::Exception& e)
207 {
208 cout << e << endl;
209 ret = 1;
210 }
211 catch (...)
212 {
213 cout << "Unknown Exception" << endl;
214 ret = 1;
215 }
216
217 //////////////////////////////////////////////////////////////////////////
218 try
219 {
220 // Convert PDF document to XPS
221 cout << "Converting PDF to XPS" << endl;
222 Convert::ToXps(inputPath + "newsletter.pdf", outputPath + "newsletter.xps");
223 cout << "Saved newsletter.xps" << endl;
224 }
225 catch (Common::Exception& e)
226 {
227 cout << e << endl;
228 ret = 1;
229 }
230 catch (...)
231 {
232 cout << "Unknown Exception" << endl;
233 ret = 1;
234 }
235
236 //////////////////////////////////////////////////////////////////////////
237 try
238 {
239 // Convert PDF document to HTML
240 cout << "Converting PDF to HTML" << endl;
241 Convert::ToHtml(inputPath + "newsletter.pdf", outputPath + "newsletter");
242 cout << "Saved newsletter as HTML" << endl;
243 }
244 catch (Common::Exception& e)
245 {
246 cout << e << endl;
247 ret = 1;
248 }
249 catch (...)
250 {
251 cout << "Unknown Exception" << endl;
252 ret = 1;
253 }
254
255 //////////////////////////////////////////////////////////////////////////
256 try
257 {
258 // Convert PDF document to EPUB
259 cout << "Converting PDF to EPUB" << endl;
260 Convert::ToEpub(inputPath + "newsletter.pdf", outputPath + "newsletter.epub");
261 cout << "Saved newsletter.epub" << endl;
262 }
263 catch (Common::Exception& e)
264 {
265 cout << e << endl;
266 ret = 1;
267 }
268 catch (...)
269 {
270 cout << "Unknown Exception" << endl;
271 ret = 1;
272 }
273
274 //////////////////////////////////////////////////////////////////////////
275 try
276 {
277 // Convert PDF document to multipage TIFF
278 cout << "Converting PDF to multipage TIFF" << endl;
279 Convert::TiffOutputOptions tiff_options;
280 tiff_options.SetDPI(200);
281 tiff_options.SetDither(true);
282 tiff_options.SetMono(true);
283 Convert::ToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options);
284 cout << "Saved newsletter.tiff" << endl;
285 }
286 catch (Common::Exception& e)
287 {
288 cout << e << endl;
289 ret = 1;
290 }
291 catch (...)
292 {
293 cout << "Unknown Exception" << endl;
294 ret = 1;
295 }
296
297 //////////////////////////////////////////////////////////////////////////
298 try
299 {
300 PDFDoc pdfdoc;
301
302 // Convert SVG file to PDF
303 cout << "Converting SVG to PDF" << endl;
304
305 Convert::FromSVG(pdfdoc, inputPath + "tiger.svg");
306 pdfdoc.Save(outputPath + "svg2pdf.pdf", SDF::SDFDoc::e_remove_unused, NULL);
307
308 cout << "Saved svg2pdf.pdf" << endl;
309 }
310 catch (Common::Exception& e)
311 {
312 cout << e << endl;
313 ret = 1;
314 }
315 catch (...)
316 {
317 cout << "Unknown Exception" << endl;
318 ret = 1;
319 }
320
321 return ret;
322}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales