Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Why is simple field notation with subtraction not working?

Engaged ,
Sep 08, 2017 Sep 08, 2017

I have set up a budget form and at the end I'm subtracting one manually entered field from another field with manually entered data.  This does nothing at all in Acrobat XI Pro. I got nothing, nada, no result.  In Acrobat DC Pro and DC Reader I get a result with like 8 decimal places (.00000000) and I can't see how to limit this to no decimal places. Now when I reopen in Acrobat XI, I am getting the same result as I got in DC, but only after a crash and restart of Acrobat.

I've even renamed the fields so they are super simple to enter... my equation "Barclay_Last-Barclay"

This should be a simple calculation -- have tried the "-" on above the keyboard and the "-" minus sign from the numpad. That makes no difference.  I have tried other calculations using *, for example, and they work fine.

Obviously something wrong here, but I don't think it's what I'm doing.  Is it because I have XI Pro and DC on the same Windows 7 machine?  Or ???

TOPICS
PDF forms
4.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
LEGEND ,
Sep 11, 2017 Sep 11, 2017

The "." is a special character and denotes a decimal place in a numeric value. It needs to be "Escaped" to indicate to the behind the scenes JavaScript that the "." is to be treated as a character and not a decimal point. Try:

Text4\.0\.6\.0 - Barclay

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 04, 2025 Feb 04, 2025

I may have a solution for you if you need to rename lots of field names.

My problem was a slightly different in that I wanted to change a field name with a "period" to an "underscore", since Adobe seems to struggle to do 'simplified field notation' calculations with fields that were named with a period, but not with an underscore.

so if i tried to do box.1 - box.2, nothing happened, but when renamed to box_1 and box_2, now the formula box_1 - box_2 in 'simplified field notation' works fine.

For some reason when you create multiple text boxes (such as when you right click on one field and then do 'create multiple copies..') it renames each one with a period, so if your first field is named "box", and you create 2 copies, it will rename them "box.1" and "box.2".

On windows, ctrl+J to open the javascript console, clear the console and paste this into the console, (the box one at the bottom). Do ctrl+A to select all of the script that you just pasted, and then ctrl+enter to execute it. It should rename the fields to "box_1" and "box_2" and so on.  Hope this can help someone.

Script:
// Loop backwards so that removal doesn't affect the loop indices
for (var i = this.numFields - 1; i >= 0; i--) {
var origName = this.getNthFieldName(i);
if (origName.indexOf('.') !== -1) {
var newName = origName.replace(/\./g, '_'); // Replace all periods with underscores

// Get the original field
var origField = this.getField(origName);
if (!origField) continue; // safety check

// Create a new field with the same type, page, and rectangle
// Note: You may need to adjust parameters depending on your field type.
var newField = this.addField(newName, origField.type, origField.page, origField.rect);

// Copy basic properties. You may want to copy additional properties as needed.
newField.value = origField.value;
newField.defaultValue = origField.defaultValue;
newField.textSize = origField.textSize;
newField.multiline = origField.multiline;
newField.alignment = origField.alignment;
newField.readonly = origField.readonly;

// Copy any additional properties if needed:
// newField.font = origField.font; etc.

// Remove the original field
this.removeField(origName);

console.println("Renamed field '" + origName + "' to '" + newName + "'");
}
}

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 08, 2017 Sep 08, 2017

Does it not work for all entered values or just certain ones? If just certain ones, can you give some examples?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 11, 2017 Sep 11, 2017

Thanks for your interest.  My simple notation formula is this

Text4.0.6.0 - Barclay

Tried formatting all 3 fields (the calculation field, Barclay and Text4.0.6.0) as numerical, that did not help.  All data in these fields is positive, not negative.  These are the only Simple Notation calculations I'm using and none of them work.  Other calculations where value is Sum, work. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2017 Sep 11, 2017

That's a different field name than you showed before. If you have punctuation like that in the field name, you'll have to escape the characters like this:

Text4\.0\.6\.0 - Barclay

It's best to use only alphanumeric characters (plus underscore) for field names so you don't have to bother with this.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 11, 2017 Sep 11, 2017

Really.  This is the name that Acrobat automatically gives the fields when you create them using Create Multiple Copies.  Will try this and see if it helps.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 11, 2017 Sep 11, 2017

That worked!  Changed all field names from 4.0.1... to Barclay_B, Barclay_C, etc. 

Thank you so much.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2017 Sep 11, 2017

Most users that use the "create multiple copies" also tend to write custom JavaScript calculations for all sorts of reasons. Many users have found that the simplified field notation and "field is the _____ of the following fields" tend to fail on complex or lengthy forms. They also may write document level functions to process rows of data by calculating the field names and perform column functions. The use of document level functions can also improve performance of the forms since the code is only syntax checked once and tokenized once.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2017 Sep 11, 2017

Really.  This is the name that Acrobat automatically gives the fields when you create them using Create Multiple Copies.  Will try this and see if it helps.

I should have said: If you're going to use the Simplified Field Notation option for calculations, it's best to use only alphanumeric characters (plus underscore) for field names so you don' have to bother with this.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 11, 2017 Sep 11, 2017

Well thank you again. 

I would think that Adobe would not create autogenerated names that violate the rules for "Simple" Notation.  I Googled this problem, and didn't find any comments or warnings regarding the use of Special Characters in filenames...   I don't do Javascript (other than copying someone else's code) but I've been using multiple copies since at least Acrobat 6.  Calculations are another thing, I've never really done much with them, thought I would try a simple budget form. I work with forms all the time, just haven't had a need to do calculations. 

You were a big help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 10, 2017 Sep 10, 2017

You should be able to control the displayed number of decimal places using the format tab and setting the format to "Number" and the number of decimal places to ")". Note this will round the result of the calculation.

You may need to have a space before and after the minus sign. Simplified field notation uses spaces to parse the entry into field names, values and operands. The "-" can indicate subtraction or a negative value. The space after the "-" indicated one wants to subtract the value and not have it as a negative value.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 11, 2017 Sep 11, 2017

Thanks, 

I should have been able to figure out the decimal place.  Geez. Sometimes the brain is just not helping. 

I have included a space before and after but still my Simple Notation calculation is not working. 

Here is what I have written:

Text4.0.6.0 - Barclay

Tried formatting those fields as numerical, that did not help. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2017 Sep 11, 2017

The "." is a special character and denotes a decimal place in a numeric value. It needs to be "Escaped" to indicate to the behind the scenes JavaScript that the "." is to be treated as a character and not a decimal point. Try:

Text4\.0\.6\.0 - Barclay

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 04, 2025 Feb 04, 2025

I may have a solution for you if you need to rename lots of field names.

My problem was a slightly different in that I wanted to change a field name with a "period" to an "underscore", since Adobe seems to struggle to do 'simplified field notation' calculations with fields that were named with a period, but not with an underscore.

so if i tried to do box.1 - box.2, nothing happened, but when renamed to box_1 and box_2, now the formula box_1 - box_2 in 'simplified field notation' works fine.

For some reason when you create multiple text boxes (such as when you right click on one field and then do 'create multiple copies..') it renames each one with a period, so if your first field is named "box", and you create 2 copies, it will rename them "box.1" and "box.2".

On windows, ctrl+J to open the javascript console, clear the console and paste this into the console, (the box one at the bottom). Do ctrl+A to select all of the script that you just pasted, and then ctrl+enter to execute it. It should rename the fields to "box_1" and "box_2" and so on.  Hope this can help someone.

Script:
// Loop backwards so that removal doesn't affect the loop indices
for (var i = this.numFields - 1; i >= 0; i--) {
var origName = this.getNthFieldName(i);
if (origName.indexOf('.') !== -1) {
var newName = origName.replace(/\./g, '_'); // Replace all periods with underscores

// Get the original field
var origField = this.getField(origName);
if (!origField) continue; // safety check

// Create a new field with the same type, page, and rectangle
// Note: You may need to adjust parameters depending on your field type.
var newField = this.addField(newName, origField.type, origField.page, origField.rect);

// Copy basic properties. You may want to copy additional properties as needed.
newField.value = origField.value;
newField.defaultValue = origField.defaultValue;
newField.textSize = origField.textSize;
newField.multiline = origField.multiline;
newField.alignment = origField.alignment;
newField.readonly = origField.readonly;

// Copy any additional properties if needed:
// newField.font = origField.font; etc.

// Remove the original field
this.removeField(origName);

console.println("Renamed field '" + origName + "' to '" + newName + "'");
}
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Mar 10, 2025 Mar 10, 2025

Hi @Braptd,

 

Hope you are doing well. 

 

Thanks for writing about what worked for you.

Marking this as a correct answer for future users to use as reference.


Regards,
Souvik.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 13, 2025 Mar 13, 2025
LATEST

To update on this, i noticed that the previous script was clearing the Format tab option, so im using a different script now.  this script also has different renaming logic.  instead of changing a period to an underscore, it now changes the first field to text01, second field to text02, and so on.  you can change the prefix by modifying this line: 

var newName = "text" + fieldNumber;  // Change "text" to your desired prefix.  

tips:
To access JavaScript debugger, ctrl+j on windows, paste the script in the lower console, ctr+a to select it, and ctrl+enter to execute it.  Save a backup copy of your .pdf before running any scripts.

 

// Collect field information first
var fieldInfo = [];
for (var i = 0; i < this.numFields; i++) {
    var origName = this.getNthFieldName(i);
    var origField = this.getField(origName);
    
    // Store field details to track creation order
    fieldInfo.push({
        name: origName,
        page: origField.page,
        rect: origField.rect.toString(), // convert to string for reliable comparison
        type: origField.type
    });
}

// Sort fields by page first, then by vertical position (top to bottom)
fieldInfo.sort(function(a, b) {
    if (a.page !== b.page) return a.page - b.page;
    
    // Parse rectangle coordinates to compare vertical position
    var rectA = a.rect.split(',').map(Number);
    var rectB = b.rect.split(',').map(Number);
    
    // Compare top coordinates (index 1 in rectangle array)
    return rectB[1] - rectA[1];
});

// Function to pad numbers with leading zeros
function padNumber(num) {
    return (num < 10) ? "0" + num : "" + num;
}

// Function to copy field format
function preserveFieldFormat(origField, newField) {
    try {
        // For text fields, check if there's special formatting (numbers, date, etc.)
        if (origField.type === "text") {
            // Try to preserve number formatting - look at the field's properties
            var formatType = null;
            
            // Copy the field's format type if it exists
            if (origField.hasOwnProperty("numberFormat")) {
                formatType = "number";
                // Direct copy of number format properties if they exist
                if (origField.hasOwnProperty("nDec")) newField.nDec = origField.nDec;
                if (origField.hasOwnProperty("sepStyle")) newField.sepStyle = origField.sepStyle;
                if (origField.hasOwnProperty("negStyle")) newField.negStyle = origField.negStyle;
                if (origField.hasOwnProperty("currStyle")) newField.currStyle = origField.currStyle;
                if (origField.hasOwnProperty("strCurrency")) newField.strCurrency = origField.strCurrency;
                if (origField.hasOwnProperty("currencyPrepend")) newField.currencyPrepend = origField.currencyPrepend;
            }
            else if (origField.hasOwnProperty("percentFormat")) {
                formatType = "percent";
            }
            else if (origField.hasOwnProperty("dateFormat")) {
                formatType = "date";
            }
            else if (origField.hasOwnProperty("timeFormat")) {
                formatType = "time";
            }
            else if (origField.hasOwnProperty("specialFormat")) {
                formatType = "special";
            }
            
            console.println("Field format type detected: " + formatType);
            
            // If we detected a format, try to replicate the actions
            if (formatType) {
                // Check existing actions
                var fieldActions = [];
                try {
                    // Use the proper method to get actions in Acrobat
                    if (origField.actions && origField.actions.length > 0) {
                        fieldActions = origField.actions;
                    }
                } catch (e) {
                    console.println("Error checking actions: " + e);
                }
                
                // Copy each action
                try {
                    for (var i = 0; i < fieldActions.length; i++) {
                        var action = fieldActions[i];
                        if (action && action.name && action.script) {
                            // Use the proper method to set actions in Acrobat
                            newField.setAction(action.name, action.script);
                        }
                    }
                } catch (e) {
                    console.println("Error copying actions: " + e);
                }
            }
        }
    } catch (e) {
        console.println("Error preserving field format: " + e);
    }
}

// Function to copy all possible field properties
function copyFieldProperties(origField, newField) {
    // Basic properties
    newField.value = origField.value;
    newField.defaultValue = origField.defaultValue;
    
    // Appearance properties
    newField.textSize = origField.textSize;
    newField.textFont = origField.textFont;
    newField.textColor = origField.textColor;
    newField.fillColor = origField.fillColor;
    newField.borderStyle = origField.borderStyle;
    newField.borderWidth = origField.borderWidth;
    newField.strokeColor = origField.strokeColor;
    newField.rotation = origField.rotation;
    
    // General properties
    newField.readonly = origField.readonly;
    newField.required = origField.required;
    newField.hidden = origField.hidden;
    newField.name = origField.name;
    newField.userName = origField.userName;
    
    // Alignment and formatting
    newField.alignment = origField.alignment;
    newField.multiline = origField.multiline;
    newField.richText = origField.richText;
    newField.doNotScroll = origField.doNotScroll;
    newField.doNotSpellCheck = origField.doNotSpellCheck;
    newField.charLimit = origField.charLimit;
    newField.comb = origField.comb;
    
    // IMPORTANT: Fix for the "scroll long text" option
    if (origField.doNotScroll === false) {
        newField.doNotScroll = false;
    }
    
    // Try to copy format settings
    preserveFieldFormat(origField, newField);
    
    // Copy additional format-specific properties - if direct property access is available
    try {
        // Format specific properties - try direct property access first
        if (origField.type === "text") {
            // These might work in some versions of Acrobat
            if (typeof origField.nDec !== "undefined") newField.nDec = origField.nDec;
            if (typeof origField.sepStyle !== "undefined") newField.sepStyle = origField.sepStyle;
            if (typeof origField.negStyle !== "undefined") newField.negStyle = origField.negStyle;
            if (typeof origField.currStyle !== "undefined") newField.currStyle = origField.currStyle;
            if (typeof origField.strCurrency !== "undefined") newField.strCurrency = origField.strCurrency;
            if (typeof origField.currencyPrepend !== "undefined") newField.currencyPrepend = origField.currencyPrepend;
        }
    } catch (e) {
        console.println("Error copying format properties: " + e);
    }
}

// Rename fields in the sorted order
for (var i = 0; i < fieldInfo.length; i++) {
    var origName = fieldInfo[i].name;
    
    try {
        var origField = this.getField(origName);
        if (!origField) continue;
        
        // Create new sequential name with padding
        var fieldNumber = padNumber(i + 1);
        var newName = "text" + fieldNumber;  // Change "text" to your desired prefix
        
        // Check if the field already exists
        if (this.getField(newName)) {
            this.removeField(newName);
        }
        
        // IMPORTANT: Create a field with exactly the same properties
        // Instead of creating and then copying, let's use the built-in methods
        // to properly duplicate the field with all its properties
        
        // Step 1: Create new field with the same type as the original
        var newField = this.addField(newName, origField.type, origField.page, origField.rect);
        
        if (newField === null) {
            console.println("Failed to create field '" + newName + "'");
            continue;
        }
        
        // Step 2: Copy all properties from original field
        copyFieldProperties(origField, newField);
        
        // Double-check the scrolling option
        if (origField.doNotScroll === false) {
            newField.doNotScroll = false;
        }
        
        // Try a different approach for format actions
        if (origField.type === "text") {
            // Look at the current field value and try to determine its format
            var fieldValue = origField.value;
            
            // Check if it looks like a formatted number field
            if (typeof fieldValue === "number" || (typeof fieldValue === "string" && !isNaN(fieldValue.replace(/[,$%]/g, '')))) {
                // Use a basic AFNumber_Format with 2 decimal places as fallback
                try {
                    var decimalPlaces = 2; // Default to 2 decimal places
                    
                    // Try to determine decimal places from the value
                    if (typeof fieldValue === "number") {
                        var valueStr = fieldValue.toString();
                        var decimalPart = valueStr.split('.')[1];
                        if (decimalPart) {
                            decimalPlaces = decimalPart.length;
                        }
                    }
                    
                    // Set number format with the detected decimal places
                    newField.setAction("Format", "AFNumber_Format(" + decimalPlaces + ", 0, 0, 0, '', false);");
                    newField.setAction("Keystroke", "AFNumber_Keystroke(" + decimalPlaces + ", 0, 0, 0, '', false);");
                    
                    console.println("Applied numeric format with " + decimalPlaces + " decimal places");
                } catch (e) {
                    console.println("Error applying numeric format: " + e);
                }
            }
        }
        
        // Remove the original field only if new field was created successfully
        if (newField) {
            this.removeField(origName);
        }
        
        console.println("Renamed field '" + origName + "' to '" + newName + "'");
    } catch (e) {
        console.println("Error processing field '" + origName + "': " + e);
    }
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines