How to perform calculations on specific fields in a PDF form using an ONLYOFFICE macro
ONLYOFFICE macros are powerful and versatile, making them useful not only for automating tasks in documents, spreadsheets, and presentations but also in PDF forms. In this blog post, we’ll show you how to create and run a unique macro that performs calculations on specific fields within a form. In this example, the macro helps to calculate taxes, making the process faster and more accurate.
Building the macro
Retrieving all the forms from the document
First we retrieve all the fields from the document:
// Get all forms from the document
const document = Api.GetDocument();
let forms = document.GetAllForms();
- Api.GetDocument() gets the active document.
- GetAllForms() retrieves all form fields in the document
Defining form keys
Then, we define the form keys:
// Formkeys of input forms
var formkey1 = "Form1";
var formkey2 = "Form2";
var taxFormkey = "TaxForm";
// Formkeys of result forms
var sumResultKey = "SumResult";
var taxResultKey = "TaxResult";
- formkey1, formkey2, and taxFormkey store the identifiers of the input fields.
- sumResultKey and taxResultKey store the identifiers of the output fields where the results will be inserted.
Retrieving form values
The getFormValue function iterates through forms to find a form matching formKey:
// Function to get the value of a form by its key
function getFormValue(formKey) {
for (let form of forms) {
if (form.GetFormKey() === formKey) {
return parseFloat(form.GetText()) || 0;
}
}
}
- GetText() retrieves the text value of the form.
- parseFloat() converts the text to a number.
If conversion fails, it defaults to 0 to prevent errors in calculations.
Inserting values into forms
The setFormValue function searches for a form field matching formKey. The SetText() method updates the field with a formatted numeric value (2 decimal places).
// Function to set the value of a result form
function setFormValue(formKey, value) {
for (let form of forms) {
if (form.GetFormKey() === formKey) {
form.SetText(value.toFixed(2));
}
}
}
Main calculation
Inside the main calculation function we:
- Retrieve user input values from form fields.
- Compute the sum of input1 and input2.
- Calculate tax as a percentage of the sum.
- Call setFormValue() to insert results into their respective fields.
// Main calculation function
function calculateAndInsert() {
let input1 = getFormValue(formkey1);
let input2 = getFormValue(formkey2);
let taxInput = getFormValue(taxFormkey);
// Perform calculations
var sum = parseFloat(input1) + parseFloat(input2);
var tax = sum * taxInput / 100; // % tax
// Insert results
setFormValue(sumResultKey, sum);
setFormValue(taxResultKey, tax);
}
The entire macro code is the following:
(function () {
// Get all forms from the document
const document = Api.GetDocument();
let forms = document.GetAllForms();
// Formkeys of input forms
var formkey1 = "Form1";
var formkey2 = "Form2";
var taxFormkey = "TaxForm";
// Formkeys of result forms
var sumResultKey = "SumResult";
var taxResultKey = "TaxResult";
// Function to get the value of a form by its key
function getFormValue(formKey) {
for (let form of forms) {
if (form.GetFormKey() === formKey) {
return parseFloat(form.GetText()) || 0;
}
}
}
// Function to set the value of a result form
function setFormValue(formKey, value) {
for (let form of forms) {
if (form.GetFormKey() === formKey) {
form.SetText(value.toFixed(2));
}
}
}
// Main calculation function
function calculateAndInsert() {
let input1 = getFormValue(formkey1);
let input2 = getFormValue(formkey2);
let taxInput = getFormValue(taxFormkey);
// Perform calculations
var sum = parseFloat(input1) + parseFloat(input2);
var tax = sum * taxInput / 100; // % tax
// Insert results
setFormValue(sumResultKey, sum);
setFormValue(taxResultKey, tax);
}
// Call the calculation function
calculateAndInsert();
})();
Now, let’s run the macro and see how it works!
At ONLYOFFICE, we strive to provide users with versatile tools tailored to their needs. Macros are powerful additions to your toolkit, and we hope this one becomes a valuable asset for you.
We also encourage you to explore our extensive library of API methods and create your own macros. If you have macros or ideas to share, feel free to reach out – we’re always open to suggestions and collaboration. Best of luck in your exploratory endeavors!
Create your free ONLYOFFICE account
View, edit and collaborate on docs, sheets, slides, forms, and PDF files online.