Julio Loizaga
Julio Loizaga

Reputation: 67

Add Image to a PDF digitally signed with itextSharp

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

Upvotes: 0

Views: 1973

Answers (1)

Paulo Soares
Paulo Soares

Reputation: 1916

The PdfStamper must be created in append mode.

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

Upvotes: 5

Related Questions