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

Creating User-Friendly Barcode PDF Form Fields In .NET

Create PDF forms that accept data entry using regular fonts but subsequently display data using barcode fonts.
By V. Subhash

PDFOne .NET supports barcode fonts. Barcode fonts can be used to render PDF page elements such as text and form fields.

Many barcode-based applications used an asterisk (*) as the start and stop character of the barcode. If you are displaying barcodes in PDF forms, then entering the asterisks can be a hassle. Fortunately, you can use JavaScript commands to the document so that viewer applications, such as Adobe Reader, will automatically add and remove the asterisks.

In the following code snippet, a text form field is created. Two JavaScript actions are added to the text field. These JavaScript actions are represented by JavaScript scripts. These scripts will be executed by a viewer application when the user navigates in and out of the form field.

// Create a PDF document
PDFDocument pdfDocument1 
    = new PDFDocument("your license key");

// Create a barcode PDF font
PDFFont fontBarCode
    = new PDFFont(new Font("IDAutomationHC39M", 18, FontStyle.Regular),
                  PDFFontEmbedType.Full);

String sNL = "\r\n";
pdfDocument1.WriteText("Bar Code", 2.0f, 1.8f);

// Create a text form field
PDFFormTextField PDFFormTextField1 = new PDFFormTextField();
PDFFormTextField1.FieldName = "textfield1";
PDFFormTextField1.Font = fontBarCode;
PDFFormTextField1.Rectangle = new Rectangle(2, 2, 4, 1);
PDFFormTextField1.BorderWidth = 1;
PDFFormTextField1.BorderColor = Color.LightGray;
PDFFormTextField1.BackgroundColor = Color.White;
PDFFormTextField1.NameAsUnicode = false;

// JavaScript script for the "on gain focus" 
// event of the text form field
String sGotFocusJS
  = "var box = this.getField(\"textfield1\");" + sNL +
    "box.textFont=\"CourierNew\";" + sNL +
    "if (box.valueAsString.length) {" + sNL +
    "  if (box.valueAsString.substr(0, 1) == \"*\") {" + sNL +
    "     box.value = box.valueAsString.substr(1,box.valueAsString.length-1);" + sNL +
    "  }" + sNL +
    "  if (box.valueAsString.substr(box.valueAsString.length-1, 1) == \"*\") {" + sNL +
    "    box.value = box.valueAsString.substr(0,box.valueAsString.length-1);" + sNL +
    "  }" + sNL +
    "}" + sNL;

// JavaScript script for the "on lose focus" 
// event of the text form field
String sLostFocusJS
  = "var box = this.getField(\"textfield1\");" + sNL +
    "if (box.valueAsString.length) {" + sNL +
    "  if (box.valueAsString.substr(0, 1) != \"*\") {" + sNL +
    "    box.value = \"*\" + box.valueAsString;" + sNL +
    "  }" + sNL +
    "  if (box.valueAsString.substr(box.valueAsString.length-1, 1) != \"*\") {" + sNL +
    "    box.value = box.valueAsString + \"*\";" + sNL +
    "  }" + sNL +
    "}" + sNL +
    "box.textFont=\"IDAutomationHC39M\";";

// Add JavaScript scripts as JavaScript actions for the text form field
PDFFormTextField1.AddAction(PDFFormFieldAction.GotFocus, sGotFocusJS);
PDFFormTextField1.AddAction(PDFFormFieldAction.LostFocus, sLostFocusJS);

// Add text form field to document
pdfDocument1.AddFormField(PDFFormTextField1);

// Create a submit push button form field
PDFFormPushButton PDFFormPushButton1 = new PDFFormPushButton();
PDFFormPushButton1.Rectangle = new RectangleF(2f, 3f, 4f, 0.2f);
PDFFormPushButton1.BorderWidth = 1;
PDFFormPushButton1.FieldName = "submit";
PDFFormPushButton1.ActionType = PDFFormFieldActionType.Submit_Action;
PDFFormPushButton1.SubmitActionType = PDFSubmitActionType.HTTP_Post;
PDFFormPushButton1.SubmitUrl
    = "http://www.gnostice.com/newsletters/demos/200804/forms_test.asp";
PDFFormPushButton1.NormalCaption = "Submit";
// Add push button to document
pdfDocument1.AddFormField(PDFFormPushButton1);

// Save document to a file
pdfDocument1.ApplyFormAppearances = true;
pdfDocument1.Save("barcode_js_example.pdf");
pdfDocument1.Close();
pdfDocument1.Dispose();

Here are some notes about the above code.

When the user enters data, the viewer application removes the asterisks. The viewer application will also use a regular font to display the form field value.

// Access the form field
var box = this.getField("textfield1");
// Set form field font to a regular font
box.textFont = "CourierNew";
// Strip any asterisk padding
if (box.valueAsString.length) {
  if (box.valueAsString.substr(0, 1) == "*") {
     box.value = box.valueAsString.substr(1,box.valueAsString.length-1);
  }
  if (box.valueAsString.substr(box.valueAsString.length-1, 1) == "*") {
    box.value = box.valueAsString.substr(0,box.valueAsString.length-1);
  }
}

When the user exits form field, the viewer application adds the asterisks and also changes the form field font.

// Access the form field
var box = this.getField("textfield1");
// Add asterisks padding
if (box.valueAsString.length) {
  if (box.valueAsString.substr(0, 1) != "*") {
    box.value = "*" + box.valueAsString;
  }
  if (box.valueAsString.substr(box.valueAsString.length-1, 1) != "*") {
    box.value = box.valueAsString + "*";
  }
}
// Set form field font to a barcode font
box.textFont="IDAutomationHC39M";

For more information on using JavaScript in your PDF documents, download:

* - With the TTF version of the font, there were some problems with Adobe Reader 6. Adobe Reader 9 handles the font well.

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