Use logical page numbering in increments of 2 within header or footer  SOLVED

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

DIV
User
Posts: 258
Joined: Fri Jun 23, 2017 1:47 am

Use logical page numbering in increments of 2 within header or footer

Post by DIV »

TrackerSupp-Daniel wrote: Wed Aug 24, 2022 4:23 pm Yes, by defualt, logical page numbering is disabled in our application. To enable display of these values, open the preferences (Ctrl+K) and enable this option under the "Page display" category:
image.png
By default, Editor won't show logical page numbers — even if a user has just finished manually adjusting the logical numbering of pages!
As noted, the user has the ability to turn on this feature, meaning that the logical numbering is shown in the page Thumbnails pane, and also in the page-number display/field at the bottom of the window.

But how can I apply the logical page numbering in a header or footer?
image(1).png
The answer is in the Product Manual for Editor: use %[Page:L] instead of just the default %[Page].
" 'L' specifies page labels as the page numbering format in cases where page labels have been specified. "

So far, so good.

But let's say I have scans from a book, and I've scanned the pages two at a time, creating a PDF file with landscape logical-pages that each represent two (facing) pages of the book.
How could I number these logically?

The most basic option would be for me to label each logical-page with a single logical-page-number, each incrementing by two.
For instance, if the first scan is of book-pages 6 & 7, I might label it as "7", next a scan of 8 & 9, labelled as "9", and so on through to the last scan of 26 & 27, labelled as "27".
However, I don't know how to create logical-page-numbers (a.k.a. labels) that increment by 2, unless I manually create a separate page range for every single PDF-page, which is extremely laborious.
(Perhaps it's possible to create a JavaScript procedure to automate this??)

Another possibility would be to use some advanced macro features to arithmetically alter the output of %[Page:L] or %[Page]. Hypothetically, something like %[2 * Page + 5]. Except that such functionality doesn't seem to exist as yet (certainly not with that syntax!).
I am speculating that it might be difficult to add that functionality to the existing macros, but maybe two new macros could be added, called %[Add] and %[Multiply]. Each would take two parameters. So the above hypothetical command would be amended to
%[Add:%[Multiply:2;%[Page]];5]
Note that subtraction could then be accomplished by inserting a hyphen (representing a negative), and division by 2 would be the same as multiplication by 0.5, and so on.

—DIV
You do not have the required permissions to view the files attached to this post.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19924
Joined: Mon Jan 12, 2009 8:07 am

Re: Use logical page numbering in increments of 2 within header or footer  SOLVED

Post by Stefan - PDF-XChange »

Hello DIV,

Probably adding such macros will be a bit too specific, however you can do your labeling with the below JS:

Code: Select all

for (var i = 0; i < this.numPages; i++){
  this.addWatermarkFromText({
    cText: "Your current label is " + (this.getPageLabel(i)*2-1), 
    nFontSize: 12,
    nTextAlign: app.constants.align.right, 
    nHorizAlign: app.constants.align.right, 
    nVertAlign: app.constants.align.bottom, 
    nHorizValue: -72, nVertValue: 72,
    nStart: i,

  })
}
I have this.getPageLabel(i)*2-1 - so that for me the created watermarks list the number of the first of the two pages:

image(1).png
And you might need to remove the -1 or maybe even add +1 depending on your book layout and e.g. if there is a cover page that you want excluded.

The Horizontal and Vertical alignment values are in increments of 1/72 of an inch - so 72 there is equal to one inch.

P.S. Please also note that if a page label has a text value - the above calculation will likely break and not work correctly as I am not using any checks to se if the "label" value can actually be used in a multiplication. My file does not really have page labels so I am effectively working with the page numbers. The same would apply to your file - if there is some custom page label that is not a numeric value - you can expect the above to not work correctly. If that is the case let me know and I can rework it so that it uses the actual page numbers and then specifies some offset.

Kind regards,
Stefan
You do not have the required permissions to view the files attached to this post.
DIV
User
Posts: 258
Joined: Fri Jun 23, 2017 1:47 am

Re: Use logical page numbering in increments of 2 within header or footer

Post by DIV »

Tracker Supp-Stefan wrote: Mon Jun 12, 2023 4:54 pm you can do your labeling with the below JS:
Cool!

I have struggled with where to find out more about the JavaScript commands.
But when I see a sample, it gives me a foundation to modify it.

So I had a go at adding in extra variables that may be of interest to other users (font name, text opacity, and text colour), I've added ability to specify the positioning in millimetres, I've added an offset if the first page of the book is more than 1, and this now prints appropriate numbers in both the lower left and lower right corners.

Code

Code: Select all

// Add page numbers to the bottom of left and right corners of each (landscape) PDF-page of
// scanned image from a book, each consisting of a pair of (facing) pages of the book.  
// Code by DIV, 2023-06-14.  
// Adapted from original code by Stefan (Tracker Software) 2023-06-12.
// https://forum.pdf-xchange.com/viewtopic.php?p=169903#p169903

// Run it in PDF-XChange Editor through the JavaScript Console (on the "Form" menu).  

firstPageOfBook = 38		// Set the number to be added to the bottom-left of the first PDF-page.  

// Using the metric system...  
mmInFromSide = 5		// Set the spacing from the side of the page (in mm).  
mmUpFromBottom = 5		// Set the spacing from the bottom of the page (in mm).  
inchesToMillimetres = 25.4

// Iterate through all pages of the currently active PDF file.  
for (var i = 0; i < this.numPages; i++){	// JavaScript indexes the pages of the PDF file starting at 0 for the first page.  
    watermarkCorner(0)	// Left corner.
    watermarkCorner(2)	// Right corner.
}

// A function for the watermarking code so that it can be reused in the left and right corners.  
// Input 0 for the left corner, or 2 for the right corner.  
function watermarkCorner(crnr) {
  if (crnr) {
    // Right corner
    txt = firstPageOfBook + (i*2 + 1)
    side = app.constants.align.right
  } else {
    // Left corner
    txt = firstPageOfBook + (i*2)
    side = app.constants.align.left
  }
  this.addWatermarkFromText({
    aColor: ["RGB", 0.5, 0.5, 0.75],	// This is a pale purple colour.  
    cText: txt, 
    cFont: "Zilla Slab SemiBold Italic",
//    cFont: "ZillaSlab-SemiBold Italic",		// Either style of specifying the font name seems to be acceptable.  
    nFontSize: 16,
    nTextAlign: side, 
    nHorizAlign: side, 
    nVertAlign: app.constants.align.bottom, 
    nHorizValue: (1-crnr) * 72 * mmInFromSide / inchesToMillimetres, 
    nVertValue: 72 * mmUpFromBottom / inchesToMillimetres,
    nOpacity: 0.75,
    nStart: i,
    nEnd: i	// The range of pages to watermark is from i to i (that is, a single page!) on each iteration.  
  })
}

// Other references:
// https://stackoverflow.com/questions/46072266/pdf-page-number-font-color-using-addwatermarkfromtext
// https://stackoverflow.com/questions/10263190/why-does-this-javascript-code-print-undefined-on-the-console
// https://acrobatusers.com/tutorials/watermarking-a-pdf-with-javascript/
// https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html#addwatermarkfromtext
I dropped use of the page label, because there's no easy way to set the logical numbering manually for these cases, and — even if it can be implemented automatically — the choice of which logical-number to assign is debatable. I couldn't figure out a dedicated command to access the page number, so I just reused iteration variable i.

The above code does not handle cases in which the scanning skipped some pages in the book, nor cases in which the scan contains front matter (e.g. table of contents) that precedes page "1" of the book. However, it could be adapted.

Screenshots

The JavaScript Console:
image.png
Result of numbering with the settings in the above code:
image(1).png
—DIV
You do not have the required permissions to view the files attached to this post.
Last edited by DIV on Mon Jun 26, 2023 3:25 am, edited 1 time in total.
DIV
User
Posts: 258
Joined: Fri Jun 23, 2017 1:47 am

Re: Use logical page numbering in increments of 2 within header or footer

Post by DIV »

Tracker Supp-Stefan wrote: Mon Jun 12, 2023 4:54 pm Probably adding such macros will be a bit too specific
Not sure: my feeling is that the %[Add] and %[Multiply] macros, as proposed by me above, might not be particularly difficult for your staff to develop, and might be used in several useful ways. Even if they turn out not to be necessary in the present case.

I'd be interested to hear whether other users would find them helpful or not.

—DIV
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19924
Joined: Mon Jan 12, 2009 8:07 am

Re: Use logical page numbering in increments of 2 within header or footer

Post by Stefan - PDF-XChange »

Hello DIV,

They might not be too difficult to implement - but then you are 'opening a can of worms' as people will try to add apples to pears (to strings) - and those won't work. And in any case - calculations should really be done when you have a bit more control over the whole process, and not as part of a naming macro. But certainly - lets see if anyone esle finds your initial proposition useful!

Kind regards,
Stefan
DIV
User
Posts: 258
Joined: Fri Jun 23, 2017 1:47 am

Re: Use logical page numbering in increments of 2 within header or footer

Post by DIV »

Tracker Supp-Stefan wrote: Tue Jun 13, 2023 4:54 pm people will try to add apples to pears (to strings) - and those won't work.
I hear what you're saying.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19924
Joined: Mon Jan 12, 2009 8:07 am

Use logical page numbering in increments of 2 within header or footer

Post by Stefan - PDF-XChange »

:)
DIV
User
Posts: 258
Joined: Fri Jun 23, 2017 1:47 am

Re: Use logical page numbering in increments of 2 within header or footer

Post by DIV »

DIV wrote: Mon Jun 12, 2023 10:58 am But let's say I have scans from a book, and I've scanned the pages two at a time, creating a PDF file with landscape logical-pages that each represent two (facing) pages of the book.
How could I number these logically?
It occurred to me that my original scenario may not have clearly illustrated my motivation, because the original pages of books are typically numbered anyway. It might make more sense for a pictorial magazine, where often page numbers are skipped.
My actual situation was old printouts of a (draft) internal report, printed two-to-a-page, where the original authors had neglected to include any page numbers on any of the pages ...but the report's table of contents refers to page numbers for each chapter, section and sub-section!

Looking back, I also realised that there was still an open question of how to label a large number of pages as individual ranges automatically-ish. Having a basis from the previous scripts to build on, I developed the script below.

Code: Select all

// Add page numbers to the middle of the header or footer region of each (landscape) PDF-page of
// scanned image from a book, each consisting of a pair of (facing) pages of the book.  
// Also assign matching page labels.  
// Code by DIV, 2023-06-26.  
// Following discussion at https://forum.pdf-xchange.com/viewtopic.php?p=169903#p169903

// Run it in PDF-XChange Editor through the JavaScript Console (on the "Form" menu).  

firstPageOfBook = 38		// Set the number to be added to the bottom-left of the first PDF-page.  

// Using the metric system...  
mmInFromEdge = 5		// Set the spacing from the top or bottom of the page (in mm).  
inchesToMillimetres = 25.4

// Iterate through all pages of the currently active PDF file.  
for (var i = 0; i < this.numPages; i++){	// JavaScript indexes the pages of the PDF file starting at 0 for the first page.  
    pgLbl = (firstPageOfBook + (i*2)) + " & " + (firstPageOfBook + (i*2 + 1))
    this.setPageLabels(i, ["a", pgLbl + " ", 16]);
    watermarkMiddle(pgLbl, 0)	// Top middle.
    watermarkMiddle(pgLbl, 1)	// Bottom middle.
}


// A function for the watermarking code so that it can be reused at the top and bottom of the page.  
// txt  = text to print.  
// locn = input 0 for the top of page (header), or 1 for bottom of page (footer).  
function watermarkMiddle(txt, locn) {
  if (locn) {
    // Top of page
    vAlignment = app.constants.align.bottom;
    vPosn = 72 * mmInFromEdge / inchesToMillimetres;
  } else {
    // Bottom of page
    vAlignment = app.constants.align.top;
    vPosn = -72 * mmInFromEdge / inchesToMillimetres;
  }
  this.addWatermarkFromText({
    aColor: ["RGB", 0.5+locn, 0.5, 0.75-locn],	// This is a pale purple colour (top) or orange (bottom).  
    cText: txt, 
    cFont: "Zilla Slab SemiBold Italic",
//    cFont: "ZillaSlab-SemiBold Italic",		// Either style of specifying the font name seems to be acceptable.  
    nFontSize: 16,
    nTextAlign: app.constants.align.center, 
    nHorizAlign: app.constants.align.center, 
    nVertAlign: vAlignment, 
    nHorizValue: 0,
    nVertValue: vPosn,
    nOpacity: 0.75,
    nStart: i,
    nEnd: i	// The range of pages to watermark is from i to i (that is, a single page!) on each iteration.  
  })
}

// References:
// https://stackoverflow.com/questions/10263190/why-does-this-javascript-code-print-undefined-on-the-console
// https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html#setpagelabels
image.png
It seems to work reasonably well.
The only thing is that I couldn't find a way to turn off the numbering, and have only a text label (the "prefix" part of the label). Hence, in the end I opted for lowercase enumeration starting with "p", which looks like it could mean "page".

—DIV
You do not have the required permissions to view the files attached to this post.
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 12152
Joined: Wed Jan 03, 2018 6:52 pm

Re: Use logical page numbering in increments of 2 within header or footer

Post by Daniel - PDF-XChange »

Hello, DIV

Glad to hear you have found a working solution. I am sorry that we couldn't offer anything on our end to accomplish this.

Kind regards,
Dan McIntyre - Support Technician
PDF-XChange Co. LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
DIV
User
Posts: 258
Joined: Fri Jun 23, 2017 1:47 am

Re: Use logical page numbering in increments of 2 within header or footer

Post by DIV »

TrackerSupp-Daniel wrote: Mon Jun 26, 2023 10:27 pm Glad to hear you have found a working solution. I am sorry that we couldn't offer anything on our end to accomplish this.
No problem at all!

I consider that my use-case was rather unusual, and nevertheless a working solution was found based upon your support team's helpful guidance and the built-in JavaScript support.

The only thing I had to go 'outside' for was the documentation on JavaScript commands for PDF files. When I finally twigged that your software implements a 'standard' set of JavaScript commands for PDF files that are documented elsewhere (per comments in my code), it was no longer a problem.

—DIV
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19924
Joined: Mon Jan 12, 2009 8:07 am

Re: Use logical page numbering in increments of 2 within header or footer

Post by Stefan - PDF-XChange »

Hello DIV,

Yes - we use the JavaScript for Acrobat API Reference - with the exception of some 3D content related commands from that file - we should support more than 99% of the rest (and if something does not work quite right - let us know and we will look into it)!

Kind regards,
Stefan