DOCX, XSLX to PDF Conversion - OfficeToPDF - C++ Sample Code

Sample code for using Apryse Server SDK to convert Office documents to PDF (including Word, Excel, PowerPoint and Publisher) without needing any external dependencies or MS Office licenses. Office to PDF conversion can be performed on a Linux or Windows server to automate Office-centric workflows, or entirely in the user's client (web browser, mobile device). The conversion functionality can be combined with our Viewer to display or annotate Office files (docx, xlsx, pptx) on all major platforms, including Web, Android, iOS, Xamarin, UWP, and Windows. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB.

Learn more about our Server SDK and Office Document 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 <PDF/OfficeToPDFOptions.h>
11#include "../../LicenseKey/CPP/LicenseKey.h"
12
13//------------------------------------------------------------------------------
14// The following sample illustrates how to use the PDF::Convert utility class
15// to convert MS Office files to PDF
16//
17// This conversion is performed entirely within the PDFNet and has *no*
18// external or system dependencies dependencies -- Conversion results will be
19// the same whether on Windows, Linux or Android.
20//
21// Please contact us if you have any questions.
22//------------------------------------------------------------------------------
23
24using namespace pdftron;
25using namespace PDF;
26
27UString input_path = "../../TestFiles/";
28UString output_path = "../../TestFiles/Output/";
29
30void SimpleDocxConvert(UString input_filename, UString output_filename)
31{
32 // Start with a PDFDoc (the conversion destination)
33 PDFDoc pdfdoc;
34
35 // perform the conversion with no optional parameters
36 Convert::OfficeToPDF(pdfdoc, input_path + input_filename, NULL);
37
38 // save the result
39 pdfdoc.Save(output_path + output_filename, SDF::SDFDoc::e_linearized, NULL);
40
41 // And we're done!
42 std::cout << "Saved " << output_filename << std::endl;
43}
44
45void FlexibleDocxConvert(UString input_filename, UString output_filename)
46{
47 // Start with a PDFDoc (the conversion destination)
48 PDFDoc pdfdoc;
49
50 OfficeToPDFOptions options;
51
52 // set up smart font substitutions to improve conversion results
53 // in situations where the original fonts are not available
54 options.SetSmartSubstitutionPluginPath(input_path);
55
56 // create a conversion object -- this sets things up but does not yet
57 // perform any conversion logic.
58 // in a multithreaded environment, this object can be used to monitor
59 // the conversion progress and potentially cancel it as well
60 DocumentConversion conversion = Convert::StreamingPDFConversion(
61 pdfdoc, input_path + input_filename, &options);
62
63 // Print the progress of the conversion.
64 /*
65 std::cout << "Status: " << conversion.GetProgress()*100 << "%, "
66 << conversion.GetProgressLabel() << std::endl;
67 */
68
69 // actually perform the conversion
70 // this particular method will not throw on conversion failure, but will
71 // return an error status instead
72
73 while (conversion.GetConversionStatus() == DocumentConversion::eIncomplete)
74 {
75 conversion.ConvertNextPage();
76 // print out the progress status as we go
77 /*
78 std::cout << "Status: " << conversion.GetProgress()*100 << "%, "
79 << conversion.GetProgressLabel() << std::endl;
80 */
81 }
82
83 if(conversion.GetConversionStatus() == DocumentConversion::eSuccess)
84 {
85 int num_warnings = conversion.GetNumWarnings();
86
87 // print information about the conversion
88 for (int i = 0; i < num_warnings; ++i)
89 {
90 std::cout << "Conversion Warning: "
91 << conversion.GetWarningString(i) << std::endl;
92 }
93
94 // save the result
95 pdfdoc.Save(output_path + output_filename, SDF::SDFDoc::e_linearized, NULL);
96 // done
97 std::cout << "Saved " << output_filename << std::endl;
98 }
99 else
100 {
101 std::cout << "Encountered an error during conversion: "
102 << conversion.GetErrorString() << std::endl;
103 }
104
105
106}
107
108
109int main(int argc, char *argv[])
110{
111 // The first step in every application using PDFNet is to initialize the
112 // library. The library is usually initialized only once, but calling
113 // Initialize() multiple times is also fine.
114 int ret = 0;
115
116 PDFNet::Initialize(LicenseKey);
117 PDFNet::SetResourcesPath("../../../Resources");
118
119 try
120 {
121 // first the one-line conversion function
122 SimpleDocxConvert("Fishermen.docx", "Fishermen.pdf");
123
124 // then the more flexible line-by-line conversion API
125 FlexibleDocxConvert("the_rime_of_the_ancient_mariner.docx",
126 "the_rime_of_the_ancient_mariner.pdf");
127
128 // conversion of RTL content
129 FlexibleDocxConvert("factsheet_Arabic.docx", "factsheet_Arabic.pdf");
130 }
131 catch (Common::Exception& e)
132 {
133 std::cout << e << std::endl;
134 ret = 1;
135 }
136 catch (...)
137 {
138 std::cout << "Unknown Exception" << std::endl;
139 ret = 1;
140 }
141
142 PDFNet::Terminate();
143 std::cout << "Done.\n";
144 return ret;
145}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales