Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active October 19, 2022 09:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/d2fe397171126d9c62830f5acdd342eb to your computer and use it in GitHub Desktop.
Save aspose-com-gists/d2fe397171126d9c62830f5acdd342eb to your computer and use it in GitHub Desktop.
Add, Remove, Extract and Update Images in PDF using Java
// Open a document
Document pdfDocument1 = new Document("input.pdf");
// Set coordinates
int lowerLeftX = 100;
int lowerLeftY = 100;
int upperRightX = 200;
int upperRightY = 200;
// Get the page you want to add the image to
Page page = pdfDocument1.getPages().get_Item(1);
// Load image into stream
java.io.FileInputStream imageStream = new java.io.FileInputStream(new java.io.File("input_image1.jpg"));
// Add an image to the Images collection of the page resources
page.getResources().getImages().add(imageStream);
// Using the GSave operator: this operator saves current graphics state
page.getContents().add(new Operator.GSave());
// Create Rectangle and Matrix objects
Rectangle rectangle = new Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Matrix matrix = new Matrix(new double[] { rectangle.getURX() - rectangle.getLLX(), 0, 0, rectangle.getURY() - rectangle.getLLY(), rectangle.getLLX(), rectangle.getLLY() });
// Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
page.getContents().add(new Operator.ConcatenateMatrix(matrix));
XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());
// Using Do operator: this operator draws image
page.getContents().add(new Operator.Do(ximage.getName()));
// Using GRestore operator: this operator restores graphics state
page.getContents().add(new Operator.GRestore());
// Save the new PDF
pdfDocument1.save("Updated_document.pdf");
// Close image stream
imageStream.close();
// Open a document
Document pdfDocument = new Document("input.pdf");
// Extract a particular image
XImage xImage = pdfDocument.getPages().get_Item(1).getResources().getImages().get_Item(1);
// Create stream object to save the output image
java.io.OutputStream output = new java.io.FileOutputStream("output.jpg");
// Save the output image
xImage.save(output);
// Close stream
output.close();
// Open a document
Document pdfDocument = new Document("input.pdf");
// Delete a particular image
pdfDocument.getPages().get_Item(1).getResources().getImages().delete(1);
// Save the updated PDF file
pdfDocument.save("output.pdf");
// Open a document
Document pdfDocument = new Document("input.pdf");
// Replace a particular image
pdfDocument.getPages().get_Item(1).getResources().getImages().replace(1, new java.io.FileInputStream(new java.io.File("aspose.png")));
// Save the updated PDF file
pdfDocument.save("output.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment