Convert Between File Types - Go Sample Code

Sample code to use Apryse Server SDK for direct, high-quality conversion between PDF, XPS, SVG, TIFF, PNG, JPEG, and other image formats ('pdftron.PDF.Convert' namespace); provided in Python, C++, C#, Java, JavaScript, PHP, Ruby, Go and VB. The sample also shows how to convert MS Office files using our built in conversion. Learn more about our Server SDK and PDF Conversion Library.

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 "runtime"
10 . "pdftron"
11)
12
13import "pdftron/Samples/LicenseKey/GO"
14
15//---------------------------------------------------------------------------------------
16// The following sample illustrates how to use the PDF.Convert utility class to convert
17// documents and files to PDF, XPS, SVG, or EMF.
18//
19// Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
20// converted to PDF or XPS. Other formats are converted using a virtual driver. To check
21// if ToPDF (or ToXPS) require that PDFNet printer is installed use Convert.RequiresPrinter(filename).
22// The installing application must be run as administrator. The manifest for this sample
23// specifies appropriate the UAC elevation.
24//
25// Note: the PDFNet printer is a virtual XPS printer supported on Vista SP1 and Windows 7.
26// For Windows XP SP2 or higher, or Vista SP0 you need to install the XPS Essentials Pack (or
27// equivalent redistributables). You can download the XPS Essentials Pack from:
28// http://www.microsoft.com/downloads/details.aspx?FamilyId=B8DCFFDD-E3A5-44CC-8021-7649FD37FFEE&displaylang=en
29// Windows XP Sp2 will also need the Microsoft Core XML Services (MSXML) 6.0:
30// http://www.microsoft.com/downloads/details.aspx?familyid=993C0BCF-3BCF-4009-BE21-27E85E1857B1&displaylang=en
31//
32// Note: Convert.fromEmf and Convert.toEmf will only work on Windows and require GDI+.
33//
34// Please contact us if you have any questions.
35//---------------------------------------------------------------------------------------
36
37// Relative path to the folder containing the test files.
38var inputPath = "../../TestFiles/"
39var outputPath = "../../TestFiles/Output/"
40
41func ConvertToPdfFromFile() bool{
42 testFiles := [][]string{
43 {"simple-word_2007.docx","docx2pdf.pdf", "false"},
44 {"simple-powerpoint_2007.pptx","pptx2pdf.pdf", "false"},
45 {"simple-excel_2007.xlsx","xlsx2pdf.pdf", "false"},
46 {"simple-publisher.pub","pub2pdf.pdf", "true"},
47 //{"simple-visio.vsd","vsd2pdf.pdf}, // requires Microsoft Office Visio
48 {"simple-text.txt","txt2pdf.pdf", "false"},
49 {"simple-rtf.rtf","rtf2pdf.pdf", "true"},
50 {"butterfly.png","png2pdf.pdf", "false"},
51 {"simple-emf.emf","emf2pdf.pdf", "true"},
52 {"simple-xps.xps","xps2pdf.pdf", "false"},
53 //{"simple-webpage.mht","mht2pdf.pdf", true},
54 {"simple-webpage.html","html2pdf.pdf", "true"}}
55
56 ret := false
57
58 if runtime.GOOS == "windows" {
59 if PrinterIsInstalled("PDFTron PDFNet"){
60 PrinterSetPrinterName("PDFTron PDFNet")
61 }else if ! PrinterIsInstalled(){
62 fmt.Println("Installing printer (requires Windows platform and administrator)")
63 PrinterInstall()
64 fmt.Println("Installed printer " + PrinterGetPrinterName())
65 }
66 }
67
68 for _, testfile := range testFiles {
69 if runtime.GOOS != "windows" {
70 if testfile[2] == "true" {
71 continue
72 }
73 }
74 pdfdoc := NewPDFDoc()
75 inputFile := testfile[0]
76 outputFile := testfile[1]
77 if ConvertRequiresPrinter(inputPath + inputFile){
78 fmt.Println("Using PDFNet printer to convert file " + inputFile)
79 }
80 ConvertToPdf(pdfdoc, inputPath + inputFile)
81 pdfdoc.Save(outputPath + outputFile, uint(SDFDocE_compatibility))
82 pdfdoc.Close()
83 fmt.Println("Converted file: " + inputFile + "\nto: " + outputFile)
84 }
85 return ret
86}
87
88func ConvertSpecificFormats() bool{
89 ret := false
90 // Start with a PDFDoc to collect the converted documents
91 pdfdoc := NewPDFDoc()
92 s1 := inputPath + "simple-xps.xps"
93 // Convert the XPS document to PDF
94 fmt.Println("Converting from XPS")
95 ConvertFromXps(pdfdoc, s1)
96 outputFile := "xps2pdf v2.pdf"
97 pdfdoc.Save(outputPath + outputFile, uint(SDFDocE_remove_unused))
98 fmt.Println("Saved " + outputFile)
99
100 // Convert the EMF document to PDF
101 if runtime.GOOS == "windows" {
102 s1 = inputPath + "simple-emf.emf"
103 fmt.Println("Converting from EMF")
104 ConvertFromEmf(pdfdoc, s1)
105 outputFile = "emf2pdf v2.pdf"
106 pdfdoc.Save(outputPath + outputFile, uint(SDFDocE_remove_unused))
107 fmt.Println("Saved " + outputFile)
108 }
109
110 // Convert the TXT document to PDF
111 set := NewObjSet()
112 options := set.CreateDict()
113 // Put options
114 options.PutNumber("FontSize", 15)
115 options.PutBool("UseSourceCodeFormatting", true)
116 options.PutNumber("PageWidth", 12)
117 options.PutNumber("PageHeight", 6)
118 s1 = inputPath + "simple-text.txt"
119 fmt.Println("Converting from txt")
120 ConvertFromText(pdfdoc, s1)
121 outputFile = "simple-text.pdf"
122 pdfdoc.Save(outputPath + outputFile, uint(SDFDocE_remove_unused))
123 fmt.Println("Saved " + outputFile)
124
125 // Convert the two page PDF document to SVG
126 outputFile = "pdf2svg v2.svg"
127 pdfdoc = NewPDFDoc(inputPath + "newsletter.pdf")
128 fmt.Println("Converting pdfdoc to SVG")
129 ConvertToSvg(pdfdoc, outputPath + outputFile)
130 fmt.Println("Saved " + outputFile)
131
132 // Convert the PNG image to XPS
133 fmt.Println("Converting PNG to XPS")
134 outputFile = "butterfly.xps"
135 ConvertToXps(inputPath + "butterfly.png", outputPath +outputFile)
136 fmt.Println("Saved " + outputFile)
137
138 // Convert PDF document to XPS
139 fmt.Println("Converting PDF to XPS")
140 outputFile = "newsletter.xps"
141 ConvertToXps(inputPath + "newsletter.pdf", outputPath + outputFile)
142 fmt.Println("Saved " + outputFile)
143
144 // Convert PDF document to HTML
145 fmt.Println("Converting PDF to HTML")
146 outputFile = "newsletter"
147 ConvertToHtml(inputPath + "newsletter.pdf", outputPath + outputFile)
148 fmt.Println("Saved newsletter as HTML")
149
150 // Convert PDF document to EPUB
151 fmt.Println("Converting PDF to EPUB")
152 outputFile = "newsletter.epub"
153 ConvertToEpub(inputPath + "newsletter.pdf", outputPath + outputFile)
154 fmt.Println("Saved " + outputFile)
155
156 fmt.Println("Converting PDF to multipage TIFF")
157 tiffOptions := NewTiffOutputOptions()
158 tiffOptions.SetDPI(200)
159 tiffOptions.SetDither(true)
160 tiffOptions.SetMono(true)
161 ConvertToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiffOptions)
162 fmt.Println("Saved newsletter.tiff")
163
164 return ret
165}
166
167func main(){
168 // The first step in every application using PDFNet is to initialize the
169 // library. The library is usually initialized only once, but calling
170 // Initialize() multiple times is also fine.
171 PDFNetInitialize(PDFTronLicense.Key)
172
173 // Demonstrate Convert.ToPdf and Convert.Printer
174 err := ConvertToPdfFromFile()
175 if err{
176 fmt.Println("ConvertFile failed")
177 }else{
178 fmt.Println("ConvertFile succeeded")
179 }
180 // Demonstrate Convert.[FromEmf, FromXps, ToEmf, ToSVG, ToXPS]
181 err = ConvertSpecificFormats()
182 if err{
183 fmt.Println("ConvertSpecificFormats failed")
184 }else{
185 fmt.Println("ConvertSpecificFormats succeeded")
186 }
187 if runtime.GOOS == "windows" {
188 fmt.Println("Uninstalling printer (requires Windows platform and administrator)")
189 PrinterUninstall()
190 fmt.Println("Uninstalled printer " + PrinterGetPrinterName())
191 }
192 PDFNetTerminate()
193 fmt.Println("Done.")
194}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales