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

Generate PDF Forms In ASP.NET Using PDFOne .NET v3

Learn how to generate PDF forms in Web applications using PDFOne .NET.
By V. Subhash

AcroForms or PDF forms are one of the reasons why PDF documents are so popular on the Internet. As they have a uniform appearance when they are viewed on a computer and when they are printed on paper, AcroForms are favored by government departments and other institutions.

Usually, when a PDF forms document is submitted online, the Web server that accepts the form simply stores the submitted values to a database. Some organizations save the submitted values back to a copy of the original form and then store the whole document in their database. This latter approach allows them to use such filled-in forms alongside printed forms that were simply scanned and stored as images.

Serving PDF Forms On The Web

In a Web application, you can serve either static (pre-built) or dynamically generated PDF forms. If you are serving static PDF forms, then you need to simply store the PDF forms document in some public location on your server and provide the link to that file on a web page.

If you need to serve dynamically generated PDF files, then you would not want to store it temporarily on the server. Instead, you would prefer to simply write the PDF document as a binary stream to the browser. In this article, we will see more about this.

PDF Forms Generation In Web Applications

To dynamically serve PDF forms in ASP.NET, you need to have the PDF forms document as a memory stream object and then write that memory stream to your ASPX page's response stream (System.Web.Page.Response property, an object of the type System.Web.HttpResponse.OutputStream.)

PDFOne .NET allows you to save PDF documents to files, memory streams, and byte arrays. So, the solution would be to use the Gnostice.PDFOne.PDFDocument.Save(Stream) method to write the forms document to the ASPX page's HTTP response stream.

PDFOne .NET Example

To create a sample C# PDF forms-generating Web application, perform the steps mentioned below.

  1. Create a new Web site application in Visual Studio.
  2. Add Gnostice.PDFOne.dll as a reference to the project.
  3. Add a button to the default.aspx page.
  4. Double-click the button to switch to the code view.
  5. Add the following Using directives.
    using System.Drawing;
    
    using Gnostice.PDFOne;
    
  6. In the button-click event that was generated, add code as shown below.
    protected void Button1_Click(object sender, EventArgs e) {
      // Create a document
      PDFDocument doc =
          new PDFDocument("your-license-key");
      
      doc.ApplyFormAppearances = true;
      doc.OpenAfterCreate = false;  
      
      // Following code adds form fields to the document.
      // For dynamic PDF generation, the choice of the form 
      // fields should be based on the requirements of your 
      // application at run-time.
      
      // Write labels
      doc.WriteText("Enter your name", 1, 1);
      doc.WriteText("Select your country", 1, 2);
      
      // Create a text field
      PDFFormTextField txtFld = new PDFFormTextField();
      // Location of the form field
      txtFld.Rectangle = new RectangleF(3, 1, 1.6f, 0.25f);
      txtFld.FieldValue = "John Doe";
      txtFld.ToolTip = "Enter your name here";
      // Specify a name for the form field. (To obtain their values,
      // form fields are identified by their name.)
      txtFld.FieldName = "TxtFld1";
      txtFld.BorderColor = Color.Black;
      txtFld.NameAsUnicode = false;
      // Add the text field to the document
      doc.AddFormField(txtFld);
      
      // Create a list box
      PDFFormListBox lstBox = new PDFFormListBox();
      lstBox.Rectangle = new RectangleF(3, 2, 0.6f, 0.5f);
      lstBox.AddItem("India");
      lstBox.AddItem("Germany");
      lstBox.AddItem("Russia");
      lstBox.SelectedItemIndex = 0;
      lstBox.FieldName = "LstBox1";
      lstBox.NameAsUnicode = false;
      lstBox.BorderColor = Color.Black;
      // Add the list box to the document
      doc.AddFormField(lstBox);
      
      // Create the push button and set it to submit
      // form contents to a website URL that will
      // process the form contents
      PDFFormPushButton pushBtn = new PDFFormPushButton();
      pushBtn.Rectangle = new RectangleF(3, 3, 1.6f, 0.4f);
      pushBtn.BorderWidth = 2;
      // Specify that the form must be submitted when
      // the button is clicked.
      pushBtn.ActionType =
           PDFFormFieldActionType.Submit_Action;
      // Specify the web page to which the form is
      // submitted.
      pushBtn.SubmitUrl =
          "http://www.gnostice.com/newsletters" +
          "/demos/200804/forms_test.asp";
      pushBtn.NormalCaption = "Submit";
      pushBtn.FieldName = "PshBtn1";
      pushBtn.BorderColor = Color.Black;
      pushBtn.NameAsUnicode = false;
      // Specify form submission method
      pushBtn.SubmitActionType = PDFSubmitActionType.HTTP_Post;
      // Add the push button to the document
      doc.AddFormField(pushBtn);
      
      // Get the page's output stream ready
      Response.Clear();
      Response.BufferOutput = true;
      // Make the browser display the forms document
      // using a PDF plug-in. (If no plug in is available, 
      // the browser will show the File -> Save As dialog box.
      Response.ContentType = "application/pdf"; 
    
      // Write the forms document to the browser
      doc.Save(Response.OutputStream);
      
      doc.Close();
      doc.Dispose();
      
      Response.Flush();
      Response.Close();
    }
    

Here is the output when you run the application.

---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.