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

Extract non sequential pages to one document in adobe acrobat pro DC using Javascript

Community Beginner ,
Jan 23, 2023 Jan 23, 2023

Copy link to clipboard

Copied

I want to extract pages in adobe acrobat pro DC using Javascript.
But I want to extract non sequential pages into one document.

 

var thisDocPath = this.path.replace(thisDocName, "") //folder path (without file name)
var thisDocName = this.documentFileName //file name with extension
    
  this.extractPages({
    nStart: [2,6,1],
    cPath: thisDocPath + thisDocName.replace(/.pdf/g, "") + "test " + ".pdf"
  });

It only extracts a single page.

I have even tried for loop with this. But it extracts 3 separate documents.

Any workaround?

Currently I am thinking of opening a new document and inserting pages with a for loop. Can you suggest a better solution?

TOPICS
Create PDFs , Edit and convert PDFs , JavaScript

Views

961

Translate

Translate

Report

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 ,
Jan 23, 2023 Jan 23, 2023

Copy link to clipboard

Copied

Delete unwanted pages and save the file with a new name.

Votes

Translate

Translate

Report

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 Beginner ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

Thank you. Yes, that's a good alternative.

Votes

Translate

Translate

Report

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 ,
Jan 24, 2023 Jan 24, 2023

Copy link to clipboard

Copied

> Currently I am thinking of opening a new document and inserting pages with a for loop. Can you suggest a better solution?

 

What's wrong with that one?

Votes

Translate

Translate

Report

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 Beginner ,
Jan 28, 2023 Jan 28, 2023

Copy link to clipboard

Copied

Nothing's wrong with that. I was looking for a better way. This is the code I am using right now:

 

var thisDocPath = this.path.replace(thisDocName, "") //folder path (without file name and extension)
var thisDocName = this.documentFileName //file name and extension (without path)


    newFileWithPageNums =
    [
        ['1', '3,6,2,8'], 
        ['2', '1,5,7,5']
    ]

    for (i = 0; i < newFileWithPageNums.length; i++) {
        var aDoc = thisDocPath + thisDocName.replace(/.pdf/g, "") + " test " + newFileWithPageNums[i][0] + ".pdf"
            for (j = 0; j < newFileWithPageNums[i][1].split(',').length; j++) {

                if (j === 0) {
                    this.extractPages({
                        nStart: newFileWithPageNums[i][1].split(',')[j],
                        cPath: aDoc
                    });

                } else {
                    var bDoc = app.openDoc(aDoc);
                    bDoc.insertPages({
                        nPage: j - 1,
                        cPath: this.path,
                        nStart: newFileWithPageNums[i][1].split(',')[j]
                    });
                }
            }
            //save without prompt
            bDoc.saveAs({
                cPath: bDoc.path,
                bCopy: false,
                bPromptToOverwrite: false
            });
        bDoc.closeDoc(true);
    }

This works fine. 

How can I write this code better? Any suggestion with syntax etc?

 

Votes

Translate

Translate

Report

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
LEGEND ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

There isn't any built in function to extract multiple page runs to separate files. So any solution based on copying pages is going to need to copy multiple times. But your code could probably be improved - made more efficient - at the cost of being made more complicated. It's inefficient because it never copies more than one page at a time, and opens and closes the output file each time. The improvements might be

1. Copy all the pages with a specific output target first. So you open each output file once, then copy the pages, then close the output file.

2. Not applicable in this case - if there were any consecutive pages to copy, copy them together rather than separately.

This all gets very complex, and the idea of making multiple copies and deleting the pages you don't want is quite attractive. Obviously, you have the waste of copying pages you don't need, but copying pages is pretty expensive, needing scans of the whole file just to copy one page. 

Votes

Translate

Translate

Report

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 Beginner ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

...and opens and closes the output file each time. 

Could you check again?

 

1. Copy all the pages with a specific output target first. So you open each output file once, then copy the pages, then close the output file.

Can you give an example? I think I am already doing that here. I am extracting with the first page and then inserting other pages into it.

 

 

Votes

Translate

Translate

Report

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
LEGEND ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

It's the line

var bDoc = app.openDoc(aDoc);

which looks (from a casual reading) to be called for each page to copy, when it could be called once per file to copy from. It looks as if aDoc is the same each time through the inner loop.

Votes

Translate

Translate

Report

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 Beginner ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

So, do you suggest 'app.newDoc()' and then 'insertPages'? 
That way, I'll have to delete a blank page in every file, right? Still it will be more efficient then opening and closing and again opening each file- is that what you mean? 

Votes

Translate

Translate

Report

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
LEGEND ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

No, I was suggesting you look at the specific line I mentioned, and consider moving it somewhere else.

Votes

Translate

Translate

Report

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 Beginner ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

No, I was suggesting you look at the specific line I mentioned, and consider moving it somewhere else.

I am sorry I don't have a clue how to do that. Can you help?

But I have come up with another version of the code.

var thisDocPath = this.path.replace(thisDocName, "") //folder path (without file name and extension)
var thisDocName = this.documentFileName //file name and extension (without path)


newFileWithPageNums =
[
	['1', '3,6,2,8'],
	['2', '1,5,7,5']
]

for (i = 0; i < newFileWithPageNums.length; i++) {
	var aDoc = app.newDoc();
	var aDocPath = thisDocPath + thisDocName.replace(/.pdf/g, "") + " test " + newFileWithPageNums[i][0] + ".pdf"
		for (j = 0; j < newFileWithPageNums[i][1].split(',').length; j++) {
			aDoc.insertPages({
				nPage: j,
				cPath: this.path,
				nStart: newFileWithPageNums[i][1].split(',')[j]
			});
		}
	aDoc.deletePages(0);
	//save without prompt
	aDoc.saveAs({
		cPath: aDocPath,
		bCopy: false,
		bPromptToOverwrite: false
	});
	aDoc.closeDoc(true);
}

 

Do you think it is better?

 

Votes

Translate

Translate

Report

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 Beginner ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

LATEST
quote

No, I was suggesting you look at the specific line I mentioned, and consider moving it somewhere else.


By @Test Screen Name

I am sorry I don't have a clue about that. Can you help ne with that?

Now I have updated the code like this:

var thisDocPath = this.path.replace(thisDocName, "") //folder path (without file name and extension)
var thisDocName = this.documentFileName //file name and extension (without path)


newFileWithPageNums =
[
	['1', '3,6,2,8'],
	['2', '1,5,7,5']
]

for (i = 0; i < newFileWithPageNums.length; i++) {
	var aDoc = app.newDoc();
	var aDocPath = thisDocPath + thisDocName.replace(/.pdf/g, "") + " test " + newFileWithPageNums[i][0] + ".pdf"
		for (j = 0; j < newFileWithPageNums[i][1].split(',').length; j++) {
			aDoc.insertPages({
				nPage: j,
				cPath: this.path,
				nStart: newFileWithPageNums[i][1].split(',')[j]
			});
		}
	aDoc.deletePages(0);
	//save without prompt
	aDoc.saveAs({
		cPath: aDocPath,
		bCopy: false,
		bPromptToOverwrite: false
	});
	aDoc.closeDoc(true);
}

Is it better now? Any suggestions?

Votes

Translate

Translate

Report

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