PDF2Office - Convert PDF to DOCX, XSLX - Go Sample Code

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 run this sample:

  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.

Learn more about our Server SDK and PDF to Office Conversion

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 . "pdftron"
10)
11
12import "pdftron/Samples/LicenseKey/GO"
13
14//---------------------------------------------------------------------------------------
15// The following sample illustrates how to use the PDF::Convert utility class to convert
16// documents and files to Word, Excel and PowerPoint.
17//
18// The Structured Output module is an optional PDFNet Add-on that can be used to convert PDF
19// and other documents into Word, Excel, PowerPoint and HTML format.
20//
21// The PDFTron SDK Structured Output module can be downloaded from
22// https://docs.apryse.com/core/info/modules/
23//
24// Please contact us if you have any questions.
25//---------------------------------------------------------------------------------------
26
27// Relative path to the folder containing the test files.
28var inputPath = "../../TestFiles/"
29var outputPath = "../../TestFiles/Output/"
30
31//---------------------------------------------------------------------------------------
32
33func catch(err *error) {
34 if r := recover(); r != nil {
35 *err = fmt.Errorf("%v", r)
36 }
37}
38
39//---------------------------------------------------------------------------------------
40
41func ConvertToWordTest() (err error) {
42 defer catch(&err)
43
44 // Convert PDF document to Word
45 fmt.Println("Converting PDF to Word")
46
47 inputFile := inputPath + "paragraphs_and_tables.pdf"
48 outputFile := outputPath + "paragraphs_and_tables.docx"
49
50 // Convert to Word
51 ConvertToWord(inputFile, outputFile)
52
53 fmt.Println("Result saved in " + outputFile)
54 return nil
55}
56
57//---------------------------------------------------------------------------------------
58
59func ConvertToWordWithOptionsTest() (err error) {
60 defer catch(&err)
61
62 // Convert PDF document to Word with options
63 fmt.Println("Converting PDF to Word with options")
64
65 inputFile := inputPath + "paragraphs_and_tables.pdf"
66 outputFile := outputPath + "paragraphs_and_tables_first_page.docx"
67
68 wordOutputOptions := NewWordOutputOptions()
69
70 // Convert only the first page
71 wordOutputOptions.SetPages(1, 1)
72
73 // Convert to Word
74 ConvertToWord(inputFile, outputFile, wordOutputOptions)
75
76 fmt.Println("Result saved in " + outputFile)
77 return nil
78}
79
80//---------------------------------------------------------------------------------------
81
82func ConvertToExcelTest() (err error) {
83 defer catch(&err)
84
85 // Convert PDF document to Excel
86 fmt.Println("Converting PDF to Excel")
87
88 inputFile := inputPath + "paragraphs_and_tables.pdf"
89 outputFile := outputPath + "paragraphs_and_tables.xlsx"
90
91 // Convert to Excel
92 ConvertToExcel(inputFile, outputFile)
93
94 fmt.Println("Result saved in " + outputFile)
95 return nil
96}
97
98//---------------------------------------------------------------------------------------
99
100func ConvertToExcelWithOptionsTest() (err error) {
101 defer catch(&err)
102
103 // Convert PDF document to Excel with options
104 fmt.Println("Converting PDF to Excel with options")
105
106 inputFile := inputPath + "paragraphs_and_tables.pdf"
107 outputFile := outputPath + "paragraphs_and_tables_second_page.xlsx"
108
109 excelOutputOptions := NewExcelOutputOptions()
110
111 // Convert only the second page
112 excelOutputOptions.SetPages(2, 2)
113
114 // Convert to Excel
115 ConvertToExcel(inputFile, outputFile, excelOutputOptions)
116
117 fmt.Println("Result saved in " + outputFile)
118 return nil
119}
120
121//---------------------------------------------------------------------------------------
122
123func ConvertToPowerPointTest() (err error) {
124 defer catch(&err)
125
126 // Convert PDF document to PowerPoint
127 fmt.Println("Converting PDF to PowerPoint")
128
129 inputFile := inputPath + "paragraphs_and_tables.pdf"
130 outputFile := outputPath + "paragraphs_and_tables.pptx"
131
132 // Convert to PowerPoint
133 ConvertToPowerPoint(inputFile, outputFile)
134
135 fmt.Println("Result saved in " + outputFile)
136 return nil
137}
138
139//---------------------------------------------------------------------------------------
140
141func ConvertToPowerPointWithOptionsTest() (err error) {
142 defer catch(&err)
143
144 // Convert PDF document to PowerPoint with options
145 fmt.Println("Converting PDF to PowerPoint with options")
146
147 inputFile := inputPath + "paragraphs_and_tables.pdf"
148 outputFile := outputPath + "paragraphs_and_tables_first_page.pptx"
149
150 powerPointOutputOptions := NewPowerPointOutputOptions()
151
152 // Convert only the first page
153 powerPointOutputOptions.SetPages(1, 1)
154
155 // Convert to PowerPoint
156 ConvertToPowerPoint(inputFile, outputFile, powerPointOutputOptions)
157
158 fmt.Println("Result saved in " + outputFile)
159 return nil
160}
161
162//---------------------------------------------------------------------------------------
163
164func main() {
165 // The first step in every application using PDFNet is to initialize the
166 // library. The library is usually initialized only once, but calling
167 // Initialize() multiple times is also fine.
168 PDFNetInitialize(PDFTronLicense.Key)
169
170 //-----------------------------------------------------------------------------------
171
172 PDFNetAddResourceSearchPath("../../../PDFNetC/Lib/")
173
174 if !StructuredOutputModuleIsModuleAvailable() {
175 fmt.Println("")
176 fmt.Println("Unable to run the sample: PDFTron SDK Structured Output module not available.")
177 fmt.Println("-----------------------------------------------------------------------------")
178 fmt.Println("The Structured Output module is an optional add-on, available for download")
179 fmt.Println("at https://docs.apryse.com/core/info/modules/. If you have already")
180 fmt.Println("downloaded this module, ensure that the SDK is able to find the required files")
181 fmt.Println("using the PDFNet::AddResourceSearchPath() function.")
182 fmt.Println("")
183 return
184 }
185
186 //-----------------------------------------------------------------------------------
187
188 // Convert PDF document to Word
189 err := ConvertToWordTest()
190 if err != nil {
191 fmt.Println(fmt.Errorf("Unable to convert PDF document to Word, error: %s", err))
192 }
193
194 //-----------------------------------------------------------------------------------
195
196 // Convert PDF document to Word with options
197 err = ConvertToWordWithOptionsTest()
198 if err != nil {
199 fmt.Println(fmt.Errorf("Unable to convert PDF document to Word, error: %s", err))
200 }
201
202 //-----------------------------------------------------------------------------------
203
204 // Convert PDF document to Excel
205 err = ConvertToExcelTest()
206 if err != nil {
207 fmt.Println(fmt.Errorf("Unable to convert PDF document to Excel, error: %s", err))
208 }
209
210 //-----------------------------------------------------------------------------------
211
212 // Convert PDF document to Excel with options
213 err = ConvertToExcelWithOptionsTest()
214 if err != nil {
215 fmt.Println(fmt.Errorf("Unable to convert PDF document to Excel, error: %s", err))
216 }
217
218 //-----------------------------------------------------------------------------------
219
220 // Convert PDF document to PowerPoint
221 err = ConvertToPowerPointTest()
222 if err != nil {
223 fmt.Println(fmt.Errorf("Unable to convert PDF document to PowerPoint, error: %s", err))
224 }
225
226 //-----------------------------------------------------------------------------------
227
228 // Convert PDF document to PowerPoint with options
229 err = ConvertToPowerPointWithOptionsTest()
230 if err != nil {
231 fmt.Println(fmt.Errorf("Unable to convert PDF document to PowerPoint, error: %s", err))
232 }
233
234 //-----------------------------------------------------------------------------------
235
236 PDFNetTerminate()
237 fmt.Println("Done.")
238}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales