How to Add Image in PDF using C#

This article explains how to add image in PDF using C# with the help of environment setting steps and sharing the logical steps to write the application using this library. All the necessary classes and methods are discussed in the detailed steps and then used in a runnable sample code for a complete demonstration of the feature to insert picture in PDF using C#. In the end, when the image is added to the target page, the resultant file is saved on the disk.

Steps to Insert Image into PDF using C#

  1. Establish the environment to use Aspose.PDF for .NET to add an image
  2. Open the source PDF file and access the target page
  3. Define the coordinates where the image is to be pasted
  4. Load the image and add it to the images type resource collection of the selected page
  5. Save the current graphics state and create a rectangle and matrix objects
  6. Define the image placement and draw the image
  7. Restore the graphics state and save the resultant PDF file

These steps describe the process to insert photo in PDF using C#. The summary of the steps is to load the PDF file, get access to the target page, loading and add the image to the collection of images of the selected page, saving the current state of the graphics that is restored at the end before saving the output PDF file, creating the rectangle and drawing the image using multiple operators in the Aspose.Pdf.Operators class. You may follow the steps in the prescribed order to achieve the desired output.

Code to Add Image to PDF using C#

This code demonstrates how to add picture in PDF using C#. Mainly it uses Aspose.Pdf.Operators class by calling the GSave() method to save the graphics state and the GRestore() method to restore the state at the end, ConcatenateMatrix to define how the image is to be placed, and the Do() method to draw the image. It also shows the declaration of the coordinates where the image is to be pasted on the PDF page.

This article has guided us to add picture to PDF using C#. If you want to learn to add drawing objects like circle to a PDF file, refer to the article on how to add circle to PDF using C#.

 English