You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Alexander Teut <al...@gmail.com> on 2017/12/12 12:29:12 UTC

How to remove a specific image from PDF?

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
well/


I'm using the following code:

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

COSDictionary imagesContainer =
(COSDictionary)resources.getCOSObject().getDictionaryObject(COSName.XOBJECT);

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)) {
                return;
            }
            PDXObject pdxObject = resources.getXObject(propertyName);
            PDImageXObject pdImageXObject = (PDImageXObject)pdxObject;
            PDMetadata metadata = pdImageXObject.getMetadata();
            if(checkMetadata(metadata)){

               itemsToRemove.add(propertyName);

                // What should I use here?
            }
        });

        itemsToRemove.forEach(imagesContainer::removeItem);
     });
    doc.save(baos);
} 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?

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

Re: How to remove a specific image from PDF?

Posted by Alexander Teut <al...@gmail.com>.
Thanks!

That's actually my question and my answer :)

On 12 December 2017 at 21:55, Tilman Hausherr <TH...@t-online.de> wrote:

> Somewhat answered in comments in
> https://stackoverflow.com/questions/47757971/how-to-remove-
> a-specific-image-from-a-pdf-with-pdfbox
>
> Tilman
>
>
> Am 12.12.2017 um 13:29 schrieb Alexander Teut:
>
>> 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
>> well/
>>
>>
>> I'm using the following code:
>>
>> List<COSName> itemsToRemove = new ArrayList<>();
>>
>> COSDictionary imagesContainer =
>> (COSDictionary)resources.getCOSObject().getDictionaryObject(
>> COSName.XOBJECT);
>>
>> 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)) {
>>                  return;
>>              }
>>              PDXObject pdxObject = resources.getXObject(propertyName);
>>              PDImageXObject pdImageXObject = (PDImageXObject)pdxObject;
>>              PDMetadata metadata = pdImageXObject.getMetadata();
>>              if(checkMetadata(metadata)){
>>
>>                 itemsToRemove.add(propertyName);
>>
>>                  // What should I use here?
>>              }
>>          });
>>
>>          itemsToRemove.forEach(imagesContainer::removeItem);
>>       });
>>      doc.save(baos);
>> } 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?
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>


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

Re: How to remove a specific image from PDF?

Posted by Tilman Hausherr <TH...@t-online.de>.
Somewhat answered in comments in
https://stackoverflow.com/questions/47757971/how-to-remove-a-specific-image-from-a-pdf-with-pdfbox

Tilman

Am 12.12.2017 um 13:29 schrieb Alexander Teut:
> 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
> well/
>
>
> I'm using the following code:
>
> List<COSName> itemsToRemove = new ArrayList<>();
>
> COSDictionary imagesContainer =
> (COSDictionary)resources.getCOSObject().getDictionaryObject(COSName.XOBJECT);
>
> 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)) {
>                  return;
>              }
>              PDXObject pdxObject = resources.getXObject(propertyName);
>              PDImageXObject pdImageXObject = (PDImageXObject)pdxObject;
>              PDMetadata metadata = pdImageXObject.getMetadata();
>              if(checkMetadata(metadata)){
>
>                 itemsToRemove.add(propertyName);
>
>                  // What should I use here?
>              }
>          });
>
>          itemsToRemove.forEach(imagesContainer::removeItem);
>       });
>      doc.save(baos);
> } 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?
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org