首页 文章

将图像添加到使用itextSharp数字签名的PDF

提问于
浏览
0

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

Googgling我找到了一个如何做到这一点的例子:

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做到这一点的想法?

谢谢

1 回答

  • 2

    必须在追加模式下创建 PdfStamper .

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

相关问题