Thursday, April 26, 2012

Collecting PDF Form Data with ASP.NET C#

I recently had to investigate the technique to capture the form input data collected on Adobe PDF for radiological procedure report results.

This, though very simple, information was scattered around all over the places so I can provide you the gist of what you need to do by means of a simple demo code.
  • First you will need to use the PDF Form Editor, for example Adobe Acrobat Pro. I am sure there are a lot of different ones.
  • Next you either scan a form or create one which output the usual .doc file.
  • Import the document and edit the form using the PDF form editor, drag and dropping the fields.
  • Finally add a submit button with a submit action of http://localhost/xfdf
  • On Visual Studio, create the xfdf web project and you can add the following code in Page_Load of Default.aspx
public partial class xdf : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var name = "junk.xfdf";
        string filename = Path.Combine(Server.MapPath(""), name);
        using (FileStream fs = new FileStream(filename, FileMode.Create))
        {
            byte[] bytes = new byte[8192];
            int bytesRead;
            while ((bytesRead = Request.InputStream.Read(bytes, 0, bytes.Length)) > 0)
            {
                fs.Write(bytes, 0, bytesRead);
            }
        }
    }
}
  • Compile above code.
  • Submit the PDF form to the URL
Then if you open junk.xfdf on your text editor, you will see something like this. The key part is that you will see the field and value elements in the XML. From this point it is quite easy to parse this stuff using the XDocument class and LINQ.

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"
><fields
><field name="Heart Rate"
><value
>10</value
></field
><field name="Submit"
/></fields
><ids original="BFF11731D28F744F901901CF60BBED7A" modified="B0328D96D4E3F242A93BC5512C7B97E6"
/></xfdf
>
Post a Comment