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

Insert two PDFs after a page when it finds a keyword

New Here ,
Oct 14, 2024 Oct 14, 2024

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
});
}
}
}

TOPICS
Acrobat SDK and JavaScript , Mac , Windows
212
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
New Here ,
Oct 14, 2024 Oct 14, 2024

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

 

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

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 ,
Oct 14, 2024 Oct 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.

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
New Here ,
Oct 14, 2024 Oct 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?

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 ,
Oct 15, 2024 Oct 15, 2024

No, that's not my website, but @PDF Automation Station 's.

I offer a complete solution for your needs, if you're interested.

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 ,
Oct 15, 2024 Oct 15, 2024
LATEST

My (paid for) course will not teach you this, specifically.  It will teach you how to read, write, test, and apply JavaScript for Acrobat Pro, and how to quickly find solutions in the reference guide.  The complete course outline is available in the Course Outline tab here.  If you are interested, message me privately and I will provide a discount code for 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