A picture is worth a thousand words. Therefore, images and graphics play an important role in PDF as well as other documents. Since PDF has become one of the most popular and widely used file formats, this article targets how to manipulate images in PDF files programmatically. More precisely, you will learn how to add, extract, remove, and replace images from PDF files using C#.
- C# API to Work with Images in PDF
- Add an Image in a PDF using C#
- Extract Images from a PDF using C#
- Remove Images from a PDF using C#
- Replace an Image in a PDF using C#
- Get a Free License
C# API to Add, Remove, and Extract Images in PDF
Aspose.PDF for .NET is a C# class library that lets you create and manipulate PDF documents from within the .NET applications. Using the API, you can perform basic as well as advanced PDF automation features quite easily. In addition, you can manipulate images in existing PDF files. The API can be downloaded as DLL or installed via NuGet.
PM> Install-Package Aspose.Pdf
Add Image in a PDF File using C#
The following are the steps to add an image to a PDF file using Aspose.PDF for .NET.
- Use Document class to create a new or load an existing PDF file.
- Get the reference of the desired page in Page object.
- Add the image to Resources collection of the page.
- Use the following operators to place the image on the page:
- GSave operator to save the current graphical state.
- ConcatenateMatrix operator to specify where the image is to be placed.
- Do operator to draw the image on the page.
- GRestore operator to save the updated graphical state.
- Save the updated PDF file using Document.Save(String) method.
The following code sample shows how to add image to a PDF file using C#.
Extract Images from PDF using C#
In case you want to extract all the images from a PDF file, you can do it by following the below steps.
- Use Document class to load an existing PDF file.
- Get the desired image in the XImage object from a particular page’s Resources collection using the index.
- Save the extracted image in the desired format using XImage.Save(FileStream, ImageFormat) method.
The following code sample shows how to extract images from PDF using C#.
Remove Images from PDF using C#
Once you have got access to the resources of a page in PDF, you can remove the images from it. The following are the steps to remove images from a PDF file using C#.
- Load the PDF file using Document class.
- Remove image(s) using one of the following methods.
- Delete() – Delete all images.
- Delete(Int32) – Delete image by index.
- Delete(String) – Delete image by name.
- Save the updated PDF file using Document.Save(String) method.
The following code sample shows how to remove images from a PDF using C#.
Replace Image in PDF using C#
Aspose.PDF for .NET also lets you replace a particular image in the PDF. For this, you can replace the image in the image collection of the page. The following are the steps to replace an image in PDF using C#.
- Load the PDF file using Document class.
- Replace the desired image using Document.Pages[1].Resources.Images.Replace(Int32, Stream, Int32, Boolean) method.
- Save the updated PDF file using Document.Save(String) method.
The following code sample shows how to replace an image in PDF using C#.
Get a Free License
You can get a free temporary license in order to try the API without evaluation limitations.
Conclusion
Images and graphical objects are important elements of PDF documents. Therefore, in this article, we have covered how to manipulate images in a PDF. The step-by-step tutorial and code samples have shown how to add, extract, remove and replace images in PDF files using C#. You can explore more about the C# PDF API using the documentation.