Checking Remaining Blank Space on the Final Page of Google Docs

In Google Apps Script, how can I determine the amount of unused space on the final page of a Google Docs document? I attempted the following code, but it fails to provide the correct blank space measurement for the last page.

function calculateEmptySpace(documentContent) {
  var topMargin = documentContent.getMarginTop();
  var bottomMargin = documentContent.getMarginBottom();
  var totalHeight = documentContent.getPageHeight() - topMargin - bottomMargin;
  return totalHeight;
}

You might wanna check out the footer and header spacings too. they also take up some space that might not be obvious at first. Getting those values might help you get a more accurate measurement of the blank space left. Don’t forget to account them in your calcs! :blush: