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