#--------------------------------------------------------------------------------------- # Copyright (c) 2001-2011 by PDFTron Systems Inc. All Rights Reserved. # Consult legal.txt regarding legal and license information. #--------------------------------------------------------------------------------------- require '../../../Lib/PDFNetRuby' include PDFNetRuby #--------------------------------------------------------------------------------------- # The following sample illustrates how to use the PDF.Convert utility class to convert # documents and files to PDF, XPS, SVG, or EMF. # # Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly # converted to PDF or XPS. Other formats are converted using a virtual driver. To check # if ToPDF (or ToXPS) require that PDFNet printer is installed use Convert.RequiresPrinter(filename). # The installing application must be run as administrator. The manifest for this sample # specifies appropriate the UAC elevation. # # Note: the PDFNet printer is a virtual XPS printer supported on Vista SP1 and Windows 7. # For Windows XP SP2 or higher, or Vista SP0 you need to install the XPS Essentials Pack (or # equivalent redistributables). You can download the XPS Essentials Pack from: # http:#www.microsoft.com/downloads/details.aspx?FamilyId=B8DCFFDD-E3A5-44CC-8021-7649FD37FFEE&displaylang=en # Windows XP Sp2 will also need the Microsoft Core XML Services (MSXML) 6.0: # http:#www.microsoft.com/downloads/details.aspx?familyid=993C0BCF-3BCF-4009-BE21-27E85E1857B1&displaylang=en # # Note: Convert.fromEmf and Convert.toEmf will only work on Windows and require GDI+. # # Please contact us if you have any questions. #--------------------------------------------------------------------------------------- # Relative path to the folder containing the test files. $inputPath = "../../TestFiles/" $outputPath = "../../TestFiles/Output/" $testfiles = [["simple-powerpoint_2007.pptx", "pptx2pdf.pdf", true], ["simple-text.txt", "txt2pdf.pdf", true], ["simple-word_2007.docx", "docx2pdf.pdf", true], ["simple-rtf.rtf", "rtf2pdf.pdf", true], ["simple-excel_2007.xlsx", "xlsx2pdf.pdf", true], ["simple-publisher.pub", "pub2pdf.pdf", true], ["butterfly.png", "png2pdf.pdf", false], ["simple-emf.emf", "emf2pdf.pdf", true], ["simple-xps.xps", "xps2pdf.pdf", false], # ["simple-webpage.mht", "mht2pdf.pdf", true], ["simple-webpage.html", "html2pdf.pdf", true]] # convert from a file to PDF automatically def ConvertToPdfFromFile() if Printer.IsInstalled("PDFTron PDFNet") Printer.SetPrinterName("PDFTron PDFNet") elsif not Printer.IsInstalled() and (ENV['OS'] == 'Windows_NT') # This will fail if not run as administrator. Harmless if PDFNet # printer already installed puts "Installing printer (requires Windows platform and administrator)" Printer.Install() puts "Installed printer " + Printer.GetPrinterName() end i = 0 while i < $testfiles.size() do if ENV['OS'] != 'Windows_NT' if $testfiles[i][2] i = i + 1 next end end pdfdoc = PDFDoc.new() inputFile = $inputPath + $testfiles[i][0] outputFile = $outputPath + $testfiles[i][1] if Convert.RequiresPrinter(inputFile) puts "Using PDFNet printer to convert file " + inputFile end Convert.ToPdf(pdfdoc, inputFile) pdfdoc.Save(outputFile, SDFDoc::E_linearized) puts "Converted file: " + inputFile + "\n to: " + outputFile pdfdoc.Close() i = i + 1 end end def ConvertSpecificFormats() # Start with a PDFDoc to collect the converted documents pdfdoc = PDFDoc.new() s1 = $inputPath + "simple-xps.xps" s2 = $inputPath + "simple-emf.emf" puts "Converting from XPS " + s1 Convert.FromXps(pdfdoc, s1) outputFile = $outputPath + "xps2pdf v2.pdf" pdfdoc.Save(outputFile, SDFDoc::E_remove_unused) puts "Saved " + outputFile if ENV['OS'] == 'Windows_NT' # append a second page puts "Converting from EMF " + s2 Convert.FromEmf(pdfdoc, s2) outputFile = $outputPath + "emf2pdf v2.pdf" pdfdoc.Save(outputFile, SDFDoc::E_remove_unused) puts "Saved " + outputFile end # Convert the two page PDF document to SVG print("Converting pdfdoc to SVG") outputFile = $outputPath + "pdf2svg v2.svg" Convert.ToSvg(pdfdoc, outputFile) puts "Saved " + outputFile # Convert the PDF document to XPS print("Converting pdfdoc to XPS") outputFile = $outputPath + "pdf2xps v2.xps" Convert.ToXps(pdfdoc, outputFile) puts "Saved " + outputFile # Convert the PNG image to XPS print("Converting PNG to XPS") outputFile = $outputPath + "butterfly.xps" Convert.ToXps($inputPath + "butterfly.png", outputFile) puts "Saved " + outputFile if ENV['OS'] == 'Windows_NT' # Convert the MSWord document to XPS puts "Convert DOCX to XPS" outputFile = $outputPath + "simple-word_2007.xps" Convert.ToXps($inputPath + "simple-word_2007.docx", outputFile) puts "Saved " + outputFile end # Convert PDF document to XPS puts "Converting PDF to XPS" outputFile = $outputPath + "newsletter.xps" Convert.ToXps($inputPath + "newsletter.pdf", outputFile) puts "Saved " + outputFile end # 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. PDFNet.Initialize() # Demonstrate Convert.ToPdf and Convert.Printer ConvertToPdfFromFile() # Demonstrate Convert.[FromEmf, FromXps, ToEmf, ToSVG, ToXPS] ConvertSpecificFormats() if Printer.IsInstalled() and (ENV['OS'] == 'Windows_NT') puts "Uninstalling printer (requires Windows platform and administrator)" Printer.Uninstall() puts "Uninstalled Printer " + Printer.GetPrinterName() end puts "Done."