JavaScript find text & create field
Copy link to clipboard
Copied
 
I have a bigger JavaScript task but I want to see if I can do the following before going further.
- FIND text “OCA/“ (no quotes)
- COPY the text that follows “OCA/“ up until and including the next SPACE (if including the space is a problem that’s not necessary)
- Create a field that has the copied text in it (field can be labeled anything)
A visual is attached as well. Thank you in advance!
Copy link to clipboard
Copied
I would like to modify my post. Thank you in advance:
I have a bigger JavaScript task but I want to see if I can do the following before going further.
- FIND text “OCA/“ (no quotes)
- COPY the text that follows “OCA/“ up until and including the next SPACE (if including the space is a problem that’s not necessary)
Create a field that has the copied text in it (field can be labeled anything)- Save as: found/copied text MISC (the save location will always be the same and the word MISC is merely tagged on to the end of the found / copied text)
A visual is attached as well.
Copy link to clipboard
Copied
Another important detail I should've added from the beginning
- the text following OCA/ will always be 12 characters (not including the space following)
Thank you!
Copy link to clipboard
Copied
Yes, this is all possible, but it requires the development of a custom-made script, which is not a trivial task, as well as installing a script file on the local computer of each user who will be using it.
If this is something you want to develop on your own we can help with some pointers on how to implement it.
If you're looking for a ready-made solution you can always hire a professional to do it for you. I've created many similar tools in the past and could do the same for you (for a fee, of course). You can contact me privately via [try6767 at gmail.com] to discuss it further.
Copy link to clipboard
Copied
Thank you try67. I may take advantage of your expertise in the future. Taking it one step at a time. I'm curious if this example requires an elaborate script, including the script that will be installed on the user's computer? As a reminder I don't need to create a field just to save what is found / copied. I know I confused my inquiry a little bit. Much appreciated!
Copy link to clipboard
Copied
Yes, saving the file under a specific name requires installing a script file on the local computer.
Copy link to clipboard
Copied
I found Thom Parker's article titled "Instructions for installing folder level scripts" and another article "Silent Save As" example of using folder level scripts. Am I getting warmer, LOL? I believe I can do that part with some guidance but some suggestions on script that will find / copy the 12 characters to the right of text "OCA/" and save as that text is my additional challenge (fingers crossed 😂).
Copy link to clipboard
Copied
Yes, you're on the right path!
In order to locate words you will need to use a double loop. One that goes over all the pages in the file and another one that goes over all the words in each one of those pages. The latter can be achieved using the getPageNthWord method. I suggest looking it up in the Acrobat JS API Reference (part of the Acrobat SDK) to see how it works. It also contains examples.
Copy link to clipboard
Copied
Wondering if I can obtain script for part of what I'm trying to accomplish to get started. I've learned that there is a way to run JavaScript using an Action Wizard. Might I be able to obtain script that could be run by an Action Wizard that would in the first page only...
- Find OCA/T--212890021 (example - text varies although OCA/ always stays the same and OCA/ appears once at varying points on a page per pdf
- Ideally...copy characters 5 through 16 from the left (Goal: to be able to paste this text in the Save As dialog box)
- Second preference...copy the the entire 16 character string
Text will always will be in this format:
<space>OCA/<letter><hyphen><hyphen><number><number><number><number><number><number><number><number><number><space>
Thank you!
Copy link to clipboard
Copied
You have a couple of options for finding text and text locations on a PDF page through the JS model in Acrobat Professional.
1. Full JS search using the "doc.getPageNthWord()" function.
here's the reference entry:
https://opensource.adobe.com/dc-acrobat-sdk-docs/acrobatsdk/html2015/index.html#t=Acro12_MasterBook%...
2. Use the Redact operation to search for a pattern that matches the OCA number. You can create your own pattern using a regular expression by modifying this file:
SearchRedactPatterns.xml
On windows you'll find it here:
C:\Users\<User Name>\AppData\Roaming\Adobe\Acrobat\DC\Redaction\ENU
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you Thom. I am pre-beginner LOL so the second option is more my speed. I read your article linked below. If the following regular expression allows me to find the whole text plus a space at the end (i.e. "OCA/T--212890021 "):
var myRegExp = /O+\s/;
If I'm understanding correctly I would edit the SearchRedactPatterns.xml file to add an entry placing /O+\s/; in between the opening <val> and closing <\val> tags. This will add that new pattern to the redact operation.
Can I then run an Action Wizard to locate this pattern (my goal is to automate) and how do I achieve the "copy what is found" component in hopes to paste that in a "save as" dialog box? I know I've bold in requesting JS but could you also suggest script that can be ran as a second step in an action Wizard to do this?
Thank you!
Thom Parker wrote:You have a couple of options for finding text and text locations on a PDF page through the JS model in Acrobat Professional.
1. Full JS search using the "doc.getPageNthWord()" function.
here's the reference entry:
https://opensource.adobe.com/dc-acrobat-sdk-docs/acrobatsdk/html2015/index.html#t=Acro12_MasterBook%...
2. Use the Redact operation to search for a pattern that matches the OCA number. You can create your own pattern using a regular expression by modifying this file:
SearchRedactPatterns.xml
On windows you'll find it here:
C:\Users\<User Name>\AppData\Roaming\Adobe\Acrobat\DC\Redaction\ENU
Thom Parker:
https://acrobatusers.com/tutorials/text-matching-regular-expressions/
Adobe blog:
Copy link to clipboard
Copied
While the text can be found using a script, your goals as to what to do with it are not achievable. However, you can simply save the file directly (if you know under which folder you want to do so), if you run the code from within an Action, using the saveAs method.
Copy link to clipboard
Copied
You cannot pre-fill the SaveAs dialog, but you can save to a specific name, without displaying the SaveAs Dialog.
And you can do it from an Action Script.
The redaction search pattern needs to be much more complete than specifying the first letter. In fact, the pattern you've shown will fine 1 or strings of capital "O" followed by a space. You'll want something like this:
rgOCA = /\bOCA\/\w\-\-(\d{9})\b/;
The redact pattern data block is contained within the <set> tag. You could replace the regular expression in an existing pattern, but it would be much better to create your own by copying everything inside a set tag to the end of list of <set> tags. Then modify it for your pattern.
When you run the redact search it will add redaction annots to the document. The text it finds will be placed in the contents of the redact annot. You'll want to delete the annots before saving to the new file name.
Truth be told, since you are only looking for a single instance of the OCA number, you'd do better using my first option.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you Thom. I agree that your first option is the better choice which for me will need to be tackled when I'm more learned with Acrobat JS or can hire a professional. I was hoping a "copy what was found" could be achieved with an Action since a beginner like me can grasp setting that up however I'm happy to have the redaction search pattern you provided. I may be able to use this with other projects. Much appreciated.
Copy link to clipboard
Copied
I was greatful to receive the below sample code from an external professional. Any thoughts on whether this gets me closer to a solution? I would need to determine how the two sets of suggested codes relate and if they are placed in the same "module"? My goal is to automate this task using an Acrobat menu button or Custom Action once I get the code established. I've attached an updated screenshot. Thank you for any additional feedback that may be offered!
<div id="string"> Some text of any length here OCA/T--212890023 and some more text here. </div>
JavaScript...
<script> (function(d) { 'use strict'; var str = d.getElementById( 'string' ).textContent, start = str.indexOf('OCA/'), required = str.substring( start + 4, start + 15 ) + ' MISC'; console.log( required ); }(document)); </script>
Copy link to clipboard
Copied
No, not at all. This looks like JS written for a web-page. The commands for JS in a PDF file are completely different, although the core syntax is (for the most part) the same.

