Generate and save a PDF file

后端 未结 7 2059
栀梦
栀梦 2020-12-30 10:36

I am using editable pdf files (created by Nitro PDF Software) in my application. These pdf files have a lot of editable fields (like textboxes) and one button (like submit).

7条回答
  •  佛祖请我去吃肉
    2020-12-30 11:13

    From Wikipedia, PDF Interactive elements there are two possibilities for integrating data and PDFs (there are also links to the specifications):

    • AcroForms (also known as Acrobat forms), introduced in the PDF 1.2 format specification and included in all later PDF specifications.
    • Adobe XML Forms Architecture (XFA) forms, introduced in the PDF 1.5 format specification. The XFA specification is not included in the PDF specification, it is only referenced as an optional feature. Adobe XFA Forms are not compatible with AcroForms.

    For compatibility issues I would go for AcroForms. In that case I would use XFDF, because it is XML and therefore easier to parse. I never used Nitro, but when you build a PDF form you usually provide a "Save" button and choose at action for this button "Send/Post form to server" with the data format XML which is just XFDF.

    This works only when the PDF is viewed in the browser. So the typical use case is: have an empty PDF template on the server, before returning the PDF to the user mix your data into the PDF, the user enters data in the form (PDF is opened in the browser via a plugin or natively in Chrome), then the user presses the save buttons which posts a xml on the server. The next time the user asks for his PDF, you took the recent data and mix it again with the template.

    So only two questions are open:

    • how to generate a XFDF:
      very easy, see http://wiki.developerforce.com/page/Adobe_XFDF or Parsing XML using XDocument for an example of the file structure
    • how to mix a XFDF with the PDF: This can be done with itext, there are several examples on stackoverflow, eg. https://stackoverflow.com/a/6274311/734687

    See the complete process here: http://itextpdf.com/book/chapter.php?id=9 . This example updates the PDF with the form dynamically at run time. Since iText is used there is no difference between Java and C#.

    Be aware that previous versions of iText (Java up to 2.1.7 and C# up to 4.1.6) were distributed under the Mozilla Public License or the LGPL, while current versions are distributed under the Affero General Public License. Thats explains why the older versions are still used.

提交回复
热议问题