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