【问题标题】:Add Image to a PDF digitally signed with itextSharp将图像添加到使用 itextSharp 进行数字签名的 PDF
【发布时间】:2015-12-13 18:48:03
【问题描述】:

我想将图像放入经过数字签名的 PDF。如果我用通常的方式来做,签名就会被破坏。但是使用 Acrobat 可以在签名的 PDF 中添加注释戳记,并且签名不会被破坏。

谷歌搜索我找到了一个如何做到这一点的例子:

http://itext.2136553.n4.nabble.com/Digital-Signature-Corrupted-after-adding-watermark-image-td4657457.html

我已经将它翻译成 c# 但没有成功:

using (Stream inputPdfStream = new FileStream("test.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream inputImageStream = new FileStream("grafo.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream("result.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
    var reader = new PdfReader(inputPdfStream);
    var stamper = new PdfStamper(reader, outputPdfStream);

    iTextSharp.text.Image image =  iTextSharp.text.Image.GetInstance(inputImageStream);
    image.SetAbsolutePosition(0, 0);

    PdfTemplate template = PdfTemplate.CreateTemplate(stamper.Writer, image.Width, image.Height);
    template.AddImage(image);

    iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(350, 250, 350 + image.Width, 250 + image.Height);

    PdfAnnotation annotation = PdfAnnotation.CreateStamp(stamper.Writer, rect, null, Guid.NewGuid().ToString());
    annotation.SetAppearance(PdfName.N, template);
    stamper.AddAnnotation(annotation, 1);
    stamper.Close();
}

当我使用 Acrobat 打开 PDF 时,签名已损坏。

关于如何使用 iText 做到这一点的一些想法?

谢谢

【问题讨论】:

  • 如果@Paulo 的回答解决了您的问题,请接受,即点击左上角的勾号。

标签: pdf itext


【解决方案1】:

PdfStamper 必须以附加模式创建。

var stamper = new PdfStamper(reader, outputPdfStream, '\0', true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多