Working with Document Conversions in File Formats PDF
25 Jul 202624 minutes to read
Converting Word documents to PDF
Essential® PDF enables you to convert a Word document into a PDF. To convert a Word document to PDF, the following assemblies need to be referenced in your application.
|
Assembly Name |
Description |
|---|---|
|
Syncfusion.DocIO.Base |
This assembly contains the core features for creating and manipulating Word documents. |
|
Syncfusion.Compression.Base |
This assembly is used to package Word documents. |
|
Syncfusion.DocToPdfConverter.Base |
This assembly is needed for converting Word documents to PDF. |
|
Syncfusion.Pdf.Base |
This assembly has the core features for creating PDF file. |
|
Syncfusion.OfficeChart.Base |
This assembly provides features to work with charts in Word documents. |
The following assemblies are also required, in addition to the assemblies listed above, to convert charts present in a Word document into PDF.
|
Assembly Name |
Description |
|---|---|
|
Syncfusion.OfficeChartToImageConverter.WPF |
This assembly is used to convert charts to images. |
|
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
The DocToPDFConverter class is responsible for converting a Word document into a PDF. The following code snippet illustrates how to convert a Word document into a 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 enables you to customize the Word to PDF conversion using the DocToPDFConverter class with the following 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.
//Customizing the Word to PDF conversion is supported only on Windows-specific (classic .NET Framework) targets.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 a PDF. Essential® PDF enables you to convert an entire workbook or a single worksheet into a PDF document. Refer to the following links for the assemblies/NuGet packages required, based on the platform, to convert an Excel document into PDF.
NOTE
Excel to PDF conversion works only in Blazor server-side, not in Blazor client-side.
Converting a workbook to PDF
The following code illustrates how to convert a workbook to a PDF document using the IWorkbook type in the 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 UsingYou can download a complete working sample from GitHub.
Converting a worksheet to PDF
The following code shows how to convert a particular sheet to a PDF document using the IWorksheet type in the 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 UsingYou can download a complete working sample from GitHub.
Creating an 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 the 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 UsingYou can download a complete working sample from GitHub.
Excel with chart to PDF
To preserve charts during Excel to PDF conversion, you should initialize the ChartToImageConverter of the IApplication interface; otherwise, the charts present in the worksheet will be skipped. The following code illustrates how to convert an Excel document with charts to a 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 UsingYou 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 elements that are not currently supported and will not be preserved in the generated PDF document.
- Grouping columns
- OLE Objects
- Text rotations
- Background images
Converting RTF documents to PDF
Essential® PDF enables you to convert an RTF document into a PDF. To convert an RTF to PDF, the following assemblies need to be referenced in your application.
|
Assembly Name |
Description |
|---|---|
|
Syncfusion.DocIO.Base |
This assembly contains the core features for creating and manipulating RTF documents. |
|
Syncfusion.Compression.Base |
This assembly is used to package RTF documents. |
|
Syncfusion.DocToPdfConverter.Base |
This assembly is needed for converting RTF documents 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
The DocToPDFConverter class is responsible for converting an RTF document into a PDF. The following code snippet illustrates how to convert an RTF document into a 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 RTF document
Dim rtfDocument As New WordDocument("Input.rtf")
'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
The total number of pages may vary based on the unsupported elements in the converted PDF document when compared to the original RTF document.
Customizing the RTF to PDF conversion
Essential® DocIO enables you to customize the RTF to PDF conversion using the DocToPDFConverter class with the following 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.
//Customizing the RTF to PDF conversion is supported only on Windows-specific (classic .NET Framework) targets.using Syncfusion.DocIO.DLS;
using Syncfusion.DocToPDFConverter;
using Syncfusion.Pdf;
using System;
using System.Collections.Generic;
//Load an existing RTF document
WordDocument rtfDocument = new WordDocument("Input.rtf");
//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
'Load an existing RTF document
Dim rtfDocument As New WordDocument("Input.rtf")
'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
A multi-frame TIFF image can be converted to a PDF document using the PdfBitmap class. This can be done by accessing each frame of the multi-frame TIFF image and rendering it on each page of the PDF document.
The following code snippet illustrates the same.
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
- Essential® PDF supports converting TIFF to PDF with a Syncfusion.Pdf.Imaging.Portable assembly reference in ASP.NET Core.
Compression in monochrome images
Essential® PDF supports JBIG2 compression for the best compression of monochrome images.
Refer to the following code snippet to draw a single-frame monochrome TIFF image with JBIG2 compression using the EncodingType enum.
| Encoding Type | Image Type | Compression Applied |
|---|---|---|
| Default | Non-TIFF images | Applies Deflate (DEFLATE) compression to monochrome, grayscale, and color images. |
| Default | TIFF images | Monochrome TIFF images use CCITT Group 4 (CCITT4) compression by default. |
| JBIG2 | Monochrome (bi-level) | Supported only in lossy mode and only for single-frame TIFF images. |
//Compressing monochrome images with JBIG2 is supported only on Windows-specific (classic .NET Framework) targets.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 = new PdfBitmap("image.tiff");
//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 New PdfBitmap("image.tiff")
'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
- Currently, JBIG2Decode compression is supported only in lossy mode, and only single-frame TIFF images are supported.
- By default, all monochrome images will be compressed using CCITT4 compression.
Converting an XPS document to PDF
The XPS (XML Paper Specification) document format is a fixed document format that consists of structured XML markup defining 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 the XPSToPdfConverter class.
The following code illustrates how to convert an XPS to a 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 following table lists the 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 enables 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 | |
| WPF | |
| ASP.NET Core Windows | |
| ASP.NET MVC Windows |
Syncfusion.PdfToImageConverter.AspNet.Mvc4.nupkg |
NOTE
The above mentioned NuGet packages are available in nuget.org.
The following code snippet illustrates how to convert a PDF page into an image using the 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
PdfToImageConverterand the 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 SVG to a 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)