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
>

5 comments:

Anonymous said...

This was very helpful to me. Thanks.

Unknown said...

hi ,
this was very interesting but can you please provide me in step by step with relevant references to libraries if they use and the code in detail etc..because i find it difficult to recreate the and run

Thank you

Unknown said...

hi ,
this was very interesting but can you please provide me in step by step with relevant references to libraries if they use and the code in detail etc..because i find it difficult to recreate the and run

Thank you

Unknown said...

Hi Safraz,

Thank you for your comment. Well this is a very basic c# web page code. if you start creating a new web app in Visual studio you will get everything you need including the libraries.

If you have never seen this type of code its best if you get some basic Visual Web Developer training from books or courses.

Mithlesh said...

Hi

I am want get value from pdf form using asp.net with c#.I will input value in pdf form and when i submit pdf form i need value of pdf form.What is best way.Please suggest me and if any reference please let me know

Thanks
Mithlesh