Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of modifying existing "word" #33

Closed
richardfrost opened this issue Sep 10, 2018 · 1 comment
Closed

Example of modifying existing "word" #33

richardfrost opened this issue Sep 10, 2018 · 1 comment

Comments

@richardfrost
Copy link

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.

@Hopding
Copy link
Owner

Hopding commented Sep 12, 2018

Hi there @richardfrost. It is not currently possible to parse the content of a page with pdf-lib. However, if you have some other means of knowing where the text is (getting its x & y coords on a page), you could use pdf-lib to draw over it. You might be able to use pdf2json to locate the text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants