简体   繁体   中英

How to add an image watermark in PDFsharp?

All samples show how to add a text watermark using PDFsharp. but what if I need to add an image watermark?

I did this:

if (!String.IsNullOrEmpty(_logo) && System.IO.File.Exists(_logo))
{
    XImage logo = XImage.FromFile(_logo);
    XRect rectLogo = new XRect(0, 0, page.Width, page.Height);
    rectLogo.Scale(0.5, 0.5);
    gfx.DrawImage(logo, rectLogo);
}

The image appears, but not as a watermark.

EDIT:

I have finally placed a watermark manually, however,there is another thing to solve. How to change opacity of an XImage? I can do it by using an image processor but that is not the idea. This is a software that will use a normal user, and I cannot tell him to modify the image before assigning it as the watermark. Any solution to this?

This is the code at the moment:

            if (!String.IsNullOrEmpty(_logo) && System.IO.File.Exists(_logo))
            {
                XImage logo = XImage.FromFile(_logo);
                double width = logo.PointWidth;
                double height = logo.PointHeight;
                double ratio = width / height;
                if (width > page.Width.Point * 0.5)
                {
                    width = page.Width.Point * 0.5;
                    height = width / ratio;
                }
                else if (height > page.Height.Point * 0.5)
                {
                    height = page.Height.Point * 0.5;
                    width = height * ratio;
                }

                double offsetX = (page.Width.Point - width) / 2;
                double offsetY = (page.Height.Point - height) / 2;

                XRect rectLogo = new XRect(offsetX, offsetY, width, height);
                gfx.DrawImage(logo, rectLogo);
            }

The problem with PDFSharp's Watermark samples is the fact that they're not using the real watermark functionality introduced in PDF 1.6 version ( reference ). The samples use the XGraphics object to draw beneath the existing content but it's just a fake watermark. Here's a direct quote from the documentation:

Note: Technically the watermarks in this sample are simple graphical output. They have nothing to do with the Watermark Annotations introduced in PDF 1.5.

Please notice that the official reference by Adobe says that Watermark annotations were introduced in PDF 1.6, so there seems to be an error in PDFSharp's documentation.

Unfortunately, I don't have a reliable source whether or not PDFSharp supports real watermarks, but I haven't seen any proof that they do. The fact is that PDF 1.4 version is fully supported and everything above it is only partially supported.

As usual the computer does what you tell him to do, not what you want him to do.

If you want to have the image below other contents, then draw it first.

If you want to have a semi-transparent image, then use a transparent image or draw it with transparency. In that case you may place the image above all other contents of the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM