//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2012 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- #include #include #include #include //--------------------------------------------------------------------------------------- // 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. //--------------------------------------------------------------------------------------- using namespace pdftron; using namespace PDF; using namespace std; UString inputPath("../../TestFiles/"); UString outputPath("../../TestFiles/Output/"); typedef struct { UString inputFile, ouputFile; bool requiresWindowsPlatform; } Testfile; Testfile testfiles[] = { { "simple-word_2007.docx", "docx2pdf.pdf", true }, { "simple-powerpoint_2007.pptx", "pptx2pdf.pdf", true }, { "simple-excel_2007.xlsx", "xlsx2pdf.pdf", true }, { "simple-publisher.pub", "pub2pdf.pdf", true }, // { "simple-visio.vsd", "vsd2pdf.pdf", true }, // requires Microsoft Office Visio { "simple-text.txt", "txt2pdf.pdf", true }, { "simple-rtf.rtf", "rtf2pdf.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 } }; int ConvertSpecificFormats(); // convert to/from PDF, XPS, EMF, SVG int ConvertToPdfFromFile(); // convert from a file to PDF automatically int main(int argc, char *argv[]) { // 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. int err = 0; PDFNet::Initialize(); // Demonstrate Convert::ToPdf and Convert::Printer err = ConvertToPdfFromFile(); if (err) { cout << "ConvertFile failed" << endl; } else { cout << "ConvertFile succeeded" << endl; } // Demonstrate Convert::[FromEmf, FromXps, ToEmf, ToSVG, ToXPS] err = ConvertSpecificFormats(); if (err) { cout << "ConvertSpecificFormats failed" << endl; } else { cout << "ConvertSpecificFormats succeeded" << endl; } if (Convert::Printer::IsInstalled()) { try { cout << "Uninstalling printer (requires Windows platform and administrator)" << endl; Convert::Printer::Uninstall(); cout << "Uninstalled Printer " << Convert::Printer::GetPrinterName().ConvertToAscii().c_str() << endl;; } catch (Common::Exception) { cout << "Unable to uninstall printer" << endl; } } PDFNet::Terminate(); cout << "Done.\n"; return err; } int ConvertToPdfFromFile() { int ret = 0; if( Convert::Printer::IsInstalled("PDFTron PDFNet") ) { Convert::Printer::SetPrinterName("PDFTron PDFNet"); } else if (!Convert::Printer::IsInstalled()) { try { // This will fail if not run as administrator. Harmless if PDFNet // printer already installed cout << "Installing printer (requires Windows platform and administrator)\n"; Convert::Printer::Install(); cout << "Installed printer " << Convert::Printer::GetPrinterName().ConvertToAscii().c_str() << endl; } catch (Common::Exception) { cout << "Unable to install printer" << endl; } } unsigned int ceTestfiles = sizeof (testfiles) / sizeof (Testfile); for (unsigned int i = 0; i < ceTestfiles; i++) { #ifndef _MSC_VER if (testfiles[i].requiresWindowsPlatform) { continue; } #endif try { PDFDoc pdfdoc; UString inputFile = inputPath + testfiles[i].inputFile; UString outputFile = outputPath + testfiles[i].ouputFile; if (Convert::RequiresPrinter(inputFile)) { cout << "Using PDFNet printer to convert file " << inputFile << endl; } Convert::ToPdf(pdfdoc, inputFile); pdfdoc.Save(outputFile, SDF::SDFDoc::e_linearized, NULL); cout << "Converted file: " << inputFile << endl << " to: " << outputFile << endl; } catch (Common::Exception& e) { cout << "Unable to convert file " << testfiles[i].inputFile.ConvertToAscii().c_str() << endl; cout << e << endl; ret = 1; } catch (...) { cout << "Unknown Exception" << endl; ret = 1; } } return ret; } int ConvertSpecificFormats() { int ret = 0; try { // Start with a PDFDoc to collect the converted documents PDFDoc pdfdoc; UString outputFile; UString s1 = inputPath + "simple-xps.xps"; UString s2 = inputPath + "simple-emf.emf"; cout << "Converting from XPS " << s1 << endl; Convert::FromXps(pdfdoc, s1 ); outputFile = outputPath + "xps2pdf v2.pdf"; pdfdoc.Save(outputFile, SDF::SDFDoc::e_remove_unused, NULL); cout << "Saved " << outputFile.ConvertToAscii() << endl; #ifdef _MSC_VER // Append a second page cout << "Converting from EMF " << s2 << endl; Convert::FromEmf(pdfdoc, s2 ); outputFile = outputPath + "emf2pdf v2.pdf"; pdfdoc.Save(outputFile, SDF::SDFDoc::e_remove_unused, NULL); cout << "Saved " << outputFile.ConvertToAscii() << endl; #endif // _MSC_VER // Convert the two page PDF document to SVG cout << "Converting pdfdoc to SVG" << endl; outputFile = outputPath + "pdf2svg v2.svg"; Convert::ToSvg(pdfdoc, outputFile); cout << "Saved " << outputFile.ConvertToAscii() << endl; // Convert the PDF document to XPS cout << "Converting pdfdoc to XPS" << endl; outputFile = outputPath + "pdf2xps v2.xps"; Convert::ToXps(pdfdoc, outputFile); cout << "Saved " << outputFile.ConvertToAscii() << endl; // Convert the PNG image to XPS cout << "Converting PNG to XPS" << endl; outputFile = outputPath + "butterfly.xps"; Convert::ToXps(inputPath + "butterfly.png", outputFile); cout << "Saved " << outputFile.ConvertToAscii() << endl; #ifdef _MSC_VER // Convert the MSWord document to XPS cout << "Converting DOCX to XPS" << endl; outputFile = outputPath + "simple-word_2007.xps"; Convert::ToXps(inputPath + "simple-word_2007.docx", outputFile); cout << "Saved " << outputFile.ConvertToAscii() << endl; #endif // _MSC_VER // Convert PDF document to XPS cout << "Converting PDF to XPS" << endl; outputFile = outputPath + "newsletter.xps"; Convert::ToXps(inputPath + "newsletter.pdf", outputFile); cout << "Saved " << outputFile.ConvertToAscii() << endl; } catch (Common::Exception& e) { cout << e << endl; ret = 1; } catch (...) { cout << "Unknown Exception" << endl; ret = 1; } return ret; }