Image replacement using PDFBox

我的未来我决定 提交于 2019-12-11 15:28:27

问题


I want to replace image on first page in my PDF. I am using the following code. However, it looks like transparency is lost. How to retain the transparency of original image

 for(int a=0;a<1;a++){
        PDPage p = doc.getPage(a);
        PDResources resources = p.getResources();
        for (COSName xObjectName : resources.getXObjectNames()) {
            PDXObject xObject = resources.getXObject(xObjectName);
            if (xObject instanceof PDImageXObject) {
                PDImageXObject original_img = ((PDImageXObject) xObject);
                PDImageXObject replacement_img = PDImageXObject.createFromFile("C:\\compression\\pdf\\5992-0891EN-1_compress.jpg", doc);
                replacement_img.setInterpolate(true);
                resources.put(xObjectName, replacement_img);
            }        
        }
    }

来源:https://stackoverflow.com/questions/48628749/image-replacement-using-pdfbox

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