These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Duplicate current page 5 times

uklogistics
Registered: Sep 18 2009
Posts: 14
Answered

Hi

I'm try to create a javascript menu option, so that when a user opens any PDF file, they can click DUPLICATE PAGE from the Edit drop down.

It would then automatcially duplicate the one page document 4 times, so the PDF file now containes 5 pages within the same document.

I have used this script, which does the job but creates a 6th blank page and also throws an error after running,

I have no javascript experience, so pls be gentle/

app.addMenuItem({
cExec: "duplicate_all_pages();",
cParent: "Edit",
cName: "Duplicate all pages"
});

function duplicate_all_pages() {
var r = this.getPageBox();
var w = r[2] - r[0];
var h = r[1] - r[3];
var oldD = this;
var newD = app.newDoc({ nWidth: w, nHeight: h });
var path = oldD.path.replace(oldD.documentFileName,"");
for(var p=oldD.numPages-1; p>=0; p--) {
pageFile = path+"page_"+p+".pdf";
oldD.extractPages({ cPath: pageFile, nStart: p });

newD.insertPages({ cPath: pageFile, nPage: -1 });
newD.insertPages({ cPath: pageFile, nPage: -1 });
newD.insertPages({ cPath: pageFile, nPage: -1 });
newD.insertPages({ cPath: pageFile, nPage: -1 });
newD.insertPages({ cPath: pageFile, nPage: -1 });

}
return true;
}

My Product Information:
Acrobat Pro 8.1.2, Windows
uklogistics
Registered: Sep 18 2009
Posts: 14
Can anyone help
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Have you looked a the 'Template' object?

For example to add a page 4 times:
function CopyPage(nPageNum, nTimes) {// create template from pagevar t = this.createTemplate({cName:"myTemplate", nPage: nPageNum });// hide templatet.hidden = true; // spawn nTimes pages from templatevar t = this.templates;var T = t[0]; // get first templagefor (var i =0; i < nTimes; i++)  {T.spawn({nPage: this.numPages, bRename: false, bOverlay: false });}// remove the templatethis.removeTemplate({cName: 'myTemplate'); return;} // end CopyPage function  // code to call CopyPage functionCopyPage(0. 4);

You will have to review the Acrobat JS API Reference for the security issues and other restrictions.

George Kaiser

uklogistics
Registered: Sep 18 2009
Posts: 14
Thanks for your reply.

I'm not familar with this, and tried to run your code in the Acrobat Javascript window, and it returns this error

SyntaxError: missing ) after argument list

I have tried to work through the code , but cannot find anything wrong other than a "}" was missing in line

this.removeTemplate({cName: 'myTemplate');

I inserted the } but still getting the error.


How do you recommand this function be easily executed by the user when after they have opened a specific document that needs to be duplicated?
revoxo
Registered: Jul 20 2009
Posts: 17
The code posted by gkaiseril is excellent, it's just there's a couple of typos. The one you've pointed out and the one below. The amended code works well for me (very useful)...

There is a period between the '0' and '4' and it should be a comma.

CopyPage(0. 4);

should read:

CopyPage(0, 4);
uklogistics
Registered: Sep 18 2009
Posts: 14
Thanks, Can you advise how i can make this option available on the users acrobat program .


Previously, I tested the addMenuItem fuction, but I understand their are security issues relating to this.
app.addMenuItem({


How else can I do this?
uklogistics
Registered: Sep 18 2009
Posts: 14
THANKS , THIS WORKS GREAT !!!!


revoxo wrote:
The code posted by gkaiseril is excellent, it's just there's a couple of typos. The one you've pointed out and the one below. The amended code works well for me (very useful)...There is a period between the '0' and '4' and it should be a comma.

CopyPage(0. 4);

should read:

CopyPage(0, 4);