Skip to content

Example of modifying existing "word" #33

@richardfrost

Description

@richardfrost

First off, thanks for this great project, it looks like you have made awesome progress on it. I am looking for a way to modify an existing word inside a pdf. For example, say I want to replace any instance of the in the document and replace it with an all caps version THE.

An example of what I'm hoping for might be something like this:

import { PDFDocumentFactory, PDFDocumentWriter, drawText } from 'pdf-lib';

const existingPdfDocBytes = fs.readFileSync('testFiles/test.pdf');
const pdfDoc = PDFDocumentFactory.load(existingPdfDocBytes);
const pages = pdfDoc.getPages();

// Code describing what I think I want to do
pages.forEach(function(page) {
  // Need to look through each word on each page:
  page.Content.magicFunction.words.forEach(function(word){
    let wordText = word.toString();
    if (wordText.match(/^the$/)) {
      wordText = wordText.toUpperCase();
    }

    drawText(
      wordText,
      word.coordinates.x,
      words.coordinates.y,
      ...
    );
  });
});

Is this in the scope of the project? Is it already implemented and I'm just missing it? Thanks for your time. If this isn't in the scope feel free to just close this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions