FormFields Property
Summary
Object that manages the form fields of the document.
Example
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Document.Writer;
using Leadtools.Document;
using Leadtools.Caching;
using Leadtools.Annotations.Engine;
using Leadtools.Ocr;
using Leadtools.Barcode;
using Leadtools.Document.Converter;
public void DocumentFormFieldsExample(string pdfFile)
{
// The value of LoadDocumentOptions.LoadFormFieldsMode affects loading the documents as follows:
//
// 1. Normal PDF without form fields: Will not be affected.
// 2. Normal PDF with fields:
// LoadFormFieldsMode = View, then the form fields will be rendered as part of the view without the ability to retrieve or update the value
// LoadFormFieldsMode = Interactive, then the form fields will be parsed to LEADDocument.FormFields
// Load the document with interactive form fields mode
Console.WriteLine($"Loading with DocumentLoadFormFieldsMode.Interactive");
var loadDocumentOptions = new LoadDocumentOptions();
loadDocumentOptions.LoadFormFieldsMode = DocumentLoadFormFieldsMode.Interactive;
using (LEADDocument document = DocumentFactory.LoadFromFile(pdfFile, loadDocumentOptions))
{
Console.WriteLine($"Document has {document.Pages.Count} pages");
// Find out if the document has parsed form fields
bool hasFormFields = document.FormFields.HasFormFields;
Console.WriteLine($"HasFormFields:{hasFormFields}");
// Extract all the form fields
DocumentFormFieldsContainer[] containers = document.FormFields.GetFormFields();
if (containers == null || containers.Length == 0)
{
Console.WriteLine($"Empty Containers");
return;
}
foreach (DocumentFormFieldsContainer container in containers)
{
// Show info on this container
Console.WriteLine($"Page Number {container.PageNumber}, Form fields count {container.Children?.Count}");
foreach (DocumentFormField formField in container.Children)
{
StringBuilder formFieldInfo = new StringBuilder();
formFieldInfo.Append($"ID : {(string.IsNullOrEmpty(formField.ID) ? string.Empty : formField.ID)}");
formFieldInfo.Append($"Name : {(string.IsNullOrEmpty(formField.Name) ? string.Empty : formField.Name)}");
formFieldInfo.Append($"Bounds : {formField.Bounds}");
formFieldInfo.Append($"BackgroundColor : {formField.BackgroundColor}");
formFieldInfo.Append($"Is Printable : {formField.Printable}");
formFieldInfo.Append($"Is Viewable : {formField.Viewable}");
formFieldInfo.Append($"Is Locked : {formField.Locked}");
formFieldInfo.Append($"Is Required : {formField.Required}");
formFieldInfo.Append($"Is ReadOnly : {formField.ReadOnly}");
formFieldInfo.Append($" ");
// DocumentFormFieldBorderStyle reference
formFieldInfo.Append($"Border Style : {formField.BorderStyle.Style}");
formFieldInfo.Append($"Border Color : {formField.BorderStyle.Color}");
formFieldInfo.Append($"Border Width : {formField.BorderStyle.Width}");
formFieldInfo.Append($" ");
// DocumentFormFieldTextStyle reference
formFieldInfo.Append($"Font Name : {formField.TextStyle.FontName}");
formFieldInfo.Append($"Font Size : {formField.TextStyle.FontSize}");
formFieldInfo.Append($"Text Color : {formField.TextStyle.Color}");
formFieldInfo.Append($"Text Alignment : {formField.TextStyle.TextAlignment}");
formFieldInfo.Append($" ");
formFieldInfo.Append($"Type : {formField.Type}");
if (formField is DocumentTextFormField)
{
DocumentTextFormField textFormField = formField as DocumentTextFormField;
formFieldInfo.Append($"Text Value : {(string.IsNullOrEmpty(textFormField.Value) ? string.Empty : textFormField.Value)}");
formFieldInfo.Append($"Content Type : {textFormField.ContentType}");
formFieldInfo.Append($"Max Length : {textFormField.MaxLength}");
formFieldInfo.Append($"Multiline : {textFormField.Multiline}");
formFieldInfo.Append($"Is Password : {textFormField.IsPassword}");
formFieldInfo.Append($"Is Comb : {textFormField.IsComb}");
}
else if (formField is DocumentChoiceFormField)
{
DocumentChoiceFormField choiceFormField = formField as DocumentChoiceFormField;
formFieldInfo.Append($"Options Display Value : {(choiceFormField.OptionsDisplayValue == null ? string.Empty : string.Join(",", choiceFormField.OptionsDisplayValue))}");
formFieldInfo.Append($"Options Exported Value : {(choiceFormField.OptionsExportedValue == null ? string.Empty : string.Join(",", choiceFormField.OptionsExportedValue))}");
formFieldInfo.Append($"Selected Indices : {(choiceFormField.SelectedIndices == null ? string.Empty : string.Join(",", choiceFormField.SelectedIndices))}");
formFieldInfo.Append($"MultiSelect : {choiceFormField.MultiSelect}");
formFieldInfo.Append($"Choice Type : {(choiceFormField.ChoiceType == DocumentChoiceFormField.ChoiceType_List ? "List" : "ComboBox")}");
}
else if (formField is DocumentButtonFormField)
{
DocumentButtonFormField buttonFormField = formField as DocumentButtonFormField;
formFieldInfo.Append($"Is Checked : {buttonFormField.IsChecked}");
formFieldInfo.Append($"Button Type : {(buttonFormField.ButtonType == DocumentButtonFormField.ButtonType_CheckBox ? "CheckBox" : "RadioButton")}");
}
formFieldInfo.Append($"==========================================================================");
Console.Write(formFieldInfo.ToString());
}
}
// The following sample shows how to update a Document Text Form Field Value in the first document page
// Use DocumentButtonFormField.IsChecked to update DocumentButtonFormField values
// Use DocumentChoiceFormField.SelectedIndices to update DocumentChoiceFormField selected items
const int pageNumber = 1;
foreach (var formField in containers[pageNumber].Children)
{
DocumentTextFormField textFormField = formField as DocumentTextFormField;
if (textFormField != null)
{
textFormField.Value = "PDF Forms";
break;
}
}
// Update the form fields containers
// DocumentFormFields reference
document.FormFields.SetFormFields(containers);
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import org.junit.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import static org.junit.Assert.*;
import leadtools.*;
import leadtools.annotations.engine.*;
import leadtools.barcode.*;
import leadtools.caching.*;
import leadtools.codecs.*;
import leadtools.document.*;
import leadtools.document.DocumentMimeTypes.UserGetDocumentStatusHandler;
import leadtools.document.converter.*;
import leadtools.document.writer.*;
import leadtools.ocr.*;
public void documentFormFieldsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
String pdfFile = combine(LEAD_VARS_IMAGES_DIR, "leadtools.pdf");
// The value of LoadDocumentOptions.LoadFormFieldsMode affects loading the
// documents as follows:
//
// 1. Normal PDF without form fields: Will not be affected.
// 2. Normal PDF with fields:
// LoadFormFieldsMode = View, then the form fields will be rendered as part of
// the view without the ability to retrieve or update the value
// LoadFormFieldsMode = Interactive, then the form fields will be parsed to
// LEADDocument.FormFields
// Load the document with interactive form fields mode
System.out.println("Loading with DocumentLoadFormFieldsMode.Interactive");
LoadDocumentOptions loadDocumentOptions = new LoadDocumentOptions();
loadDocumentOptions.setLoadFormFieldsMode(DocumentLoadFormFieldsMode.INTERACTIVE);
LEADDocument document = DocumentFactory.loadFromFile(pdfFile, loadDocumentOptions);
System.out.println("Document has {document.Pages.Count} pages");
// Find out if the document has parsed form fields
boolean hasFormFields = document.getFormFields().hasFormFields();
System.out.println("HasFormFields:" + hasFormFields);
// Extract all the form fields
DocumentFormFieldsContainer[] containers = document.getFormFields().getFormFields();
if (containers == null || containers.length == 0) {
System.out.println("Empty Containers");
return;
}
for (DocumentFormFieldsContainer container : containers) {
// Show info on this container
System.out.println("Page Number {container.PageNumber}, Form fields count " + container.getChildren().size());
for (DocumentFormField formField : container.getChildren()) {
StringBuilder formFieldInfo = new StringBuilder();
if (formField.getID() == null || formField.getID().equals("")) {
formFieldInfo.append("ID : ");
} else {
formFieldInfo.append("ID : " + formField.getID());
}
if (formField.getName() == null || formField.getName().equals("")) {
formFieldInfo.append("Name : ");
} else {
formFieldInfo.append("Name : " + formField.getName());
}
formFieldInfo.append("Bounds : " + formField.getBounds());
formFieldInfo.append("BackgroundColor : " + formField.getBackgroundColor());
formFieldInfo.append("Is Printable : " + formField.getPrintable());
formFieldInfo.append("Is Viewable : " + formField.getViewable());
formFieldInfo.append("Is Locked : " + formField.getLocked());
formFieldInfo.append("Is Required : " + formField.getRequired());
formFieldInfo.append("Is ReadOnly : " + formField.getReadOnly());
formFieldInfo.append(" ");
// DocumentFormFieldBorderStyle reference
formFieldInfo.append("Border Style : " + formField.getBorderStyle().getStyle());
formFieldInfo.append("Border Color : " + formField.getBorderStyle().getColor());
formFieldInfo.append("Border Width : " + formField.getBorderStyle().getWidth());
formFieldInfo.append(" ");
// DocumentFormFieldTextStyle reference
formFieldInfo.append("Font Name : " + formField.getTextStyle().getFontName());
formFieldInfo.append("Font Size : " + formField.getTextStyle().getFontSize());
formFieldInfo.append("Text Color : " + formField.getTextStyle().getColor());
formFieldInfo.append("Text Alignment : " + formField.getTextStyle().getTextAlignment());
formFieldInfo.append(" ");
formFieldInfo.append("Type : " + formField.getType());
if (formField.getClass().equals(DocumentTextFormField.class)) {
DocumentTextFormField textFormField = (DocumentTextFormField) formField;
if (textFormField.getValue() == null || textFormField.getValue().equals("")) {
formFieldInfo.append("Text Value : ");
} else {
formFieldInfo.append("Text Value : " + textFormField.getValue());
}
formFieldInfo.append("Content Type : " + textFormField.getContentType());
formFieldInfo.append("Max Length : " + textFormField.getMaxLength());
formFieldInfo.append("Multiline : " + textFormField.getMultiline());
formFieldInfo.append("Is Password : " + textFormField.isPassword());
formFieldInfo.append("Is Comb : " + textFormField.isComb());
} else if (formField.getClass().equals(DocumentChoiceFormField.class)) {
DocumentChoiceFormField choiceFormField = (DocumentChoiceFormField) formField;
if (choiceFormField.getOptionsDisplayValue() == null
|| choiceFormField.getOptionsDisplayValue().equals("")) {
formFieldInfo.append("Options Display Value : ");
} else {
formFieldInfo.append(
"Options Display Value : " + String.join(",", choiceFormField.getOptionsDisplayValue()));
}
if (choiceFormField.getOptionsExportedValue() == null
|| choiceFormField.getOptionsExportedValue().equals("")) {
formFieldInfo.append("Options Exported Value : ");
} else {
formFieldInfo.append(
"Options Exported Value : " + String.join(",", choiceFormField.getOptionsExportedValue()));
}
if (choiceFormField.getSelectedIndices() == null || choiceFormField.getSelectedIndices().equals("")) {
formFieldInfo.append("Selected Indices : ");
} else {
List<String> strings = new ArrayList<String>();
for (int num : choiceFormField.getSelectedIndices()) {
strings.add(String.valueOf(num));
}
formFieldInfo.append("Selected Indices : " + String.join(",", strings));
}
formFieldInfo.append("MultiSelect : " + choiceFormField.getMultiSelect());
if (choiceFormField.getChoiceType() == DocumentChoiceFormField.CHOICE_TYPE_LIST) {
formFieldInfo.append("Choice Type : List");
}
formFieldInfo.append("Choice Type : ComboBox");
} else if (formField.equals(DocumentButtonFormField.class)) {
DocumentButtonFormField buttonFormField = (DocumentButtonFormField) formField;
formFieldInfo.append("Is Checked : " + buttonFormField.isChecked());
if (buttonFormField.getButtonType() == DocumentButtonFormField.BUTTON_TYPE_CHECK_BOX) {
formFieldInfo.append("Button Type : CheckBox");
} else {
formFieldInfo.append("Button Type : RadioButton");
}
}
formFieldInfo.append("==========================================================================");
System.out.println(formFieldInfo.toString());
}
}
// The following sample shows how to update a Document Text Form Field Value in
// the first document page
// Use DocumentButtonFormField.IsChecked to update DocumentButtonFormField
// values
// Use DocumentChoiceFormField.SelectedIndices to update DocumentChoiceFormField
// selected items
final int pageNumber = 1;
for (DocumentFormField formField : containers[pageNumber].getChildren()) {
DocumentTextFormField textFormField = (DocumentTextFormField) formField;
if (textFormField != null) {
textFormField.setValue("PDF Forms");
break;
}
}
// Update the form fields containers
// DocumentFormFields reference
document.getFormFields().setFormFields(containers);
}