How do I set opacity on an image, so that it appers like a watermark?

Q: I'm trying to add a watermark image to a given page in my PDF
document. How do I set opacity on an image, so that it appers like a

My code is as follows:

            PDFDoc doc = new PDFDoc(DIRtextBox.Text + "\\" +
            ElementBuilder bld = new ElementBuilder();
            ElementWriter writer = new ElementWriter();
                Page page = doc.GetPage(1);
                string input_path = DIRtextBox.Text + "\\";

                Image img = Image.Create(doc, input_path +

                double pageWidth =
                double imgHeight = img.GetImageHeight();

                double xPos = (pageWidth / 2) -
((img.GetImageWidth() / 4) / 2);
                double imgScaledHeight = img.GetImageHeight() / 4;
                double yPos = 0;
                double imgScaledWidth = img.GetImageWidth() / 4;

                Element element = bld.CreateImage(img, new
Matrix2D(imgScaledWidth, 0, 0, imgScaledHeight, xPos, yPos));


                doc.Save(input_path + "addimage.pdf",
            catch (Exception e)
                MessageBox.Show(e.Message, "", MessageBoxButtons.OK);
A: There are several ways to apply transparency effect to an element
in PDF. For example, to set the constant opacity for the entire
element (i.e. image in your case), use
element.GetGState.SetFillOpacity(num). For example:

// In C# (VB.NET, Java, C++ is essentially the same).
// For relevant sample code please see to AddImage and ElementBuilder
Image image = Image.Create(doc, "my.jpg");
Element element = f.CreateImage(image, new
Matrix2D(image.GetImageWidth(), 0, 0, image.GetImageHeight(), 10,
element.GetGState().SetFillOpacity(0.5); // opacity is a number is the
range [0..1]
// Optional: Alter the current blend mode...
// element.GetGState().SetBlendMode(...);

It is also possible to apply a monochrome image mask or a grayscale
(i.e. soft) mask to an existing image using image.SetSoftMask(smask)
or image.SetImageMask(imask) methods. In this case the alpha channel
of the base image will correspond to the values from the mask image.

Another way to apply transparency effect to images or to any content
on the page is by setting the SoftMask property in the extended
graphics state (element.GetGState().SetSoftMask(msk)). This is the
most powerful way to control transparency in PDF, but is also the most
complicated to use.