Working with Document Conversions in File Formats PDF

15 Sep 202324 minutes to read

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.

// Open the file as Stream.
FileStream docStream = new FileStream(@"Template.docx", FileMode.Open, FileAccess.Read);
//Loads file stream into Word document.
WordDocument wordDocument = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic);
//Instantiation of DocIORenderer for Word to PDF conversion.
DocIORenderer render = new DocIORenderer();
//Converts Word document into PDF document.
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
//Releases all resources used by the Word document and DocIO Renderer objects.
render.Dispose();
wordDocument.Dispose();

//Save the document into stream.
MemoryStream stream = new MemoryStream();
pdfDocument.Save(stream);
//Close the document.
pdfDocument.Close(true);
//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();
'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.
//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();
'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 (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  //Load the document.
  FileStream excelStream = new FileStream("ExcelToPDF.xlsx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(excelStream);
  //Initialize XlsIO renderer.
  XlsIORenderer renderer = new XlsIORenderer();
  //Convert Excel document into PDF document.
  PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

  //Save the PDF document to stream.
  Stream stream = new FileStream("ExcelToPDF.pdf", FileMode.Create, FileAccess.ReadWrite);
  pdfDocument.Save(stream);
  //Dispose the stream.
  excelStream.Dispose();
  stream.Dispose();
}
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");
}
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 (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  FileStream excelStream = new FileStream("ExcelToPDF.xlsx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(excelStream);
  IWorksheet worksheet = workbook.Worksheets[0];
   
  //Initialize XlsIO renderer.
  XlsIORenderer renderer = new XlsIORenderer();
  //Convert Excel document into PDF document. 
  PdfDocument pdfDocument = renderer.ConvertToPDF(worksheet);

  //Saving the PDF to the MemoryStream.
  Stream stream = new FileStream("ExcelToPDF.pdf", FileMode.Create, FileAccess.ReadWrite);
  pdfDocument.Save(stream);
  //Dispose the stream.
  excelStream.Dispose();
  stream.Dispose();
}
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 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 (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  FileStream excelStream = new FileStream("ExcelToPDF.xlsx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(excelStream);
   
  //Initialize XlsIO renderer.
  XlsIORenderer renderer = new XlsIORenderer();
  //Create a new PDF document.
  PdfDocument pdfDocument = new PdfDocument();         	
  foreach (IWorksheet sheet in workbook.Worksheets)
  {
    pdfDocument = renderer.ConvertToPDF(sheet);
   
    //Save the PDF file.
    Stream stream = new FileStream(sheet.Name+".pdf", FileMode.Create, FileAccess.ReadWrite);
    pdfDocument.Save(stream);
    stream.Dispose();
  }
  excelStream.Dispose();
}
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 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 (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;   
  //Initialize XlsIO renderer.
  XlsIORenderer renderer = new XlsIORenderer();
  //Load the document as stream.
  FileStream excelStream = new FileStream("chart.xlsx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(excelStream);

  //Convert Excel document with charts into PDF document. 
  PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);

  //Save the PDF document.
  Stream stream = new FileStream("ExcelToPDF.pdf", FileMode.Create, FileAccess.ReadWrite);
  pdfDocument.Save(stream);
  //Dispose the stream.
  excelStream.Dispose();
  stream.Dispose();
}
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 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.

//Open the file as Stream
FileStream docStream = new FileStream(@"Input.rtf", FileMode.Open, FileAccess.Read);
//Loads file stream into Word document
WordDocument wordDocument = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic);
//Instantiation of DocIORenderer for Word to PDF conversion
DocIORenderer render = new DocIORenderer();
//Converts Word document into PDF document
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);

//Releases all resources used by the Word document and DocIO Renderer objects
render.Dispose();
wordDocument.Dispose();

//Save the document into stream
MemoryStream stream = new MemoryStream();
pdfDocument.Save(stream);
//Close the document
pdfDocument.Close(true);
//Load an existing RTF document.
WordDocument rtfDocument = new WordDocument(inputFileName);
//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();
'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.
//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();
'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.

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

//Load the multi frame TIFF image from the disk
FileStream imageStream = new FileStream("image.tiff", FileMode.Open, FileAccess.Read);
PdfTiffImage tiffImage = new PdfTiffImage(imageStream);
//Get the frame count
int frameCount = tiffImage.FrameCount;
//Access each frame and draw into the page
for (int i = 0; i < frameCount; i++)
{
	//Add a section to the PDF document
	PdfSection section = document.Sections.Add();
	//Set page margins
	section.PageSettings.Margins.All = 0;
	tiffImage.ActiveFrame = i;
	//Create a PDF unit converter instance
	PdfUnitConvertor converter = new PdfUnitConvertor();
	//Convert to point
	Syncfusion.Drawing.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, Syncfusion.Drawing.PointF.Empty, size);
}

//Creating the stream object
MemoryStream stream = new MemoryStream();
//Save the document as stream
document.Save(stream);
//Close the document
document.Close(true);
//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);
'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.
//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);
'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.

//Initialize XPS to PDF converter.
XPSToPdfConverter converter = new XPSToPdfConverter();
//Open the XPS file as stream.
FileStream fileStream = new FileStream("Input.xps", FileMode.Open, FileAccess.ReadWrite);
//Convert the XPS to PDF.
PdfDocument document = converter.Convert(fileStream);

//Creating the stream object.
MemoryStream stream = new MemoryStream(); 
//Save the document into stream.
document.Save(stream); 
//Close the documents.
document.Close(true);
//Create converter class
XPSToPdfConverter converter = new XPSToPdfConverter();
//Convert the XPS to PDF
PdfDocument document = converter.Convert(xpsFileName);

//Save and close the document
document.Save("Sample.pdf");
document.Close(true);
'Create converter class
Dim converter As New XPSToPdfConverter()
'Convert the XPS to PDF
Dim document As PdfDocument = converter.Convert(xpsFileName)

'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.

//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);
MemoryStream stream = outputStream as MemoryStream;
return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Image.Jpeg, "sample.jpeg");
//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");
'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

MHTML to PDF

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

HTML to PDF Features: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/features

Troubleshooting: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/troubleshooting

//Initialize HTML to PDF converter 
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert MHTML to PDF
PdfDocument document = htmlConverter.Convert(@"input.mhtml");
//Save the document into stream
MemoryStream stream = new MemoryStream();
//Close the document
document.Close(true);
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert MHTML to PDF
PdfDocument document = htmlConverter.Convert("input.mhtml");  
//Save the document
document.Save("Sample.pdf");
document.Close();
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()
'Convert MHTML to PDF
Dim document As PdfDocument = htmlConverter.Convert("input.mhtml")
'Save and close the document
document.Save("Sample.pdf")
document.Close()

You can download a complete working sample from GitHub.

HTML to MHTML

The HTML to PDF converter library supports converting the webpage to an MHTML file. Please refer to the following code example.

HTML to PDF Features: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/features

Troubleshooting: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/troubleshooting

//PDF doesn't support converting HTML to MHTML C#.NET Cross platforms.
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert URL to MHTML
htmlConverter.ConvertToMhtml("http://www.syncfusion.com", "sample.mhtml");
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()      
'Convert URL to MHTML
htmlConverter.ConvertToMhtml("http://www.syncfusion.com", "sample.mhtml")

You can download a complete working sample from GitHub.

HTML to Raster Image

The HTML to PDF converter library supports converting the webpage to an image. Please refer to the following code example.

HTML to PDF Features: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/features

Troubleshooting: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/troubleshooting

//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert URL to Image
Image image = htmlConverter.ConvertToImage("http://www.google.com");
byte[] imageByte = image.ImageData;
//Save the image
File.WriteAllBytes("Sample.jpg", imageByte);
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert URL to Image
Image[] image = htmlConverter.ConvertToImage("http://www.syncfusion.com");
//Save the image
image[0].Save("Sample.jpg");
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()
'Convert URL to Image
Dim image As Image() = htmlConverter.ConvertToImage("http://www.syncfusion.com")
'Save the image
image(0).Save("Sample.jpg")

You can download a complete working sample from GitHub.

HTML string to Raster Image

The HTML to PDF converter library supports converting the HTML string to an image. Please refer to the following code example.

HTML to PDF Features: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/features

Troubleshooting: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/troubleshooting

//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//HTML string and Base URL
string htmlString = "<html><body>Hello World!!!</body></html>";
string baseURL = "";
//Convert HTML string to Image
Image image = htmlConverter.ConvertToImage(htmlString, baseURL);
byte[] imageByte = image.ImageData;
//Save the image
File.WriteAllBytes("Output.jpg", imageByte);
string htmlString = "<html><body>Hello World!!!</body></html>";
string baseURL = "";
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert HTML string to Image
Image[] image = htmlConverter.ConvertToImage(htmlString, baseURL);
//Save the image
image[0].Save("Sample.jpg");
Dim htmlString As String = "<html><body>Hello World!!!</body></html>"
Dim baseURL As String = ""
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()
'Convert HTML string to Image
Dim image As Image() = htmlConverter.ConvertToImage(htmlString, baseURL)
'Save the image
image(0).Save("Sample.jpg")

You can download a complete working sample from GitHub.

Partial webpage to Raster Image

The HTML to PDF converter library supports converting the partial webpage to an image. Please refer to the following code example.

HTML to PDF Features: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/features

Troubleshooting: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/troubleshooting

//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert Partial HTML to Image
Image image = htmlConverter.ConvertPartialHtmlToImage("http://www.google.com", "lga");
byte[] imageByte = image.ImageData;
//Save the image
File.WriteAllBytes("Output.jpg", imageByte);
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert Partial HTML to Image
Image[] image = htmlConverter.ConvertPartialHtmlToImage("input.html", "pic");
//Save Image
image[0].Save("Output.jpg");
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()
'Convert Partial HTML to Image
Dim image As Image() = htmlConverter.ConvertPartialHtmlToImage("input.html", "pic")
'Save Image
image(0).Save("Output.jpg")
<html>
<head>
</head>
<body>
Hello world
	<div id="pic">
		<img src=" syncfusion_logo.gif" alt="Smiley face" width="42" height="42"><br>
		This is a Syncfusion Logo
	</div>
	<div>
		Hello world
	</div>
</body>
</html>

You can download a complete working sample from GitHub.

HTML to SVG

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

HTML to PDF Features: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/features

Troubleshooting: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/troubleshooting

//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
//Initialize the memory stream
MemoryStream stream = new MemoryStream();
//Convert URL to SVG
htmlConverter.ConvertToSvg("http://www.syncfusion.com", stream);
//Save the document
using (FileStream output = new FileStream("Sample.svg", FileMode.Create))
{
    stream.CopyTo(output);
}
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
//Convert URL to SVG
htmlConverter.ConvertToSvg("http://www.syncfusion.com", "sample.svg");
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter(HtmlRenderingEngine.WebKit)
'Convert URL to SVG
htmlConverter.ConvertToSvg("http://www.syncfusion.com", "sample.svg")

You can download a complete working sample from GitHub.

NOTE

  1. Syncfusion PDF supports converting HTML to SVG with Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core package reference in the .NET Core application.
  2. HTML to SVG conversion is not supported on Mac platforms.

Partial webpage to SVG

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

HTML to PDF Features: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/features

Troubleshooting: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/troubleshooting

//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
//Initialize the memory stream
MemoryStream stream = new MemoryStream();
//Convert a partial HTML to SVG
htmlConverter.ConvertPartialHtmlToSvg("input.html", "pic", stream);
//Save the document
using (FileStream output = new FileStream("Sample.svg", FileMode.Create))
{
    stream.CopyTo(output);
}
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
//Convert Partial HTML to SVG
htmlConverter.ConvertPartialHtmlToSvg("input.html", "pic", "Output.svg");
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter(HtmlRenderingEngine.WebKit)
'Convert Partial HTML to SVG
htmlConverter.ConvertPartialHtmlToSvg("input.html", "pic", "Output.svg")
<html>
<head>
</head>
<body>
Hello world
	<div id="pic">
		<img src=" syncfusion_logo.gif" alt="Smiley face" width="42" height="42"><br>
		This is a Syncfusion Logo
	</div>
	<div>
		Hello world
	</div>
</body>
</html>

You can download a complete working sample from GitHub.

NOTE

  1. Syncfusion PDF supports Partial HTML to SVG conversion with Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core package reference in the .NET Core application.
  2. Partial HTML to SVG conversion is not supported on Mac platforms.

SVG to PDF

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

//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert URL to PDF document. 
PdfDocument document = htmlConverter.Convert("inputSVG.svg");
//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document
document.Close(true);
//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);
'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)