Skip to main content
TᴀW
Legend
June 8, 2023
Question

Is there a built-in function for parsing simplified field calculations

  • June 8, 2023
  • 3 replies
  • 2811 views

I'm writing a script that allows the user to type in a line of simplified field calculations.

I could write a parser to turn this into proper Javascript myself, but my guess is that there is one of those built-in, hidden functions in Acrobat that will do it for me.

If there is, it would be better to use it for the sake of compatibility (same quirks and bugs as Acrobat's own).

Does anyone know if it exists, and what it might be?

Thanks!

Ariel

 

This topic has been closed for replies.

3 replies

TᴀW
TᴀWAuthor
Legend
June 10, 2023

Well, since Acrobat doesn't expose the function they use, and in light of all the discussion above, this is my best attempt so far at parsing simplified field notation. Note that the point here is to emulate as nearly as possible Acrobat's own parser, warts and all.

 

"(a\\+b > 5) * 20"
	.replace(/(\\[-+*\/=%><^:~?! \(\)]|[^+ *\/=%><^:~?!\(\)-])+/g, function (token) {
		if (!isNaN(token)) return token;
		return "this.getField(\"" + token + "\").value"
	})
	.replace(/\\/g, "");

 

This correctly returns:

 

(this.getField("a+b").value > 5) * 20

 

This mini-parser deals with escaped characters in fields names and trims the field names as well.

P.S. It also works with @Test Screen Name 's example (Text1?Text2:Text3), correctly returning:

 

this.getField("Text1").value?this.getField("Text2").value:this.getField("Text3").value

 

 

 

Thom Parker
Community Expert
Community Expert
June 11, 2023

Pretty nifty!!

But, the "+" is on the wrong side of the closing parentheses. Only one character can be escaped at a time. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Community Expert
June 8, 2023

The answer is NO.  Simplifiled Field Notation is parsed by code in Acrobat, not by a built-in script.  

However, for a parsing problem it's pretty simple. Just split the SFN string into tokens (names and operators). Put the names into  getField calls, i.e. getField("Name").value. Then put back together with the operators. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
TᴀW
TᴀWAuthor
Legend
June 8, 2023

Thanks Thom.

I see that Acrobat wraps each field value in AFMakerNumber().

For example, (field1 + field2) / (field3 * 6) becomes

event.value=(AFMakeNumber(getField("field1").value) + AFMakeNumber(getField("field2").value)) / (AFMakeNumber(getField("field3").value) * 6)

 

But to be clear, the only allowed operators are + - * /, is that correct? And field names with special characters must have those chars escaped with \ ? 

Anything else I should know before putting this all together?

Thom Parker
Community Expert
Community Expert
June 8, 2023

Yes, if you are writing the parser then you make the rules.

Don't allow special characters or spaces in names so you don't have to deal with escape sequences. And the reason only simple binary operators are used is because they follow a simple consistent pattern. In fact, given this restriction, the string can be parsed by splitting on the binary operators. You can assume every thing on either side of a binary operator is a name.  Of course, you also have to deal with implied precedence and explicit parentheses. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Legend
June 8, 2023

No, I don't think there is any such function exposed.

TᴀW
TᴀWAuthor
Legend
June 8, 2023

I remember seeing a list of all such functions somewhere. I couldn't find it this time. Do you have a link?

Thom Parker
Community Expert
Community Expert
June 8, 2023

Finding hidden functions is not difficult.

Run this code in the console window.

 

for(var nm in this) {if(typeof(this[nm]) == "function")console.println(nm);} 

 

This will expose all functions in the scope of the current document. Which includes global functions.  

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often