我正在使用 itextSharp 在 pdf 文档中添加注释。
我有一个 pdf 文档,其中已经包含保存在其中的图像,它是一个邮票。
因此,我在邮票中的这个 pdf 上画了一些笔触,当我在 WPF 中绘制它们时一切都很好,但是当我使用 iTextSharp 通过电子邮件发送 pdf 进行转换时,我画的线现在位于邮票下方。
我该如何解决这个问题?
谢谢
我正在使用 itextSharp 在 pdf 文档中添加注释。
我有一个 pdf 文档,其中已经包含保存在其中的图像,它是一个邮票。
因此,我在邮票中的这个 pdf 上画了一些笔触,当我在 WPF 中绘制它们时一切都很好,但是当我使用 iTextSharp 通过电子邮件发送 pdf 进行转换时,我画的线现在位于邮票下方。
我该如何解决这个问题?
谢谢
您作为答案发布的解释(顺便说一句,更恰当的是编辑您的问题以包含该数据)解释了该问题。
PDF 页面上有两种主要类型的对象可见:
注释总是显示在页面内容之上,如果它们被显示的话。
在您的情况下,您将图像添加到 PDF 页面内容(使用 OverContent 或 UnderContent 只会更改与您添加的其他 PDF 页面内容相关的位置)。另一方面,印章很可能是通过注释来实现的。因此,戳记注释始终高于您的添加。
如果您想让您的添加出现在图章上方,您要么必须将添加的内容添加为某种注释,要么您必须在添加内容之前将图章注释展平到页面内容中。
这些变体中哪个更好,取决于您的要求。是否有任何要求强制印章保留印章注释?是否有任何要求强制您添加的内容保留为内容的一部分?请详细说明您的要求。由于内容和注释在显示或打印时有一些不同的属性,请说明所有要求。
此外,请提供样本文件。
所以就像我说的原始pdf里面保存了一个印章,如果我用acrobat reader打开pdf,我可以移动印章。
所以在这里我的代码写了一些笔画:
using (var outputStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
using (var intputStream = new FileStream(pathPdf, FileMode.Open, FileAccess.Read, FileShare.Read))
{
PdfReader reader = new PdfReader(intputStream);
using (var pdfStamper = new PdfStamper(reader, outputStream))
{
foreach (var page in pages)
{
if (page != null && page.ExportedImages.HasItems())
{
PdfContentByte pdfContent = pdfStamper.GetOverContent(page.PageIndex);
Rectangle pageSize = reader.GetPageSizeWithRotation(page.PageIndex);
PdfLayer pdfLayer = new PdfLayer(string.Format(ANNOTATIONNAMEWITHPAGENAME, page.PageIndex), pdfContent.PdfWriter);
foreach (ExporterEditPageInfoImage exportedInfo in page.ExportedImages)
{
Image image = PngImage.GetImage(exportedInfo.Path);
image.Layer = pdfLayer;
if (quality == PublishQuality.Normal || quality == PublishQuality.Medium || quality == PublishQuality.High)
{
float width = (float)Math.Ceiling((image.Width / image.DpiX) * 72);
float height = (float)Math.Ceiling((image.Height / image.DpiY) * 72);
image.ScaleAbsolute(width, height);
float x = (float)(exportedInfo.HorizontalTile * (page.TileSize * (72 / 96d)));
float y = (float)Math.Max(0, (pageSize.Height - ((exportedInfo.VerticalTile + 1) * (page.TileSize * (72 / 96d)))));
image.SetAbsolutePosition(x, y);
}
else
throw new NotSupportedException();
pdfContent.AddImage(image);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
pdfStamper.Close();
}
}
所以我的笔画在 pdf 中保存得很好,邮票总是在一切之上,我认为这是正常的,所以我可以为此做一个解决方法吗?