Skip to main content
Known Participant
March 15, 2023
解決済み

Delete Pages By Text Search

  • March 15, 2023
  • 返信数 2.
  • 5985 ビュー

Hello everyone, everything good? I have a document that I need to separate from different classes, for example, it is a document with 200 pages mixed up. I have the list of names for the morning class and the list of names for the night class, so I wanted to put the list of names and delete it to be able to separate the list quickly

Example:
- Alice Says
-Francisco Campos
- etc...
- etc...


Those above I wanted the PDF to automatically find and delete, how would that be? I believe that only with correct script?

このトピックへの返信は締め切られました。
解決に役立った回答 clay.78558856

Sorry, there was a small mistake in the code. I fixed it above. Try it again with the new code.


That did it.  Thank you!

返信数 2

Participating Frequently
May 7, 2024

I want to do a similar thing, but instead of deleting the pages I want to extract them as one separate file.  I changed the last line of code from "deletePages" to "extractPages."  However, it creates a separate file for each page that's extracted and I just need one file for all of them.  BTW I do not know ANYTHING about Java script.  -Thanks!

try67
Community Expert
Community Expert
May 7, 2024

An easier way to do that is delete the pages which don't have the search term, and then you'll end up with a document that has all the pages that has them. Then you can save it under a new name, or print it, etc.

Participating Frequently
May 7, 2024

So how do I change the code to delete the pages that don't have the search term?

Thom Parker
Community Expert
Community Expert
March 15, 2023

You are correct that is can only be done with a script. However, it's not particularly easy. 

I would suggest doing this.

 

  1. Create a new 'Action' with the Action Wizard,
  2.  Add "Search and Remove Text", from the "Protection" category. This is where you enter your list of names. This step will mark the entered names with a redact annotations. It won't remove the text. 
  3. Add "Execute JavaScript", from the   "More Tools" category.  Then enter the following script to find the page numbers for all the redact annotations and then remove them. 

 

(getAnnots() || []).filter(a=>a.type = "Redact").map(a=>a.page).sort().filter((ele,i,arr)=> arr.indexOf(ele) == i).reverse().forEach(a=>this.deletePages(a))

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
CleberRafael作成者
Known Participant
March 16, 2023

Hello Thom, how are you? Thank you immensely for the answer, your method has really helped me a lot, the question of the first step of the script which is the search and remove text is working 100%, the part of the Script has some pages that are marked that it is not deleting for some reason

In case you wanted to test with the file I'm using and see if that's right I'll send you the file and the list of names for you to check if your script will delete 100%

But even so, he already gave me an 80% help to facilitate this part, thanks again!

Thom Parker
Community Expert
Community Expert
March 16, 2023

Please look in the console window (Ctrl-J) and see if there are any errors reported there. 

 

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