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

Combine 2 documents via JS

goofy78270
Registered: Dec 7 2009
Posts: 15

I created the following JS to combine tow documents, but there are no bookmarks present. Is there a way to combine these two documents via JS and maintain the bookmarks?

Current code:

var filepath = "/c/filepath/"

// Create a new PDF document for Profile:
doc = app.newDoc();
doc.saveAs({ cPath: filepath + "Temp.pdf" });
doc.closeDoc(true);

doc = app.openDoc({cPath: filepath + "Temp.pdf" })
// List of files of different extensions
aFiles = new Array( "Doc1.xls", "Doc1.doc");
for ( var i=0; i < aFiles.length; i++) {
// Open and convert the document
newDoc = app.openDoc({
oDoc: doc,
cPath: aFiles[i],
bUseConv: true
})
// Save the new PDF file to a temp folder
newDoc.saveAs({ cPath: filepath + "Combined.pdf" });
// Close it without notice
newDoc.closeDoc(true);
// Now insert that PDF file just saved to the end of the first document
doc.insertPages ({
nPage: doc.numPages-1,
cPath: filepath + "Combined.pdf",
nStart: 0
});
}

My Product Information:
Acrobat Pro 8.1.6, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
This script is inserting pages from a generated PDF into the original temp.pdf. So only the bookmarks in the temp.pdf are maintained. No bookmark info comes with pages. You'll need to add code to copy the bookmarks over.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

goofy78270
Registered: Dec 7 2009
Posts: 15
thomp wrote:
This script is inserting pages from a generated PDF into the original temp.pdf. So only the bookmarks in the temp.pdf are maintained. No bookmark info comes with pages. You'll need to add code to copy the bookmarks over.
If I create the second document first and then insert the single page first document, How can I create a bookmark for the first document and haow do I place it at the beginning of the combined document instead of the end?

Would the insert need to be 0 or 1 for the start page?

doc.insertPages ({
nPage: 0,
cPath: filepath + "Combined.pdf",
nStart: 0
});

Or is there a way to copy over the bookmarks using JS? - This method would be prefered in the instances that we want to combine more documents together.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Please take a look in the JavaScript Reference at the Bookmark Object. You'll find everything there.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

goofy78270
Registered: Dec 7 2009
Posts: 15
I have looked at the reference material, but it does not tell me how to loop through different formats within a document. While the creation of a bookmark may be simple, How do I get to the location I want? If I want to bookmark a title section, yet the same text is held within the document, I search by text will not help. Could you please point me to a method or way to loop through the different formatting styles, or structure as they are called in the PDF, in order to create the bookmarks?

var myRoot = this.bookmarkRoot;
myRoot.createChild("myBookmark", "app.alert('
Hello!');");

and

var current = myRoot.children[0];
current.createChild("myBookmarkChild",
"app.alert('Hello child!');");
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I would image that what you want to do is to create a bookmark for the inserted page and set the action for that bookmark to navigate to the page. Creating the bookmark is done with the code you'ver already shown in your post. To goto a page use:

this.pageNum = ;The script knows the page number because it just inserted it. As long as the script is inserting pages at the end of the document this methodology will work fine, i.e., the script inserts a page at the end of the PDF, then creates a bookmark at the end of the bookmark list. If you want to insert pages a different locations then you'll need another methodolgy.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

goofy78270
Registered: Dec 7 2009
Posts: 15
As stated, I am looking to create bookmarks based on the formatting type. Some pages may have multiple bookmarks on a single page. Without being able to loop through the structure types I am not able to create bookmarks as bookmarks to a single page is not very effective.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Using Acrobat JavaScript, you can create a script that loops through all the words on a page with the "doc.getPageNthWord()" function. You can also get the coordinates of the words and even highlight words. But the font type, text size, etc. is not accessible.

Creating a navigation script to a particular word on a page is tricky, but it can be done in a couple of ways. First by controling the "doc.zoom" and and using the "doc.scroll()" function, or by using the "doc.viewState" parameter. I've written about using both of these techniques and created several scripts and samples files at the pdfscripting.com membership site:
http://www.pdfscripting.com/public/department43.cfm

Writing this type of automation script is not trivial, but all the info you need is in the Acrobat JavaScript reference. There are a couple of different examples of searching for words, take a look at the "doc.addLink()" function.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script