Do you need to replace images in a PDF programmatically in C#? If yes, this article is going to provide you with a powerful and easy-to-code solution for replacing PDF images with high fidelity. You can easily use this solution to replace the confidential images in PDF files before sharing. So let’s see how to replace images in a PDF in C#.
C# Library to Replace Images in PDF
To replace images in PDF files, we will use Aspose.PDF for .NET. The library is designed to perform PDF generation and manipulation from within the .NET applications. It allows you to create PDF documents of simple as well as complex layouts seamlessly. You can either download the library’s binaries or install it using NuGet.
PM> Install-Package Aspose.PDF
How to Replace Images in a PDF using C#
In PDF documents, the images are kept in an image collection of each page. Thus, you can access any of the images from the collection using its index and manipulate it. The following are the steps to replace a particular image in a PDF in C#.
- Load the PDF file using Document class.
- Get reference of the desired page from the PDF using Document.Pages collection.
- Replace the image in the collection using Pages[index].Resources.Images.Replace(imageIndex, new FileStream(“lovely.jpg”, FileMode.Open)) method.
- Save the updated document using Document.Save(string) method.
The following code sample shows how to replace an image in a PDF using C# .NET.
Free C# Library to Replace PDF Images
You can get a free temporary license and replace images in your PDF files without any limitations.
Explore C# PDF Library
You can explore more about the C# PDF library using the documentation. In case you would have any questions or queries, you can contact us via our forum.
Conclusion
In this article, you have learned how to replace images in PDF files using C#. You can simply integrate the provided code sample and replace PDF images from within your applications. Following the provided steps, you can easily implement the image replacing feature for PDF in your applications.