开发者

ASP.NET - Collect user input from PDF Form fields

We are considering a number of application requirements, and we are examining a number of solutions. Considering that we are probably not the only developer ever to be presented with these requirements, we are wondering what solution and techniques others may have opted for…

Below are, in summary, the requirements for one specific application feature:

-- Platform: ASP.N开发者_开发技巧ET

-- Collect user input (via standard web forms)

-- Populate PDF forms with collected user input AND DO NOT FLATTEN the PDF forms

-- Immediately display the PDF forms (in browser), and allow the user to edit the content of fields (the PDF forms are to be displayed within an iFrame).

-- Allow the user to "submit" the changed PDF to the web server (where it will be finally flattened and stored into a back-end database)

The last point, as you can imagine, is the one that has us scratching our heads… how to "save" the PDF file with the user changes - back to web server's file system… where it will be finally flattened and stored in the database.

Any practical suggestions? Any recommendations? Alternate solutions NOT involving PDF forms?

Keep in mind that, given the target audience, solutions involving Adobe's LiveCycle licenses are definitely NOT in the picture….

Thanks

Giorgio


Given an existing PDF, iTextSharp can fill in the field values (AcroForm or XFA/LiveCycle forms).

HOWEVER, while iTextSharp (and iText) can handle any old AcroForm, it can only FLATTEN static liveCycle forms.

Also, a PDF form can submit itself in a number of ways:

  • FDF: a dialect of PDF. Mostly name/value pairs, with some Extra Stuff around it.
  • HTML-style
  • PDF: PDFs can submit themselves (with form data) to a URL. You can just open & flatten, no need to worry about filling the form in a second time. I believe this is restricted to "Reader Enabled" forms in Reader.

iText's PdfStamper (in append mode!) can fill in field values without breaking a PDF's Reader-Enabled-ness.

Your workflow becomes:

  1. Fill in the reader-enabled form with iText's PdfStamper in append mode, and serve that PDF up to the user.
  2. Receive the filled PDF from the user
  3. Flatten the PDF with another PdfStamper. Simply "open, setFormFlattening(true), close()".

Rather than filling in the PDF, you can serve up FDF's with the field values and a reference to the PDF template (which again, to submit the full PDF IIRC requires Reader Enabling, from Acrobat). This is noticeably more efficient than parsing a PDF and serving it up every time.

An FDF looks something like this:

%FDF-1.2
%âãÏÓ
1 0 obj
<</F(http://www.mySite.com/myPDF.pdf)
/Fields[
  <</T(Check1)/V/Off>>
  <</T(Some Field)/V(A value)>>
  <</T(AnotherFld)/V(1.0)>>
  <</T(You get)/V(the idea)>>
 ]
>>/Type/Catalog>>
endobj
trailer
<</Root 1 0 R>>
%%EOF

<< and >> wrap "dictionaries" (name value pairs).
( and ) wrap strings
[ and ] wrap arrays
/ leads "names". Names are used for dictionary keys, and occasionally Other Stuff.

You have to escape some characters in names and strings:

String escapes start with a '\'. \r, \n, \t, \b, \f, (, ), and \. You can also escape any old character value IN OCTAL thusly: \ooo, where the 'o' are octal values.

Names escape Completely Differently (grr).

Name escapes start with a '#', followed by a two digit hex number. HEX this time, not octal. Grr grr. Anything not in a-z A-Z 0-9 . _ ; * @ should be escaped. You CAN escape everything, it's just horribly inefficient.

You may well need to worry about string escapes (for field values in particular, and possibly for your PDF path/URL), but you almost certainly won't need to worry about name escapes.

There are various libraries to write FDF for you, but I find it easier to just bang out FDFs with simple string concatenation.

Note that FDF supports quite a bit more than that, but many FDF libraries do not, so you must build them yourself if you go off the beaten path.


I've never used it, but I've heard people having some success with iTextSharp. There's a code project article on how to save PDF files using it here. You'll want to check the license of iTextSharp to make sure it's compatible with what you're doing.


You can use a submit button inside your PDF file for submitting the name/value pairs, in a similar way you do for html forms (see this SO question for details). Then on the server side you can use a PDF processing tool (Amyuni PDF Creator .Net, iText, etc) to re-insert the values on your PDF form and save the resulting file in the database.


How to create a PDF or web form:

  • In Acrobat XI, select the Tools pane > Forms > Create.
  • Click From Scratch or Template to launch the FormsCentral desktop app.
  • Click Create a New Blank Form or Start with a Template.
  • Customize your form using the intuitive tools
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜