This article provides details on how to get images from a PDF in C# with the help of configuration details and a runnable sample code. Complete program logic is shared that can be used to write this application as all the necessary classes and methods are given which are required to get images from PDF in C# in different formats like JPEG, PNG, etc. You will also learn different options to enhance the process by customizing the output images after fetching them from the PDF file.
Steps to Get PDF Images in C#
- Configure the IDE to use Aspose.PDF for .NET to extract images from a PDF
- Load the source PDF file having images in it using the Document class object
- Get access to a particular image using the XImage class object
- Create a new file stream using the name of the desired image
- Save the image as JPEG and close the stream
These steps explain how to get image from PDF in C# by sharing a step-by-step approach where first we load the source PDF file and then get access to a particular page of the PDF. Each page has a collection of resources including images that can be referred to with the help of an index. Once the desired image reference is accessed into the XImage class object, it can be saved as an image into the stream in any of the desired formats.
Code to Get an Image from a PDF in C#
This code demonstrates the process to get image out of PDF in C# by loading it into the Document class object and then getting the list of resources of a particular page and then from the resources, getting the list of images on that page. Once we have the access to a particular image, we can rename an image along with the changes in the references within the document. You can also get different properties like name, height, and width to filter the images before saving them on the disk.
This tutorial has guided us to extract images from a PDF page. If you want to learn the process to add a watermark in a PDF file, refer to the article on how to add watermark to PDF in C#.