Add, Delete and Extract Images from the PDF file

Add Image to Existing PDF File

Every PDF page object contains the getResources() and getContents() methods. Resources can be images and forms, for example, while content is represented by a set of PDF operators. Each operator has its own name and argument. This example use operators to add an image to a PDF file.

To add an image to an existing PDF file:

  1. Create a Document object and open the input PDF document.
  2. Get the page you want to add an image to.
  3. Add the image into the page’s getResources() collection.
  4. Use operators to place the image on the page:
    1. Use the GSave operator to save the current graphical state.
    2. Use the ConcatenateMatrix operator to specify where the image is to be placed.
    3. Use the Do operator to draw the image on the page.
    4. Finally, use the GRestore operator to save the updated graphical state.
  5. Save the file.

The following code snippet shows how to add image to a PDF document.

Adding image from BufferedImage into PDF

Starting release of Aspose.PDF for Java 9.5.0, we have introduced the support to add image from BufferedImage instance to PDF document. In order to support this requirement, a method is implemented: XImageCollection.add(BufferedImage image);

You can use any InputStream and not just only FileInputStream object to add image. So when using java.io.ByteArrayInputStream object, you do not need to store any files over system:

Delete Images from the PDF File

To delete an image from a PDF file, simply use the Images collection’s delete(..) method.

The Images collection can be found in a page’s Resources collection, and you get a given Page from the Document object’s Pages collection.

  1. Create Document object and open the input PDF file.
  2. Delete the image.
  3. Save the file.

The following code snippet shows how.

Extract Images from the PDF File

Each page holds a Resources collection, and this, in turn, holds the Images collection, were all images in a page are kept. The XImage object gets a given image in the Images collection.

To extract and image from a page:

  1. Get the image from the Images collection using the image index.
  2. Use the XImage object’s save(..) method to save the extracted image.

The following code snippet shows you how to extract images from the PDF file.

Replace Image in an Existing PDF File

The Images collection’s replace(..) method allows you to replace an image in a PDF file. The Images collection can be found in a page’s Resources collection.

To replace an image, open the PDF file using the Document object. Once the image is replaced, save the updated PDF file using the Document object’s save(..) method.

The following code snippet shows how to replace an image in a PDF file.

Delete Image from PDF Resources found by ImagePlacementAbsorber

To delete an image from a PDF file, you can simply use the Images collection’s delete(..) method. However, to delete image from the PDF resources found by ImagePlacementAbsorber, use the com.aspose.pdf.XImage.delete() method.

The following approach gets images from a PDF file and tries to delete them. Please note that an exception might be generated while removing image from PDF files.

Support for DICOM Images

Aspose.PDF for .NET supports functionality to add DICOM images to PDF documents. The following code snippet shows how to use this functionality.