pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Teut <alexander.a.t...@gmail.com>
Subject How to remove a specific image from PDF?
Date Tue, 12 Dec 2017 12:29:12 GMT
I need to remove a specific image from PDF file according its
metadata. Sadly. all examples I can find in Internet are using
discarded methods.

I draw this image with contentStream.drawImage() method and mark it
with a metadata. I needto allow my soft to remove added images as

I'm using the following code:

List<COSName> itemsToRemove = new ArrayList<>();

COSDictionary imagesContainer =

try (PDDocument doc = PDDocument.load(new ByteArrayInputStream(pdf))) {
doc.getPages().forEach(page ->
        PDResources resources = page.getResources();
        List<COSName> itemsToRemove = new ArrayList<>();

        resources.getXObjectNames().forEach(propertyName -> {
            if(!resources.isImageXObject(propertyName)) {
            PDXObject pdxObject = resources.getXObject(propertyName);
            PDImageXObject pdImageXObject = (PDImageXObject)pdxObject;
            PDMetadata metadata = pdImageXObject.getMetadata();


                // What should I use here?

} catch (Exception e) {
    //Code here


It produces a cleared PDF that shows an error when I open it. Looks like I
should remove it from contentStream too. How to do that?

Alexander Teut
http://g <http://rikkimongoose.ru>ithub.com/rikkimongoose

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message