0

I'd like to put an image into a digitally signed PDF. if I do it using the usual way, the signature is broken. But with Acrobat it's possible to add an annotation stamp to a signed PDF and the signature is not broken.

Googgling I've found an example of how to do that:

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

I've translate it to c# but without success:

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

When I open the PDF with Acrobat, signature is broken.

Some idea of how to do that with iText?

Thank's

1
  • If @Paulo's answer solved your issue, please accept it, i.e. click on the tick at its upper left.
    – mkl
    Commented Feb 1, 2016 at 11:18

1 Answer 1

5

The PdfStamper must be created in append mode.

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.