How can I count total pages in a Google Document using Apps Script?

I need help figuring out how to determine the total page count of a Google Document when it gets exported as PDF using Google Apps Script.

I wrote some code to try this but it keeps giving me 0 as the result instead of showing the actual number of pages. Here’s what I attempted:

function countDocumentPages() {
  var pdfBlob = DocumentApp.getActiveDocument().getAs("application/pdf");
  var blobData = pdfBlob.getDataAsString();
  var pagePattern = /Pages\/Count (\d+)/g;
  var regexMatch;
  var totalPages = 0;

  while(regexMatch = pagePattern.exec(blobData)) {
    Logger.log("FOUND MATCH = " + regexMatch[1]);
    
    var pageNum = parseInt(regexMatch[1]);
    
    if (pageNum > totalPages) {
      totalPages = pageNum;
    }
  }

  Logger.log("total pages = " + totalPages);
  
  return totalPages;
}

What am I doing wrong here? Is there a better way to get the page count from a Google Doc?

PDF parsing is a nightmare because Google’s conversion doesn’t keep consistent structure. I found a workaround that actually works - use the Document service API directly instead of fighting with binary PDF data. Just estimate pages by calculating the document’s content height against standard page dimensions. Build a function that grabs total character count, applies average characters-per-line ratios, then divides by estimated lines per page. It handles different font sizes and spacing pretty well. Won’t be pixel-perfect, but you’ll get way more accurate page counts than trying to regex parse PDF internals, especially with mixed formatting or embedded stuff.

Your regex won’t work because PDF binary data usually doesn’t have that /Pages/Count pattern, especially with Google’s PDF conversion. I’ve hit this same wall before.

Here’s what actually works - convert to PDF first, then count the page objects directly:

function getPageCount() {
  var doc = DocumentApp.getActiveDocument();
  var pdfBlob = doc.getAs('application/pdf');
  var tempFile = DriveApp.createFile(pdfBlob);
  
  // Use a different regex pattern for PDF structure
  var content = pdfBlob.getDataAsString();
  var matches = content.match(/\/Type\s*\/Page[^s]/g);
  
  DriveApp.getFileById(tempFile.getId()).setTrashed(true);
  
  return matches ? matches.length : 1;
}

This counts actual page objects in the PDF instead of hunting for a count header. Way more reliable since those page type declarations are almost always there.

the pdf parsing is kinda hacky. better to just use the doc body - check paragraph count or estimate from content length. try DocumentApp.getActiveDocument().getBody().getParagraphs().length and divide by average paragraphs per page. not perfect, but way simpler than messy pdf data.