News Category

Delete Images in PDF in Java

2018-10-30 00:52:19 Written by  support iceblue
Rate this item
(0 votes)

This article shows how to delete a particular picture on a PDF page and how to delete all pictures of the entire document by using Spire.PDF for Java.

Delete a particular image

import com.spire.pdf.PdfDocument;

public class DeleteImage {

	public static void main(String[] args) {

		//create a PdfDocument object
		PdfDocument doc = new PdfDocument();
		
		//load a sample PDF file
		doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Images.pdf");

		//get the specified page
		PdfPageBase page = doc.getPages().get(0);
		
		//delete a particular image by its index
		page.deleteImage(1);

		//save the file
		doc.saveToFile("DeleteSpecificImage.pdf", FileFormat.PDF);
	}

}

Delete all images

import com.spire.pdf.exporting.PdfImageInfo;

public class DeleteAllImages {

    public static void main(String[] args) {

        //create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //load a sample PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\asda.pdf");

        //loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            //get the specific page
            PdfPageBase page = doc.getPages().get(i);

            //get the image information of the page
            PdfImageInfo[] imageInfo = page.getImagesInfo();

            //loop through the image information collection
            for (int j = imageInfo.length; j > 0; j--) {

                //delete image by its index
                page.deleteImage(j - 1);
            }
        }

        //save the file
        doc.saveToFile("DeleteAllImages.pdf", FileFormat.PDF);
    }
}

Additional Info

  • tutorial_title:
Last modified on Thursday, 02 September 2021 06:31