Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created November 14, 2025 18:39
Show Gist options
  • Select an option

  • Save aspose-com-gists/c7d4a8d05c2d9176c7be33a41fe48356 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/c7d4a8d05c2d9176c7be33a41fe48356 to your computer and use it in GitHub Desktop.
How to Remove Images from PDF using Java
import com.aspose.pdf.*;
public class RemoveAllImages {
public static void main(String[] args) {
// Load the PDF document
Document doc = new Document("DocumentWithImages.pdf");
// Iterate through each page
for (Page page : doc.getPages()) {
// Remove all images from the page
page.getResources().getImages().delete();
}
// Save the modified document
doc.save("DocumentWithoutImages.pdf");
}
}
import com.aspose.pdf.*;
public class RemoveGraysclaeImages {
public static void main(String[] args) {
// Load the PDF document
Document doc = new Document("DocumentWithImages.pdf");
// iterate through all pages of PDF file
for (Page page : (Iterable<Page>) doc.getPages()) {
// create Image Placement Absorber instance
ImagePlacementAbsorber abs = new ImagePlacementAbsorber();
page.accept(abs);
for (ImagePlacement ia : (Iterable<ImagePlacement>) abs.getImagePlacements()) {
// ColorType
ColorType colorType = ia.getImage().getColorType();
if(colorType == ColorType.Grayscale)
{
ia.getImage().delete();
}
}
}
// Save the modified document
doc.save("RemoveGraysclaeImages.pdf");
}
}
import com.aspose.pdf.*;
public class RemoveSpecificImage {
public static void main(String[] args) {
// Load the PDF document
Document doc = new Document("DocumentWithImages.pdf");
// Access the specific page (e.g., page 1)
// Delete a particular image
doc.getPages().get_Item(4).getResources().getImages().delete(1);
// Save the modified document
doc.save("RemoveSpecificImages.pdf");
}
}
import com.aspose.pdf.*;
public class RemoveSpecificPageImages {
public static void main(String[] args) {
// Load the PDF document
Document doc = new Document("DocumentWithImages.pdf");
// Access the specific page (e.g., page 1)
Page page = doc.getPages().get_Item(1);
Resources resources = page.getResources();
XImageCollection images = resources.getImages();
images.delete();
// Save the modified document
doc.save("RemoveSpecificPageImages.pdf");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment