Legacy notice!

iText 5 is the previous major version of iText's leading PDF SDK. iText 5 has been EOL, and is no longer developed. Switch your project to iText 7, integrating the latest developments.
Check related iText 7 content!

How to make a background image transparent in iText 5.

I want to make my background image in iText transparent.

Here is my code for the image:

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);
Any idea?

Posted on StackOverflow on Dec 2, 2014 by dandy

Please take a look at the BackgroundTransparent example. It is a variation on the BackgroundImage example.

In your code, you're adding the Image to the Document instance. That's OK, but if you want to make such an image transparent, you need to introduce a soft mask. That's not difficult, but there's an easier way to make your background transparent: add the image to the direct content, and introduce a PdfGState defining the opacity:

PdfContentByte canvas = writer.getDirectContentUnder();
Image image = Image.getInstance(IMAGE);
image.SetAbsolutePosition(0, 0);
PdfGState state = new PdfGState();

Compare background_image.pdf with background_transparent.pdf to see the difference.

My example is written in Java, but it's very easy to port this to C#:

PdfContentByte canvas = writer.DirectContentUnder;
Image image = Image.GetInstance(IMAGE);
image.SetAbsolutePosition(0, 0);
PdfGState state = new PdfGState();
state.FillOpacity = 0.6f;