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

Use Visual Studio To Create and Design PDF Forms (AcroForms) Documents

A novel way of designing PDF documents with Visual Studio, using Windows forms as document pages and Windows controls as PDF form fields.
By M. V. Niranjan

PDF form elements or form fields are not very different from the basic Windows controls, such as labels, text boxes, and buttons that you see in Microsoft Visual Studio®.

And, if you've been thinking if only there was a way you could create PDF forms documents with the same ease as creating Windows forms applications in Visual Studio, well... here is the solution!

For this article, I created a C# project (offered as a download below) that creates PDF forms documents based on labels, text boxes, and buttons placed on a Windows form in Visual Studio.

When you run the project, what you see is a Windows form with some text boxes, buttons and labels. When the form is closed, the forms document creation code in the event gets executed. Each text box in the form is rendered as a text form field in the output PDF document. Buttons are converted to forms push buttons. Labels are rendered as formatted text in the PDF document.

Code Snippet

private void Form1_Closed(object sender, System.EventArgs e)
{
 // Create a PDF document object
 PDFDocument doc = new PDFDocument();
 // Creates a PDF page
 PDFPage page = new PDFPage(this.Width,
                            this.Height,
                            PDFMeasurementUnit.Points);
 page.MeasurementUnit = PDFMeasurementUnit.Points;

 // Parses controls on the form
 for (int i = 0; i < this.Controls.Count; i++)
 {
  
  Label label = this.Controls[i] as Label;
  if (label != null)
  {
   // Renders labels as formatted text
   page.WriteText(label.Text,
                  new PDFFont(new Font(label.Font.Name,
                                       label.Font.Size),
                                       false),
                  label.Left,
                  label.Top);
   continue;
  }
  
  TextBox text = this.Controls[i] as TextBox;
  if (text != null)
  {
   // Renders text boxes as text form fields
   TextBox text = this.Controls[i] as TextBox;

   PDFFormTextField field = new PDFFormTextField();
   field.BackgroundColor = text.BackColor;
   field.BorderColor = Color.Black;
   field.Font = new PDFFont(new Font(text.Font.Name,
                                     text.Font.Size),
                            false);
   field.FieldName = text.Name;
   field.BorderWidth = 1;
   field.FieldValue = text.Text;
   field.Rectangle = new RectangleF(text.Left,
                                    text.Top,
                                    text.Width,
                                    text.Height);
   field.Alignment = PDFTextAlignment.Left;
   field.Multiline = text.Multiline;
   field.Visible = text.Visible;
   page.AddFormField(field);
   continue;
  }

  Button button = this.Controls[i] as Button;
  if (button != null)
  {
   // Renders buttons as push button form fields
   Button button = this.Controls[i] as Button;

   PDFFormPushButton field = new PDFFormPushButton();
   field.BackgroundColor = button.BackColor;
   field.BorderColor = Color.Black;
   field.Font = new PDFFont(new Font(button.Font.Name,
                                     button.Font.Size),
                            false);
   field.FieldName = button.Name;
   field.BorderWidth = 1;
   field.Rectangle = new RectangleF(button.Left,
                                    button.Top,
                                    button.Width,
                                    button.Height);
   field.NormalCaption = button.Text;
   field.Visible = button.Visible;
   page.AddFormField(field);
   continue;
  }
 }

 // Adds page to PDF document
 doc.AddPage(page);
 // Writes PDF document to file and displays it
 doc.OpenAfterCreate = true;
 doc.ApplyFormAppearances = true;
 doc.Save("Output.pdf");
 doc.Close();
}

Screenshots

  1. Design A Windows Form in Visual Studio
  2. Run Form and Enter Values If Necessary
  3. View PDF in Adobe Reader®
---o0O0o---

Downloads:

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .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
Gnostice Document Studio 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
Gnostice Document Studio 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-2024 Gnostice Information Technologies Private Limited. All rights reserved.