DocumentFormFields Class (original) (raw)

Summary

Manages the form fields of the document.

Syntax

[DataContractAttribute()] public class DocumentFormFields

public: [SerializableAttribute, DataContractAttribute] ref class DocumentFormFields

class DocumentFormFields: 

Example

This example shows how to extract information from a PDF Form Field compatible PDF file. Checks whether the document has any form fields.

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); } }