Converting Images to PDF

24 Jul 20266 minutes to read

The Syncfusion® .NET PDF library provides comprehensive support for converting both raster and vector images to PDF documents. The PdfImage class is an abstract base class that provides common functionality for converting images to PDF documents. It is used as the base class for two concrete image classes in the Syncfusion.Pdf.Graphics namespace: PdfBitmap and PdfMetafile.

Check the following video for a quick guide on converting an image to a PDF document using the PDF Library.

The library supports a wide range of image formats for PDF conversion. The supported image formats include:

  • JPEG (Joint Photographic Experts Group)
  • JPEG with Exif standard
  • PNG (Portable Network Graphics)
  • BMP (Windows Bitmap)
  • GIF (Graphics Interchange Format)
  • TIFF (Tagged Image File Format)
  • EMF (Enhanced Metafile)
  • ICO and ICON (Windows Icon)

NOTE

To use image formats other than PNG and JPEG in ASP.NET Core, include the Syncfusion.Pdf.Imaging.Net.Core package in your project. This package provides the necessary support for handling other raster image formats such as BMP, GIF, TIFF, and ICO.

The ImageToPdfConverter class converts image files into PDF documents, with options to customize page size and image positioning.

The following code example shows how to convert an image to a PDF document.

// Create an instance of the ImageToPdfConverter class.
ImageToPdfConverter imageToPdfConverter = new ImageToPdfConverter();
// Set the page size for the document.
imageToPdfConverter.PageSize = PdfPageSize.A4;
// Set the position of the image in the document.
imageToPdfConverter.ImagePosition = PdfImagePosition.TopLeftCornerOfPage;
// Create a file stream to read the image file.
using (FileStream imageStream = new FileStream("Image.jpg", FileMode.Open, FileAccess.Read))
// Convert the image to a PDF document using the ImageToPdfConverter.
using (PdfDocument pdfDocument = imageToPdfConverter.Convert(imageStream))
{
    // Save the PDF document.
    pdfDocument.Save("Output.pdf");
}
// Create an instance of the ImageToPdfConverter class.
ImageToPdfConverter imageToPdfConverter = new ImageToPdfConverter();
// Set the page size for the document.
imageToPdfConverter.PageSize = PdfPageSize.A4;
// Set the position of the image in the document.
imageToPdfConverter.ImagePosition = PdfImagePosition.TopLeftCornerOfPage;
// Create a file stream to read the image file.
using (FileStream imageStream = new FileStream("Image.jpg", FileMode.Open, FileAccess.Read))
// Convert the image to a PDF document using the ImageToPdfConverter.
using (PdfDocument pdfDocument = imageToPdfConverter.Convert(imageStream))
{
    // Save the PDF document.
    pdfDocument.Save("Output.pdf");
}
' Create an instance of the ImageToPdfConverter class
Dim imageToPdfConverter As New ImageToPdfConverter()

' Set the page size for the document
imageToPdfConverter.PageSize = PdfPageSize.A4

' Set the position of the image in the document
imageToPdfConverter.ImagePosition = PdfImagePosition.TopLeftCornerOfPage

' Create a file stream to read the image file
Using imageStream As New FileStream("Image.jpg", FileMode.Open, FileAccess.Read)
    ' Convert the image to a PDF document using the ImageToPdfConverter
    Using pdfDocument As PdfDocument = imageToPdfConverter.Convert(imageStream)
        ' Save the PDF document
        pdfDocument.Save("Output.pdf")
    End Using
End Using

You can download a complete working sample from GitHub.

NOTE

In ASP.NET Core, converting TIFF to PDF requires the Syncfusion.Pdf.Imaging.Net.Core NuGet package.

Converting a Vector Image to PDF

The Syncfusion® .NET PDF library supports adding vector images in the Metafile format to PDF documents. During the conversion, Metafile graphics are transformed into native PDF graphics that support text selection and searching. The following types of Metafiles are supported in Essential® PDF:

  • EMF only
  • EMF plus
  • EMF plus dual
  • WMF

The PdfMetafile class loads EMF images, and the PdfMetafileLayoutFormat class allows you to control how text and images are split across pages in the PDF document. The following code example illustrates this.

// Vector image conversion is not supported on cross-platform .NET platforms.
//Create a PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();

//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Create the layout format.
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
//Split text and images between pages.
format.SplitImages = true;
format.SplitTextLines = true;
//Create a Metafile instance.
PdfMetafile metaChart = new PdfMetafile("MetaChart.emf");
//Draw the Metafile on the page.
metaChart.Draw(page, PointF.Empty, format);

//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()

'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Create the layout format.
Dim format As New PdfMetafileLayoutFormat()
'Split text and images between pages.
format.SplitImages = True
format.SplitTextLines = True
'Create a Metafile instance.
Dim metaChart As New PdfMetafile("MetaChart.emf")
'Draw the Metafile on the page.
metaChart.Draw(page, PointF.Empty, format)

'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)

You can download a complete working sample from GitHub.

NOTE

EMF and WMF images are not supported on the ASP.NET Core platform.