Convert PDF to Images using C#

Convert PDF to Images using C#

You may need to share the pages of PDF documents as image files with others. As a C# developer, you can easily convert PDF files to PNG, JPG, TIFF, or BMP programmatically in your .NET applications. In this article, you will learn how to convert PDF to Images using C#.

The following topics are discussed/covered in this article:

PDF to Image Conversion C# API

For converting PDF to images, I will be using Aspose.PDF for .NET API. It is a powerful PDF file management API that lets you manipulate PDF documents from within your .NET applications. It allows you to create, modify, convert, render, secure and print documents without using Adobe Acrobat.

You can either download the DLL of the API or install it using the NuGet.

Install-Package Aspose.Pdf

Convert PDF to PNG Images using C#

You can convert PDF files to PNG images programmatically by following the steps given below:

  • Create an instance of the Document class with the input PDF file path.
  • Loop through all the pages of the PDF using Document.Pages collection and do the following:
    • Create an instance of the Resolution class and set its value.
    • Create an instance of the PngDevice class and pass Width, Height and Resolution object.
    • Call the Process(Page, String) method with the page number and the output PNG image path to convert the page to a PNG.

The following code sample shows how to convert PDF pages to PNG images using C#.

Convert PDF to PNG Images using C#
Convert PDF to PNG Images using C#

The Document class represents the PDF document. It provides several properties and methods to perform various functionalities. The Document.Pages collection is a collection of document pages and the page numbering starts from 1 in the collection. The Resolution class defines the image resolution. The PngDevice class allows saving pages of the PDF document into PNG images. This class provides the following methods to save pages into PNG images:

Convert PDF to JPG Images using C#

You can convert PDF files to JPG images programmatically by following the steps given below:

  • Create an instance of the Document class with the input file path.
  • Loop through all the pages of the PDF using Document.Pages collection and do the following:
    • Create an instance of the Resolution class and set its value.
    • Create an instance of the JpegDevice class and pass Width, Height and Resolution object.
    • Call the Process(Page, String) method with the page number and the output JPG image path to convert the page to a JPG.

The following code sample shows how to convert PDF pages to JPG images using C#.

Convert PDF to JPG Images using C#
Convert PDF to JPG Images using C#

PDF to BMP Conversion using C#

You can convert PDF files to BMP images programmatically by following the steps given below:

  • Create an instance of the Document class with the input file path.
  • Loop through all the pages of the PDF using Document.Pages collection and do the following:
    • Create an instance of the Resolution class and set its value.
    • Create an instance of the BmpDevice class and pass Width, Height and Resolution object.
    • Call the Process(Page, String) method with the page number and the output BMP image path to convert the page to a BMP.

The following code sample shows how to convert PDF pages to BMP images using C#.

PDF to BMP Conversion using C#
PDF to BMP Conversion using C#

PDF to TIFF Conversion using C#

You can convert PDF files to TIFF programmatically by following the steps given below:

  • Create an instance of the Document class with the input file path.
  • Initialize an instance of the Resolution class and set its value.
  • Create an instance of the TiffSettings class.
  • Set various properties such as Compression, Depth, Shape, and SkipBlankPages, etc.
  • Create an instance of the TiffDevice class with the Resolution and the TiffSettings objects.
  • Call the Process(Document, String) method with the Document object and the output TIFF file path to convert the document to a TIFF.

The following code sample shows how to convert a PDF file to a TIFF using C#.

PDF to TIFF Conversion using C#
PDF to TIFF Conversion using C#

The TiffSettings class provides several settings for converting a PDF to TIFF. You can set Brightness, Compression, CoordinateType, Depth, Margins, Shape, and SkipBlankPages while converting PDF to TIFF.

Extract Images from PDF Documents using C#

You can extract all the images from any PDF file programmatically by following the steps given below:

  • Create an instance of the Document class with the input file path.
  • For each page, create XImage instance for every image in the Page.Resources.Images collection.
  • Create an instance of the FileStream class with the output image file path.
  • Call the Save() method with the FileStream object to save the image
  • Finally, close the FileStream using Close() method.

The following code sample shows how to extract images from a PDF document using C#.

Extract Images from PDF Documents using C#
Extract Images from PDF Documents using C#

The XImage class represents the image X-Object. It provides several properties and methods to work with images. The XImage class provides the following methods to save the image object:

The Page.Resources.Images collection represents the collection of images for the specific page.

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to convert PDF file pages to images using C#. You have also learned how to convert PDF to PNG, PDF to JPG, PDF to BMP, and PDF to TIFF programmatically. Moreover, you have learned how to extract images from a PDF file using C#. The API also offers compression options, table creation & manipulation, graph & image functions, extensive hyperlink functionality, stamp and watermark tasks, extended security controls and custom font handling. You can learn more about Aspose.PDF for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also