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

别等时光非礼了梦想. 提交于 2020-01-17 04:36:06

问题


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.


回答1:


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.




回答2:


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);


来源:https://stackoverflow.com/questions/15888977/how-can-i-prefill-a-pdf-form-with-itextsharp-while-still-having-it-be-editable-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!