I
I
iText Software2019-06-18 08:34:31
Java
iText Software, 2019-06-18 08:34:31

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

How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iText_A, 2019-06-18
@iText_A

See the BackgroundTransparent example . This is a variant of the BackgroundImage
example. In your code, you add Imageto 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);

You can compare the results of the examples above and see the difference.
My example is written in Java, but it's easy to modify for C#.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question