Sample C# code to use Apryse SDK for direct, high-quality conversion between PDF, XPS, SVG, TIFF, PNG, JPEG, and other image formats ('pdftron.PDF.Convert' namespace). 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-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using System.Drawing;
7using System.Drawing.Drawing2D;
8
9using pdftron;
10using pdftron.Common;
11using pdftron.Filters;
12using pdftron.SDF;
13using pdftron.PDF;
14
15namespace ConvertTestCS
16{
17 /// <summary>
18 // The following sample illustrates how to use the PDF::Convert utility class to convert
19 // documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
20 // using our built in conversion.
21 //
22 // Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
23 // converted to PDF or XPS.
24 //
25 // Also note that conversion under ASP.NET can be tricky to configure. Please see the following document for advice:
26 // http://www.pdftron.com/pdfnet/faq_files/Converting_Documents_in_Windows_Service_or_ASP.NET_Application_using_PDFNet.pdf
27 /// </summary>
28 class Testfile
29 {
30 public string inputFile, outputFile;
31 public Testfile(string inFile, string outFile)
32 {
33 inputFile = inFile;
34 outputFile = outFile;
35 }
36 };
37
38 class Class1
39 {
40 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
41 static Class1() { }
42
43 // Relative path to the folder containing test files.
44 const string inputPath = "../../../../TestFiles/";
45 const string outputPath = "../../../../TestFiles/Output/";
46
47 static bool ConvertSpecificFormats()
48 {
49 //////////////////////////////////////////////////////////////////////////
50 bool err = false;
51 try
52 {
53 using (PDFDoc pdfdoc = new PDFDoc())
54 {
55 Console.WriteLine("Converting from XPS");
56
57 pdftron.PDF.Convert.FromXps(pdfdoc, inputPath + "simple-xps.xps");
58 pdfdoc.Save(outputPath + "xps2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
59 Console.WriteLine("Saved xps2pdf v2.pdf");
60 }
61 }
62 catch (PDFNetException e)
63 {
64 Console.WriteLine(e.Message);
65 err = true;
66 }
67
68 //////////////////////////////////////////////////////////////////////////
69 try
70 {
71 using (PDFDoc pdfdoc = new PDFDoc())
72 {
73 // Add a dictionary
74 ObjSet set = new ObjSet();
75 Obj options = set.CreateDict();
76
77 // Put options
78 options.PutNumber("FontSize", 15);
79 options.PutBool("UseSourceCodeFormatting", true);
80 options.PutNumber("PageWidth", 12);
81 options.PutNumber("PageHeight", 6);
82
83 // Convert from .txt file
84 Console.WriteLine("Converting from txt");
85 pdftron.PDF.Convert.FromText(pdfdoc, inputPath + "simple-text.txt", options);
86 pdfdoc.Save(outputPath + "simple-text.pdf", SDFDoc.SaveOptions.e_remove_unused);
87 Console.WriteLine("Saved simple-text.pdf");
88 }
89 }
90 catch (PDFNetException e)
91 {
92 Console.WriteLine(e.Message);
93 err = true;
94 }
95
96 //////////////////////////////////////////////////////////////////////////
97 try
98 {
99 using (PDFDoc pdfdoc = new PDFDoc(inputPath + "newsletter.pdf"))
100 {
101 // Convert PDF document to SVG
102 Console.WriteLine("Converting pdfdoc to SVG");
103 pdftron.PDF.Convert.ToSvg(pdfdoc, outputPath + "pdf2svg v2.svg");
104 Console.WriteLine("Saved pdf2svg v2.svg");
105 }
106 }
107 catch (PDFNetException e)
108 {
109 Console.WriteLine(e.Message);
110 err = true;
111 }
112
113 //////////////////////////////////////////////////////////////////////////
114 try
115 {
116 // Convert PNG image to XPS
117 Console.WriteLine("Converting PNG to XPS");
118 pdftron.PDF.Convert.ToXps(inputPath + "butterfly.png", outputPath + "butterfly.xps");
119 Console.WriteLine("Saved butterfly.xps");
120 }
121 catch (PDFNetException e)
122 {
123 Console.WriteLine(e.Message);
124 err = true;
125 }
126
127
128 //////////////////////////////////////////////////////////////////////////
129 try
130 {
131 // Convert PDF document to XPS
132 Console.WriteLine("Converting PDF to XPS");
133 pdftron.PDF.Convert.ToXps(inputPath + "newsletter.pdf", outputPath + "newsletter.xps");
134 Console.WriteLine("Saved newsletter.xps");
135 }
136 catch (PDFNetException e)
137 {
138 Console.WriteLine(e.Message);
139 err = true;
140 }
141
142 //////////////////////////////////////////////////////////////////////////
143 try
144 {
145 // Convert PDF document to HTML
146 Console.WriteLine("Converting PDF to HTML");
147 pdftron.PDF.Convert.ToHtml(inputPath + "newsletter.pdf", outputPath + "newsletter");
148 Console.WriteLine("Saved newsletter as HTML");
149 }
150 catch (PDFNetException e)
151 {
152 Console.WriteLine(e.Message);
153 err = true;
154 }
155
156 //////////////////////////////////////////////////////////////////////////
157 try
158 {
159 // Convert PDF document to EPUB
160 Console.WriteLine("Converting PDF to EPUB");
161 pdftron.PDF.Convert.ToEpub(inputPath + "newsletter.pdf", outputPath + "newsletter.epub");
162 Console.WriteLine("Saved newsletter.epub");
163 }
164 catch (PDFNetException e)
165 {
166 Console.WriteLine(e.Message);
167 err = true;
168 }
169
170 //////////////////////////////////////////////////////////////////////////
171 try
172 {
173 // Convert PDF document to multipage TIFF
174 Console.WriteLine("Converting PDF to multipage TIFF");
175 pdftron.PDF.Convert.TiffOutputOptions tiff_options = new pdftron.PDF.Convert.TiffOutputOptions();
176 tiff_options.SetDPI(200);
177 tiff_options.SetDither(true);
178 tiff_options.SetMono(true);
179 pdftron.PDF.Convert.ToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options);
180 Console.WriteLine("Saved newsletter.tiff");
181 }
182 catch (PDFNetException e)
183 {
184 Console.WriteLine(e.Message);
185 err = true;
186 }
187
188 //////////////////////////////////////////////////////////////////////////
189 try
190 {
191 using (PDFDoc pdfdoc = new PDFDoc())
192 {
193 // Convert SVG file to PDF
194 Console.WriteLine("Converting SVG to PDF");
195
196 pdftron.PDF.Convert.FromSVG(pdfdoc, inputPath + "tiger.svg", null);
197 pdfdoc.Save(outputPath + "svg2pdf.pdf", SDFDoc.SaveOptions.e_remove_unused);
198
199 Console.WriteLine("Saved svg2pdf.pdf");
200 }
201 }
202 catch (PDFNetException e)
203 {
204 Console.WriteLine(e.Message);
205 err = true;
206 }
207
208 return err;
209 }
210
211 static Boolean ConvertToPdfFromFile()
212 {
213 System.Collections.ArrayList testfiles = new System.Collections.ArrayList();
214 testfiles.Add(new ConvertTestCS.Testfile("simple-word_2007.docx", "docx2pdf.pdf"));
215 testfiles.Add(new ConvertTestCS.Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf"));
216 testfiles.Add(new ConvertTestCS.Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf"));
217 testfiles.Add(new ConvertTestCS.Testfile("simple-text.txt", "txt2pdf.pdf"));
218 testfiles.Add(new ConvertTestCS.Testfile("butterfly.png", "png2pdf.pdf"));
219 testfiles.Add(new ConvertTestCS.Testfile("simple-xps.xps", "xps2pdf.pdf"));
220
221 bool err = false;
222
223 foreach (Testfile file in testfiles)
224 {
225 try
226 {
227 using (pdftron.PDF.PDFDoc pdfdoc = new PDFDoc())
228 {
229 pdftron.PDF.Convert.Printer.SetMode(pdftron.PDF.Convert.Printer.Mode.e_prefer_builtin_converter);
230 pdftron.PDF.Convert.ToPdf(pdfdoc, inputPath + file.inputFile);
231 pdfdoc.Save(outputPath + file.outputFile, SDFDoc.SaveOptions.e_linearized);
232 Console.WriteLine("Converted file: " + file.inputFile);
233 Console.WriteLine("to: " + file.outputFile);
234 }
235 }
236 catch (PDFNetException e)
237 {
238 Console.WriteLine("ERROR: on input file " + file.inputFile);
239 Console.WriteLine(e.Message);
240 err = true;
241 }
242 }
243
244 return err;
245 }
246
247
248 /// <summary>
249 /// The main entry point for the application.
250 /// </summary>
251 [STAThread]
252 static void Main(string[] args)
253 {
254 PDFNet.Initialize(PDFTronLicense.Key);
255 bool err = false;
256
257 err = ConvertToPdfFromFile();
258 if (err)
259 {
260 Console.WriteLine("ConvertFile failed");
261 }
262 else
263 {
264 Console.WriteLine("ConvertFile succeeded");
265 }
266
267 err = ConvertSpecificFormats();
268 if (err)
269 {
270 Console.WriteLine("ConvertSpecificFormats failed");
271 }
272 else
273 {
274 Console.WriteLine("ConvertSpecificFormats succeeded");
275 }
276
277 PDFNet.Terminate();
278 Console.WriteLine("Done.");
279 }
280 }
281}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <iostream>
7#include <sstream>
8#include <PDF/PDFNet.h>
9#include <PDF/Convert.h>
10#include "../../LicenseKey/CPP/LicenseKey.h"
11
12//---------------------------------------------------------------------------------------
13// The following sample illustrates how to use the PDF::Convert utility class to convert
14// documents and files to PDF, XPS, or SVG. The sample also shows how to convert MS Office files
15// using our built in conversion.
16//
17// Certain file formats such as XPS, PDF, and raster image formats can be directly
18// converted to PDF or XPS.
19
20// Please contact us if you have any questions.
21//---------------------------------------------------------------------------------------
22
23using namespace pdftron;
24using namespace PDF;
25using namespace std;
26
27UString inputPath("../../TestFiles/");
28UString outputPath("../../TestFiles/Output/");
29
30typedef struct
31{
32 UString inputFile, outputFile;
33}
34Testfile;
35
36Testfile testfiles[] =
37{
38 { "simple-word_2007.docx", "docx2pdf.pdf"},
39 { "simple-powerpoint_2007.pptx", "pptx2pdf.pdf"},
40 { "simple-excel_2007.xlsx", "xlsx2pdf.pdf"},
41 { "simple-text.txt", "txt2pdf.pdf"},
42 { "butterfly.png", "png2pdf.pdf"},
43 { "simple-xps.xps", "xps2pdf.pdf"}
44};
45
46int ConvertSpecificFormats(); // convert to/from PDF, XPS, SVG
47int ConvertToPdfFromFile(); // convert from a file to PDF automatically
48
49int main(int argc, char *argv[])
50{
51 // The first step in every application using PDFNet is to initialize the
52 // library. The library is usually initialized only once, but calling
53 // Initialize() multiple times is also fine.
54 int err = 0;
55
56 PDFNet::Initialize(LicenseKey);
57
58 // Demonstrate Convert::ToPdf
59 err = ConvertToPdfFromFile();
60 if (err)
61 {
62 cout << "ConvertFile failed" << endl;
63 }
64 else
65 {
66 cout << "ConvertFile succeeded" << endl;
67 }
68
69 // Demonstrate Convert::[FromXps, ToSVG, ToXPS]
70 err = ConvertSpecificFormats();
71 if (err)
72 {
73 cout << "ConvertSpecificFormats failed" << endl;
74 }
75 else
76 {
77 cout << "ConvertSpecificFormats succeeded" << endl;
78 }
79
80 PDFNet::Terminate();
81 cout << "Done.\n";
82 return err;
83}
84
85int ConvertToPdfFromFile()
86{
87 int ret = 0;
88
89 unsigned int ceTestfiles = sizeof (testfiles) / sizeof (Testfile);
90
91 for (unsigned int i = 0; i < ceTestfiles; i++)
92 {
93
94 try
95 {
96 PDFDoc pdfdoc;
97 UString inputFile = inputPath + testfiles[i].inputFile;
98 UString outputFile = outputPath + testfiles[i].outputFile;
99
100 Convert::Printer::SetMode(Convert::Printer::e_prefer_builtin_converter);
101 Convert::ToPdf(pdfdoc, inputFile);
102 pdfdoc.Save(outputFile, SDF::SDFDoc::e_linearized, NULL);
103 cout << "Converted file: " << testfiles[i].inputFile << endl << "to: " << testfiles[i].outputFile << endl;
104 }
105 catch (Common::Exception& e)
106 {
107 cout << "Unable to convert file " << testfiles[i].inputFile.ConvertToAscii().c_str() << endl;
108 cout << e << endl;
109 ret = 1;
110 }
111 catch (...)
112 {
113 cout << "Unknown Exception" << endl;
114 ret = 1;
115 }
116 }
117
118 return ret;
119}
120
121int ConvertSpecificFormats()
122{
123 //////////////////////////////////////////////////////////////////////////
124 int ret = 0;
125 try
126 {
127 PDFDoc pdfdoc;
128
129 cout << "Converting from XPS" << endl;
130 Convert::FromXps(pdfdoc, inputPath + "simple-xps.xps");
131 pdfdoc.Save(outputPath + "xps2pdf v2.pdf", SDF::SDFDoc::e_remove_unused, NULL);
132 cout << "Saved xps2pdf v2.pdf" << endl;
133 }
134 catch (Common::Exception& e)
135 {
136 cout << e << endl;
137 ret = 1;
138 }
139 catch (...)
140 {
141 cout << "Unknown Exception" << endl;
142 ret = 1;
143 }
144
145 //////////////////////////////////////////////////////////////////////////
146 try
147 {
148 PDFDoc pdfdoc;
149
150 // Add a dictionary
151 SDF::ObjSet set;
152 SDF::Obj options = set.CreateDict();
153
154 // Put options
155 options.PutNumber("FontSize", 15);
156 options.PutBool("UseSourceCodeFormatting", true);
157 options.PutNumber("PageWidth", 12);
158 options.PutNumber("PageHeight", 6);
159
160 // Convert from .txt file
161 cout << "Converting from txt" << endl;
162 Convert::FromText(pdfdoc, inputPath + "simple-text.txt", options);
163 pdfdoc.Save(outputPath + "simple-text.pdf", SDF::SDFDoc::e_remove_unused, NULL);
164 cout << "Saved simple-text.pdf" << endl;
165 }
166 catch (Common::Exception& e)
167 {
168 cout << e << endl;
169 ret = 1;
170 }
171 catch (...)
172 {
173 cout << "Unknown Exception" << endl;
174 ret = 1;
175 }
176
177 //////////////////////////////////////////////////////////////////////////
178 try
179 {
180 PDFDoc pdfdoc(inputPath + "newsletter.pdf");
181
182 // Convert PDF document to SVG
183 cout << "Converting pdfdoc to SVG" << endl;
184 Convert::ToSvg(pdfdoc, outputPath + "pdf2svg v2.svg");
185 cout << "Saved pdf2svg v2.svg" << endl;
186 }
187 catch (Common::Exception& e)
188 {
189 cout << e << endl;
190 ret = 1;
191 }
192 catch (...)
193 {
194 cout << "Unknown Exception" << endl;
195 ret = 1;
196 }
197
198 //////////////////////////////////////////////////////////////////////////
199 try
200 {
201 // Convert PNG image to XPS
202 cout << "Converting PNG to XPS" << endl;
203 Convert::ToXps(inputPath + "butterfly.png", outputPath + "butterfly.xps");
204 cout << "Saved butterfly.xps" << endl;
205 }
206 catch (Common::Exception& e)
207 {
208 cout << e << endl;
209 ret = 1;
210 }
211 catch (...)
212 {
213 cout << "Unknown Exception" << endl;
214 ret = 1;
215 }
216
217 //////////////////////////////////////////////////////////////////////////
218 try
219 {
220 // Convert PDF document to XPS
221 cout << "Converting PDF to XPS" << endl;
222 Convert::ToXps(inputPath + "newsletter.pdf", outputPath + "newsletter.xps");
223 cout << "Saved newsletter.xps" << endl;
224 }
225 catch (Common::Exception& e)
226 {
227 cout << e << endl;
228 ret = 1;
229 }
230 catch (...)
231 {
232 cout << "Unknown Exception" << endl;
233 ret = 1;
234 }
235
236 //////////////////////////////////////////////////////////////////////////
237 try
238 {
239 // Convert PDF document to HTML
240 cout << "Converting PDF to HTML" << endl;
241 Convert::ToHtml(inputPath + "newsletter.pdf", outputPath + "newsletter");
242 cout << "Saved newsletter as HTML" << endl;
243 }
244 catch (Common::Exception& e)
245 {
246 cout << e << endl;
247 ret = 1;
248 }
249 catch (...)
250 {
251 cout << "Unknown Exception" << endl;
252 ret = 1;
253 }
254
255 //////////////////////////////////////////////////////////////////////////
256 try
257 {
258 // Convert PDF document to EPUB
259 cout << "Converting PDF to EPUB" << endl;
260 Convert::ToEpub(inputPath + "newsletter.pdf", outputPath + "newsletter.epub");
261 cout << "Saved newsletter.epub" << endl;
262 }
263 catch (Common::Exception& e)
264 {
265 cout << e << endl;
266 ret = 1;
267 }
268 catch (...)
269 {
270 cout << "Unknown Exception" << endl;
271 ret = 1;
272 }
273
274 //////////////////////////////////////////////////////////////////////////
275 try
276 {
277 // Convert PDF document to multipage TIFF
278 cout << "Converting PDF to multipage TIFF" << endl;
279 Convert::TiffOutputOptions tiff_options;
280 tiff_options.SetDPI(200);
281 tiff_options.SetDither(true);
282 tiff_options.SetMono(true);
283 Convert::ToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options);
284 cout << "Saved newsletter.tiff" << endl;
285 }
286 catch (Common::Exception& e)
287 {
288 cout << e << endl;
289 ret = 1;
290 }
291 catch (...)
292 {
293 cout << "Unknown Exception" << endl;
294 ret = 1;
295 }
296
297 //////////////////////////////////////////////////////////////////////////
298 try
299 {
300 PDFDoc pdfdoc;
301
302 // Convert SVG file to PDF
303 cout << "Converting SVG to PDF" << endl;
304
305 Convert::FromSVG(pdfdoc, inputPath + "tiger.svg");
306 pdfdoc.Save(outputPath + "svg2pdf.pdf", SDF::SDFDoc::e_remove_unused, NULL);
307
308 cout << "Saved svg2pdf.pdf" << endl;
309 }
310 catch (Common::Exception& e)
311 {
312 cout << e << endl;
313 ret = 1;
314 }
315 catch (...)
316 {
317 cout << "Unknown Exception" << endl;
318 ret = 1;
319 }
320
321 return ret;
322}
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}
1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.common.PDFNetException;
7import com.pdftron.pdf.*;
8import com.pdftron.sdf.Obj;
9import com.pdftron.sdf.ObjSet;
10import com.pdftron.sdf.SDFDoc;
11import java.util.ArrayList;
12//---------------------------------------------------------------------------------------
13// The following sample illustrates how to use the PDF::Convert utility class to convert
14// documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
15// using our built in conversion.
16//
17// Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
18// converted to PDF or XPS.
19//
20// Please contact us if you have any questions.
21//---------------------------------------------------------------------------------------
22class Testfile
23{
24 public String inputFile, outputFile;
25 public Testfile(String inFile, String outFile)
26 {
27 inputFile = inFile;
28 outputFile = outFile;
29 }
30}
31
32public class ConvertTest
33{
34 // Relative path to the folder containing test files.
35 static String inputPath = "../../TestFiles/";
36 static String outputPath = "../../TestFiles/Output/";
37
38 static boolean ConvertSpecificFormats()
39 {
40 //////////////////////////////////////////////////////////////////////////
41 boolean err = false;
42 try (PDFDoc pdfdoc = new PDFDoc())
43 {
44
45 System.out.println("Converting from XPS");
46
47 Convert.fromXps(pdfdoc, inputPath + "simple-xps.xps");
48 pdfdoc.save(outputPath + "xps2pdf v2.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
49 System.out.println("Saved xps2pdf v2.pdf");
50 }
51 catch (PDFNetException e)
52 {
53 System.out.println(e);
54 err = true;
55 }
56
57 //////////////////////////////////////////////////////////////////////////
58 try (PDFDoc pdfdoc = new PDFDoc())
59 {
60 // add a dictionary
61 ObjSet set = new ObjSet();
62 Obj options = set.createDict();
63
64 // Put options
65 options.putNumber("FontSize", 15);
66 options.putBool("UseSourceCodeFormatting", true);
67 options.putNumber("PageWidth", 12);
68 options.putNumber("PageHeight", 6);
69
70 // Convert from .txt file
71 System.out.println("Converting from txt");
72 Convert.fromText(pdfdoc, inputPath + "simple-text.txt", options);
73 pdfdoc.save(outputPath + "simple-text.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
74 System.out.println("Saved simple-text.pdf");
75 }
76 catch (PDFNetException e)
77 {
78 System.out.println(e);
79 err = true;
80 }
81
82 //////////////////////////////////////////////////////////////////////////
83 try (PDFDoc pdfdoc = new PDFDoc(inputPath + "newsletter.pdf"))
84 {
85 // Convert PDF document to SVG
86 System.out.println("Converting pdfdoc to SVG");
87 Convert.toSvg(pdfdoc, outputPath + "pdf2svg v2.svg");
88 System.out.println("Saved pdf2svg v2.svg");
89 }
90 catch (PDFNetException e)
91 {
92 System.out.println(e);
93 err = true;
94 }
95
96 //////////////////////////////////////////////////////////////////////////
97 try
98 {
99 // Convert PNG image to XPS
100 System.out.println("Converting PNG to XPS");
101 Convert.toXps(inputPath + "butterfly.png", outputPath + "butterfly.xps");
102 System.out.println("Saved butterfly.xps");
103 }
104 catch (PDFNetException e)
105 {
106 System.out.println(e);
107 err = true;
108 }
109
110 //////////////////////////////////////////////////////////////////////////
111 try
112 {
113 // Convert PDF document to XPS
114 System.out.println("Converting PDF to XPS");
115 Convert.toXps(inputPath + "newsletter.pdf", outputPath + "newsletter.xps");
116 System.out.println("Saved newsletter.xps");
117 }
118 catch (PDFNetException e)
119 {
120 System.out.println(e);
121 err = true;
122 }
123
124 //////////////////////////////////////////////////////////////////////////
125 try
126 {
127 // Convert PDF document to HTML
128 System.out.println("Converting PDF to HTML");
129 Convert.toHtml(inputPath + "newsletter.pdf", outputPath + "newsletter");
130 System.out.println("Saved newsletter as HTML");
131 }
132 catch (PDFNetException e)
133 {
134 System.out.println(e);
135 err = true;
136 }
137
138 //////////////////////////////////////////////////////////////////////////
139 try
140 {
141 // Convert PDF document to EPUB
142 System.out.println("Converting PDF to EPUB");
143 Convert.toEpub(inputPath + "newsletter.pdf", outputPath + "newsletter.epub");
144 System.out.println("Saved newsletter.epub");
145 }
146 catch (PDFNetException e)
147 {
148 System.out.println(e);
149 err = true;
150 }
151
152 //////////////////////////////////////////////////////////////////////////
153 try
154 {
155 // Convert PDF document to multipage TIFF
156 System.out.println("Converting PDF to multipage TIFF");
157 Convert.TiffOutputOptions tiff_options = new Convert.TiffOutputOptions();
158 tiff_options.setDPI(200);
159 tiff_options.setDither(true);
160 tiff_options.setMono(true);
161 Convert.toTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options);
162 System.out.println("Saved newsletter.tiff");
163 }
164 catch (PDFNetException e)
165 {
166 System.out.println(e);
167 err = true;
168 }
169
170 //////////////////////////////////////////////////////////////////////////
171 try (PDFDoc pdfdoc = new PDFDoc())
172 {
173 // Convert SVG file to PDF
174 System.out.println("Converting SVG to PDF");
175 Convert.fromSVG(pdfdoc, inputPath + "tiger.svg", null);
176 pdfdoc.save(outputPath + "svg2pdf.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
177
178 System.out.println("Saved svg2pdf.pdf");
179 }
180 catch (PDFNetException e)
181 {
182 System.out.println(e);
183 err = true;
184 }
185
186 return err;
187 }
188
189 static boolean ConvertToPdfFromFile()
190 {
191 ArrayList<Testfile> testfiles = new ArrayList<Testfile>();
192 testfiles.add(new Testfile("simple-word_2007.docx", "docx2pdf.pdf"));
193 testfiles.add(new Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf"));
194 testfiles.add(new Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf"));
195 testfiles.add(new Testfile("simple-text.txt", "txt2pdf.pdf"));
196 testfiles.add(new Testfile("butterfly.png", "png2pdf.pdf"));
197 testfiles.add(new Testfile("simple-xps.xps", "xps2pdf.pdf"));
198
199 boolean err = false;
200
201 for (Testfile file : testfiles)
202 {
203 try (PDFDoc pdfdoc = new PDFDoc())
204 {
205 //use built in converter
206 ConvertPrinter.setMode(ConvertPrinter.e_convert_printer_prefer_builtin_converter);
207 Convert.toPdf(pdfdoc, inputPath + file.inputFile);
208 pdfdoc.save(outputPath + file.outputFile, SDFDoc.SaveMode.LINEARIZED, null);
209 System.out.println("Converted file: " + file.inputFile);
210 System.out.println("to: " + file.outputFile);
211 }
212 catch (PDFNetException e)
213 {
214 System.out.println("ERROR: on input file " + file.inputFile);
215 System.out.println(e);
216 err = true;
217 }
218 }
219
220 return err;
221 }
222
223 /// <summary>
224 /// The main entry point for the application.
225 /// </summary>
226 public static void main(String[] args)
227 {
228 PDFNet.initialize(PDFTronLicense.Key());
229 boolean err = false;
230
231 err = ConvertToPdfFromFile();
232 if (err)
233 {
234 System.out.println("ConvertFile failed");
235 }
236 else
237 {
238 System.out.println("ConvertFile succeeded");
239 }
240
241 err = ConvertSpecificFormats();
242 if (err)
243 {
244 System.out.println("ConvertSpecificFormats failed");
245 }
246 else
247 {
248 System.out.println("ConvertSpecificFormats succeeded");
249 }
250
251 System.out.println("Done.");
252
253 PDFNet.terminate();
254 }
255
256}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6//---------------------------------------------------------------------------------------
7// The following sample illustrates how to use the PDF::Convert utility class to convert
8// documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
9// using our built in conversion.
10//
11// Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
12// converted to PDF or XPS.
13//
14// Please contact us if you have any questions.
15//---------------------------------------------------------------------------------------
16
17const { PDFNet } = require('@pdftron/pdfnet-node');
18const PDFTronLicense = require('../LicenseKey/LicenseKey');
19
20((exports) => {
21 'use strict';
22
23 let Testfile = function (inputFile, outputFile) {
24 this.inputFile = inputFile;
25 this.outputFile = outputFile;
26 }
27
28 const testfiles = [
29 new Testfile('simple-word_2007.docx', 'docx2pdf.pdf'),
30 new Testfile('simple-powerpoint_2007.pptx', 'pptx2pdf.pdf'),
31 new Testfile('simple-excel_2007.xlsx', 'xlsx2pdf.pdf'),
32 new Testfile('simple-text.txt', 'txt2pdf.pdf'),
33 new Testfile('butterfly.png', 'png2pdf.pdf'),
34 new Testfile('simple-xps.xps', 'xps2pdf.pdf'),
35 ]
36
37 const inputPath = '../TestFiles/';
38 const outputPath = '../TestFiles/Output/';
39
40 exports.runConvertTest = () => {
41
42 const main = async () => {
43 try {
44 await convertToPdfFromFile();
45 console.log('ConvertFile succeeded');
46 } catch (err) {
47 console.log('ConvertFile failed');
48 console.log(err);
49 }
50
51 try {
52 await convertSpecificFormats();
53 console.log('ConvertSpecificFormats succeeded');
54 } catch (err) {
55 console.log('ConvertSpecificFormats failed');
56 console.log(err);
57 }
58
59 console.log('Done.');
60 };
61
62 const convertToPdfFromFile = async () => {
63
64 for (const testfile of testfiles) {
65
66 try {
67 const pdfdoc = await PDFNet.PDFDoc.create();
68 await pdfdoc.initSecurityHandler();
69 const inputFile = inputPath + testfile.inputFile;
70 const outputFile = outputPath + testfile.outputFile;
71 await PDFNet.Convert.printerSetMode(PDFNet.Convert.PrinterMode.e_prefer_builtin_converter);
72 await PDFNet.Convert.toPdf(pdfdoc, inputFile);
73 await pdfdoc.save(outputFile, PDFNet.SDFDoc.SaveOptions.e_linearized);
74 console.log('Converted file: ' + testfile.inputFile + '\nto: ' + testfile.outputFile);
75 } catch (err) {
76 console.log('Unable to convert file ' + testfile.inputFile);
77 console.log(err);
78 }
79 }
80 };
81
82 const convertSpecificFormats = async () => {
83 try {
84 const pdfdoc = await PDFNet.PDFDoc.create();
85 await pdfdoc.initSecurityHandler();
86
87 console.log('Converting from XPS');
88 await PDFNet.Convert.fromXps(pdfdoc, inputPath + 'simple-xps.xps');
89 await pdfdoc.save(outputPath + 'xps2pdf v2.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
90 console.log('Saved xps2pdf v2.pdf');
91 } catch (err) {
92 console.log(err);
93 }
94
95
96 try {
97 const pdfdoc = await PDFNet.PDFDoc.create();
98 await pdfdoc.initSecurityHandler();
99
100 // Add a dictionary
101 const set = await PDFNet.ObjSet.create();
102 const options = await set.createDict();
103
104 // Put options
105 options.putNumber('FontSize', 15);
106 options.putBool('UseSourceCodeFormatting', true);
107 options.putNumber('PageWidth', 12);
108 await options.putNumber('PageHeight', 6);
109
110 // Convert from .txt file
111 console.log('Converting from txt');
112 await PDFNet.Convert.fromText(pdfdoc, inputPath + 'simple-text.txt', options);
113 await pdfdoc.save(outputPath + 'simple-text.pdf', PDFNet.SDFDoc.SaveOptions.e_remove_unused);
114 console.log('Saved simple-text.pdf');
115 } catch (err) {
116 console.log(err);
117 }
118
119 try {
120 const pdfdoc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
121 await pdfdoc.initSecurityHandler();
122
123 // Convert PDF document to SVG
124 console.log('Converting pdfdoc to SVG');
125 await PDFNet.Convert.docToSvg(pdfdoc, outputPath + 'pdf2svg v2.svg');
126 console.log('Saved pdf2svg v2.svg');
127 } catch (err) {
128 console.log(err);
129 }
130
131 try {
132 // Convert PNG image to XPS
133 console.log('Converting PNG to XPS');
134 await PDFNet.Convert.fileToXps(inputPath + 'butterfly.png', outputPath + 'butterfly.xps');
135 console.log('Saved butterfly.xps');
136 } catch (err) {
137 console.log(err);
138 }
139
140
141 try {
142 // Convert PDF document to XPS
143 console.log('Converting PDF to XPS');
144 await PDFNet.Convert.fileToXps(inputPath + 'newsletter.pdf', outputPath + 'newsletter.xps');
145 console.log('Saved newsletter.xps');
146 } catch (err) {
147 console.log(err);
148 }
149
150 try {
151 // Convert PDF document to HTML
152 console.log('Converting PDF to HTML');
153 await PDFNet.Convert.fileToHtml(inputPath + 'newsletter.pdf', outputPath + 'newsletter');
154 console.log('Saved newsletter as HTML');
155 } catch (err) {
156 console.log(err);
157 }
158
159 try {
160 // Convert PDF document to EPUB
161 console.log('Converting PDF to EPUB');
162 await PDFNet.Convert.fileToEpub(inputPath + 'newsletter.pdf', outputPath + 'newsletter.epub');
163 console.log('Saved newsletter.epub');
164 } catch (err) {
165 console.log(err);
166 }
167
168 try {
169 // Convert PDF document to multipage TIFF
170 console.log('Converting PDF to multipage TIFF');
171 const tiff_options = new PDFNet.Convert.TiffOutputOptions();
172 tiff_options.setDPI(200);
173 tiff_options.setDither(true);
174 tiff_options.setMono(true);
175
176 await PDFNet.Convert.fileToTiff(inputPath + 'newsletter.pdf', outputPath + 'newsletter.tiff', tiff_options);
177 console.log('Saved newsletter.tiff');
178 } catch (err) {
179 console.log(err);
180 }
181
182 try {
183 const pdfdoc = await PDFNet.PDFDoc.create();
184 await pdfdoc.initSecurityHandler();
185
186 // Convert SVG file to PDF
187 console.log('Converting SVG to PDF');
188 await PDFNet.Convert.fromSVG(pdfdoc, inputPath + 'tiger.svg');
189 await pdfdoc.save(outputPath + 'svg2pdf.pdf', PDFNet.SDFDoc.SaveOptions.e_remove_unused);
190
191 console.log('Saved svg2pdf.pdf');
192 } catch (err) {
193 console.log(err);
194 }
195 };
196 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
197 console.log('Error: ' + JSON.stringify(error));
198 }).then(function(){ return PDFNet.shutdown(); });
199 };
200 exports.runConvertTest();
201})(exports);
202// eslint-disable-next-line spaced-comment
203//# sourceURL=ConvertTest.js
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10//---------------------------------------------------------------------------------------
11// The following sample illustrates how to use the PDF::Convert utility class to convert
12// documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
13// using our built in conversion.
14//
15// Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
16// converted to PDF or XPS.
17//
18// Please contact us if you have any questions.
19//
20// Please contact us if you have any questions.
21//---------------------------------------------------------------------------------------
22
23// Relative path to the folder containing the test files.
24$inputPath = getcwd()."/../../TestFiles/";
25$outputPath = $inputPath."Output/";
26
27
28function ConvertSpecificFormats()
29{
30 global $inputPath, $outputPath;
31
32 $pdfdoc = new PDFDoc();
33 $s1 = $inputPath."simple-xps.xps";
34
35 $ret = 0;
36 try{
37 // Convert the XPS document to PDF
38 echo(nl2br("Converting from XPS\n"));
39 Convert::FromXps($pdfdoc, $s1 );
40 $outputFile = "xps2pdf v2.pdf";
41 $pdfdoc->Save($outputPath.$outputFile, SDFDoc::e_remove_unused);
42 echo(nl2br("Saved ".$outputFile."\n"));
43
44
45 // Convert the TXT document to PDF
46 $set = new ObjSet();
47 $options = $set->CreateDict();
48 // Put options
49 $options->PutNumber("FontSize", 15);
50 $options->PutBool("UseSourceCodeFormatting", true);
51 $options->PutNumber("PageWidth", 12);
52 $options->PutNumber("PageHeight", 6);
53 $s1 = $inputPath . "simple-text.txt";
54 echo(nl2br("Converting from txt\n"));
55 Convert::FromText($pdfdoc, $s1);
56 $outputFile = "simple-text.pdf";
57 $pdfdoc->Save($outputPath.$outputFile, SDFDoc::e_remove_unused);
58 echo(nl2br("Saved ".$outputFile ."\n"));
59
60 // Convert the two page PDF document to SVG
61 $pdfdoc = new PDFDoc($inputPath . "newsletter.pdf");
62 echo(nl2br("Converting pdfdoc to SVG\n"));
63 $outputFile = "pdf2svg v2.svg";
64 Convert::ToSvg($pdfdoc, $outputPath.$outputFile);
65 echo(nl2br("Saved ".$outputFile."\n"));
66
67
68
69 // Convert the PNG image to XPS
70 echo(nl2br("Converting PNG to XPS\n"));
71 $outputFile = "butterfly.xps";
72 Convert::ToXps($inputPath."butterfly.png", $outputPath.$outputFile);
73 echo(nl2br("Saved ".$outputFile."\n"));
74
75 // Convert PDF document to XPS
76 echo(nl2br("Converting PDF to XPS\n"));
77 $outputFile = "newsletter.xps";
78 Convert::ToXps($inputPath."newsletter.pdf", $outputPath.$outputFile);
79 echo(nl2br("Saved ".$outputFile."\n"));
80
81 // Convert PDF document to HTML
82 echo(nl2br("Converting PDF to HTML\n"));
83 $outputFile = "newsletter";
84 Convert::ToHtml($inputPath."newsletter.pdf", $outputPath.$outputFile);
85 echo(nl2br("Saved newsletter as HTML\n"));
86
87 // Convert PDF document to EPUB
88 echo(nl2br("Converting PDF to EPUB\n"));
89 $outputFile = "newsletter.epub";
90 Convert::ToEpub($inputPath."newsletter.pdf", $outputPath.$outputFile);
91 echo(nl2br("Saved ".$outputFile."\n"));
92
93 echo(nl2br("Converting PDF to multipage TIFF\n"));
94 $tiff_options = new TiffOutputOptions();
95 $tiff_options->SetDPI(200);
96 $tiff_options->SetDither(true);
97 $tiff_options->SetMono(true);
98 Convert::ToTiff($inputPath . "newsletter.pdf", $outputPath. "newsletter.tiff", $tiff_options);
99 echo(nl2br("Saved newsletter.tiff\n"));
100
101 // Convert SVG file to PDF
102 echo(nl2br("Converting SVG to PDF\n"));
103 $pdfdoc = new PDFDoc();
104 Convert::FromSVG($pdfdoc, $inputPath . "tiger.svg");
105 $pdfdoc->Save($outputPath . "svg2pdf.pdf", SDFDoc::e_remove_unused);
106 echo(nl2br("Saved svg2pdf.pdf\n"));
107 }
108 catch(Exception $e){
109 $ret = 1;
110 }
111 return $ret;
112}
113
114function ConvertToPdfFromFile()
115{
116 global $inputPath, $outputPath;
117
118 $testfiles = array(
119 array("simple-word_2007.docx","docx2pdf.pdf"),
120 array("simple-powerpoint_2007.pptx","pptx2pdf.pdf"),
121 array("simple-excel_2007.xlsx","xlsx2pdf.pdf"),
122 array("simple-text.txt","txt2pdf.pdf"),
123 array("butterfly.png", "png2pdf.pdf"),
124 array("simple-xps.xps", "xps2pdf.pdf"),
125 );
126 $ret = 0;
127 foreach ($testfiles as &$testfile) {
128 try{
129 $pdfdoc = new PDFDoc();
130 $inputFile = $testfile[0];
131 $outputFile = $testfile[1];
132 Printer::SetMode(Printer::e_prefer_builtin_converter);
133 Convert::ToPdf($pdfdoc, $inputPath.$inputFile);
134 $pdfdoc->Save($outputPath.$outputFile, SDFDoc::e_linearized);
135 $pdfdoc->Close();
136 echo(nl2br("Converted file: ".$inputFile."\n"));
137 echo(nl2br("to: ".$outputFile."\n"));
138 }
139 catch(Exception $e)
140 {
141 $ret = 1;
142 }
143 }
144 return $ret;
145}
146
147function main()
148{
149 // The first step in every application using PDFNet is to initialize the
150 // library. The library is usually initialized only once, but calling
151 // Initialize() multiple times is also fine.
152 global $LicenseKey;
153 PDFNet::Initialize($LicenseKey);
154 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
155
156 // Demonstrate Convert::ToPdf and Convert::Printer
157 $err = ConvertToPdfFromFile();
158 if ($err)
159 echo(nl2br("ConvertFile failed\n"));
160 else
161 echo(nl2br("ConvertFile succeeded\n"));
162
163 // Demonstrate Convert::[FromEmf, FromXps, ToEmf, ToSVG, ToXPS]
164 $err = ConvertSpecificFormats();
165 if ($err)
166 echo(nl2br("ConvertSpecificFormats failed\n"));
167 else
168 echo(nl2br("ConvertSpecificFormats succeeded\n"));
169
170 PDFNet::Terminate();
171 echo(nl2br("Done.\n"));
172}
173
174main();
175?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11import platform
12
13sys.path.append("../../LicenseKey/PYTHON")
14from LicenseKey import *
15
16#---------------------------------------------------------------------------------------
17# The following sample illustrates how to use the PDF::Convert utility class to convert
18# documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
19# using our built in conversion.
20#
21# Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
22# converted to PDF or XPS.
23#
24# Please contact us if you have any questions.
25#---------------------------------------------------------------------------------------
26
27# Relative path to the folder containing the test files.
28inputPath = "../../TestFiles/"
29outputPath = "../../TestFiles/Output/"
30
31
32
33def ConvertSpecificFormats():
34 ret = 0
35 try:
36 # Start with a PDFDoc to collect the converted documents
37 pdfdoc = PDFDoc()
38 s1 = inputPath + "simple-xps.xps"
39
40 # Convert the XPS document to PDF
41 print("Converting from XPS")
42 Convert.FromXps(pdfdoc, s1)
43 outputFile = "xps2pdf v2.pdf"
44 pdfdoc.Save(outputPath + outputFile, SDFDoc.e_remove_unused)
45 print("Saved " + outputFile)
46
47
48 # Convert the TXT document to PDF
49 set = ObjSet()
50 options = set.CreateDict()
51 # Put options
52 options.PutNumber("FontSize", 15)
53 options.PutBool("UseSourceCodeFormatting", True)
54 options.PutNumber("PageWidth", 12)
55 options.PutNumber("PageHeight", 6)
56 s1 = inputPath + "simple-text.txt"
57 print("Converting from txt")
58 Convert.FromText(pdfdoc, s1)
59 outputFile = "simple-text.pdf"
60 pdfdoc.Save(outputPath + outputFile, SDFDoc.e_remove_unused)
61 print("Saved " + outputFile)
62
63 # Convert the two page PDF document to SVG
64 outputFile = "pdf2svg v2.svg"
65 pdfdoc = PDFDoc(inputPath + "newsletter.pdf")
66 print("Converting pdfdoc to SVG")
67 Convert.ToSvg(pdfdoc, outputPath + outputFile)
68 print("Saved " + outputFile)
69
70 # Convert the PNG image to XPS
71 print("Converting PNG to XPS")
72 outputFile = "butterfly.xps"
73 Convert.ToXps(inputPath + "butterfly.png", outputPath +outputFile)
74 print("Saved " + outputFile)
75
76 # Convert PDF document to XPS
77 print("Converting PDF to XPS")
78 outputFile = "newsletter.xps"
79 Convert.ToXps(inputPath + "newsletter.pdf", outputPath + outputFile)
80 print("Saved " + outputFile)
81
82 # Convert PDF document to HTML
83 print("Converting PDF to HTML")
84 outputFile = "newsletter"
85 Convert.ToHtml(inputPath + "newsletter.pdf", outputPath + outputFile)
86 print("Saved newsletter as HTML")
87
88 # Convert PDF document to EPUB
89 print("Converting PDF to EPUB")
90 outputFile = "newsletter.epub"
91 Convert.ToEpub(inputPath + "newsletter.pdf", outputPath + outputFile)
92 print("Saved " + outputFile)
93
94 print("Converting PDF to multipage TIFF")
95 tiff_options = TiffOutputOptions()
96 tiff_options.SetDPI(200)
97 tiff_options.SetDither(True)
98 tiff_options.SetMono(True)
99 Convert.ToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options)
100 print("Saved newsletter.tiff")
101
102 # Convert SVG file to PDF
103 print("Converting SVG to PDF")
104 pdfdoc = PDFDoc()
105 Convert.FromSVG(pdfdoc, inputPath + "tiger.svg")
106 pdfdoc.Save(outputPath + "svg2pdf.pdf", SDFDoc.e_remove_unused)
107 print("Saved svg2pdf.pdf")
108
109 except:
110 ret = 1
111 return ret
112
113# convert from a file to PDF automatically
114def ConvertToPdfFromFile():
115 testfiles = [
116 [ "simple-word_2007.docx","docx2pdf.pdf"],
117 [ "simple-powerpoint_2007.pptx","pptx2pdf.pdf"],
118 [ "simple-excel_2007.xlsx","xlsx2pdf.pdf"],
119 [ "simple-text.txt","txt2pdf.pdf"],
120 [ "butterfly.png","png2pdf.pdf"],
121 [ "simple-xps.xps","xps2pdf.pdf"],
122 ]
123 ret = 0
124
125
126 for testfile in testfiles:
127 try:
128 pdfdoc = PDFDoc()
129 inputFile = testfile[0]
130 outputFile = testfile[1]
131 Printer.SetMode(Printer.e_prefer_builtin_converter)
132
133 Convert.ToPdf(pdfdoc, inputPath + inputFile)
134 pdfdoc.Save(outputPath + outputFile, SDFDoc.e_linearized)
135 pdfdoc.Close()
136 print("Converted file: " + inputFile + "\nto: " + outputFile)
137 except:
138 ret = 1
139 print("ERROR: on input file " + inputFile)
140 return ret
141
142
143def main():
144 # The first step in every application using PDFNet is to initialize the
145 # library. The library is usually initialized only once, but calling
146 # Initialize() multiple times is also fine.
147 PDFNet.Initialize(LicenseKey)
148
149 # Demonstrate Convert.ToPdf and Convert.Printer
150 err = ConvertToPdfFromFile()
151 if err:
152 print("ConvertFile failed")
153 else:
154 print("ConvertFile succeeded")
155
156 # Demonstrate Convert.[FromEmf, FromXps, ToEmf, ToSVG, ToXPS]
157 err = ConvertSpecificFormats()
158 if err:
159 print("ConvertSpecificFormats failed")
160 else:
161 print("ConvertSpecificFormats succeeded")
162
163
164 PDFNet.Terminate()
165 print("Done.")
166
167if __name__ == '__main__':
168 main()
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#---------------------------------------------------------------------------------------
13# The following sample illustrates how to use the PDF::Convert utility class to convert
14# documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
15# using our built in conversion.
16#
17# Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
18# converted to PDF or XPS.
19#
20# Please contact us if you have any questions.
21#
22# Please contact us if you have any questions.
23#---------------------------------------------------------------------------------------
24
25# Relative path to the folder containing the test files.
26$inputPath = "../../TestFiles/"
27$outputPath = "../../TestFiles/Output/"
28
29
30def ConvertSpecificFormats()
31 ret = 0
32 begin
33 # Start with a PDFDoc to collect the converted documents
34 pdfdoc = PDFDoc.new()
35 s1 = $inputPath + "simple-xps.xps"
36
37 puts "Converting from XPS"
38 Convert.FromXps(pdfdoc, s1)
39 outputFile = "xps2pdf v2.pdf"
40 pdfdoc.Save($outputPath + outputFile, SDFDoc::E_remove_unused)
41 puts "Saved " + outputFile
42
43 # Convert the TXT document to PDF
44 set = ObjSet.new
45 options = set.CreateDict()
46 # Put options
47 options.PutNumber("FontSize", 15)
48 options.PutBool("UseSourceCodeFormatting", true)
49 options.PutNumber("PageWidth", 12)
50 options.PutNumber("PageHeight", 6)
51
52 s1 = $inputPath + "simple-text.txt"
53 puts "Converting from txt"
54 Convert.FromText(pdfdoc, s1)
55 outputFile = "simple-text.pdf"
56 pdfdoc.Save($outputPath + outputFile, SDFDoc::E_remove_unused)
57 puts("Saved " + outputFile)
58
59 # Convert the two page PDF document to SVG
60 outputFile = "pdf2svg v2.svg"
61 pdfdoc = PDFDoc.new($inputPath + "newsletter.pdf")
62 puts "Converting pdfdoc to SVG"
63 Convert.ToSvg(pdfdoc, $outputPath + outputFile)
64 puts "Saved " + outputFile
65
66 # Convert the PNG image to XPS
67 puts "Converting PNG to XPS"
68 outputFile = "butterfly.xps"
69 Convert.ToXps($inputPath + "butterfly.png", $outputPath + outputFile)
70 puts "Saved " + outputFile
71
72 # Convert PDF document to XPS
73 puts "Converting PDF to XPS"
74 outputFile = "newsletter.xps"
75 Convert.ToXps($inputPath + "newsletter.pdf", $outputPath + outputFile)
76 puts "Saved " + outputFile
77
78 # Convert PDF document to HTML
79 puts "Converting PDF to HTML"
80 outputFile = "newsletter"
81 Convert.ToHtml($inputPath + "newsletter.pdf", $outputPath + outputFile)
82 puts "Saved newsletter as HTML"
83
84 # Convert PDF document to EPUB
85 puts "Converting PDF to EPUB"
86 outputFile = "newsletter.epub"
87 Convert.ToEpub($inputPath + "newsletter.pdf", $outputPath + outputFile)
88 puts "Saved " + outputFile
89
90 puts "Converting PDF to multipage TIFF"
91 tiff_options = TiffOutputOptions.new
92 tiff_options.SetDPI(200)
93 tiff_options.SetDither(true)
94 tiff_options.SetMono(true)
95 Convert.ToTiff($inputPath + "newsletter.pdf", $outputPath + "newsletter.tiff", tiff_options)
96 puts "Saved newsletter.tiff"
97
98 pdfdoc = PDFDoc.new()
99 puts "Converting SVG to PDF"
100 Convert.FromSVG(pdfdoc, $inputPath + "tiger.svg")
101 pdfdoc.Save($outputPath + "svg2pdf.pdf", SDFDoc::E_remove_unused)
102 puts "Saved svg2pdf.pdf"
103 rescue
104 ret = 1
105 end
106 return ret
107end
108
109# convert from a file to PDF automatically
110def ConvertToPdfFromFile()
111 testfiles = [
112 ["simple-word_2007.docx","docx2pdf.pdf"],
113 ["simple-powerpoint_2007.pptx","pptx2pdf.pdf"],
114 ["simple-excel_2007.xlsx","xlsx2pdf.pdf"],
115 ["simple-text.txt","txt2pdf.pdf"],
116 ["butterfly.png", "png2pdf.pdf"],
117 ["simple-xps.xps", "xps2pdf.pdf"]
118 ]
119
120
121
122 ret = 0
123 for testfile in testfiles
124 begin
125 pdfdoc = PDFDoc.new()
126 inputFile = testfile[0]
127 outputFile = testfile[1]
128 Printer.SetMode(Printer::E_prefer_builtin_converter)
129 Convert.ToPdf(pdfdoc, $inputPath + inputFile)
130 pdfdoc.Save($outputPath + outputFile, SDFDoc::E_compatibility)
131 pdfdoc.Close()
132 puts "Converted file: " + inputFile + "\nto: " + outputFile
133 rescue
134 ret = 1
135 end
136 end
137
138 return ret
139end
140
141
142def main()
143 # The first step in every application using PDFNet is to initialize the
144 # library. The library is usually initialized only once, but calling
145 # Initialize() multiple times is also fine.
146 PDFNet.Initialize(PDFTronLicense.Key)
147
148 # Demonstrate Convert.ToPdf and Convert.Printer
149 err = ConvertToPdfFromFile()
150 if err == 1
151 puts "ConvertFile failed"
152 else
153 puts "ConvertFile succeeded"
154 end
155 # Demonstrate Convert.[FromEmf, FromXps, ToEmf, ToSVG, ToXPS]
156 err = ConvertSpecificFormats()
157 if err == 1
158 puts "ConvertSpecificFormats failed"
159 else
160 puts "ConvertSpecificFormats succeeded"
161 end
162 PDFNet.Terminate
163 puts "Done."
164end
165
166main()
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6Imports System.Drawing
7Imports System.Drawing.Drawing2D
8Imports pdftron
9Imports pdftron.Common
10Imports pdftron.Filters
11Imports pdftron.SDF
12Imports pdftron.PDF
13
14' The following sample illustrates how to use the PDF::Convert utility class to convert
15' documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
16' using our built in conversion.
17'
18' Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
19' converted to PDF or XPS.
20'
21' Also note that conversion under ASP.NET can be tricky to configure. Please see the following document for advice:
22' http://www.pdftron.com/pdfnet/faq_files/Converting_Documents_in_Windows_Service_or_ASP.NET_Application_using_PDFNet.pdf
23Module ConvertTestVB
24 Class Testfile
25 Public inputFile, outputFile As String
26
27 Public Sub New(ByVal inFile As String, ByVal outFile As String)
28 inputFile = inFile
29 outputFile = outFile
30 End Sub
31 End Class
32
33 Class Class1
34 Shared pdfNetLoader As pdftron.PDFNetLoader = pdftron.PDFNetLoader.Instance()
35
36 Shared Sub New()
37 End Sub
38
39 Const inputPath As String = "../../../../TestFiles/"
40 Const outputPath As String = "../../../../TestFiles/Output/"
41
42 Private Shared Function ConvertSpecificFormats() As Boolean
43 Dim err As Boolean = False
44
45 Try
46
47 Using pdfdoc As PDFDoc = New PDFDoc()
48 Console.WriteLine("Converting from XPS")
49 pdftron.PDF.Convert.FromXps(pdfdoc, inputPath & "simple-xps.xps")
50 pdfdoc.Save(outputPath & "xps2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused)
51 Console.WriteLine("Saved xps2pdf v2.pdf")
52 End Using
53
54 Catch e As PDFNetException
55 Console.WriteLine(e.Message)
56 err = True
57 End Try
58
59 Try
60 Using pdfdoc As PDFDoc = New PDFDoc()
61 Dim [set] As ObjSet = New ObjSet()
62 Dim options As Obj = [set].CreateDict()
63 options.PutNumber("FontSize", 15)
64 options.PutBool("UseSourceCodeFormatting", True)
65 options.PutNumber("PageWidth", 12)
66 options.PutNumber("PageHeight", 6)
67 Console.WriteLine("Converting from txt")
68 pdftron.PDF.Convert.FromText(pdfdoc, inputPath & "simple-text.txt", options)
69 pdfdoc.Save(outputPath & "simple-text.pdf", SDFDoc.SaveOptions.e_remove_unused)
70 Console.WriteLine("Saved simple-text.pdf")
71 End Using
72
73 Catch e As PDFNetException
74 Console.WriteLine(e.Message)
75 err = True
76 End Try
77
78 Try
79
80 Using pdfdoc As PDFDoc = New PDFDoc(inputPath & "newsletter.pdf")
81 Console.WriteLine("Converting pdfdoc to SVG")
82 pdftron.PDF.Convert.ToSvg(pdfdoc, outputPath & "pdf2svg v2.svg")
83 Console.WriteLine("Saved pdf2svg v2.svg")
84 End Using
85
86 Catch e As PDFNetException
87 Console.WriteLine(e.Message)
88 err = True
89 End Try
90
91 Try
92 Console.WriteLine("Converting PNG to XPS")
93 pdftron.PDF.Convert.ToXps(inputPath & "butterfly.png", outputPath & "butterfly.xps")
94 Console.WriteLine("Saved butterfly.xps")
95 Catch e As PDFNetException
96 Console.WriteLine(e.Message)
97 err = True
98 End Try
99
100 Try
101 Console.WriteLine("Converting PDF to XPS")
102 pdftron.PDF.Convert.ToXps(inputPath & "newsletter.pdf", outputPath & "newsletter.xps")
103 Console.WriteLine("Saved newsletter.xps")
104 Catch e As PDFNetException
105 Console.WriteLine(e.Message)
106 err = True
107 End Try
108
109 Try
110 Console.WriteLine("Converting PDF to HTML")
111 pdftron.PDF.Convert.ToHtml(inputPath & "newsletter.pdf", outputPath & "newsletter")
112 Console.WriteLine("Saved newsletter as HTML")
113 Catch e As PDFNetException
114 Console.WriteLine(e.Message)
115 err = True
116 End Try
117
118 Try
119 Console.WriteLine("Converting PDF to EPUB")
120 pdftron.PDF.Convert.ToEpub(inputPath & "newsletter.pdf", outputPath & "newsletter.epub")
121 Console.WriteLine("Saved newsletter.epub")
122 Catch e As PDFNetException
123 Console.WriteLine(e.Message)
124 err = True
125 End Try
126
127 Try
128 Console.WriteLine("Converting PDF to multipage TIFF")
129 Dim tiff_options As pdftron.PDF.Convert.TiffOutputOptions = New pdftron.PDF.Convert.TiffOutputOptions()
130 tiff_options.SetDPI(200)
131 tiff_options.SetDither(True)
132 tiff_options.SetMono(True)
133 pdftron.PDF.Convert.ToTiff(inputPath & "newsletter.pdf", outputPath & "newsletter.tiff", tiff_options)
134 Console.WriteLine("Saved newsletter.tiff")
135 Catch e As PDFNetException
136 Console.WriteLine(e.Message)
137 err = True
138 End Try
139
140 Try
141
142 Using pdfdoc As PDFDoc = New PDFDoc()
143 Console.WriteLine("Converting SVG to PDF")
144
145 pdftron.PDF.Convert.FromSvg(pdfdoc, inputPath & "tiger.svg", Nothing)
146 pdfdoc.Save(outputPath & "svg2pdf.pdf", SDFDoc.SaveOptions.e_remove_unused)
147
148 Console.WriteLine("Saved svg2pdf.pdf")
149 End Using
150
151 Catch e As PDFNetException
152 Console.WriteLine(e.Message)
153 err = True
154 End Try
155
156 Return err
157 End Function
158
159 Private Shared Function ConvertToPdfFromFile() As Boolean
160 Dim testfiles As System.Collections.ArrayList = New System.Collections.ArrayList()
161 testfiles.Add(New ConvertTestVB.Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf"))
162 testfiles.Add(New ConvertTestVB.Testfile("simple-text.txt", "txt2pdf.pdf"))
163 testfiles.Add(New ConvertTestVB.Testfile("simple-word_2007.docx", "docx2pdf.pdf"))
164 testfiles.Add(New ConvertTestVB.Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf"))
165 testfiles.Add(New ConvertTestVB.Testfile("butterfly.png", "png2pdf.pdf"))
166 testfiles.Add(New ConvertTestVB.Testfile("simple-xps.xps", "xps2pdf.pdf"))
167
168 Dim err As Boolean = False
169
170 For Each file As Testfile In testfiles
171 Try
172
173 Using pdfdoc As pdftron.PDF.PDFDoc = New PDFDoc()
174
175 pdftron.PDF.Convert.Printer.SetMode(PDF.Convert.Printer.Mode.e_prefer_builtin_converter)
176 pdftron.PDF.Convert.ToPdf(pdfdoc, inputPath & file.inputFile)
177 pdfdoc.Save(outputPath & file.outputFile, SDFDoc.SaveOptions.e_linearized)
178 Console.WriteLine("Converted file: " & file.inputFile)
179 Console.WriteLine("to: " & file.outputFile)
180 End Using
181
182 Catch e As PDFNetException
183 Console.WriteLine("ERROR: on input file " & file.inputFile)
184 Console.WriteLine(e.Message)
185 err = True
186 End Try
187 Next
188
189 Return err
190 End Function
191
192 <STAThread>
193 Shared Sub Main(ByVal args As String())
194 PDFNet.Initialize(PDFTronLicense.Key)
195 Dim err As Boolean = False
196 err = ConvertToPdfFromFile()
197
198 If err Then
199 Console.WriteLine("ConvertFile failed")
200 Else
201 Console.WriteLine("ConvertFile succeeded")
202 End If
203
204 err = ConvertSpecificFormats()
205
206 If err Then
207 Console.WriteLine("ConvertSpecificFormats failed")
208 Else
209 Console.WriteLine("ConvertSpecificFormats succeeded")
210 End If
211
212 PDFNet.Terminate()
213 Console.WriteLine("Done.")
214 End Sub
215 End Class
216End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales