Replace Image in Existing PDF File
Contents
[
Hide
]
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:
- Open the PDF file using the Document object.
- 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 java.io.FileInputStream;
import java.io.FileNotFoundException;
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
e.printStackTrace();
}
// Save updated PDF file
pdfDocument.save(_dataDir + "output.pdf");
}
}