Replace Image in Existing PDF File

[ ]

The XImages collection’s Replace method allows you to replace an image in an existing PDF file.

The Images collection can be found in a page’s Resources collection. To replace an image:

  1. Open the PDF file using the Document object.
  2. Replace a particular image, save the updated PDF file using Save method of the Document object.

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

package com.aspose.pdf.examples;


import com.aspose.pdf.Document;

public class ExampleReplaceImage {
    private static String _dataDir = "/home/admin1/pdf-examples/Samples/";
    public static void Replace() {
        // Open document
        Document pdfDocument = new Document("input.pdf");
        // Replace a particular image
        try {
            pdfDocument.getPages().get_Item(1).getResources().getImages().replace(1, new FileInputStream("lovely.jpg"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
        // Save updated PDF file + "output.pdf");