PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

Save Form Submit Data Back To Original PDF Document In .NET

Create “personalized” copies of PDF forms documents using C# and PDFOne .NET in an ASP.NET handler.
By V. Subhash

Recently, we got a mail from a user who wanted to know if there was an up-to-date version of an old PDFOne .NET article - Save Captured Form Field Data Back To AcroForms PDF Files... Online - written in 2006.

PDFOne .NET enables developers to dynamically create PDF forms documents from desktop, console and ASP.NET applications. Developers can provide end-users the ability to save the entered data to the original forms document so that they have their own "personal" copy.

Instead of a ASP.NET Web Forms project, I decided to use a ASP.NET handler for this. An ASP.NET handler (characterized by the .ashx extension) is the better option when you are trying to generate binary files (which PDF files are). With a Web Forms project, you would have to be very careful that you or Visual Studio does not drop any spaces or stray bytes into the http response output stream. That can wreak havoc with the constitution of the binary file that you are trying to generate!

When you request the URL of this handler, it will generate a PDF forms document. The form fields in the document are set up to submit back to the handler's URL. When the handler receives, the form data, it simply generates the same PDF document but with the data submitted by the user.

<%@ WebHandler Language="C#" Class="save_pdf" %>

using System;
using System.Web;

using System.Collections;
using System.Collections.Specialized;
using System.Drawing;
using Gnostice.PDFOne;


public class save_pdf : IHttpHandler {

  public void ProcessRequest (HttpContext context) {

    context.Response.Buffer = true;
    NameValueCollection frm = context.Request.Form;

    int i, n = frm.Count;

    // Create a document
    PDFDocument doc = new PDFDocument("your-license-key");

    doc.ApplyFormAppearances = true;
    doc.OpenAfterCreate = false;

    // Render text  identifying the form fields
    doc.WriteText("Enter your name", 1, 1);
    doc.WriteText("Select your country", 1, 2);

    // Create a text field
    PDFFormTextField txtFld = new PDFFormTextField();
    txtFld.Rectangle = new RectangleF(3, 1, 1.6f, 0.25f);
    txtFld.ToolTip = "Enter your name here";
    txtFld.FieldName = "TxtFld1";
    txtFld.BorderColor = Color.Black;
    txtFld.NameAsUnicode = false;

    // Create a list box
    PDFFormComboBox cmbBox = new PDFFormComboBox();
    cmbBox.Rectangle = new RectangleF(3, 2, 1f, 0.25f);
    cmbBox.AddItem("Select an option");
    cmbBox.AddItem("India");
    cmbBox.AddItem("Germany");
    cmbBox.AddItem("Russia");
    cmbBox.FieldName = "cmbBox1";
    cmbBox.NameAsUnicode = false;
    cmbBox.BorderColor = Color.Black;

    // If the form was submitted, update the default values
    if (context.Request.Form.Count > 0) {
      // Set submitted text form field value as default
      txtFld.FieldValue = frm[txtFld.FieldName];

      // Set the submitted list box option as selected
      for (i = 0; i < cmbBox.OptionList.Count; i++) {
        // Check if current item in OptionList array list
        // matches the submitted combo box option
        if (cmbBox.OptionList[i].ToString() ==
            frm[cmbBox.FieldName]) {  // same as frm["cmbBox1"]
            cmbBox.SelectedItemIndex = i;
        }
      }
    }

    // Create the push button and set it to submit
    // form contents to the handler's URL
    PDFFormPushButton pushBtn = new PDFFormPushButton();
    pushBtn.Rectangle = new RectangleF(3, 3, 1.6f, 0.4f);
    pushBtn.BorderWidth = 2;
    pushBtn.ActionType =
         PDFFormFieldActionType.Submit_Action;
    pushBtn.SubmitUrl =
         // Update! Update! Update!
        "http://localhost/save_pdf_forms/save_pdf.ashx";
    pushBtn.NormalCaption = "Submit";
    pushBtn.FieldName = "PshBtn1";
    pushBtn.BorderColor = Color.Black;
    pushBtn.SubmitActionType = PDFSubmitActionType.HTTP_Post;

    // Add form fields to the document
    doc.AddFormField(txtFld);
    doc.AddFormField(cmbBox);
    doc.AddFormField(pushBtn);

    // Write the forms document to the browser
    context.Response.ContentType = "application/pdf";
    doc.Save(context.Response.OutputStream);
    context.Response.Flush();

    // Close the document
    doc.Close();

  }

  public bool IsReusable {
    get {
      return false;
    }
  }
}
An animation showing the generation of "personalized" PDF forms using a ASP.NET handler.

On my computer, this handler was hosted at the URL http://localhost/save_pdf_forms/save_pdf.ashx. When I access the URL, the handler generates the PDF form and writes it to the browser. The PDF form has three form fields - a text box, combo box and a submit button. No text is placed as default in the text box field. Similarly, no option is selected in the combo box form field. The submit button is set to post the form data to the same URL. When I fill the form and submit it, the handler captures the submit data and generates another PDF form. In this new form, the data I had previously submitted is set as default options in the form and I have a personalized copy of the original PDF. Any other person who can access to the URL can create his/her own copy too. The personalized copy can be filled again with different data and then submitted to create a different version of the PDF.

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.