Adding Page Numbers to PDF using Google Apps Script

Kanshi Tanaike
Google Cloud - Community
2 min readMay 15, 2024

Description

This is a simple sample script for adding the page numbers to PDF data using Google Apps Script.

When you use this script, please copy and paste the following script to the script editor of Google Apps Script. And, please set the file ID of the PDF file.

Sample script

In this script, pdf-lib is used.

/**
* ### Description
* Add page numbers to PDF.
*
* @param {Object} blob PDF blob.
* @param {Object} pageFormat Format of page number.
* @returns {Blob} Updated PDF blob.
*/
async function addPageNumbers_(blob, pageFormat) {
if (blob.getContentType() != MimeType.PDF) {
throw new Error("Blob is not PDF.");
}

// Load pdf-lib
const cdnjs = "https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js";
eval(UrlFetchApp.fetch(cdnjs).getContentText().replace(/setTimeout\(.*?,.*?(\d*?)\)/g, "Utilities.sleep($1);return t();"));

const data = new Uint8Array(blob.getBytes());
const pdfData = await PDFLib.PDFDocument.load(data);
const pdfDoc = await PDFLib.PDFDocument.create();
(await pdfDoc.copyPages(pdfData, pdfData.getPageIndices()))
.forEach((page, i) => {
const { width } = page.getSize();
const obj = { center: width / 2, left: 20, right: width - 20 };
const pageFormatObj = { ...pageFormat };
pageFormatObj.x = obj[pageFormat.x];
page.drawText(`${i + 1}`, pageFormatObj);
pdfDoc.addPage(page);
});
const bytes = await pdfDoc.save();
return Utilities.newBlob([...new Int8Array(bytes)], MimeType.PDF, `new_${blob.getName()}`);
}

// Please run this function.
function sample1() {
const fileId = "###"; // Please set the file ID of the PDF file.
const pdfBlob = DriveApp.getFileById(fileId).getBlob(); // Of course, you can directly give the PDF blob.

const pageFormat = { size: 10, x: "center", y: 10 };
addPageNumbers_(pdfBlob, pageFormat).then((newBlob) =>
DriveApp.createFile(newBlob)
);
}

// This function is a simple demonstration script.
function sample2() {
// Create a sample Google Document.
const tempDoc = DocumentApp.create("tempDoc");
const body = tempDoc.getBody();
for (let p = 0; p < 5; p++) {
body.appendParagraph(`sample text ${p + 1}`).appendPageBreak();
}
tempDoc.saveAndClose();
const pdfBlob = tempDoc.getBlob();

// Add page numbers.
const pageFormat = { size: 10, x: "center", y: 10 };
addPageNumbers_(pdfBlob, pageFormat).then((newBlob) =>
DriveApp.createFile(newBlob)
);
}
  • When you run the function sample1, the page numbers are added to the center of each page.
  • When you run the function sample2, a new Google Document is created including 5 pages. And, the page numbers are added to the center of each page.
  • In this sample, a simple format like { size: 10, x: "center", y: 10 } is used for the page numbers. Here, the page numbers are put to only “left”, “center”, and “right” of the bottom of the page. But, there are various parameters in DrawTextOptions. Ref So, when you want to customize more, please modify the script.

--

--

Kanshi Tanaike
Google Cloud - Community

Physicist / Ph.D. in Physics / Google Developer Expert / Google Cloud Champion Innovator