Why doesn't the .trim() function work?

Hi guys,

I was wondering why Acrobat doesn't support the .trim() function? Even PDF Expert(a limited PDF editing app) can handle it. Do I need to update Acrobat or?

Thanks for the help!


Lane Pemberton


3 Answers

Voted Best Answer

String.trim() is not supported in Acrobat's JavaScript. You will have to come up with a different method to trim whitespace at the beginning or the end of a string. You can do that for example with a regular expression:

var str = "    test   ";
str = str.replace(/^\s+|\s+$/gm,'');

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


By Karl Heinz Kremer   

Just to add a little bit more explanation. The JavaScript implementation in the current Acrobat is based on ECMAScript 3 (or, better, the Mozilla JavaScript version 1.7 or 1.8).

The trim() method is new in ECMAScript 5, and therefore not supported.

It may become supported when/if Acrobat gets an upgrade of the JavaScript version to ECMAScript 5.

Hope this can help.

Max Wyss.


Max Wyss   

Actually it is possible to check if the trim function or method is available to Acrobat or Reader by testing for the prototype for that method and if it does not exist, then one can define it. This can work as a folder or document level script. If you add it as a document level script then it would be available to anyone you shared the PDF with.

StackOverFlow Trim String in JavaScript

One could even create a left and right trim function.


George Kaiser   


Please specify a reason: