How to get the number of pages in Google Docs via Google script? [duplicate]
Asked Answered
S

2

6

What should I do to get amount of pages in Google Docs (when converted to PDF) via Google script?

I have tried this, but it returns 0 instead of the number of pages.

function getNumPages() 
{
  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; 

}
Synodic answered 5/4, 2016 at 16:51 Comment(0)
P
6

Your regular expression expects a string like Pages/Count 3 in the PDF file. Logging the contents of the file with Logger.log(data) shows there isn't such a string. Instead, I find the number of pages near the beginning of the file:

<< /Linearized 1 /L 18937 /H [ 687 137 ] /O 10 /E 17395 /N 3 /T 18641 >>

The number following /N is the number of pages. Here is a function extracting it:

function getNumPages() {
  var blob = DocumentApp.getActiveDocument().getAs("application/pdf");
  var data = blob.getDataAsString();
  var pages = parseInt(data.match(/ \/N (\d+) /)[1], 10);
  Logger.log("pages = " + pages);
  return pages; 
}
Parse answered 6/4, 2016 at 21:33 Comment(1)
Why this need to be converted as PDF? @ParseBaalman
K
-2
function getNumPages(docId) {
    var pages = 0;
    var blob = DocumentApp.openById(docId).getAs("application/pdf");
    var data = blob.getDataAsString();
    try {
        var matched = data.match(/\/Type[\s]*\/Page[^s]/g);
        pages = matched.length; 
    } catch(err) {
        // NOOP
    }
    return pages; 
}
Kaliski answered 17/6, 2021 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.