PDF2Office - Convert PDF to DOCX, XSLX - C++ Sample Code

Requirements

Sample code for using Apryse SDK to programmatically convert generic PDF documents to Word, Excel, PowerPoint; provided in Python, C++, C#, Go, Java, Node.js (JavaScript), PHP, Ruby and VB.

To convert files to Office with this Apryse Server SDK sample code:

  1. Complete the Get started with Server SDK process in your language/framework.
  2. After you complete the Get Started with Server SDK work in your language/framework from Step 1 above, next, download the Structured Output Module.
  3. Add sample code provided in this guide

To use this feature in production, your license key will need the Office Conversion Package. Trial keys already include all packages.

Learn more about our Server SDK and PDF to Office Conversion.

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/StructuredOutputModule.h>
11#include "../../LicenseKey/CPP/LicenseKey.h"
12
13//---------------------------------------------------------------------------------------
14// The following sample illustrates how to use the PDF::Convert utility class to convert
15// documents and files to Office.
16//
17// The Structured Output module is an optional PDFNet Add-on that can be used to convert PDF
18// and other documents into Word, Excel, PowerPoint and HTML format.
19//
20// The Apryse SDK Structured Output module can be downloaded from
21// https://docs.apryse.com/core/info/modules/
22//
23// Please contact us if you have any questions.
24//---------------------------------------------------------------------------------------
25
26using namespace pdftron;
27using namespace PDF;
28using namespace std;
29
30UString inputPath("../../TestFiles/");
31UString outputPath("../../TestFiles/Output/");
32
33int main(int argc, char *argv[])
34{
35 // The first step in every application using PDFNet is to initialize the
36 // library. The library is usually initialized only once, but calling
37 // Initialize() multiple times is also fine.
38 PDFNet::Initialize(LicenseKey);
39
40 PDFNet::AddResourceSearchPath("../../../Lib/");
41
42 if (!StructuredOutputModule::IsModuleAvailable())
43 {
44 cout << endl;
45 cout << "Unable to run the sample: Apryse SDK Structured Output module not available." << endl;
46 cout << "-----------------------------------------------------------------------------" << endl;
47 cout << "The Structured Output module is an optional add-on, available for download" << endl;
48 cout << "at https://docs.apryse.com/core/info/modules/. If you have already" << endl;
49 cout << "downloaded this module, ensure that the SDK is able to find the required files" << endl;
50 cout << "using the PDFNet::AddResourceSearchPath() function." << endl;
51 cout << endl;
52 return 1;
53 }
54
55 int err = 0;
56
57 //////////////////////////////////////////////////////////////////////////
58 // Word
59 //////////////////////////////////////////////////////////////////////////
60
61 try
62 {
63 // Convert PDF document to Word
64 cout << "Converting PDF to Word" << endl;
65
66 UString outputFile = outputPath + "paragraphs_and_tables.docx";
67
68 Convert::ToWord(inputPath + "paragraphs_and_tables.pdf", outputFile);
69
70 cout << "Result saved in " << outputFile.ConvertToUtf8().c_str() << endl;
71 }
72 catch (Common::Exception& e)
73 {
74 cout << "Unable to convert PDF document to Word, error: " << e << endl;
75 err = 1;
76 }
77 catch (...)
78 {
79 cout << "Unknown Exception" << endl;
80 err = 1;
81 }
82
83 //////////////////////////////////////////////////////////////////////////
84
85 try
86 {
87 // Convert PDF document to Word with options
88 cout << "Converting PDF to Word with options" << endl;
89
90 UString outputFile = outputPath + "paragraphs_and_tables_first_page.docx";
91
92 Convert::WordOutputOptions wordOutputOptions;
93
94 // Convert only the first page
95 wordOutputOptions.SetPages(1, 1);
96
97 Convert::ToWord(inputPath + "paragraphs_and_tables.pdf", outputFile, wordOutputOptions);
98
99 cout << "Result saved in " << outputFile.ConvertToUtf8().c_str() << endl;
100 }
101 catch (Common::Exception& e)
102 {
103 cout << "Unable to convert PDF document to Word, error: " << e << endl;
104 err = 1;
105 }
106 catch (...)
107 {
108 cout << "Unknown Exception" << endl;
109 err = 1;
110 }
111
112 //////////////////////////////////////////////////////////////////////////
113 // Excel
114 //////////////////////////////////////////////////////////////////////////
115
116 try
117 {
118 // Convert PDF document to Excel
119 cout << "Converting PDF to Excel" << endl;
120
121 UString outputFile = outputPath + "paragraphs_and_tables.xlsx";
122
123 Convert::ToExcel(inputPath + "paragraphs_and_tables.pdf", outputFile);
124
125 cout << "Result saved in " << outputFile.ConvertToUtf8().c_str() << endl;
126 }
127 catch (Common::Exception& e)
128 {
129 cout << "Unable to convert PDF document to Excel, error: " << e << endl;
130 err = 1;
131 }
132 catch (...)
133 {
134 cout << "Unknown Exception" << endl;
135 err = 1;
136 }
137
138 //////////////////////////////////////////////////////////////////////////
139
140 try
141 {
142 // Convert PDF document to Excel with options
143 cout << "Converting PDF to Excel with options" << endl;
144
145 UString outputFile = outputPath + "paragraphs_and_tables_second_page.xlsx";
146
147 Convert::ExcelOutputOptions excelOutputOptions;
148
149 // Convert only the second page
150 excelOutputOptions.SetPages(2, 2);
151
152 Convert::ToExcel(inputPath + "paragraphs_and_tables.pdf", outputFile, excelOutputOptions);
153
154 cout << "Result saved in " << outputFile.ConvertToUtf8().c_str() << endl;
155 }
156 catch (Common::Exception& e)
157 {
158 cout << "Unable to convert PDF document to Excel, error: " << e << endl;
159 err = 1;
160 }
161 catch (...)
162 {
163 cout << "Unknown Exception" << endl;
164 err = 1;
165 }
166
167 //////////////////////////////////////////////////////////////////////////
168 // PowerPoint
169 //////////////////////////////////////////////////////////////////////////
170
171 try
172 {
173 // Convert PDF document to PowerPoint
174 cout << "Converting PDF to PowerPoint" << endl;
175
176 UString outputFile = outputPath + "paragraphs_and_tables.pptx";
177
178 Convert::ToPowerPoint(inputPath + "paragraphs_and_tables.pdf", outputFile);
179
180 cout << "Result saved in " << outputFile.ConvertToUtf8().c_str() << endl;
181 }
182 catch (Common::Exception& e)
183 {
184 cout << "Unable to convert PDF document to PowerPoint, error: " << e << endl;
185 err = 1;
186 }
187 catch (...)
188 {
189 cout << "Unknown Exception" << endl;
190 err = 1;
191 }
192
193 //////////////////////////////////////////////////////////////////////////
194
195 try
196 {
197 // Convert PDF document to PowerPoint with options
198 cout << "Converting PDF to PowerPoint with options" << endl;
199
200 UString outputFile = outputPath + "paragraphs_and_tables_first_page.pptx";
201
202 Convert::PowerPointOutputOptions powerPointOutputOptions;
203
204 // Convert only the first page
205 powerPointOutputOptions.SetPages(1, 1);
206
207 Convert::ToPowerPoint(inputPath + "paragraphs_and_tables.pdf", outputFile, powerPointOutputOptions);
208
209 cout << "Result saved in " << outputFile.ConvertToUtf8().c_str() << endl;
210 }
211 catch (Common::Exception& e)
212 {
213 cout << "Unable to convert PDF document to PowerPoint, error: " << e << endl;
214 err = 1;
215 }
216 catch (...)
217 {
218 cout << "Unknown Exception" << endl;
219 err = 1;
220 }
221
222 //////////////////////////////////////////////////////////////////////////
223
224 PDFNet::Terminate();
225 cout << "Done.\n";
226 return err;
227}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales