More languages
Some test text!
More languages
Sample C# code to use PDFTron SDK for direct, high-quality conversion between PDF, XPS, EMF, SVG, TIFF, PNG, JPEG, and other image formats ('pdftron.PDF.Convert' namespace). The sample also shows how to convert any printable document (ex. TXT, RTF, Word, MS Office, DXF, DWG, etc) to PDF or XPS using a universal document converter. Learn more about our C# PDF Library and PDF Conversion Library.
Get Started Samples DownloadTo run this sample, get started with a free trial of Apryse SDK.
//
// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
//
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;
namespace ConvertTestCS
{
/// <summary>
// 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, or .NET Framework
// 3.x or higher. For older versions of .NET Framework running on Windows XP or Vista SP0 you need to install
// the XPS Essentials Pack (or equivalent redistributables).
//
// Also note that conversion under ASP.NET can be tricky to configure. Please see the following document for advice:
// http://www.pdftron.com/pdfnet/faq_files/Converting_Documents_in_Windows_Service_or_ASP.NET_Application_using_PDFNet.pdf
/// </summary>
class Testfile
{
public string inputFile, outputFile;
public bool requiresWindowsPlatform;
public Testfile(string inFile, string outFile, bool requiresWindowsPlatform_)
{
inputFile = inFile;
outputFile = outFile;
requiresWindowsPlatform = requiresWindowsPlatform_;
}
};
class Class1
{
private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
static Class1() {}
// Relative path to the folder containing test files.
const string inputPath = "../../../../TestFiles/";
const string outputPath = "../../../../TestFiles/Output/";
static bool ConvertSpecificFormats()
{
//////////////////////////////////////////////////////////////////////////
bool err = false;
try
{
using (PDFDoc pdfdoc = new PDFDoc())
{
Console.WriteLine("Converting from XPS");
pdftron.PDF.Convert.FromXps(pdfdoc, inputPath + "simple-xps.xps");
pdfdoc.Save(outputPath + "xps2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
Console.WriteLine("Saved xps2pdf v2.pdf");
}
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
//////////////////////////////////////////////////////////////////////////
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
try
{
using (PDFDoc pdfdoc = new PDFDoc())
{
Console.WriteLine("Converting from EMF");
pdftron.PDF.Convert.FromEmf(pdfdoc, inputPath + "simple-emf.emf");
pdfdoc.Save(outputPath + "emf2pdf v2.pdf", SDFDoc.SaveOptions.e_remove_unused);
Console.WriteLine("Saved emf2pdf v2.pdf");
}
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
}
//////////////////////////////////////////////////////////////////////////
try
{
using (PDFDoc pdfdoc = new PDFDoc())
{
// Add a dictionary
ObjSet set = new ObjSet();
Obj options = set.CreateDict();
// Put options
options.PutNumber("FontSize", 15);
options.PutBool("UseSourceCodeFormatting", true);
options.PutNumber("PageWidth", 12);
options.PutNumber("PageHeight", 6);
// Convert from .txt file
Console.WriteLine("Converting from txt");
pdftron.PDF.Convert.FromText(pdfdoc, inputPath + "simple-text.txt", options);
pdfdoc.Save(outputPath + "simple-text.pdf", SDFDoc.SaveOptions.e_remove_unused);
Console.WriteLine("Saved simple-text.pdf");
}
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
//////////////////////////////////////////////////////////////////////////
try
{
using (PDFDoc pdfdoc = new PDFDoc(inputPath + "newsletter.pdf"))
{
// Convert PDF document to SVG
Console.WriteLine("Converting pdfdoc to SVG");
pdftron.PDF.Convert.ToSvg(pdfdoc, outputPath + "pdf2svg v2.svg");
Console.WriteLine("Saved pdf2svg v2.svg");
}
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
//////////////////////////////////////////////////////////////////////////
try
{
// Convert PNG image to XPS
Console.WriteLine("Converting PNG to XPS");
pdftron.PDF.Convert.ToXps(inputPath + "butterfly.png", outputPath + "butterfly.xps");
Console.WriteLine("Saved butterfly.xps");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
//////////////////////////////////////////////////////////////////////////
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
try
{
// Convert MSWord document to XPS
Console.WriteLine("Converting DOCX to XPS");
pdftron.PDF.Convert.ToXps(inputPath + "simple-word_2007.docx", outputPath + "simple-word_2007.xps");
Console.WriteLine("Saved simple-word_2007.xps");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
}
//////////////////////////////////////////////////////////////////////////
try
{
// Convert PDF document to XPS
Console.WriteLine("Converting PDF to XPS");
pdftron.PDF.Convert.ToXps(inputPath + "newsletter.pdf", outputPath + "newsletter.xps");
Console.WriteLine("Saved newsletter.xps");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
//////////////////////////////////////////////////////////////////////////
try
{
// Convert PDF document to HTML
Console.WriteLine("Converting PDF to HTML");
pdftron.PDF.Convert.ToHtml(inputPath + "newsletter.pdf", outputPath + "newsletter");
Console.WriteLine("Saved newsletter as HTML");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
//////////////////////////////////////////////////////////////////////////
try
{
// Convert PDF document to EPUB
Console.WriteLine("Converting PDF to EPUB");
pdftron.PDF.Convert.ToEpub(inputPath + "newsletter.pdf", outputPath + "newsletter.epub");
Console.WriteLine("Saved newsletter.epub");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
//////////////////////////////////////////////////////////////////////////
try
{
// Convert PDF document to multipage TIFF
Console.WriteLine("Converting PDF to multipage TIFF");
pdftron.PDF.Convert.TiffOutputOptions tiff_options = new pdftron.PDF.Convert.TiffOutputOptions();
tiff_options.SetDPI(200);
tiff_options.SetDither(true);
tiff_options.SetMono(true);
pdftron.PDF.Convert.ToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options);
Console.WriteLine("Saved newsletter.tiff");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
//////////////////////////////////////////////////////////////////////////
try
{
using (PDFDoc pdfdoc = new PDFDoc())
{
// Convert SVG file to PDF
Console.WriteLine("Converting SVG to PDF");
pdftron.PDF.Convert.FromSVG(pdfdoc, inputPath + "tiger.svg", null);
pdfdoc.Save(outputPath + "svg2pdf.pdf", SDFDoc.SaveOptions.e_remove_unused);
Console.WriteLine("Saved svg2pdf.pdf");
}
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
err = true;
}
return err;
}
static Boolean ConvertToPdfFromFile()
{
System.Collections.ArrayList testfiles = new System.Collections.ArrayList();
testfiles.Add(new ConvertTestCS.Testfile("simple-word_2007.docx", "docx2pdf.pdf", false));
testfiles.Add(new ConvertTestCS.Testfile("simple-powerpoint_2007.pptx", "pptx2pdf.pdf", false));
testfiles.Add(new ConvertTestCS.Testfile("simple-excel_2007.xlsx", "xlsx2pdf.pdf", false));
testfiles.Add(new ConvertTestCS.Testfile("simple-publisher.pub", "pub2pdf.pdf", true));
testfiles.Add(new ConvertTestCS.Testfile("simple-text.txt", "txt2pdf.pdf", false));
testfiles.Add(new ConvertTestCS.Testfile("simple-rtf.rtf", "rtf2pdf.pdf", true));
testfiles.Add(new ConvertTestCS.Testfile("butterfly.png", "png2pdf.pdf", false));
testfiles.Add(new ConvertTestCS.Testfile("simple-emf.emf", "emf2pdf.pdf", true));
testfiles.Add(new ConvertTestCS.Testfile("simple-xps.xps", "xps2pdf.pdf", false));
// testfiles.Add(new ConvertTestCS.Testfile("simple-webpage.mht", "mht2pdf.pdf", true));
testfiles.Add(new ConvertTestCS.Testfile("simple-webpage.html", "html2pdf.pdf", true));
bool err = false;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
try
{
if (pdftron.PDF.Convert.Printer.IsInstalled("PDFTron PDFNet"))
{
pdftron.PDF.Convert.Printer.SetPrinterName("PDFTron PDFNet");
}
else if (!pdftron.PDF.Convert.Printer.IsInstalled())
{
try
{
Console.WriteLine("Installing printer (requires Windows platform and administrator)");
pdftron.PDF.Convert.Printer.Install();
Console.WriteLine("Installed printer " + pdftron.PDF.Convert.Printer.GetPrinterName());
// the function ConvertToXpsFromFile may require the printer so leave it installed
// uninstallPrinterWhenDone = true;
}
catch (PDFNetException e)
{
Console.WriteLine("ERROR: Unable to install printer.");
Console.WriteLine(e.Message);
err = true;
}
catch
{
Console.WriteLine("ERROR: Unable to install printer. Make sure that the package's bitness matches your operating system's bitness and that you are running with administrator privileges.");
}
}
}
catch (PDFNetException e)
{
Console.WriteLine("ERROR: Unable to install printer.");
Console.WriteLine(e.Message);
err = true;
}
}
foreach (Testfile file in testfiles)
{
if ( Environment.OSVersion.Platform != PlatformID.Win32NT)
{
if (file.requiresWindowsPlatform)
{
continue;
}
}
try
{
using (pdftron.PDF.PDFDoc pdfdoc = new PDFDoc())
{
if (pdftron.PDF.Convert.RequiresPrinter(inputPath + file.inputFile))
{
Console.WriteLine("Using PDFNet printer to convert file " + file.inputFile);
}
pdftron.PDF.Convert.ToPdf(pdfdoc, inputPath + file.inputFile);
pdfdoc.Save(outputPath + file.outputFile, SDFDoc.SaveOptions.e_linearized);
Console.WriteLine("Converted file: " + file.inputFile);
Console.WriteLine("to: " + file.outputFile);
}
}
catch (PDFNetException e)
{
Console.WriteLine("ERROR: on input file " + file.inputFile);
Console.WriteLine(e.Message);
err = true;
}
}
return err;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
PDFNet.Initialize(PDFTronLicense.Key);
bool err = false;
err = ConvertToPdfFromFile();
if (err)
{
Console.WriteLine("ConvertFile failed");
}
else
{
Console.WriteLine("ConvertFile succeeded");
}
err = ConvertSpecificFormats();
if (err)
{
Console.WriteLine("ConvertSpecificFormats failed");
}
else
{
Console.WriteLine("ConvertSpecificFormats succeeded");
}
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
if (pdftron.PDF.Convert.Printer.IsInstalled())
{
try
{
Console.WriteLine("Uninstalling printer (requires Windows platform and administrator)");
pdftron.PDF.Convert.Printer.Uninstall();
Console.WriteLine("Uninstalled Printer " + pdftron.PDF.Convert.Printer.GetPrinterName());
}
catch
{
Console.WriteLine("Unable to uninstall printer");
}
}
}
PDFNet.Terminate();
Console.WriteLine("Done.");
}
}
}