Delete Images from PDF File

Contents
[ ]

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

  1. Create a Document object and open the input PDF file.
  2. Get the Page that holds the image from the Document object’s Pages collection.
  3. Images are held in the Images collection, found in the page’s Resources collection.
  4. Delete an image with the Images collection’s Delete method.
  5. Saved the output like using the Document object’s Save method.

The following code snippet shows how to delete an image from a PDF file.

package com.aspose.pdf.examples;

import com.aspose.pdf.Color;
import com.aspose.pdf.Document;
import com.aspose.pdf.FontRepository;
import com.aspose.pdf.FontStyles;
import com.aspose.pdf.HorizontalAlignment;
import com.aspose.pdf.PageNumberStamp;

public class ExampleDeleteImages {

    private static String _dataDir = "/home/aspose/pdf-examples/Samples/";

    public static void ExampleDeleteImage() {

        // Open document
        String inputFile = Paths.get(_dataDir, "DeleteImages.pdf").toString();
        String outputFile = Paths.get(_dataDir, "DeleteImages_out.pdf").toString();
        Document pdfDocument = new Document(inputFile);
        pdfDocument.getPages().get_Item(1).getResources().getImages().delete(1);
        pdfDocument.save(outputFile);
        pdfDocument.close();
    }
}