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

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