88 "testing"
99 "fmt"
1010 "flag"
11- "runtime"
1211 . "github.com/pdftron/pdftron-go/v2"
1312)
1413
@@ -21,23 +20,12 @@ func init() {
2120}
2221
2322//---------------------------------------------------------------------------------------
24- // The following sample illustrates how to use the PDF.Convert utility class to convert
25- // documents and files to PDF, XPS, SVG, or EMF.
23+ // The following sample illustrates how to use the PDF::Convert utility class to convert
24+ // documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
25+ // using our built in conversion.
2626//
2727// Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
28- // converted to PDF or XPS. Other formats are converted using a virtual driver. To check
29- // if ToPDF (or ToXPS) require that PDFNet printer is installed use Convert.RequiresPrinter(filename).
30- // The installing application must be run as administrator. The manifest for this sample
31- // specifies appropriate the UAC elevation.
32- //
33- // Note: the PDFNet printer is a virtual XPS printer supported on Vista SP1 and Windows 7.
34- // For Windows XP SP2 or higher, or Vista SP0 you need to install the XPS Essentials Pack (or
35- // equivalent redistributables). You can download the XPS Essentials Pack from:
36- // http://www.microsoft.com/downloads/details.aspx?FamilyId=B8DCFFDD-E3A5-44CC-8021-7649FD37FFEE&displaylang=en
37- // Windows XP Sp2 will also need the Microsoft Core XML Services (MSXML) 6.0:
38- // http://www.microsoft.com/downloads/details.aspx?familyid=993C0BCF-3BCF-4009-BE21-27E85E1857B1&displaylang=en
39- //
40- // Note: Convert.fromEmf and Convert.toEmf will only work on Windows and require GDI+.
28+ // converted to PDF or XPS.
4129//
4230// Please contact us if you have any questions.
4331//---------------------------------------------------------------------------------------
@@ -46,51 +34,6 @@ func init() {
4634var inputPath = "../TestFiles/"
4735var outputPath = "../TestFiles/Output/"
4836
49- func ConvertToPdfFromFile () bool {
50- testFiles := [][]string {
51- {"simple-word_2007.docx" ,"docx2pdf.pdf" , "false" },
52- {"simple-powerpoint_2007.pptx" ,"pptx2pdf.pdf" , "false" },
53- {"simple-excel_2007.xlsx" ,"xlsx2pdf.pdf" , "false" },
54- {"simple-publisher.pub" ,"pub2pdf.pdf" , "true" },
55- //{"simple-visio.vsd","vsd2pdf.pdf}, // requires Microsoft Office Visio
56- {"simple-text.txt" ,"txt2pdf.pdf" , "false" },
57- {"simple-rtf.rtf" ,"rtf2pdf.pdf" , "true" },
58- {"butterfly.png" ,"png2pdf.pdf" , "false" },
59- {"simple-emf.emf" ,"emf2pdf.pdf" , "true" },
60- {"simple-xps.xps" ,"xps2pdf.pdf" , "false" },
61- //{"simple-webpage.mht","mht2pdf.pdf", true},
62- {"simple-webpage.html" ,"html2pdf.pdf" , "true" }}
63- ret := false
64-
65- if runtime .GOOS == "windows" {
66- if PrinterIsInstalled ("PDFTron PDFNet" ){
67- PrinterSetPrinterName ("PDFTron PDFNet" )
68- }else if ! PrinterIsInstalled (){
69- fmt .Println ("Installing printer (requires Windows platform and administrator)" )
70- PrinterInstall ()
71- fmt .Println ("Installed printer " + PrinterGetPrinterName ())
72- }
73- }
74-
75- for _ , testfile := range testFiles {
76- if runtime .GOOS != "windows" {
77- if testfile [2 ] == "true" {
78- continue
79- }
80- }
81- pdfdoc := NewPDFDoc ()
82- inputFile := testfile [0 ]
83- outputFile := testfile [1 ]
84- if ConvertRequiresPrinter (inputPath + inputFile ){
85- fmt .Println ("Using PDFNet printer to convert file " + inputFile )
86- }
87- ConvertToPdf (pdfdoc , inputPath + inputFile )
88- pdfdoc .Save (outputPath + outputFile , uint (SDFDocE_compatibility ))
89- pdfdoc .Close ()
90- fmt .Println ("Converted file: " + inputFile + "\n to: " + outputFile )
91- }
92- return ret
93- }
9437
9538func ConvertSpecificFormats () bool {
9639 ret := false
@@ -104,15 +47,6 @@ func ConvertSpecificFormats() bool{
10447 pdfdoc .Save (outputPath + outputFile , uint (SDFDocE_remove_unused ))
10548 fmt .Println ("Saved " + outputFile )
10649
107- // Convert the EMF document to PDF
108- if runtime .GOOS == "windows" {
109- s1 = inputPath + "simple-emf.emf"
110- fmt .Println ("Converting from EMF" )
111- ConvertFromEmf (pdfdoc , s1 )
112- outputFile = "emf2pdf v2.pdf"
113- pdfdoc .Save (outputPath + outputFile , uint (SDFDocE_remove_unused ))
114- fmt .Println ("Saved " + outputFile )
115- }
11650
11751 // Convert the TXT document to PDF
11852 set := NewObjSet ()
@@ -177,6 +111,31 @@ func ConvertSpecificFormats() bool{
177111
178112 return ret
179113}
114+ func ConvertToPdfFromFile () bool {
115+ testFiles := [][]string {
116+ {"simple-word_2007.docx" ,"docx2pdf.pdf" },
117+ {"simple-powerpoint_2007.pptx" ,"pptx2pdf.pdf" },
118+ {"simple-excel_2007.xlsx" ,"xlsx2pdf.pdf" },
119+
120+ {"simple-text.txt" ,"txt2pdf.pdf" },
121+ {"butterfly.png" ,"png2pdf.pdf" },
122+ {"simple-xps.xps" ,"xps2pdf.pdf" }}
123+ ret := false
124+
125+
126+ for _ , testfile := range testFiles {
127+
128+ pdfdoc := NewPDFDoc ()
129+ inputFile := testfile [0 ]
130+ outputFile := testfile [1 ]
131+ PrinterSetMode (PrinterE_prefer_builtin_converter )
132+ ConvertToPdf (pdfdoc , inputPath + inputFile )
133+ pdfdoc .Save (outputPath + outputFile , uint (SDFDocE_linearized ))
134+ pdfdoc .Close ()
135+ fmt .Println ("Converted file: " + inputFile + "\n to: " + outputFile )
136+ }
137+ return ret
138+ }
180139
181140func TestConvert (t * testing.T ){
182141 // The first step in every application using PDFNet is to initialize the
@@ -198,11 +157,7 @@ func TestConvert(t *testing.T){
198157 }else {
199158 fmt .Println ("ConvertSpecificFormats succeeded" )
200159 }
201- if runtime .GOOS == "windows" {
202- fmt .Println ("Uninstalling printer (requires Windows platform and administrator)" )
203- PrinterUninstall ()
204- fmt .Println ("Uninstalled printer " + PrinterGetPrinterName ())
205- }
160+
206161 PDFNetTerminate ()
207162 fmt .Println ("Done." )
208163}
0 commit comments