5

I want to create a new document based on a template and need to know when my insertion or append results in a new page in the final printed output is there any property/attribute eg number of pages that can be used for this?

DavidF
  • 1,335
  • 1
  • 15
  • 26

2 Answers2

3

I've search this a lot in the past and I don't think there's any property or any other way to know page info.

The solution I use is to insert page breaks on my template or via the script, using my own knowledge of how my template works, i.e. how much space it takes as I iterate, etc. And then I know which page I am by counting the page breaks.

Anyway, you could an enhancement request on the issue tracker.

Henrique G. Abreu
  • 17,406
  • 3
  • 56
  • 65
0

One way to get total number of pages:

function countPages() {
   var blob = DocumentApp.getActiveDocument().getAs("application/pdf");
   var data = blob.getDataAsString();

   var re = /Pages\/Count (\d+)/g;
   var match;

   var pages = 0;

   while(match = re.exec(data)) {
      Logger.log("MATCH = " + match[1]);

      var value = parseInt(match[1]);

      if (value > pages) {
         pages = value;
      }
   }

   Logger.log("pages = " + pages);

   return pages; 
}
ColdCold
  • 4,181
  • 2
  • 26
  • 20