Does anyone know if there is a way to modify a text written in drawtext with the pdf-lib library or another library that I can use in script apps?
I wrote this code:
setTimeout = (func, sleep) => (Utilities.sleep(sleep),func())
async function modifyPdf() {
//async
const pdfData = new Uint8Array(DriveApp.getFileById("1Zy4Fbq__ubjX0NLs7J5WPGj7JlV_8-KJ").getBlob().getBytes());
const { PDFDocument, StandardFonts, PDFName, PDFRawStream, decodePDFRawStream, arrayAsString, rgb, degrees} = PDFLib;
const pdfDoc = await PDFDocument.load(pdfData);
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const pages = pdfDoc.getPages();
const firstPage = pages[0];
const { width, height } = firstPage.getSize();
//firstPage.drawText(`This text was added with JavaScriptnn${' '.repeat(10)}(Google Apps script)!`, {
firstPage.drawText(`This text was added with JavaScript`, {
x: 10,
y: height - 10,
size: 20,
font: helveticaFont,
color: rgb(0.95, 0.1, 0.1)
//rotate: degrees(20),
//opacity: 0.5,
});
const pdfBytes = await pdfDoc.save();
Drive.Files.update(
{
title: "newpdf from apps script v22",
mimeType: "application/pdf"
},
"1Zy4Fbq__ubjX0NLs7J5WPGj7JlV_8-KJ",
Utilities.newBlob(pdfBytes)
)
the problem is that if I run it the first time it will write on the PDF “this text was added…” but if I run it a second time, it will write over it… I would like to be able to change the text so Should we be able to ensure that it replaces it?
Is there a way via this library or another, or another way to achieve this?
for the moment it overlaps every time