Created
November 14, 2025 18:39
-
-
Save aspose-com-gists/c7d4a8d05c2d9176c7be33a41fe48356 to your computer and use it in GitHub Desktop.
How to Remove Images from PDF using Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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