HummusJS
HummusJS copied to clipboard
adding text into existing pdf
I need to add text to first page of existing pdf. I try this, but new pdf not opened in some pdf-viewers, adobe reader don't render new text. Here is my code:
const hummus = require('hummus');
const pdfWriter = hummus.createWriterToModify(__dirname + '/existing.pdf', {
modifiedFilePath: __dirname + '/output.pdf',
});
const pageModifier = new hummus.PDFPageModifier(pdfWriter, 0);
pageModifier.startContext().getContext().writeText(
'Test Text', 10, 10
);
pageModifier.endContext().writePage();
pdfWriter.end();
is there is mistake, thank you
should specify font/color/size. you're missing this
@galkahana how to get font in existing pdf file instead get font from ttf file?
Doesn't work like that. have to have the font file. you could theoretically extract the font from the pdf to a file and use it like that, but given that the font may not be embedded, or that it will have just the glyphs used and not necessarily those you want to use, i wouldn't recommend this as a path.
wanna tell more about the scenario you have in mind?
I simply need add text using fonts already embedded in pdf. I saw some other node packages for creating pdf files and these don't needed external font file, but these packages don't allow modify existing pdf.
K. Cant do that. Need to specify the font
wanna give this a try? - https://www.npmjs.com/package/font-manager
For me also after writing text. pdf is not opening in Adobe Reader. What I have to do for opening in Adobe Reader. It's opening in Browser and in ubuntu system default pdf reader.
@galkahana It works when I added the fonts. My question is I have the position of a specific object which is a text it's form is like this:
placement { text: '{{C1}}',
matrix: [ 1, 0, 0, 1, 168.912, 643.602 ],
localBBox: [ 0, -2.898, 36.596000000000004, 10.052 ],
globalBBox: [ 168.912, 640.704, 205.508, 653.654 ] }
How can I remove that object or better delete that object and render a new text on top of it.
Thanks in advance.