Status Update
Comments
br...@gmail.com <br...@gmail.com> #2
I very much agree.
I have a situation where I am populating a document with various elements through a script, and it is necessary that certain elements are not split across two pages. In this case I would insert a page break to bump the element onto the next page.
However this is currently impossible as there is absolutely no way that I'm aware of to access information about which element a page is on, or even the number of pages in a document. Even if I work of a template document that has a page count in the footer, when I access this from the script it is read as an empty string.
Calling findElement() to look for DocumentApp.ElementType.UNSUPPORTED types causes a server error.
I therefore would appreciate API even just to access how many pages in a document would solve my problem.
I have a situation where I am populating a document with various elements through a script, and it is necessary that certain elements are not split across two pages. In this case I would insert a page break to bump the element onto the next page.
However this is currently impossible as there is absolutely no way that I'm aware of to access information about which element a page is on, or even the number of pages in a document. Even if I work of a template document that has a page count in the footer, when I access this from the script it is read as an empty string.
Calling findElement() to look for DocumentApp.ElementType.UNSUPPORTED types causes a server error.
I therefore would appreciate API even just to access how many pages in a document would solve my problem.
br...@gmail.com <br...@gmail.com> #3
I would also like access to the page number. I haven't found a work around.
br...@gmail.com <br...@gmail.com> #4
I found a work-around that enables you to determine the number of pages in the document, but it is not very elegant.
The function getNumPages(doc) below will return the number of pages in a document. The parameter is of type Document.
function getNumPages(doc) {
doc.saveAndClose();
var blob = doc.getBlob();
var strData = blob.getDataAsString();
var match = strData.match("Pages/Count [0-9+]");
var nStr = match[0].split(" ")[1];
return parseInt(nStr);
}
What it does is export the document to a Blob, which is the data of the document, which happens to be in PDF format. It then searches the data for a specific field which represents the number of pages present. Unfortunately, this means that the document must be closed first in order to update the Blob, which means any references to any elements in the document will be invalidated, and the document must be reopened.
Consequently, this is quite slow. Like I said, it is not elegant.
In my case, I wanted to make sure that a certain element is not split across two pages, so I would check the number of pages in the document, then insert a page break before the element, then check the number of pages again. If the page count increased, then the element was originally not split across two pages, and I would remove the page break. This method requires that the element in question is the last element in the document.
Hope that can help someone. Actually, I hope the Document API is extended to incorporate this functionality!
The function getNumPages(doc) below will return the number of pages in a document. The parameter is of type Document.
function getNumPages(doc) {
doc.saveAndClose();
var blob = doc.getBlob();
var strData = blob.getDataAsString();
var match = strData.match("Pages/Count [0-9+]");
var nStr = match[0].split(" ")[1];
return parseInt(nStr);
}
What it does is export the document to a Blob, which is the data of the document, which happens to be in PDF format. It then searches the data for a specific field which represents the number of pages present. Unfortunately, this means that the document must be closed first in order to update the Blob, which means any references to any elements in the document will be invalidated, and the document must be reopened.
Consequently, this is quite slow. Like I said, it is not elegant.
In my case, I wanted to make sure that a certain element is not split across two pages, so I would check the number of pages in the document, then insert a page break before the element, then check the number of pages again. If the page count increased, then the element was originally not split across two pages, and I would remove the page break. This method requires that the element in question is the last element in the document.
Hope that can help someone. Actually, I hope the Document API is extended to incorporate this functionality!
br...@gmail.com <br...@gmail.com> #5
Thanks for that. I have given up on this because I believe it is a specific design feature of Google Docs (design non-feature). All I wanted was a table of contents with page numbers. I think Google may see their product as exclusively electronic and therefor deliberately omit page numbers given hyperlinks are possible. But this does not satisfy professional document design and print media.
je...@gmail.com <je...@gmail.com> #6
The solution provided in #3 didn't seem to work when there were 10+ pages.
Thanks to Jason Millard for this solution.
function testCountPages() {
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;
}
Thanks to Jason Millard for this solution.
function testCountPages() {
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;
}
Description
It would help automated header section generation script if document API provided mean to create and access page number element.
Notes:
* HeaderSection and FooterSection would need additional interface to add and insert page numbering elements.
* New PageNumber object type would be needed to have script access to existing page numbering element.