PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2008 VS 2005 CLR 2.0

How To Generate PDF Forms In ASP.NET

PDFOne .NET sure generates PDF forms documents but how do you send them to a browser?
By Raju Sinha
Important

The information provided in this article has become outdated. Please refer the article Generate PDF Forms In ASP.NET Using PDFOne .NET v3 for up-to-date information.

In this article, we will see how to generate PDF forms in a Web application using PDFOne .NET.

Usually, most applications generate PDF forms and save it to a file. If this is done by a Web application, the forms document will be simply sitting in a folder on the server. So, how can the Web application send the document to the browser?

The trick is to save the document to a memory stream and render that memory stream using the System.Web.HttpResponse.BinaryWrite(byte[] buffer) method.

To try it out, create a new C# Web application and add a button to the Default.aspx page. Next, add the following code to the Button_Click event of the button. Finally, run the application.

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

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

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.FieldValue = "Enter Name";
txtFld.ToolTip = "Enter Your Name Here";
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;
pushBtn.ActionType = 
     PDFFormFieldActionType.Submit_Action;
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;
// Add the push button to the document
doc.AddFormField(pushBtn);

// Save the document to a memory stream
MemoryStream ms = new MemoryStream();
doc.Save(ms);
doc.Close();

// Copy the memory stream to a byte array
byte[] ba = ms.ToArray();
ms.Close();

// Write the byte array containing the PDF forms
// document to the browser
Response.Clear();
Response.ContentType = "application/pdf";
Response.BinaryWrite(ba);
Response.Flush();
Response.Close();

---o0O0o---

Our Developer Tools
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.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

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

XtremeDocumentStudio .NET

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

PDFOne (for Java™)

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

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

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

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).