MichaelK
MichaelK

Reputation: 339

insert transparent image to pdf using pdfbox

I load my image to the pdf in the following way:

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

I'm trying to make the image look transparent, like it would look in a header of a document(google docs, word etc.) is there an easy way to do this?

Upvotes: 3

Views: 2384

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18851

Use an extended graphics state:

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

Upvotes: 7

Related Questions