OL Learn

Page Number & Page Count within a script?

I see the designer has a button for insterting the page number and page count to a document, which is great.
But, I need this information in a script which is then used in a barcode (barcode is used by our inserting machine to match and enclose into an envelope)

Currently, my only work around is a unique barcode per page that I hardcode these values in, but this falls apart when it’s a variable length document.

Current script example:
var field, result = “”;

field = record.fields[“pitcode”];
if (field !== “”) result += field + “0102000000”;

results.text(result);

So the 0102 in there needs replacing with the page number and page count (codes {#}{##} from the inbuilt designer functionality)
It also needs to take into account that it’s over multiple sections, so needs to the page and total across all sections.

Many thanks

You’ll want to use an output preset to add these barcodes. At the time the design template is run, it doesn’t yet know how many pages there will be, so that information is not yet available to design scripts. You’ll note that the button for adding page numbers in design simply adds a placeholder which is replaced after the pagination has completed.

First and foremost, you’re going to want to refer to this list of variables:
https://help.objectiflune.com/en/planetpress-connect-user-guide/2020.1/#designer/Output/Print/Variables_available_in_the_Output.htm

This contains a list of all of the variables that you can access in the output preset. You can also pass values forward from your datamapper through metadata if you include a job preset. Your pitcode, for example, would need to be forwarded on in this way and called using ${document.metadata[‘pitcode’]}

For instance, if you wanted a barcode that displayed the current page and total page count, you’d put

${page.sequence.document)${document.count.pages}

If you wanted to do the same for sheet count:

${sheet.sequence.document}${document.count.sheets}

Now these are just the plain count, no padding or anything. so page 1 of 9 would simply be 19. If you want to pad them, simply indicate the total number of characters in 0s after each variable like this

${sheet.sequence.document,0000}${document.count.sheets,0000}

This will zero pad out to 4 total digits, so our page 1 of 9 from above now comes out 00010009.

You can get fancy with these as well, building more conditional logic into them, since the ${} construct is simply a wraper for javascript code in what is otherwise a text field. Similarly to the datamapper, the last value returned in the brackets is your output value. So for instance, some inserters want to know the start, middle, and end of the document. The following would allow you to do that. You’d simply substitute your inserter’s code for the ‘start’, ‘end’, and ‘middle’ values. Maybe that corresponds to 10 for start, 00 for middle, and 01 for end, for instance.

${
    var sCollation = '';
    var SheetNum = sheet.sequence.document;
    var SheetCount = document.count.sheets;
    if(SheetNum == 1){
        sCollation = 'Start';
    } else if(SheetNum == SheetCount){
        sCollation = 'End';
    } else {
        sCollation = 'Middle'
    }
}

The window is a bit cramped for typing longer code like this, however, so I’d recommend writing it up in a text editor first, then pasting it in.

One final note, you can, of course, include static text in these without needing to use the ${} wrapper. For instance

${sheet.sequence.document}${document.count.sheets} My barcode is getting long

This will print a barcode with the string 19 My barcode is getting long, including all the spacing and line breaks you include.

Thanks. I haven’t been using Connect for that long, what do you mean by an Output Preset? Sorry, I don’t see how to use the $ variables within the barcode?

You’ll want to take a look at this first then. https://learn.objectiflune.com/courses/output-planetpress-connect/

Output presets run after your Connect template and determine what the final output will be. Is it a PDF? A Postscript file? Will it be sent via LPR to a printer or written directly to a folder? It handles this and much more, like imposition settings, extra content on the page (be it barcodes, plain text, images, etc).

Ah, haven’t had a use yet for Output Presets as pretty much every job is different.

After a bit of experimenting I’ve got a barcode being added now with ${document.metadata.pitcode}${sheet.sequence.set,00}${document.count.sheets,00}000000

but how do I ensure the barcode then only prints on the front of every page? Being a duplex document, barcode is not needed on the reverse.

Edit: Fixed by adding page.front to the condition, simple!

1 Like