iText 7 just came out May 2016, and while some of the tutorials have been helpful, some of the more advanced functions have been harder to figure out. This page has an example of how to use text as a watermark (about 90% of the way down the page), but I can't figure out how to use an Image as a watermark, and I really have no idea where to start with the new release. Anyone know how to use an Image as a watermark in iText 7? Any ideas where to start?
I'm not 100% positive this is the right way to do this but I'd say I'm 95% confident.
(The code below is C# but you should be able to convert it to Java pretty easily, pretty much just lowercase the first letter of properties and methods. Also, I'm using full namespace paths just so you know where things are at.)
First, create a custom state and set its transparency:
//Create a transparent state iText.Kernel.Pdf.Extgstate.PdfExtGState tranState = new iText.Kernel.Pdf.Extgstate.PdfExtGState(); tranState.SetFillOpacity(0.5f);
Second, get your image:
//Get your image somehow iText.IO.Image.ImageData myImageData = ImageDataFactory.Create("D:\\14.jpg", false); iText.Layout.Element.Image myImage = new iText.Layout.Element.Image(myImageData);
Third (and optional), change your image if needed:
//Position, rotate and scale it as needed myImage.SetFixedPosition(100, 100); myImage.SetRotationAngle(45); myImage.ScaleAbsolute(200, 200);
Fourth, save the pdfCanvas (from the tutorial) state and set a new one:
Fifth, add your image to the higher level canvas (once again, from the tutorial):
And sixth, reset the pdfCanvas state:
Update by Bruno:
Adding images is explained in Chapter 3 of the "iText 7: Building Blocks" tutorial. In chapter 3 of "iText 7: Jump-Start tutorial", we work with a
Canvas object. The missing information about how to create and add an image is in the "Building Blocks" tutorial.