Creating and Modifying PDF Documents
Creating and Modifying PDF Files
4
You can combine these operations to extract pages from one document and move them to
another (they will be deleted from the first document). The following code will extract
pages 2 through 5 in
mySource.pdf
and move them into
myTarget.pdf
:
// this represents myTarget.pdf
//
// First copy the pages from the source to the target document
this.insertPages({
nPage: -1,
cPath: "/C/mySource.pdf",
nStart: 2,
nEnd: 5
});
//
// Now delete the pages from the source document
var source = app.openDoc("/C/mySource.pdf");
source.deletePages({nStart: 2, nEnd: 5});
To replace pages in one document with pages from another document, invoke the target
document’s
replacePages
method, which accepts four parameters:
●
●
●
●
nPage
: the zero-based index of the page at which to start replacing pages
cPath
: the device-independent pathname of the source file
nStart
: the zero-based index of the beginning page
nEnd
: the zero-based index of the last page
In the following example, pages 2 through 5 from
mySource.pdf
replace pages 30 through
33 of
myTarget.pdf
:
// this represents myTarget.pdf
this.replacePages({
nPage: 30,
cPath: "/C/mySource.pdf",
nStart: 2,
nEnd: 5
});
To safely move pages within the same document, it is advisable to perform the following
sequence:
1.
Copy the source pages to a temporary file.
2.
Insert the pages in the temporary file at the new desired location in the original
document.
3.
Delete the source pages from the original document.
Acrobat JavaScript Scripting Guide
73