Convert File Types with Virtual Printer on Windows - C++ 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
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 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//---------------------------------------------------------------------------------------
32
33using namespace pdftron;
34using namespace PDF;
35using namespace std;
36
37UString inputPath("../../TestFiles/");
38UString outputPath("../../TestFiles/Output/");
39
40typedef struct
41{
42 UString inputFile, outputFile;
43}
44Testfile;
45
46Testfile testfiles[] =
47{
48 { "simple-word_2007.docx", "docx2pdf.pdf"},
49 { "simple-powerpoint_2007.pptx", "pptx2pdf.pdf"},
50 { "simple-excel_2007.xlsx", "xlsx2pdf.pdf"},
51 { "simple-publisher.pub", "pub2pdf.pdf"},
52 { "simple-text.txt", "txt2pdf.pdf"},
53 { "simple-rtf.rtf", "rtf2pdf.pdf"},
54 { "simple-emf.emf", "emf2pdf.pdf"},
55 { "simple-webpage.mht", "mht2pdf.pdf"},
56 { "simple-webpage.html", "html2pdf.pdf"}
57};
58
59int ConvertSpecificFormats(); // convert to/from PDF, XPS, EMF, SVG
60int ConvertToPdfFromFile(); // convert from a file to PDF automatically
61
62int main(int argc, char *argv[])
63{
64
65//Virtual printer only available on Windows
66#if defined(_WIN32)
67 // The first step in every application using PDFNet is to initialize the
68 // library. The library is usually initialized only once, but calling
69 // Initialize() multiple times is also fine.
70 int err = 0;
71
72 PDFNet::Initialize(LicenseKey);
73
74 // Demonstrate Convert::ToPdf and Convert::Printer
75 err = ConvertToPdfFromFile();
76 if (err)
77 {
78 cout << "ConvertFile failed" << endl;
79 }
80 else
81 {
82 cout << "ConvertFile succeeded" << endl;
83 }
84
85 // Demonstrate Convert::[FromEmf, FromXps, ToEmf, ToSVG, ToXPS]
86 err = ConvertSpecificFormats();
87 if (err)
88 {
89 cout << "ConvertSpecificFormats failed" << endl;
90 }
91 else
92 {
93 cout << "ConvertSpecificFormats succeeded" << endl;
94 }
95
96 if (Convert::Printer::IsInstalled())
97 {
98 try
99 {
100 cout << "Uninstalling printer (requires Windows platform and administrator)" << endl;
101 Convert::Printer::Uninstall();
102 cout << "Uninstalled printer " << Convert::Printer::GetPrinterName().ConvertToAscii().c_str() << endl;;
103 }
104 catch (Common::Exception)
105 {
106 cout << "Unable to uninstall printer" << endl;
107 }
108 }
109
110 PDFNet::Terminate();
111 cout << "Done.\n";
112 return err;
113#else
114 cout << "ConvertPrintTest only available on Windows\n";
115#endif // defined(_WIN32)
116
117}
118
119int ConvertToPdfFromFile()
120{
121 int ret = 0;
122
123 if( Convert::Printer::IsInstalled("PDFTron PDFNet") )
124 {
125 Convert::Printer::SetPrinterName("PDFTron PDFNet");
126 }
127 else if (!Convert::Printer::IsInstalled())
128 {
129 try
130 {
131 // This will fail if not run as administrator. Harmless if PDFNet
132 // printer already installed
133 cout << "Installing printer (requires Windows platform and administrator)\n";
134 Convert::Printer::Install();
135 cout << "Installed printer " << Convert::Printer::GetPrinterName().ConvertToAscii().c_str() << endl;
136 }
137 catch (Common::Exception)
138 {
139 cout << "Unable to install printer" << endl;
140 }
141 }
142
143 unsigned int ceTestfiles = sizeof (testfiles) / sizeof (Testfile);
144
145 for (unsigned int i = 0; i < ceTestfiles; i++)
146 {
147 try
148 {
149 PDFDoc pdfdoc;
150 UString inputFile = inputPath + testfiles[i].inputFile;
151 UString outputFile = outputPath + testfiles[i].outputFile;
152 if (Convert::RequiresPrinter(inputFile))
153 {
154 cout << "Using PDFNet printer to convert file " << testfiles[i].inputFile << endl;
155 }
156 Convert::ToPdf(pdfdoc, inputFile);
157 pdfdoc.Save(outputFile, SDF::SDFDoc::e_linearized, NULL);
158 cout << "Converted file: " << testfiles[i].inputFile << endl << "to: " << testfiles[i].outputFile << endl;
159 }
160 catch (Common::Exception& e)
161 {
162 cout << "Unable to convert file " << testfiles[i].inputFile.ConvertToAscii().c_str() << endl;
163 cout << e << endl;
164 ret = 1;
165 }
166 catch (...)
167 {
168 cout << "Unknown Exception" << endl;
169 ret = 1;
170 }
171 }
172
173 return ret;
174}
175
176int ConvertSpecificFormats()
177{
178 //////////////////////////////////////////////////////////////////////////
179 int ret = 0;
180 try
181 {
182 PDFDoc pdfdoc;
183
184 cout << "Converting from EMF" << endl;
185 Convert::FromEmf(pdfdoc, inputPath + "simple-emf.emf");
186 pdfdoc.Save(outputPath + "emf2pdf v2.pdf", SDF::SDFDoc::e_remove_unused, NULL);
187 cout << "Saved emf2pdf v2.pdf" << endl;
188 }
189 catch (Common::Exception& e)
190 {
191 cout << e << endl;
192 ret = 1;
193 }
194 catch (...)
195 {
196 cout << "Unknown Exception" << endl;
197 ret = 1;
198 }
199
200 //////////////////////////////////////////////////////////////////////////
201 try
202 {
203 // Convert MSWord document to XPS
204 cout << "Converting DOCX to XPS" << endl;
205 Convert::ToXps(inputPath + "simple-word_2007.docx", outputPath + "simple-word_2007.xps");
206 cout << "Saved simple-word_2007.xps" << endl;
207 }
208 catch (Common::Exception& e)
209 {
210 cout << e << endl;
211 ret = 1;
212 }
213 catch (...)
214 {
215 cout << "Unknown Exception" << endl;
216 ret = 1;
217 }
218
219 return ret;
220}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales