Answer the question
In order to leave comments, you need to log in
How to turn a background image into a watermark by changing the transparency?
I want to make background image transparent using iText.
Here is the image code:
string root = Server.MapPath("~");
string parent = Path.GetDirectoryName(root);
string grandParent = Path.GetDirectoryName(parent);
string imageFilePath = parent + "/Images/logo.png";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(1700, 800);
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
jpg.SetAbsolutePosition(100, 250);
jpg.ScaleAbsoluteHeight(500);
jpg.ScaleAbsoluteWidth(500);
Answer the question
In order to leave comments, you need to log in
See the BackgroundTransparent example . This is a variant of the BackgroundImage
example.
In your code, you add Image
to the Document
. There is nothing wrong with this, but if you want to make the image transparent, you need to create a soft mask. It's not difficult, but there's an easier way to make the background transparent: add an image directly to the content, and then define transparency with PdfExtGState
:
PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
ImageData image = ImageDataFactory.create(imgSrc);
canvas.saveState();
PdfExtGState state = new PdfExtGState();
state.setFillOpacity(0.6f);
canvas.setExtGState(state);
canvas.addImage(image, 0, 0, pageSize.getWidth(), false);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question