Convert File Types with Virtual Printer on Windows - Go Sample Code

Sample code to convert to PDF with virtual printer on Windows; provided in Python, C++, C#, Java, Node.js (JavaScript), Go and VB. It supports several input formats like docx, xlsx, rtf, txt, html, pub, emf, etc.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "testing"
9 "fmt"
10 "flag"
11 "runtime"
12 . "github.com/ApryseSDK/pdftron-go/v2"
13)
14
15var licenseKey string
16var modulePath string
17
18func init() {
19 flag.StringVar(&licenseKey, "license", "", "License key for Apryse SDK")
20 flag.StringVar(&modulePath, "modulePath", "", "Module path for Apryse SDK")
21}
22
23//---------------------------------------------------------------------------------------
24// The following sample illustrates how to convert to PDF with virtual printer on Windows.
25// It supports several input formats like docx, xlsx, rtf, txt, html, pub, emf, etc. For more details, visit
26// https://docs.apryse.com/windows/guides/features/conversion/convert-other/
27//
28// To check if ToPDF (or ToXPS) require that PDFNet printer is installed use Convert::RequiresPrinter(filename).
29// The installing application must be run as administrator. The manifest for this sample
30// specifies appropriate the UAC elevation.
31//
32// Note: the PDFNet printer is a virtual XPS printer supported on Vista SP1 and Windows 7.
33// For Windows XP SP2 or higher, or Vista SP0 you need to install the XPS Essentials Pack (or
34// equivalent redistributables). You can download the XPS Essentials Pack from:
35// http://www.microsoft.com/downloads/details.aspx?FamilyId=B8DCFFDD-E3A5-44CC-8021-7649FD37FFEE&displaylang=en
36// Windows XP Sp2 will also need the Microsoft Core XML Services (MSXML) 6.0:
37// http://www.microsoft.com/downloads/details.aspx?familyid=993C0BCF-3BCF-4009-BE21-27E85E1857B1&displaylang=en
38//
39// Note: Convert.fromEmf and Convert.toEmf will only work on Windows and require GDI+.
40//
41// Please contact us if you have any questions.
42//---------------------------------------------------------------------------------------
43
44// Relative path to the folder containing the test files.
45var inputPath = "../../TestFiles/"
46var outputPath = "../../TestFiles/Output/"
47
48
49func ConvertSpecificFormats() bool{
50 ret := false
51
52
53 // Convert MSWord document to XPS
54 fmt.Println("Converting DOCX to XPS")
55 outputFile := "simple-word_2007.xps"
56 ConvertToXps(inputPath + "simple-word_2007.docx", outputPath + outputFile)
57 fmt.Println("Saved " + outputFile)
58
59 // Start with a PDFDoc to collect the converted documents
60 pdfdoc := NewPDFDoc()
61 // Convert the EMF document to PDF
62 s1 := inputPath + "simple-emf.emf"
63
64 fmt.Println("Converting from EMF")
65 ConvertFromEmf(pdfdoc, s1)
66 outputFile = "emf2pdf v2.pdf"
67 pdfdoc.Save(outputPath + outputFile, uint(SDFDocE_remove_unused))
68 fmt.Println("Saved " + outputFile)
69
70 return ret
71}
72func ConvertToPdfFromFile() bool{
73 testFiles := [][]string{
74 {"simple-word_2007.docx","docx2pdf.pdf"},
75 {"simple-powerpoint_2007.pptx","pptx2pdf.pdf"},
76 {"simple-excel_2007.xlsx","xlsx2pdf.pdf"},
77 {"simple-publisher.pub","pub2pdf.pdf"},
78 {"simple-text.txt","txt2pdf.pdf"},
79 { "simple-rtf.rtf","rtf2pdf.pdf"},
80 { "simple-emf.emf","emf2pdf.pdf"},
81 { "simple-webpage.mht","mht2pdf.pdf"},
82 { "simple-webpage.html","html2pdf.pdf"}}
83 ret := false
84
85 if PrinterIsInstalled("PDFTron PDFNet"){
86 PrinterSetPrinterName("PDFTron PDFNet")
87 }else if ! PrinterIsInstalled(){
88 fmt.Println("Installing printer (requires Windows platform and administrator)")
89 PrinterInstall()
90 fmt.Println("Installed printer " + PrinterGetPrinterName())
91 }
92
93 for _, testfile := range testFiles {
94
95 pdfdoc := NewPDFDoc()
96 inputFile := testfile[0]
97 outputFile := testfile[1]
98 if ConvertRequiresPrinter(inputPath + inputFile){
99 fmt.Println("Using PDFNet printer to convert file " + inputFile)
100 }
101 ConvertToPdf(pdfdoc, inputPath + inputFile)
102 pdfdoc.Save(outputPath + outputFile, uint(SDFDocE_linearized))
103 pdfdoc.Close()
104 fmt.Println("Converted file: " + inputFile + "\nto: " + outputFile)
105 }
106 return ret
107}
108
109func TestConvertPrint(t *testing.T){
110 if runtime.GOOS == "windows" {
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 PDFNetInitialize(licenseKey)
115
116 // Demonstrate Convert.ToPdf and Convert.Printer
117 err := ConvertToPdfFromFile()
118 if err{
119 fmt.Println("ConvertFile failed")
120 }else{
121 fmt.Println("ConvertFile succeeded")
122 }
123 // Demonstrate Convert.[FromEmf, FromXps, ToEmf, ToSVG, ToXPS]
124 err = ConvertSpecificFormats()
125 if err{
126 fmt.Println("ConvertSpecificFormats failed")
127 }else{
128 fmt.Println("ConvertSpecificFormats succeeded")
129 }
130 fmt.Println("Uninstalling printer (requires Windows platform and administrator)")
131 PrinterUninstall()
132 fmt.Println("Uninstalled printer " + PrinterGetPrinterName())
133
134 PDFNetTerminate()
135 fmt.Println("Done.")
136 }else{
137 fmt.Println("ConvertPrintTest only available on Windows")
138 }
139}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales