0

I have Digitally Signed a PDF named as "Original" with Succes Message : "Signed and all signatures are valid." When i try to modify that pdf's data using below code and open the document named as "NewlyCreated" ,signatures become invalid with Error Message : AtLeast one signature is valid

public class Program
{
    public const String src = @"C:\Original.pdf";
    public const String dest = @"C:\NewlyCreated.pdf";
    public void createPdf1(String dest)
    {
        PdfReader reader = new PdfReader(src);
        PdfDocument pdfDoc = new PdfDocument(reader,new PdfWriter(dest),new StampingProperties().UseAppendMode());
        PageSize ps = pdfDoc.GetDefaultPageSize();
        Paragraph p = new Paragraph("This is the text added in the rectangle.");
        PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage());
        Rectangle rect = new Rectangle(ps.GetWidth() - 90, ps.GetHeight() - 100, 50, 50);
        new Canvas(canvas, pdfDoc, rect)
            .Add(p);
        canvas.Rectangle(rect);
        canvas.Stroke();
        pdfDoc.Close();
    }

    public static void Main(string[] args)
    {
        Program objProgram = new Program();
        objProgram.createPdf1(dest);
    }
}

How can i modify pdf without invalidating its signature using iText7

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 6
    Isn't that the point of a signature? You sign to show that you agree with the original, but not any future edits. If you really need to edit a document, but might be able to split it into sections, sign one and edit the other. – Robin Bennett Aug 20 '19 at 11:50
  • 5
    The whole point of digitally signing something is so you can verify that it has not been modified since it was signed. If what you're asking for was possible then what would be the purpose of having signatures at all, do you think? – ADyson Aug 20 '19 at 11:50
  • what is wanna do here is not to get the signature identity as unknown. As in adobe reader, if there are unsigned changes it will simply say the signature is valid but there are unsigned changes. This is what is desired.But if i use the above code, it will simply invalidate the signature.I want to preserve the signature somehow. – user11925666 Aug 20 '19 at 11:59
  • I don't know for sure since I don't use it but as a guess I would think Adobe will say there are unsigned changes if it knows that it has been doing the editing, rather than the file being amended by something else? Or maybe it's something in the way you structure the changes. I don't know Adobe reader or the PDF format well enough to do any more than speculate - I just commented on what appeared to be an attempt to subvert the security provided by digital signatures. Maybe someone with specific pdf expertise can help you further - I edited your tags so that your question shows as a PDF query – ADyson Aug 20 '19 at 12:30
  • 1
    See the answer to the question yours has been closed as duplicate of - there are only very few types of changes allowed to signed documents; in particular changes to the page contents never are allowed, and your code changes page content. *"As in adobe reader, if there are unsigned changes it will simply say the signature is valid but there are unsigned changes."* - that is incorrect. At least since Adobe Acrobat 8 or 9, changes to page content are always considered to break signatures. Changes to form field values or annotations are a different matter. – mkl Aug 20 '19 at 13:59
  • 1
    Concerning your question *"How can i modify pdf without invalidating its signature using iText7"* - restrict yourself to the allowed modifications as enumerated in the answer to the question yours has been closed as duplicate of. I.e. in essence form field fill-ins and probably annotation changes unless the document is completely locked down. And of course addition of validation information and document time stamps. If you have access to the documents before initial signing, you might even feel cocky and make use of page templates. – mkl Aug 20 '19 at 14:10

1 Answers1

1

It depends on how the original pdf was created. If it was created with the Appendable attribute of thePdfReader instance set to true it should be possible to update and preserve the signature.

Hintham
  • 1,078
  • 10
  • 29
  • I have tried the same , initially created the PDF document using .UseAppendMode() and then digitally signed document after that When i am trying to add text through program in the same document it gives error as : "At least one signature is invalid". – user11925666 Aug 22 '19 at 12:01