如何在 PDFsharp 中添加图像水印?
How to add an image watermark in PDFsharp?
所有示例都展示了如何使用 PDFsharp 添加文本水印。但是如果我需要添加图片水印怎么办?
我这样做了:
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);
}
出现图像,但不是水印。
编辑:
终于手动加水印了,不过还有一个问题要解决。如何改变 XImage 的不透明度?我可以使用图像处理器来完成,但这不是我的想法。这是一个将使用普通用户的软件,我不能告诉他在将图像分配为水印之前修改图像。有什么解决办法吗?
这是目前的代码:
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);
}
PDFSharp 水印示例的问题在于它们没有使用 PDF 1.6
版本 (reference) 中引入的真正水印功能。示例使用 XGraphics
对象在现有内容下方绘制,但它只是一个假水印。这是文档的直接引用:
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.
请注意,Adobe 的官方参考文献说水印注释是在 PDF 1.6 中引入的,因此 PDFSharp 的文档中似乎有错误。
不幸的是,关于 PDFSharp 是否支持 real 水印,我没有可靠的消息来源,但我没有看到任何证据证明它们支持。事实是 PDF 1.4
版本得到完全支持,高于它的所有内容仅得到部分支持。
像往常一样,计算机会执行您告诉他执行的操作,而不是您希望他执行的操作。
如果你想把图片放在其他内容下面,那就先画出来。
如果你想有一个半透明的图像,然后使用透明图像或用透明度绘制它。在这种情况下,您可以将图像放在页面的所有其他内容之上。
所有示例都展示了如何使用 PDFsharp 添加文本水印。但是如果我需要添加图片水印怎么办?
我这样做了:
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);
}
出现图像,但不是水印。
编辑:
终于手动加水印了,不过还有一个问题要解决。如何改变 XImage 的不透明度?我可以使用图像处理器来完成,但这不是我的想法。这是一个将使用普通用户的软件,我不能告诉他在将图像分配为水印之前修改图像。有什么解决办法吗?
这是目前的代码:
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);
}
PDFSharp 水印示例的问题在于它们没有使用 PDF 1.6
版本 (reference) 中引入的真正水印功能。示例使用 XGraphics
对象在现有内容下方绘制,但它只是一个假水印。这是文档的直接引用:
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.
请注意,Adobe 的官方参考文献说水印注释是在 PDF 1.6 中引入的,因此 PDFSharp 的文档中似乎有错误。
不幸的是,关于 PDFSharp 是否支持 real 水印,我没有可靠的消息来源,但我没有看到任何证据证明它们支持。事实是 PDF 1.4
版本得到完全支持,高于它的所有内容仅得到部分支持。
像往常一样,计算机会执行您告诉他执行的操作,而不是您希望他执行的操作。
如果你想把图片放在其他内容下面,那就先画出来。
如果你想有一个半透明的图像,然后使用透明图像或用透明度绘制它。在这种情况下,您可以将图像放在页面的所有其他内容之上。