Working with Document Conversions

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.

//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()
// 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);

stream.Position = 0;

//Close the documents.

pdfDocument.Close(true);

//Defining the ContentType for pdf file.

string contentType = "application/pdf";

//Define the file name.

string fileName = " WordtoPDF.pdf";

//Creates a FileContentResult object by using the file contents, content type, and file name.

return File(stream, contentType, fileName);
//Load the Word document as stream

Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Template.docx");

//Load the 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 memory stream

MemoryStream stream = new MemoryStream();

pdfDocument.Save(stream);

stream.Position = 0;

//Close the document 

pdfDocument.Close();

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("WordtoPDF.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("WordtoPDF.pdf", "application/pdf", stream);
}

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 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.
//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()
//Essential PDF supports customizing the Word document to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC Platforms.
//Essential PDF supports customizing the Word document to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC Platforms.

Converting Excel documents to PDF

Essential PDF allows you to convert an entire workbook or a single worksheet into PDF document. For converting an Excel document to PDF, the following assemblies need to be referenced in your application

  • Syncfusion.XlsIO.Base.dll
  • Syncfusion.Compression.Base.dll
  • Syncfusion.ExcelToPDFConverter.Base.dll
  • Syncfusion.ExcelChartToImageConverter.Wpf.dll
  • Syncfusion.SfChart.Wpf.dll
  • Syncfusion.Shared.Wpf.dll
  • Syncfusion.Pdf.Base.dll

Converting a Workbook to PDF

The following code illustrates how to convert a workbook to PDF Document.

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");

//Dispose the objects

pdfDocument.Close();

converter.Dispose();

workbook.Close();

excelEngine.Dispose();
Dim 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")

'Dispose the objects

pdfDocument.Close()

converter.Dispose()

workbook.Close()

excelEngine.Dispose()
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

Converting a Worksheet to PDF

The following code shows how to convert a particular sheet to PDF Document.

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);

PdfDocument pdfDocument= new PdfDocument();

pdfDocument = converter.Convert();

pdfDocument.Save("ExcelToPDF.pdf");

pdfDocument.Close();

converter.Dispose();

workbook.Close();
excelEngine.Dispose();
Dim 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)

Dim pdfDocument As PdfDocument = New PdfDocument()

pdfDocument = converter.Convert()

'Save the pdf file

pdfDocument.Save("ExcelToPDF.pdf")

'Dispose the objects

pdfDocument.Close()

converter.Dispose()

workbook.Close()

excelEngine.Dispose()
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

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.

ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;

IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);

PdfDocument pdfDocument = new PdfDocument();



foreach (IWorksheet sheet in workbook.Worksheets)

{

ExcelToPdfConverter converter = new ExcelToPdfConverter(sheet);

pdfDocument = converter.Convert();

//Save the pdf file

pdfDocument.Save(sheet.Name+".pdf");

converter.Dispose();

}

//Dispose the objects

pdfDocument.Close();

workbook.Close();

excelEngine.Dispose();
Dim excelEngine As New ExcelEngine()

Dim application As IApplication = excelEngine.Excel

application.DefaultVersion = ExcelVersion.Excel2013

Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")

Dim pdfDocument As New PdfDocument()

For Each sheet As IWorksheet In workbook.Worksheets

Dim converter As New ExcelToPdfConverter(sheet)

PdfDocument = converter.Convert()

'Save the pdf file

PdfDocument.Save(sheet.Name + ".pdf")

converter.Dispose()

Next

pdfDocument.Close()

workbook.Close()

excelEngine.Dispose()
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

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.

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];

ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);

PdfDocument pdfDocument = new PdfDocument();

pdfDocument = converter.Convert();

pdfDocument.Save("ExcelToPDF.pdf");

converter.Dispose();

pdfDocument.Close();

workbook.Close();

excelEngine.Dispose();
Dim excelEngine As 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)

Dim converter As New ExcelToPdfConverter(workbook)

Dim pdfDocument As New PdfDocument()

pdfDocument = converter.Convert()

pdfDocument.Save("ExcelToPDF.pdf")

converter.Dispose()

pdfDocument.Close()

workbook.Close()

excelEngine.Dispose()
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Essential PDF supports Excel to PDF conversion only in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

NOTE

This section is applicable only to the Windows Forms, ASP.NET, MVC and WPF platforms.

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.

//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()
// 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);

stream.Position = 0;

//Close the documents.

pdfDocument.Close(true);

//Defining the ContentType for pdf file.

string contentType = "application/pdf";

//Define the file name.

string fileName = " RTFToPDF.pdf";

//Creates a FileContentResult object by using the file contents, content type, and file name.

return File(stream, contentType, fileName);
//Load the Word document as stream

Stream docStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Input.rtf");

//Load an existing Word document

WordDocument rtfDocument = 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(rtfDocument);

//Releases all resources used by the Word document and DocIO Renderer objects

render.Dispose();

rtfDocument.Dispose();

//Save the document into memory stream

MemoryStream stream = new MemoryStream();

pdfDocument.Save(stream);

stream.Position = 0;

//Close the document 

pdfDocument.Close();

//Save the stream into pdf file

//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.

if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
{
    Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("RTFToPDF.pdf", "application/pdf", stream);
}
else
{
    Xamarin.Forms.DependencyService.Get<ISave>().Save("RTFToPDF.pdf", "application/pdf", stream);
}

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 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.
//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()
//Essential PDF supports customizing the RTF to PDF conversion only Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.
//Essential PDF supports customizing the RTF to PDF conversion only Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms.

Converting TIFF to PDF

Converting multi page TIFF to PDF

Multi frame TIFF image can be converted to PDF document. 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 PDF document

PdfDocument pdfDocument = new PdfDocument();

//Add a section to the PDF document

PdfSection section = pdfDocument.Sections.Add();

//Declare the PDF page

PdfPage page;

//Declare PDF page graphics

PdfGraphics graphics;

//Load multi frame TIFF image

PdfBitmap tiffImage = new PdfBitmap("image.tiff");

//Get the frame count

int frameCount = tiffImage.FrameCount;

//Access each frame draw into the page

for (int i = 0; i < frameCount; i++)

{

page = section.Pages.Add();

section.PageSettings.Margins.All = 0;

graphics = page.Graphics;

tiffImage.ActiveFrame = i;

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 section to the PDF document

Dim section As PdfSection = pdfDocument.Sections.Add()

'Declare the PDF page

Dim page As PdfPage

'Declare PDF page graphics

Dim graphics As PdfGraphics

'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 draw into the page

For i As Integer = 0 To frameCount - 1

page = section.Pages.Add()

section.PageSettings.Margins.All = 0

graphics = page.Graphics

tiffImage.ActiveFrame = i

graphics.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height)

Next

'Save and close the document

pdfDocument.Save("Sample.pdf")

pdfDocument.Close(True)
//Create a PDF document

PdfDocument pdfDocument = new PdfDocument();

//Add a section to the PDF document

PdfSection section = pdfDocument.Sections.Add();

//Declare the PDF page

PdfPage page;

//Declare PDF page graphics

PdfGraphics graphics;

//Load multi frame TIFF image

Stream imageStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.image.tiff");

PdfBitmap tiffImage = new PdfBitmap(imageStream);

//Get the frame count

int frameCount = tiffImage.FrameCount;

//Access each frame draw into the page

for (int i = 0; i < frameCount; i++)

{

    page = section.Pages.Add();

    section.PageSettings.Margins.All = 0;

    graphics = page.Graphics;

    tiffImage.ActiveFrame = i;

    graphics.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);

}

MemoryStream memoryStream = new MemoryStream();

//Save the document.

await pdfDocument.SaveAsync(memoryStream);

//Close the documents.

pdfDocument.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(memoryStream, "Sample.pdf");
//Essential PDF supports converting multi page TIFF to PDF only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.
//Essential PDF supports converting multi page TIFF to PDF only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.

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

//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)
//Essential PDF supports compressing monochrome images only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.
//Essential PDF supports compressing monochrome images only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.
//Essential PDF supports compressing monochrome images only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.

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.

//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)
//Create converter class.

XPSToPdfConverter converter = new XPSToPdfConverter();

//Load the XPS file

Stream fileStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.input.xps");

//Convert the XPS to PDF.

PdfDocument document = converter.Convert(fileStream);

MemoryStream memoryStream = new MemoryStream();

//Save the document.

await document.SaveAsync(memoryStream);

//Close the documents.

document.Close(true);

//Save the stream as PDF document file in local machine. Refer to PDF/UWP section for respected code samples.

Save(memoryStream, "Sample.pdf");
//Essential PDF supports converting XPS document to PDF only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.
//Essential PDF supports converting XPS document to PDF only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and UWP platforms.

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

PDF pages can be converted to images. To add PDF to image functionality in an application, you need to add the below mentioned assemblies as reference to the project.

  1. Syncfusion.Compression.Base.dll
  2. Syncfusion.Pdf.Base.dll
  3. Syncfusion.PdfViewer.Windows.dll

The following code snippet illustrates how to convert PDF page into image.

PdfDocumentView documentViewer = new PdfDocumentView();

//Load the PDF document

documentViewer.Load("Input.pdf");

//Export PDF page to image

Bitmap image = documentViewer.ExportAsImage(0);

//Save the image.

image.Save("Output.png", ImageFormat.Png);

documentViewer.Dispose();
Dim documentViewer As New PdfDocumentView()

'Load the PDF document

documentViewer.Load("Input.pdf")

'Export PDF page to image

Dim image As Bitmap = documentViewer.ExportAsImage(0)

'Save the image.

image.Save("Sample.png", ImageFormat.Png)

documentViewer.Dispose()

MHTML to PDF

The MHTML file can be converted to PDF using WebKit rendering engine. Please refer the below code snippet,

Prerequisites - https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf#prerequisites-for-windows

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

//Initialize HTML converter with WebKit rendering engine
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

//Set WebKit path
webKitSettings.WebKitPath = @"/QtBinaries/"; 
           
//Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = webKitSettings;

//Convert MHTML to PDF
PdfDocument document = htmlConverter.Convert("input.mhtml");  
    
//Save the document.
document.Save("Sample.pdf");

document.Close();
'Initialize HTML converter with WebKit rendering engine
Dim htmlConverter As New HtmlToPdfConverter(HtmlRenderingEngine.WebKit)

Dim webKitSettings As New WebKitConverterSettings()

'Set WebKit path
webKitSettings.WebKitPath = "/QtBinaries/"

'Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = webKitSettings

'Convert MHTML to PDF
Dim document As PdfDocument = htmlConverter.Convert("input.mhtml")

'Save the document.
document.Save("Sample.pdf")

document.Close()
//Essential PDF supports converting MHTML to PDF only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and ASP.NET Core platforms.
//Initialize HTML to PDF converter 

HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

//Set WebKit path

webKitSettings.WebKitPath = @"/QtBinariesDotNetCore/";

//Assign WebKit settings to HTML converter

htmlConverter.ConverterSettings = webKitSettings;

//Convert MHTML to PDF

PdfDocument document = htmlConverter.Convert(@"input.mhtml");

//Save the document into stream.

MemoryStream stream = new MemoryStream();

document.Save(stream);

stream.Position = 0;

//Close the documents.

document.Close(true);

//Defining the ContentType for pdf file.

string contentType = "application/pdf";

//Define the file name.

string fileName = " Sample.pdf";

//Creates a FileContentResult object by using the file contents, content type, and file name.

return File(stream, contentType, fileName);
//Essential PDF supports converting MHTML to PDF only in Windows Forms, WPF, ASP.NET, ASP.NET MVC and ASP.NET Core platforms.

HTML to MHTML

The WebKit HTML Converter provides support for converting the webpage to MHTML. Please refer the below code snippet,

Prerequisites - https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf#prerequisites-for-windows

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

//Initialize HTML converter with WebKit rendering engine
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

//Set WebKit path
webKitSettings.WebKitPath = @"/QtBinaries/";

//Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = webKitSettings;

//Convert URL to MHTML
htmlConverter.ConvertToMhtml("http://www.syncfusion.com", "sample.mhtml");
'Initialize HTML converter with WebKit rendering engine
Dim htmlConverter As New HtmlToPdfConverter(HtmlRenderingEngine.WebKit)      
  
Dim webKitSettings As New WebKitConverterSettings()

'Set WebKit path
webKitSettings.WebKitPath = "/QtBinaries/"

'Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = webKitSettings

'Convert URL to MHTML
htmlConverter.ConvertToMhtml("http://www.syncfusion.com", "sample.mhtml")
//Essential PDF supports converting HTML to MHTML only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting HTML to MHTML only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting HTML to MHTML only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.

HTML to Raster Image

The WebKit HTML Converter provides support for converting webpage to Image. Please refer the below code snippet,

Prerequisites - https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf#prerequisites-for-windows

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

//Initialize HTML converter with WebKit rendering engine
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

//Set WebKit path
webKitSettings.WebKitPath = @"/QtBinaries/";

//Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = webKitSettings;

//Convert URL to Image
Image[] image = htmlConverter.ConvertToImage("http://www.syncfusion.com");

//Save the image.
image[0].Save("Sample.jpg");
'Initialize HTML converter with WebKit rendering engine
Dim htmlConverter As New HtmlToPdfConverter(HtmlRenderingEngine.WebKit)

Dim webKitSettings As New WebKitConverterSettings()

'Set WebKit path
webKitSettings.WebKitPath = "/QtBinaries/"

'Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = webKitSettings

'Convert URL to Image
Dim image As Image() = htmlConverter.ConvertToImage("http://www.syncfusion.com ")

'Save the image.
image(0).Save("Sample.jpg")
//Essential PDF supports converting HTML to raster image only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting HTML to raster image only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting HTML to raster image only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.

HTML string to Raster Image

The WebKit HTML Converter provides support for converting HTML string to Image. Please refer the below code snippet,

Prerequisites - https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf#prerequisites-for-windows

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

string htmlString = "<html><body>Hello World!!!</body></html>";
string baseURL = "";

//Initialize HTML converter with WebKit rendering engine
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

//Set WebKit path
webKitSettings.WebKitPath = @"/QtBinaries/";

//Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = webKitSettings;

//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 converter with WebKit rendering engine
Dim htmlConverter As New HtmlToPdfConverter(HtmlRenderingEngine.WebKit)

Dim webKitSettings As New WebKitConverterSettings()

'Set WebKit path
webKitSettings.WebKitPath = "/QtBinaries/"

'Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = webKitSettings

'Convert HTML string to Image
Dim image As Image() = htmlConverter.ConvertToImage(htmlString, baseURL)

'Save the image.
image(0).Save("Sample.jpg")
//Essential PDF supports converting HTML string to raster image only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting HTML string to raster image only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting HTML string to raster image only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.

Partial webpage to Raster Image

The WebKit HTML Converter provides support for converting partial webpage to Image. Please refer the below code snippet,

Prerequisites - https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf#prerequisites-for-windows

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

//Initialize HTML converter 

HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

// WebKit converter settings

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

//Assign the WebKit binaries path

webKitSettings.WebKitPath = @"/QtBinaries/";

//Assign the WebKit settings

htmlConverter.ConverterSettings = webKitSettings;

//Convert Partial HTML to Image

Image[] image = htmlConverter.ConvertPartialHtmlToImage("input.html", "pic");

//Save Image

image[0].Save("Output.jpg");
'Initialize HTML converter 

Dim htmlConverter As New HtmlToPdfConverter(HtmlRenderingEngine.WebKit)

' WebKit converter settings

Dim webKitSettings As New WebKitConverterSettings()

'Assign the WebKit binaries path

webKitSettings.WebKitPath = "/QtBinaries/"

'Assign the WebKit settings

htmlConverter.ConverterSettings = webKitSettings

'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>
//Essential PDF supports converting partial webpage to raster image only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting partial webpage to raster image only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting partial webpage to raster image only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.

HTML to SVG

The WebKit HTML Converter provides support for converting HTML to SVG. Please refer the below code snippet,

Prerequisites - https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf#prerequisites-for-windows

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

//Initialize HTML converter with WebKit rendering engine
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

//Set WebKit path
webKitSettings.WebKitPath = @"/QtBinaries/";

//Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = webKitSettings;

//Convert URL to SVG
htmlConverter.ConvertToSvg("http://www.syncfusion.com", "sample.svg");
'Initialize HTML converter with WebKit rendering engine
Dim htmlConverter As New HtmlToPdfConverter(HtmlRenderingEngine.WebKit)

Dim webKitSettings As New WebKitConverterSettings()

'Set WebKit path
webKitSettings.WebKitPath = "/QtBinaries/"

'Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = webKitSettings

'Convert URL to SVG
htmlConverter.ConvertToSvg("http://www.syncfusion.com", "sample.svg")
//Essential PDF supports converting HTML to SVG only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting HTML to SVG only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting HTML to SVG only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.

Partial webpage to SVG

The WebKit HTML Converter provides support for converting partial webpage to SVG. Please refer the below code snippet,

Prerequisites - https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf#prerequisites-for-windows

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

//Initialize HTML converter 

HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

// WebKit converter settings

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

//Assign the WebKit binaries path

webKitSettings.WebKitPath = @"/QtBinaries/";

//Assign the WebKit settings

htmlConverter.ConverterSettings = webKitSettings;

//Convert Partial HTML to SVG

htmlConverter.ConvertPartialHtmlToSvg("input.html", "pic", "Output.svg");
'Initialize HTML converter 

Dim htmlConverter As New HtmlToPdfConverter(HtmlRenderingEngine.WebKit)

' WebKit converter settings

Dim webKitSettings As New WebKitConverterSettings()

'Assign the WebKit binaries path

webKitSettings.WebKitPath = "/QtBinaries/"

'Assign the WebKit settings

htmlConverter.ConverterSettings = webKitSettings

'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>
//Essential PDF supports converting partial webpage to SVG only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting partial webpage to SVG only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.
//Essential PDF supports converting partial webpage to SVG only in Windows Forms, WPF, ASP.NET, ASP.NET MVC platforms.