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

JavaScript find text & create field

Explorer ,
Nov 05, 2021 Nov 05, 2021

9699C762-C1BC-4928-B113-A3C877B13D36.jpegexpand image

 

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!

 

TOPICS
How to , JavaScript
3.6K
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
Explorer ,
Nov 06, 2021 Nov 06, 2021

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.

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
Explorer ,
Nov 06, 2021 Nov 06, 2021

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!

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 Expert ,
Nov 07, 2021 Nov 07, 2021

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.

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
Explorer ,
Nov 07, 2021 Nov 07, 2021

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!

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 Expert ,
Nov 07, 2021 Nov 07, 2021

Yes, saving the file under a specific name requires installing a script file on the local computer.

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
Explorer ,
Nov 07, 2021 Nov 07, 2021

I found Thom Parker's article titled "Instructions for installing folder level scriptsand 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 😂).

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 Expert ,
Nov 08, 2021 Nov 08, 2021

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.

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
Explorer ,
Nov 27, 2021 Nov 27, 2021

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!

 

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 Expert ,
Nov 27, 2021 Nov 27, 2021

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 - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Nov 27, 2021 Nov 27, 2021

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:

Creating and Using Custom Redaction Patterns 

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 Expert ,
Nov 27, 2021 Nov 27, 2021

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.

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 Expert ,
Nov 27, 2021 Nov 27, 2021

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. 

 

 

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

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
Explorer ,
Nov 27, 2021 Nov 27, 2021
LATEST

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. 

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
Explorer ,
Nov 11, 2021 Nov 11, 2021

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!

 

Code:
  <div id="string">
   Some text of any length here OCA/T--212890023 and some more text here.
 </div>

JavaScript...

Code:
<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>
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 Expert ,
Nov 11, 2021 Nov 11, 2021

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.

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