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

Acrobat DC page replace script

Explorer ,
Oct 24, 2022 Oct 24, 2022

Hi Team,

 

I have 700+ pages in PDF, I want to replace multiple page numbers via script.

Random pages need to replace.

 

For example, old pdf page numbers 54, 109, 154, and 209 replace with the same page numbers with new pdf.

In the manual process, I click on replace pages and do the process.

NewInScript_0-1666673306719.png

Can we do this in the script in Acrobat DC?

 

I found one script but that script is not working

var doc = app.openDoc({
    cPath: "/C/Users/username/directory/oldpdf.pdf"
});
doc.replacePages({
    nPage: 2,
    cPath: "/C/Users/username/directory/newpdf.pdf",
    nStart: 0,
    nEnd: -1
});



 

Thanks in Advanced.

 

TOPICS
JavaScript
1.0K
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
1 ACCEPTED SOLUTION
Community Expert ,
Oct 25, 2022 Oct 25, 2022

Try the following code:

 

var pagesToReplace = [54, 109, 154, 209];
var newFilePath = "/C/Users/username/directory/newpdf.pdf"; // change to actual file path, keeping the same syntax!
for (var i=0; i<pagesToReplace.length; i++) {
	this.replacePages({nPage: pagesToReplace[i]-1, cPath: newFilePath, nStart: pagesToReplace[i]-1})
}

 

You can run it from the JS Console or even from a Custom Command. If it doesn't work specify exactly what happened, including the full text of any error message you got. Just saying "it doesn't work" is not very helpful to us.

View solution in original post

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 25, 2022 Oct 25, 2022

Try the following code:

 

var pagesToReplace = [54, 109, 154, 209];
var newFilePath = "/C/Users/username/directory/newpdf.pdf"; // change to actual file path, keeping the same syntax!
for (var i=0; i<pagesToReplace.length; i++) {
	this.replacePages({nPage: pagesToReplace[i]-1, cPath: newFilePath, nStart: pagesToReplace[i]-1})
}

 

You can run it from the JS Console or even from a Custom Command. If it doesn't work specify exactly what happened, including the full text of any error message you got. Just saying "it doesn't work" is not very helpful to us.

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 ,
Oct 25, 2022 Oct 25, 2022

Thanks, @try67 code is perfect I just need to do some changes in my process.

NewInScript_0-1666722111751.png

 

Actually, it is giving an Access Denied error but after modifying the pdf it is working perfectly fine.

 

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 02, 2022 Nov 02, 2022

Hi @try67,

var pagesToReplace = [54, 109, 154, 209];

Can we add the above page numbers directly from excel or a text file?
When we have 300+ pages that need to do in color that time we need to type all page numbers manually.
That will be time-consuming and also might be a typo mistake.

It will be great if we get the page number directly from excel or a text file

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 02, 2022 Nov 02, 2022

Yes, it's possible, but converting the list from Excel to a string is not too difficult. You just need a capable plain text editor (I recommend Notepad++), and then you could copy the column from Excel in to it and do a simple RegExp search & replace command to replace the line-breaks ("\r\n") with commas.

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 02, 2022 Nov 02, 2022

@try67, thanks for the quick reply and advice!!

Yes, I 100% agree with you better if we convert excel data into strings by Notepad++ or excel formula.

In fact, I'm planning to use CONCATENATE excel formula because I always get data in excel sheets.
So in the same sheet, we can convert multiple-row data into a single cell and copy and paste it into our code.

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 ,
Apr 28, 2025 Apr 28, 2025
LATEST

this i great

 

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