How can I prefill a PDF form with iTextSharp while

2019-08-28 02:39发布

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.

2条回答
虎瘦雄心在
2楼-- · 2019-08-28 03:05

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.

查看更多
放我归山
3楼-- · 2019-08-28 03:30

Please consult the example ReaderEnabledForm.cs. It describes different ways to fill out a Reader Enabled form:

  1. breaking the Reader-enabling
  2. Removing the Reader-enabling
  3. Preserving the Reader-enabling

You've already tried 1 and 2, whereas you're asking for 3, involving adding extra parameters when creating a PdfStamper instance:

new PdfStamper(pdfReader, New FileStream(outputPath, FileMode.Create), '\0', true);
查看更多
登录 后发表回答