Images are widely used for different types of depictions and presentations in PDF files. In this article, you’ll learn how to programmatically process images in PDF files. In particular, this article shows you how to add, extract, delete, or replace images in PDF files using Java.

  • Add images to PDF using Java
  • Extract images from PDF using Java
  • Delete images from PDF using Java
  • Replace images in PDF with Java

Aspose.PDF for Java is a powerful API that provides you with a variety of PDF manipulation functions. The API lets you seamlessly work with text, comments, or images in PDF files. Interested friends can click to download the latest version.

Add images to PDF files using Java

Here are the steps to add images to a PDF file using Java.

  • First, create an instance of the Document class to load the PDF Document.
  • Use document.getPages (). The get_Item (int) method gets the page to which the image is to be added.
  • Load the image file into the FileInputStream object.
    • Add the new page to the PDF document and set its properties.
    • Load each image file from the list into the file stream.
    • Adds the image to the paragraph collection of the page.
  • Use the page.getResources ().getimages ().add (FileInputStream) method to add an image to a Page’s resources.
  • Use the operator to place the image on the page:
    • The GSave operator saves the current state of the graph.
    • The ConcatenateMatrix operator, used to specify where to place the image.
    • The Do operator draws an image on the page.
    • GRestore operator saves updated graphics status.
  • Finally, use the document.save (string) method to save the updated PDF file.

The following code example shows how to add an image to a PDF file 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();
Copy the code

Extract images from PDF files using Java

Here are the steps to extract images from PDF documents using Java.

  • Create an instance of the Document class to load the PDF Document.
  • Use document.getPages (). Get_Item (int).getResources (). GetImages (). The get_Item (int) method extracts the desired image into an XImage object.
  • You can also traverse a collection of images to extract and save all of them.
  • Finally, the extracted image is saved as a file using OutputStream.

The following code example shows how to use Java to extract images from PDF files.

// 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();Copy the code

Delete images from PDF files using Java

Here are the steps to remove images from PDF files using Java.

  • Load the PDF file into the Document object.
  • Delete the desired image using one of the following methods.
    • Delete () deletes the image from the collection.
    • Delete (int index) Deletes an image from the collection by index.
    • Delete (String name) Deletes an image from the collection by name.
  • Finally, use the document.save (string) method to save the updated PDF file.

The following code example shows how to delete an image in a PDF using Java.

// 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");
Copy the code

Replace images in PDF files with Java

Here are the steps to replace images in PDF files using Java.

  • Load the PDF file into the Document object.
  • Load the new image into the FileInputStream object.
  • Use document.getPages (). Get_Item (int).getResources (). GetImages (). The replace (int, FileInputStream) method replaces the image by specifying an index.
  • Finally, use the document.save (string) method to save the updated PDF file.

The following code example shows how to replace an image in a PDF using Java.

// 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("apose.png")));

// Save the updated PDF file
pdfDocument.save("output.pdf");
Copy the code

If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.