How to remove a specific image from a PDF with PDFBox

我是研究僧i 提交于 2021-01-07 02:52:55

问题


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 write it something like this:

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)){
            // What should I use here?
            page.getCOSObject().removeItem(propertyName);
        }
    });
    // Should I use page.setResources(resources); ?
 });
doc.save(baos);
} catch (Exception e) {
//Code here

}


回答1:


It works same way like it does in example RemoveAllText.java, just with different tag.

Use code from this example, just use "Do" instead of "Tj".

Of course, if you need to load metadata, etc, you should enumerate and check images threw page resources (like in my example)



来源:https://stackoverflow.com/questions/47757971/how-to-remove-a-specific-image-from-a-pdf-with-pdfbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!