使用PDFBox将透明图像插入PDF文件

3
我会把图片加载到pdf中,具体步骤如下:

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

我想让图像看起来透明,就像在文档的标题中一样(Google Docs、Word等),有没有简单的方法可以做到这一点?

1个回答

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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接