Working with Document Conversions in File Formats PDF

Converting Word documents to PDF

Essential® PDF allows you to convert a Word document into PDF. For converting a Word document to PDF, the following assemblies need to be referenced in your application.

Assembly Name

Description

Syncfusion.DocIO.Base

This assembly has the core features for creating and manipulating Word documents.

Syncfusion.Compression.Base

This assembly is used to package the Word documents

Syncfusion.DocToPdfConverter.Base

This assembly is needed for converting the Word document to PDF.

Syncfusion.Pdf.Base

This assembly has the core features for creating PDF file.

Syncfusion.OfficeChart.Base

This assembly has features to work with chart in Word document.

The following assemblies are need to be referred in addition to the above mentioned assemblies for converting the chart present in the Word document into PDF.

Assembly Name

Description

Syncfusion.OfficeChartToImageConverter.WPF

This assembly is used to convert the chart to image.

Syncfusion.SfChart.WPF

This is supporting assembly for Syncfusion.OfficeChartToImageConverter.WPF

Syncfusion.Shared.WPF

This is supporting assembly for Syncfusion.OfficeChartToImageConverter.WPF

The following namespaces are required to compile the code in this topic.

For Windows Forms, WPF, ASP.NET and ASP.NET MVC applications

  • using Syncfusion.OfficeChart
  • using Syncfusion.DocIO
  • using Syncfusion.DocIO.DLS
  • using Syncfusion.DocToPDFConverter
  • using Syncfusion.Pdf
  • using Syncfusion.OfficeChartToImageConverter

For ASP.NET Core and Xamarin applications

  • using Syncfusion.DocIO
  • using Syncfusion.DocIO.DLS
  • using Syncfusion.DocIORenderer
  • using Syncfusion.Pdf

DocToPDFConverter class is responsible for converting a Word document into PDF. The following code snippet illustrates how to convert a Word document into PDF document.

using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

//Load an existing Word document.
WordDocument wordDocument = new WordDocument("Template.docx", FormatType.Docx);
//Initialize chart to image converter for converting charts during Word to pdf conversion.
wordDocument.ChartToImageConverter = new ChartToImageConverter();
//Create an instance of DocToPDFConverter.
DocToPDFConverter converter = new DocToPDFConverter();
//Convert Word document into PDF document.
PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument);

//Save the PDF file.
pdfDocument.Save("WordtoPDF.pdf");
//Close the instance of document objects.
pdfDocument.Close(true);
wordDocument.Close();
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

//Load an existing Word document.
WordDocument wordDocument = new WordDocument("Template.docx", FormatType.Docx);
//Initialize chart to image converter for converting charts during Word to pdf conversion.
wordDocument.ChartToImageConverter = new ChartToImageConverter();
//Create an instance of DocToPDFConverter.
DocToPDFConverter converter = new DocToPDFConverter();
//Convert Word document into PDF document.
PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument);

//Save the PDF file.
pdfDocument.Save("WordtoPDF.pdf");
//Close the instance of document objects.
pdfDocument.Close(true);
wordDocument.Close();
Imports Syncfusion.DocIO.DLS
Imports Syncfusion.DocIORenderer
Imports Syncfusion.Pdf

'Load an existing Word document
Dim wordDocument As New WordDocument("Template.docx", FormatType.Docx)
'Initialize chart to image converter for converting charts during Word to pdf conversion
wordDocument.ChartToImageConverter = New ChartToImageConverter()
'Create an instance of DocToPDFConverter
Dim converter As New DocToPDFConverter()
'Convert Word document into PDF document
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(wordDocument)

'Save the PDF file
pdfDocument.Save("WordtoPDF.pdf")
'Close the instance of document objects
pdfDocument.Close(True)
wordDocument.Close()

You can download a complete working sample from GitHub.

Note:

  • Initializing the ChartToImageConverter is mandatory to convert the charts present in the Word document to PDF. Otherwise the charts will not be exported to the converted PDF.
  • ChartToImageConverter is supported from .NET Framework 4.0 onwards.
  • Total number of pages may vary based on unsupported elements in the converted PDF document when compare to Word document.

Customizing the Word document to PDF conversion

Essential® DocIO allows you to customize the Word to PDF conversion using DocToPDFConverter class with the below options:

  • Allows to determine the quality of the charts in the converted PDF.
  • Allows to determine the quality of the JPEG images in the converted PDF.
  • Allows to reduce the Main Memory usage in Word to PDF conversion by reusing the identical images.
//PDF doesn't support customizing the Word document C#.NET Cross platforms.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocToPDFConverter;
using Syncfusion.OfficeChart;
using Syncfusion.OfficeChartToImageConverter;
using Syncfusion.Pdf;

//Loads an existing Word document.
WordDocument wordDocument = new WordDocument("Sample_Image.docx", FormatType.Docx);
//Initialize chart to image converter for converting charts during Word to pdf conversion.
wordDocument.ChartToImageConverter = new ChartToImageConverter();
//Set the scaling mode for charts.
wordDocument.ChartToImageConverter.ScalingMode = ScalingMode.Normal;

//create an instance of DocToPDFConverter - responsible for Word to PDF conversion.
DocToPDFConverter converter = new DocToPDFConverter();
//Set the image quality.
converter.Settings.ImageQuality = 100;
//Set the image resolution.
converter.Settings.ImageResolution = 640;
//Set true to optimize the memory usage for identical images.
converter.Settings.OptimizeIdenticalImages = true;
//Convert Word document into PDF document.
PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument);

//Save the PDF file to file system.
pdfDocument.Save("WordtoPDF.pdf");
//close the instance of document objects.
pdfDocument.Close(true);
wordDocument.Close();
Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
Imports Syncfusion.DocToPDFConverter
Imports Syncfusion.OfficeChart
Imports Syncfusion.OfficeChartToImageConverter
Imports Syncfusion.Pdf

'Loads an existing Word document
Dim wordDocument As New WordDocument("Sample_Image.docx", FormatType.Docx)
'Initialize chart to image converter for converting charts during Word to pdf conversion
wordDocument.ChartToImageConverter = New ChartToImageConverter()
'Set the scaling mode for charts
wordDocument.ChartToImageConverter.ScalingMode = ScalingMode.Normal

'create an instance of DocToPDFConverter - responsible for Word to PDF conversion
Dim converter As New DocToPDFConverter()
'Set the image quality
converter.Settings.ImageQuality = 100
'Set the image resolution
converter.Settings.ImageResolution = 640
'Set true to optimize the memory usage for identical images
converter.Settings.OptimizeIdenticalImages = True
'Convert Word document into PDF document
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(wordDocument)

'Save the PDF file to file system
pdfDocument.Save("WordtoPDF.pdf")
'close the instance of document objects
pdfDocument.Close(True)
wordDocument.Close()

You can download a complete working sample from GitHub.

Converting Excel documents to PDF

ExcelToPdfConverter is responsible for converting an Excel document into PDF. Essential® PDF allows you to convert an entire workbook or a single worksheet into PDF document. Refer to the following links for assemblies/nuget packages required based on platforms to convert Excel document into PDF.

NOTE

Excel to PDF conversion works proper in Blazor server-side alone and not in client-side.

Converting a Workbook to PDF

The following code illustrates how to convert a workbook to PDF Document using IWorkbook type in ExcelToPdfConverter class.

using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  //Load the document.
  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
  //Initialize XlsIO renderer.
  XlsIORenderer renderer = new XlsIORenderer();
  //Convert Excel document into PDF document.
  PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

  //Save the PDF file.
  pdfDocument.Save("ExcelToPDF.pdf");
}
using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

using(ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
  //Open the Excel document to Convert.
  ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
  //Initialize PDF document.
  PdfDocument pdfDocument = new PdfDocument();
  //Convert Excel document into PDF document.
  pdfDocument = converter.Convert();

  //Save the PDF file.
  pdfDocument.Save("ExcelToPDF.pdf");
}
Imports Syncfusion.Pdf
Imports Syncfusion.XlsIO
Imports Syncfusion.XlsIORenderer

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
  'Open the Excel document to convert
  Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
  'Initialize the PDF document
  Dim pdfDocument As PdfDocument = New PdfDocument()
  'Convert Excel document into PDF document
  pdfDocument = converter.Convert()

  'Save the PDF file
  pdfDocument.Save("ExcelToPDF.pdf")
End Using

You can download a complete working sample from GitHub.

Converting a Worksheet to PDF

The following code shows how to convert a particular sheet to PDF Document using IWorksheet type in ExcelToPdfConverter class.

using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

Using(ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
  IWorksheet sheet = workbook.Worksheets[0];

  //convert the sheet to PDF.
  ExcelToPdfConverter converter = new ExcelToPdfConverter(sheet);
  //Initialize PDF document.
  PdfDocument pdfDocument= new PdfDocument();
  //Convert Excel document into PDF document.
  pdfDocument = converter.Convert();

  //Save the PDF file.
  pdfDocument.Save("ExcelToPDF.pdf");       
}
using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

Using(ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
  IWorksheet sheet = workbook.Worksheets[0];

  //convert the sheet to PDF.
  ExcelToPdfConverter converter = new ExcelToPdfConverter(sheet);
  //Initialize PDF document.
  PdfDocument pdfDocument= new PdfDocument();
  //Convert Excel document into PDF document.
  pdfDocument = converter.Convert();

  //Save the PDF file.
  pdfDocument.Save("ExcelToPDF.pdf");       
}
Imports Syncfusion.Pdf
Imports Syncfusion.XlsIO
Imports Syncfusion.XlsIORenderer

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
  Dim sheet As IWorksheet = workbook.Worksheets(0)

  'Converts the particular sheet. 
  Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(sheet)
  'Initialize PDF document.
  Dim pdfDocument As PdfDocument = New PdfDocument()
  'Convert Excel document into PDF document.
  pdfDocument = converter.Convert()

  'Save the PDF file.
  pdfDocument.Save("ExcelToPDF.pdf")
End Using

You can download a complete working sample from GitHub.

Creating individual PDF document for each worksheet

The following code snippet shows how to create an individual PDF document for each worksheet in a workbook using ExcelToPdfConverter class.

using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

Using(ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);

  //Create a new PDF document.
  PdfDocument pdfDocument = new PdfDocument();     
  foreach (IWorksheet sheet in workbook.Worksheets)
  {
    //Open the Excel document to Convert.
    ExcelToPdfConverter converter = new ExcelToPdfConverter(sheet);
    pdfDocument = converter.Convert();

    //Save the PDF file.
    pdfDocument.Save(sheet.Name +".pdf");
    converter.Dispose();
  }
}
using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

Using(ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);

  //Create a new PDF document.
  PdfDocument pdfDocument = new PdfDocument();     
  foreach (IWorksheet sheet in workbook.Worksheets)
  {
    //Open the Excel document to Convert.
    ExcelToPdfConverter converter = new ExcelToPdfConverter(sheet);
    pdfDocument = converter.Convert();

    //Save the PDF file.
    pdfDocument.Save(sheet.Name +".pdf");
    converter.Dispose();
  }
}
Imports Syncfusion.Pdf
Imports Syncfusion.XlsIO
Imports Syncfusion.XlsIORenderer

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")

  'Create a new PDF document.
  Dim pdfDocument As New PdfDocument()
  For Each sheet As IWorksheet In workbook.Worksheets
    'Open the Excel document to Convert.
    Dim converter As New ExcelToPdfConverter(sheet)
    PdfDocument = converter.Convert()

    'Save the PDF file.
    PdfDocument.Save(sheet.Name + ".pdf")
    converter.Dispose()
  Next
End Using

You can download a complete working sample from GitHub.

Excel with Chart to PDF

To preserve the charts during Excel to PDF conversion, you should initialize the ChartToImageConverter of IApplication interface, otherwise the charts present in worksheet will get skipped. The following code illustrate how to convert an Excel with chart to PDF document.

using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

Using(ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  //Instantiating the ChartToImageConverter and assigning the ChartToImageConverter instance of XlsIO application.
  application.ChartToImageConverter = new ChartToImageConverter();
  //Tuning chart image quality.
  application.ChartToImageConverter.ScalingMode = ScalingMode.Best;
  IWorkbook workbook = application.Workbooks.Open("chart.xlsx");
  IWorksheet worksheet = workbook.Worksheets[0];

  //Open the Excel document to Convert.
  ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);

  //Initialize PDF document.
  PdfDocument pdfDocument = new PdfDocument();
  //Convert Excel document into PDF document.
  pdfDocument = converter.Convert();
  //Save the PDF file.
  pdfDocument.Save("ExcelToPDF.pdf");
}
using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

Using(ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  //Instantiating the ChartToImageConverter and assigning the ChartToImageConverter instance of XlsIO application.
  application.ChartToImageConverter = new ChartToImageConverter();
  //Tuning chart image quality.
  application.ChartToImageConverter.ScalingMode = ScalingMode.Best;
  IWorkbook workbook = application.Workbooks.Open("chart.xlsx");
  IWorksheet worksheet = workbook.Worksheets[0];

  //Open the Excel document to Convert.
  ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);

  //Initialize PDF document.
  PdfDocument pdfDocument = new PdfDocument();
  //Convert Excel document into PDF document.
  pdfDocument = converter.Convert();
  //Save the PDF file.
  pdfDocument.Save("ExcelToPDF.pdf");
}
Imports Syncfusion.Pdf
Imports Syncfusion.XlsIO
Imports Syncfusion.XlsIORenderer

Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  'Instantiating the ChartToImageConverter and assigning the ChartToImageConverter instance of XlsIO application.
  application.ChartToImageConverter = New ChartToImageConverter()
  'Tuning chart image quality.
  application.ChartToImageConverter.ScalingMode = ScalingMode.Best
  Dim workbook As IWorkbook = application.Workbooks.Open("chart.xlsx")
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Open the Excel document to Convert.
  Dim converter As New ExcelToPdfConverter(workbook)
  
  'Initialize PDF document.
  Dim pdfDocument As New PdfDocument()
  'Convert Excel document into PDF document.
  pdfDocument = converter.Convert()
  'Save the PDF file.
  pdfDocument.Save("ExcelToPDF.pdf")
End Using

You can download a complete working sample from GitHub.

Supported Elements

This feature provides support for the following elements:

  • Styles
  • Character formatting
  • Headers and footers
  • Images
  • Text box
  • Hyperlinks
  • Document properties
  • Comments
  • Encryption
  • Table Style Support
  • Text Rotations
  • Excel Page Setup Options
  • Unicode Support
  • Background Images
  • Printing Titles when Converting the Excel to PDF
  • Page Break Support
  • Print Area Support
  • Print Order Support
  • Unicode in Headers and Footers

Unsupported Elements

The following list contains unsupported elements that presently will not be preserved in the generated PDF document.

  • Grouping columns
  • OLE Objects
  • Text rotations
  • Background images

Converting RTF documents to PDF

Essential® PDF allows you to convert a RTF to PDF document. For converting a RTF to PDF, the following assemblies need to be referenced in your application.

Assembly Name

Description

Syncfusion.DocIO.Base

This assembly has the core features for creating and manipulating RTF documents.

Syncfusion.Compression.Base

This assembly is used to package the RTF documents

Syncfusion.DocToPdfConverter.Base

This assembly is needed for converting the RTF to PDF.

Syncfusion.Pdf.Base

This assembly has the core features for creating PDF file.

The following namespaces are required to compile the code in this topic.

For Windows Forms, WPF, ASP.NET and ASP.NET MVC applications

  • using Syncfusion.DocIO
  • using Syncfusion.DocIO.DLS
  • using Syncfusion.DocToPDFConverter
  • using Syncfusion.Pdf

For ASP.NET Core and Xamarin applications

  • using Syncfusion.DocIO
  • using Syncfusion.DocIO.DLS
  • using Syncfusion.DocIORenderer
  • using Syncfusion.Pdf

DocToPDFConverter class is responsible for converting a RTF to PDF. The following code snippet illustrates how to convert a RTF to PDF document.

using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

//Load an existing RTF document.
WordDocument rtfDocument = new WordDocument("Input.rtf");
//Create an instance of DocToPDFConverter.
DocToPDFConverter converter = new DocToPDFConverter();
//Convert Word document into PDF document.
PdfDocument pdfDocument = converter.ConvertToPDF(rtfDocument);

//Save the PDF file.
pdfDocument.Save("RTFToPDF.pdf");
//Close the instance of document objects.
pdfDocument.Close(true);
rtfDocument.Close();
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

//Load an existing RTF document.
WordDocument rtfDocument = new WordDocument("Input.rtf");
//Create an instance of DocToPDFConverter.
DocToPDFConverter converter = new DocToPDFConverter();
//Convert Word document into PDF document.
PdfDocument pdfDocument = converter.ConvertToPDF(rtfDocument);

//Save the PDF file.
pdfDocument.Save("RTFToPDF.pdf");
//Close the instance of document objects.
pdfDocument.Close(true);
rtfDocument.Close();
Imports Syncfusion.DocIO.DLS
Imports Syncfusion.DocIORenderer
Imports Syncfusion.Pdf

'Load an existing Word document
Dim rtfDocument As New WordDocument(inputFileName)
'Create an instance of DocToPDFConverter
Dim converter As New DocToPDFConverter()
'Convert Word document into PDF document
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(rtfDocument)

'Save the PDF file
pdfDocument.Save("RTFToPDF.pdf")
'Close the instance of document objects
pdfDocument.Close(True)
rtfDocument.Close()

You can download a complete working sample from GitHub.

NOTE

Total number of pages may vary based on unsupported elements in the converted PDF document when compare to RTF document.

Customizing the RTF to PDF conversion

Essential® DocIO allows you to customize the RTF to PDF conversion using DocToPDFConverter class with the below options:

  • Allows to determine the quality of the JPEG images in the converted PDF.
  • Allows to reduce the Main Memory usage in RTF to PDF conversion by reusing the identical images.
//PDF doesn't support customizing the RTF to PDF conversion C#.NET Cross platforms.
using Syncfusion.DocIO.DLS;
using Syncfusion.DocToPDFConverter;
using Syncfusion.Pdf;
using System;
using System.Collections.Generic;

//Loads an existing Word document
WordDocument rtfDocument = new WordDocument(inputFileName);
//create an instance of DocToPDFConverter - responsible for Word to PDF conversion
DocToPDFConverter converter = new DocToPDFConverter();
//Set the image quality
converter.Settings.ImageQuality = 100;
//Set the image resolution
converter.Settings.ImageResolution = 640;
//Set true to optimize the memory usage for identical images
converter.Settings.OptimizeIdenticalImages = true;
//Convert Word document into PDF document
PdfDocument pdfDocument = converter.ConvertToPDF(rtfDocument);

//Save the PDF file to file system
pdfDocument.Save("RTFToPDF.pdf");
//close the instance of document objects
pdfDocument.Close(true);
rtfDocument.Close();
Imports Syncfusion.DocIO.DLS
Imports Syncfusion.DocToPDFConverter
Imports Syncfusion.Pdf
Imports System
Imports System.Collections.Generic

'Loads an existing Word document
Dim rtfDocument As New WordDocument(inputFileName)
'create an instance of DocToPDFConverter - responsible for Word to PDF conversion
Dim converter As New DocToPDFConverter()
'Set the image quality
converter.Settings.ImageQuality = 100
'Set the image resolution
converter.Settings.ImageResolution = 640
'Set true to optimize the memory usage for identical images
converter.Settings.OptimizeIdenticalImages = True
'Convert Word document into PDF document
Dim pdfDocument As PdfDocument = converter.ConvertToPDF(rtfDocument)

'Save the PDF file to file system
pdfDocument.Save("RTFToPDF.pdf")
'close the instance of document objects
pdfDocument.Close(True)
rtfDocument.Close()

You can download a complete working sample from GitHub.

Converting TIFF to PDF

Converting multi page TIFF to PDF

Multi frame TIFF image can be converted to PDF document using PdfBitmap class. This can be done by accessing each frame of the multi frame TIFF image and rendering it in each page of the PDF document.

The code snippet to illustrate the same is given below.

using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;

//Create a PDF document
PdfDocument pdfDocument = new PdfDocument();

//Load multi frame TIFF image
PdfBitmap tiffImage = new PdfBitmap("image.tiff");
//Get the frame count
int frameCount = tiffImage.FrameCount;
//Access each frame and draw into the page
for (int i = 0; i < frameCount; i++)
{
	tiffImage.ActiveFrame = i;
	//Add a section to the PDF document
	PdfSection section = pdfDocument.Sections.Add();
	//Set page margins
	section.PageSettings.Margins.All = 0;
	//Create a PDF unit converter instance
	PdfUnitConvertor converter = new PdfUnitConvertor();
	//Convert to point
	SizeF size = converter.ConvertFromPixels(tiffImage.PhysicalDimension, PdfGraphicsUnit.Point);
	//Set page orientation
	section.PageSettings.Orientation = (size.Width > size.Height) ? PdfPageOrientation.Landscape : PdfPageOrientation.Portrait;
	//Set page size
	section.PageSettings.Size = size;
	//Add a page to the section
	PdfPage page = section.Pages.Add();
	//Draw TIFF image into the PDF page
	page.Graphics.DrawImage(tiffImage, PointF.Empty, size);
}

//Save and close the document
pdfDocument.Save("Sample.pdf");
pdfDocument.Close(true);
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;

//Create a PDF document
PdfDocument pdfDocument = new PdfDocument();

//Load multi frame TIFF image
PdfBitmap tiffImage = new PdfBitmap("image.tiff");
//Get the frame count
int frameCount = tiffImage.FrameCount;
//Access each frame and draw into the page
for (int i = 0; i < frameCount; i++)
{
	tiffImage.ActiveFrame = i;
	//Add a section to the PDF document
	PdfSection section = pdfDocument.Sections.Add();
	//Set page margins
	section.PageSettings.Margins.All = 0;
	//Create a PDF unit converter instance
	PdfUnitConvertor converter = new PdfUnitConvertor();
	//Convert to point
	SizeF size = converter.ConvertFromPixels(tiffImage.PhysicalDimension, PdfGraphicsUnit.Point);
	//Set page orientation
	section.PageSettings.Orientation = (size.Width > size.Height) ? PdfPageOrientation.Landscape : PdfPageOrientation.Portrait;
	//Set page size
	section.PageSettings.Size = size;
	//Add a page to the section
	PdfPage page = section.Pages.Add();
	//Draw TIFF image into the PDF page
	page.Graphics.DrawImage(tiffImage, PointF.Empty, size);
}

//Save and close the document
pdfDocument.Save("Sample.pdf");
pdfDocument.Close(true);
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf

'Create a PDF document
Dim pdfDocument As New PdfDocument()

'Load multi frame TIFF image
Dim tiffImage As New PdfBitmap("image.tiff")
'Get the frame count
Dim frameCount As Integer = tiffImage.FrameCount
'Access each frame and draw into the page
For i As Integer = 0 To frameCount - 1
  tiffImage.ActiveFrame = i
  'Add a section to the PDF document
  Dim section As PdfSection = pdfDocument.Sections.Add()
  'Set page margins
  section.PageSettings.Margins.All = 0
  'Create a PDF unit converter instance
  Dim converter As New PdfUnitConvertor()
  'Convert to point
  Dim size As SizeF = converter.ConvertFromPixels(tiffImage.PhysicalDimension, PdfGraphicsUnit.Point)
  'Set page orientation
  If size.Width > size.Height Then section.PageSettings.Orientation = PdfPageOrientation.Landscape
  'Set page size
  section.PageSettings.Size = size
  'Add a page to the section
  Dim page As PdfPage = section.Pages.Add()
  'Draw TIFF image into the PDF page
  page.Graphics.DrawImage(tiffImage, PointF.Empty, size)
Next

'Save and close the document
pdfDocument.Save("Sample.pdf")
pdfDocument.Close(True)

You can download a complete working sample from GitHub.

NOTE

  1. Essential® PDF supports converting TIFF to PDF with Syncfusion.Pdf.Imaging.Portable assembly reference in ASP.NET Core.

Compression in monochrome images

Essential® PDF supports JBIG2 compression for best compression of monochrome images.

Refer the below code snippet to draw a single frame monochrome TIFF image with JBIG2 compression using EncodingType Enum.

//PDF doesn't support compressing monochrome images C#.NET Cross platforms.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System;
using System.Collections.Generic;

//Create a PDF document
PdfDocument pdfDocument = new PdfDocument();
//Add a page
PdfPage page = pdfDocument.Pages.Add();

//Load single frame TIFF image
PdfBitmap tiffImage = PdfImage.FromFile("image.tiff") as PdfBitmap;
//Set encode type
tiffImage.Encoding = EncodingType.JBIG2;
//Draw an image
page.Graphics.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);

//Save and close the document
pdfDocument.Save("Sample.pdf");
pdfDocument.Close(true);
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf
Imports System
Imports System.Collections.Generic

'Create a PDF document
Dim pdfDocument As New PdfDocument()
'Add a page
Dim page As PdfPage = pdfDocument.Pages.Add()

'Load single frame TIFF image
Dim tiffImage As PdfBitmap = TryCast(PdfImage.FromFile("image.tiff"), PdfBitmap)
'Set encode type
tiffImage.Encoding = EncodingType.JBIG2
'Draw an image
page.Graphics.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height)

'Save and close the document
pdfDocument.Save("Sample.pdf")
pdfDocument.Close(True)

You can download a complete working sample from GitHub.

NOTE

  1. Currently the JBIG2Decode compression is supported only in lossy mode and also only single frame TIFF images are supported.
  2. By default, all monochrome images will be compressed in CITTT4 compression.

Converting XPS document to PDF

The XPS (XML Paper Specification) document format is a fixed document format which consists of structured XML markup that defines the layout of a document and the visual appearance of each page, along with rendering rules for distributing, archiving, rendering, processing and printing the documents.

Essential® PDF provides support for converting XPS to PDF using XPSToPdfConverter class.

The below code illustrates how to convert XPS to PDF.

using Syncfusion.Pdf;
using Syncfusion.XPS;

//Create converter class
XPSToPdfConverter converter = new XPSToPdfConverter();
//Convert the XPS to PDF
PdfDocument document = converter.Convert("Input.xps");

//Save and close the document
document.Save("Sample.pdf");
document.Close(true);
using Syncfusion.Pdf;
using Syncfusion.XPS;

//Create converter class
XPSToPdfConverter converter = new XPSToPdfConverter();
//Convert the XPS to PDF
PdfDocument document = converter.Convert("Input.xps");

//Save and close the document
document.Save("Sample.pdf");
document.Close(true);
Imports Syncfusion.Pdf
Imports Syncfusion.XPS

'Create converter class
Dim converter As New XPSToPdfConverter()
'Convert the XPS to PDF
Dim document As PdfDocument = converter.Convert("Input.xps")

'Save and close the document
document.Save("Sample.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

NOTE

Essential® PDF supports converting XPS to PDF with Syncfusion.XpsToPdfConverter.Net.Core package reference in .NET Core application.

Supported Elements

The below table shows the list of elements supported in XPS during the conversion.

Element

Convert to PDF

ArcSegment

Yes

Canvas

Yes

DocumentOutline

No

DocumentReference

No

FigureStructure

No

FixedPageResources

Yes

Glyphs

Yes

Gradient

Yes

ImageBrush
(JPG, BMP, GIF, TIFF)
Yes

Intent

Yes

LinkTarget

Yes

ListItemStructure

Yes

ListStructure

Yes

MatrixTransform

Yes

NamedElement

No

OutlineEntry

No

PageContent

Yes

PageContentLinkTargets

No

ParagraphStructure

No

Path

Yes

PolyBezierSegment

Yes

PolyLineSegment

Yes

PolyQuadraticBezierSegment

Yes

ResourceDictionary

Yes

SectionStructure

No

SignBy

No

SignatureDefinition

No

SignatureDefinitions

No

SigningLocation

No

SolidColorBrush

Yes

SpotLocation

No

Story

No

TableStructure

No

VisualBrush

No

Converting PDF to Image

This PDF to image converter library allows converting PDF documents to images without opening the document in the PDF Viewer control. It allows you to selectively export pages as a stream by utilizing the ‘Convert’ method, facilitating the transformation of PDF files into images.

NuGet

Platform(s) NuGet Package
Windows Forms

Syncfusion.PdfToImageConverter.WinForms.nupkg

WPF

Syncfusion.PdfToImageConverter.WPF.nupkg

ASP.NET Core Windows

Syncfusion.PdfToImageConverter.Net.nupkg

ASP.NET MVC Windows

Syncfusion.PdfToImageConverter.AspNet.Mvc4.nupkg


Syncfusion.PdfToImageConverter.AspNet.Mvc5.nupkg

NOTE

The above mentioned NuGet packages are available in nuget.org.

The following code snippet illustrates how to convert PDF page into image using Convert method in PdfToImageConverter.

using Syncfusion.PdfToImageConverter;
using System.IO;

//Initialize PDF to Image converter.
PdfToImageConverter imageConverter = new PdfToImageConverter();
//Load the PDF document as a stream
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite);
imageConverter.Load(inputStream);
//Convert PDF to Image.
Stream outputStream = imageConverter.Convert(0, false, false);
Bitmap image = new Bitmap(outputStream);
image.Save("sample.png");
using Syncfusion.PdfToImageConverter;
using System.IO;

//Initialize PDF to Image converter.
PdfToImageConverter imageConverter = new PdfToImageConverter();
//Load the PDF document as a stream
FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite);
imageConverter.Load(inputStream);
//Convert PDF to Image.
Stream outputStream = imageConverter.Convert(0, false, false);
Bitmap image = new Bitmap(outputStream);
image.Save("sample.png");
Imports Syncfusion.PdfToImageConverter
Imports System.IO

'Initialize PDF to Image converter.
Dim imageConverter As PdfToImageConverter = New PdfToImageConverter()
'Load the PDF document as a stream
Dim inputStream As FileStream = New FileStream("Input.pdf", FileMode.Open, FileAccess.ReadWrite)
imageConverter.Load(inputStream)
'Convert PDF to Image.
Dim outputStream As Stream = imageConverter.Convert(0, False, False)
Dim image As Bitmap = New Bitmap(outputStream)
image.Save("sample.png")

You can download a complete working sample from GitHub.

NOTE

To know more about PdfToImageConverter and features it provides, please refer to PdfToImageConverter

HTML to PDF

The HTML to PDF converter library supports converting web pages and HTML content into high-quality PDF documents.

Learn more about the features supported in HTML to PDF conversion here: HTML to PDF Features

Check out the troubleshooting guide here: Troubleshooting

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
 
//Save and close the PDF document.
document.Save("Output.pdf");
document.Close(true);
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
 
//Save and close the PDF document.
document.Save("Output.pdf");
document.Close(true);
Imports Syncfusion.HtmlConverter
Imports Syncfusion.Pdf

' Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()

' Convert HTML to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")

' Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

SVG to PDF

The HTML to PDF converter library supports converting the SVG to PDF document. Please refer to the following code example.

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert a SVG file to PDF with HTML converter
PdfDocument document = htmlConverter.Convert("inputSVG.svg");
//Save and close the PDF document
document.Save("SVGToPDF.pdf");
document.Close(true);
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert a SVG file to PDF with HTML converter
PdfDocument document = htmlConverter.Convert("inputSVG.svg");
//Save and close the PDF document
document.Save("SVGToPDF.pdf");
document.Close(true);
Imports Syncfusion.HtmlConverter
Imports Syncfusion.Pdf

'Initialize HTML to PDF converter 
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()
'Convert a SVG file to PDF with HTML converter 
Dim document As PdfDocument = htmlConverter.Convert("inputSVG.svg")
'Save and close the PDF document
document.Save("SVGToPDF.pdf")
document.Close(True)