Batch add same action / same javascript on page x .. y

New Here ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

I have pdf's from scanned documents. Most pages have size A4 (210x297mm). 'Document Properties' > 'Initial View' > 'Page layout' is set to 'Two-up' (Cover page)', so that always 2 pages will be showed side by side, like reading a book. Now, few pages have the double size, A3 (297x410mm). Viewing them, 'Page layout' should switch to 'Single sided'. Going back or forward to an A4 page, 'Page layout' should switch back to 'Two-up'.

The only method I found for achieving this, was by adding an action to each page of the document. The trigger is 'Open page', the action 'javascript'. Depending on the page size:

this.layout = "TwoPageRight";

this.layout = "SinglePage";

My quesion: How can I add such (identical) actions with (identical) javascript snippets conveniently for page x to y ? I could not find any feature in preflight.

TOPICS
Acrobat SDK and JavaScript, Macintosh, Windows

Views

221

Likes

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
Most Valuable Participant ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

Preflight is not the right tool for this. It can be done using a script.

Do you want to apply it to all pages, or just some?

Likes

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
New Here ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

Actually I want to apply it just to some pages:  
this.layout = "TwoPageRight";
   to all pages with size A4 (210x297mm),

this.layout = "SinglePage";   to all pages with size A3 (297x410mm).

As usually most pages are A4, I'd like to apply the first javascript to e.g. pages 1 to 56 and 58 to 123, the last javascript to page 57.


But a workaround could be to separate the document in pdfs containing 1 page only, moving all pdfs with an A4 page in one directory and all with A3 in another. Then I could run an batch job, that applies exactly the same javascript action to all pdfs in a directory, and then combine all pdfs of both directories in the right order. (however I don't know yet how to do this). In this procedure I would apply the javascrpt to the one and only page per pdf.

Likes

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
Most Valuable Participant ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

There's no need to split the file, but I don't understand if you want to do it based on the page numbers or their sizes...

Likes

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
New Here ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

Their sizes.

Likes

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
New Here ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

But if I see, that pages with no. x to no. y have the size 'A4', I can do it based on the page numbers as well. Then it's just not fully automatic...

Likes

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
Most Valuable Participant ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

OK, you keep going back and forth, but here you go:

for (var p=0; p<this.numPages; p++) {

    var pageBox = this.getPageBox("Crop", p);

    var pageWidth = Math.floor(pageBox[2]-pageBox[0]);

    if (pageWidth==595) // A4

        this.setPageAction(p, "Open", "this.layout = \"TwoPageRight\";");

    else if (pageWidth==841) // A3

        this.setPageAction(p, "Open", "this.layout = \"SinglePage\";");

    else console.println("Unknown page size: " + (p+1));

}

Likes

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
New Here ,
Nov 17, 2018 Nov 17, 2018

Copy link to clipboard

Copied

LATEST

You are the King! Thank you so much! I worked on that code, as I wanted to have some other requirements fulfilled: if a small (A4) page is the neighbour of a wide (A3) page, both could be still displayed together (as a pair), when I select the smaller page directly by clicking on it's thumbnail, or, when I browse the document and Acrobat reaches that pair of pages with the smaller page on the left side. In both cases, the action ('switching to single layout') will not be triggered. So I altered the code, so that the action (switch to page layout 'SinglePage') will be added also to all smaller (A4) pages, that could be "endangered neighbors" of wide (A3) pages. The positions / page numbers of that smaller (A4) pages depend on if you have a cover page and if the overall number of pages is odd/even. Here's the code:

var maxWidth_mm = 220; // max. page width [mm]for double sided layout (A4)

var maxWith = maxWidth_mm / 25.4 * 72; // max. page width [points] for double sided layout (A4)

// Define page layout for pdf.

var coverSheet = 1; // [0|1]: pdf [without|with] cover sheet

// If 1st page == (single) 'cover' sheet, add page layout action for it.

if (coverSheet == 1) { // add page layout action to cover sheet

    this.setPageAction(0, "Open", "this.layout = \"TwoPageRight\";"); 

    }

// If last page == single sheet, add page layout action for it.

// Calculate number of last left page, that could be displayed together with a right page

if (((this.numPages-coverSheet)%2)==1) {  // number of pages AFTER possible cover sheet is odd

    var lastLeftPage=(this.numPages-3);

    this.setPageAction(((this.numPages)-1), "Open", "this.layout = \"TwoPageRight\";");

    }

else { // number of pages AFTER possible cover sheet is even

    var lastLeftPage=(this.numPages-2);

    }

// Loop through all pairs of pages, that could be displayed together,

// add page layout action for single layout, if only one of them is too wide

for (var p=coverSheet; p<=lastLeftPage; p=p+2) { 

    var pageBox_LeftPage = this.getPageBox("Crop", p); 

    var pageWidth_LeftPage = Math.floor(pageBox_LeftPage[2]-pageBox_LeftPage[0]); 

    var pageBox_RightPage = this.getPageBox("Crop", p+1); 

    var pageWidth_RightPage = Math.floor(pageBox_RightPage[2]-pageBox_RightPage[0]);

    if ((pageWidth_LeftPage<maxWith)&&(pageWidth_RightPage<maxWith)) { // Both pages narrow enough

        this.setPageAction(p, "Open", "this.layout = \"TwoPageRight\";");      

        this.setPageAction((p+1), "Open", "this.layout = \"TwoPageRight\";");

        }

    else { // At least one page too wide

        this.setPageAction(p, "Open", "this.layout = \"SinglePage\";");      

        this.setPageAction((p+1), "Open", "this.layout = \"SinglePage\";");

        }

    }

Likes

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