使用pdfbox将透明图像插入pdf

Mic*_*elK 3 java pdfbox

我通过以下方式将图像加载到 pdf 中:

PDImageXObject image= PDImageXObject.createFromFile(<image_path>, doc);
contentStream.drawImage(image, 15, pdfData.getPageHeight() - 80,
image.getWidth(), image.getHeight());
Run Code Online (Sandbox Code Playgroud)

我试图使图像看起来透明,就像它在文档(谷歌文档,word 等)的标题中看起来一样,有没有一种简单的方法可以做到这一点?

Til*_*err 7

使用扩展图形状态:

stream.saveGraphicsState();
PDExtendedGraphicsState pdExtGfxState = new PDExtendedGraphicsState();
pdExtGfxState.getCOSObject().setItem(COSName.BM, COSName.MULTIPLY); // pdExtGfxState.setBlendMode(BlendMode.MULTIPLY) doesn't work yet, maybe in later version 
pdExtGfxState.setNonStrokingAlphaConstant(0.5f);
contentStream.setGraphicsStateParameters(pdExtGfxState);
// do your stuff
stream.restoreGraphicsState();
Run Code Online (Sandbox Code Playgroud)