Search code examples
c#pdfitext7

How to prevent flattening of interactive fields in Pdf file using iText7


I have PDF file with interactive fields. I can change these fields manually without any problems.

I need change interactive fields in Pdf file using library iText7. I able change interactive fields using iText7, but when I open file after operation, I will able see that interactive fields were flatten, so I can’t change fields values anymore in manual mode.

I created concise example, in that I only open and close pdf file. I have same result: after program work, all fields in PDF file were flattened.

Here this example.

string pdfReadPath = @"D:\IT\PROJECTS\PMIC\TASKS\PDF\TEMP\Read_Pdf.pdf";
string pdfWritePath = @"D:\IT\PROJECTS\PMIC\TASKS\PDF\TEMP\Write_Pdf.pdf";

//Initialize pdf document
PdfReader pdfReader = new PdfReader(pdfReadPath);
PdfWriter pdfWriter = new PdfWriter(pdfWritePath);

PdfDocument pdfDoc = new PdfDocument(pdfReader, pdfWriter);

pdfDoc.Close();

I don’t want the interactive fields to flatten.

Does anybody know what way I can use if I want the interactive fields will not flatten after program work?


Solution

  • The fields in your output aren't flattened. You can check this by opening the PDF in any PDF viewer supporting interactive forms except Adobe Acrobat Reader, for example Foxit Reader.

    Instead the problem is that your PDF contains a hybrid XFA/AcroForm form definition and a usage rights signature and that you in your code ignore this. Thus, your code invalidates said signature making Adobe Reader show you the form in a read-only mode.

    By manipulating your PDF in append mode, you can fix this.

    The problem in detail

    Your PDF contains a hybrid XFA/AcroForm form definition and a usage rights signature.

    The former means that the interactive form in your PDF is defined twice, once in the native PDF way (AcroForm) and once as a special XML riding piggyback in your PDF (XFA). XFA forms have been deprecated/obsoleted since 2017.

    Only a few PDF processors support XFA forms. Adobe Acrobat and Adobe Acrobat Reader do. But Adobe Acrobat Reader only supports this if the PDF with the form has been generated by an Adobe product and only form fill-ins have been done since.

    To check whether a PDF with a XFA form has been generated by an Adobe product, Adobe Acrobat Reader looks for a usage rights signature therein, a special kind of signature to mark PDFs generated by Adobe PDF generators to signal to the Adobe Reader that it shall extend its functionality.

    But it does not only look for that signature but also validates it. As your code writes the PDF completely anew, though, the signature in your output PDF is broken. Adobe Acrobat Reader in such a case does not allow editing the form anymore but merely displays a read-only representation thereof which you incorrectly interpreted as flattened form.

    Writing in append mode

    To not invalidate the signature with iText 7, you have to keep the original PDF bytes as they are and only apply changes by appending them as incremental updates.

    You do so by instantiating the PdfDocument with StampingProperties that have the AppendMode selected, so instead of

    PdfDocument pdfDoc = new PdfDocument(pdfReader, pdfWriter);
    

    use

    PdfDocument pdfDoc = new PdfDocument(pdfReader, pdfWriter, new StampingProperties().UseAppendMode());
    

    Beware, though: Validation does not only check whether the usage rights signature is valid, it also checks whether later incremental updates only contain allowed changes. Thus, make sure that you only change the form contents in your code.

    An alternative fix

    As you have a PDF with a hybrid form definition, not a pure XFA form definition, you can alternatively remove both the XFA form definition and the usage rights signature. Thereafter you can freely manipulate the PDF. This was proposed in this answer to a similar question with the difference that the PDF additionally was encrypted.