Skip to main content

Count form fields

Counts the number of form fields in a document and categorizes them by type.

(function () {
// Get the document object.
let doc = Api.GetDocument();

// Retrieve all form fields from the document.
let forms = doc.GetAllForms();

// Initialize counters.
let totalFields = forms.length;
let fieldTypes = {
textForm: 0,
pictureForm: 0,
checkboxForm: 0,
comboBoxForm: 0,
radioButtonForm: 0,
complexForm: 0,
other: 0
};

// Iterate through all form fields.
for (let i = 0; i < totalFields; i++) {
let formField = forms[i];
let formType = formField.GetFormType(); // e.g., "textForm", "pictureForm", etc.

// Increment the corresponding counter if known, else count as "other".
if (fieldTypes.hasOwnProperty(formType)) {
fieldTypes[formType]++;
} else {
fieldTypes.other++;
}
}

// Create a result message.
let result =
"Total Form Fields: " + totalFields + "\n\n" +
"Field Type Breakdown:\n" +
"- Text Forms: " + fieldTypes.textForm + "\n" +
"- Picture Forms: " + fieldTypes.pictureForm + "\n" +
"- Checkbox Forms: " + fieldTypes.checkboxForm + "\n" +
"- Combo Box Forms: " + fieldTypes.comboBoxForm + "\n" +
"- Radio Button Forms: " + fieldTypes.radioButtonForm + "\n" +
"- Complex Forms: " + fieldTypes.complexForm + "\n" +
"- Other Form Fields: " + fieldTypes.other;

// Uncomment the below block if you want the results inserted into the document.
let summaryParagraph = Api.CreateParagraph();
summaryParagraph.AddText(result);
summaryParagraph.SetFontSize(20);
summaryParagraph.SetBold(true);
summaryParagraph.SetJc("center");

doc.Push(summaryParagraph);
})();

Methods used: GetDocument, GetAllForms, GetFormType, CreateParagraph, AddText, SetFontSize, SetBold, SetJc, Push, Save

Result

FormFieldCounter FormFieldCounter