Convert PNG to PDF in C#

PNG to PDF

This article explains how to convert PNG image files to PDF.

You’ll learn how to convert the image to a PDF document without optical character recognition (OCR). This means that the text in the image isn’t recognized, and you cannot search in the output PDF file. For more information, see the guide on converting images to searchable PDFs.

For more information on using mixed raster content (MRC) compression during the conversion, see the guide on hypercompression.

Creating a PDF from a PNG file

To create a PDF from a PNG file, follow the steps below:

  1. Create a GdPictureDocumentConverter object.
  2. Load the source document by passing its path to the LoadFromFile method. Recommended: Specify the source document format with a member of the DocumentFormat enumeration.
  3. Save the output in a new PDF document with the SaveAsPDF method.

The example below creates a PDF document from a PNG file:

using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();
// Load the source document.
gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.png", GdPicture14.DocumentFormat.DocumentFormatPNG);
// Save the output in a new PDF document.
gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");

Optional PDF configuration properties

Optionally, configure the conversion with the following properties of the GdPictureDocumentConverter object:

  • PdfBitonalImageCompression is a member of the PdfCompression enumeration that specifies the compression scheme used for bitonal images in the output PDF file.
  • PdfColorImageCompression is a member of the PdfCompression enumeration that specifies the compression scheme used for color images in the output PDF file.
  • PdfEnableColorDetection is a Boolean value that specifies whether to use automatic color detection during the conversion that preserves image quality and reduces the output file size.
  • PdfEnableLinearization is a Boolean value that specifies whether to linearize the output PDF to enable Fast Web View mode.
  • PdfImageQuality is an integer from 0 to 100 that specifies the image quality in the output PDF file.

The example below creates a PDF document from a PNG file with a custom configuration:

using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();
// Load the source document.
gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.png", GdPicture14.DocumentFormat.DocumentFormatPNG);
// Configure the conversion.
gdpictureDocumentConverter.PdfColorImageCompression = PdfCompression.PdfCompressionJPEG;
gdpictureDocumentConverter.PdfImageQuality = 50;
// Save the output in a new PDF document.
gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");