Skip to main content
Participant
October 14, 2024
Question

Insert two PDFs after a page when it finds a keyword

  • October 14, 2024
  • 1 reply
  • 261 views

Hi, I have JS that will not work for me. My goal here is insert two PDFs after it find a page with the keyword "Please make checks payable to:". I can do it for a single PDF and add to the last page. The issue here is I can have two or more page with that keyword. I.E. A single PDF with hundreds of pages. the first instance the keyword could be found on page 3. So this is when I would like to add the two PDF then the next instance would be page 5. so this is when  would like to add the same two PDFs. And this process would continue until it no long find the keyword. The problem here is all the files are downloaded as one PDF instead of single PDFs. This is my code:

 

// Define the two PDFs to be inserted
var pdf1 = "C:/Users/MyName/Desktop/Billing Test/letterhead.pdf";
var pdf2 = "C:/Users/MyName/Desktop/Billing Test/RATE - FY 25.pdf";

// Function to insert PDFs after pages with specific text
function insertPDFsAfterText() {
var numPages = this.numPages;
for (var i = numPages - 1; i >= 0; i--) {
var pageText = this.getPageNthWord(i, 0, true);
if (pageText.includes ('Please make checks payable to:')) {
this.insertPages({
nPage: i + 1,
cPath: pdf1
});
this.insertPages({
nPage: i + 2,
cPath: pdf2
});
}
}
}

This topic has been closed for replies.

1 reply

Participant
October 14, 2024

By the way. When I run the debugger I get this:

 

TypeError: pageText.includes is not a function
7:Console:Exec
undefined

try67
Community Expert
Community Expert
October 14, 2024

Correct, it's not, at least not one that is supported in Acrobat. You can use indexOf, instead. However, that won't work, either, since getPageNthWord returns a single word, not the full text of the page, so you can't compare what it returns to a whole phrase.

You would need to either check the phrase one word at a time, or to first extract the full page text, save it as a single string, and then search it.

Participant
October 14, 2024

I take it you are the one offering E-Learning from PDFAutomationStation.com. If so, does your course teach this kind  stuff? Or this something we have to think outside the box?