Skip to main content
Inspiring
October 25, 2022
Answered

Acrobat DC page replace script

  • October 25, 2022
  • 2 replies
  • 1396 views

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.

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.

 

Correct answer try67

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.

2 replies

Known Participant
April 28, 2025

this i great

 

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
October 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.

ShanpubAuthor
Inspiring
October 25, 2022

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

 

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