Search code examples
securitypdfitextadobe-readerpdf-form

How can I prefill a PDF form with iTextSharp while still having it be editable and saveable in Adobe Reader by other users?


Right now I have a form template that has some fields that are prefilled from a database using the iTextSharp library. Users will then fill in the rest of the fields and save the filled out forms. I'll then extract the data and put it in the database.

Currently I am able to create the PDFs and they are pre-filling just fine. I'm even able to edit and save them in Adobe Reader on my computer. However I'm finding when anyone else opens those same files, they're not allowed to save the forms.

What do I need to do to allow the forms to be saved by all users using Adobe Reader? Here's the code I have to create the PDF:

Dim pdfReader As PdfReader = New PdfReader(formPath)
pdfReader.RemoveUsageRights()
Dim pdfStamper As PdfStamper = New PdfStamper(pdfReader, New FileStream(outputPath, FileMode.Create))
Dim pdfFormFields As AcroFields = pdfStamper.AcroFields
Dim xfdfReader As XfdfReader = New XfdfReader(xfdfPath)

pdfFormFields.SetFields(xfdfReader)
pdfStamper.Close()

I used to have the problem that even I couldn't save the forms in Reader, and that's why I added this line:

pdfReader.RemoveUsageRights()

That made it so that I can edit the PDF it creates, which made me think everything was resolved. But nobody else can.


Solution

  • You just use this in your code. When creating PdfStamper you need to add extra parameters like this:

    PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
        newPath, FileMode.CreateNew, FileAccess.Write), '\0', true);
    

    This will do the trick.