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

invisible text layer #1455

Open
lbr991 opened this issue Jul 4, 2023 · 1 comment
Open

invisible text layer #1455

lbr991 opened this issue Jul 4, 2023 · 1 comment

Comments

@lbr991
Copy link

lbr991 commented Jul 4, 2023

I'm trying to take a non-searchable pdf and convert it to searchable pdf by superimposing an invisible text layer.
I need the invisible text bboxes to align exactly with the original bboxes (extracted by Textract).
I also don't want to specify a font and font_size because then the bboxes wouldn't align perfectly.

Is this possible with pdfkit?

@bugamehmet
Copy link

const PDFDocument = require('pdfkit');
const fs = require('fs');

const originalText = 'This text will be searchable';
const invisibleText = 'This text will be invisible';

const doc = new PDFDocument();

const outputPath = 'output.pdf';

// Set the position where you want to add the text
const x = 100;
const y = 100;

// Get the bounding box of the original text without specifying fontSize and font
const originalBBox = doc.widthOfString(originalText);

doc.text(originalText, x, y);

doc.opacity(0.0); // Set opacity (0.0: completely invisible, 1.0: fully visible)
doc.text(invisibleText, x + originalBBox.width, y);

// Save the file
doc.pipe(fs.createWriteStream(outputPath));
doc.end();

console.log('PDF created:', outputPath);

With this updated code, the invisible text will be positioned exactly to the right of the visible text, maintaining the same alignment without specifying the font size and font family.

You try it.

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