HummusJS icon indicating copy to clipboard operation
HummusJS copied to clipboard

adding text into existing pdf

Open code-by opened this issue 7 years ago • 8 comments

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

code-by avatar Nov 17 '16 09:11 code-by

should specify font/color/size. you're missing this

galkahana avatar Nov 17 '16 09:11 galkahana

@galkahana how to get font in existing pdf file instead get font from ttf file?

code-by avatar Nov 17 '16 09:11 code-by

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?

galkahana avatar Nov 17 '16 10:11 galkahana

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.

code-by avatar Nov 17 '16 10:11 code-by

K. Cant do that. Need to specify the font

galkahana avatar Nov 17 '16 11:11 galkahana

wanna give this a try? - https://www.npmjs.com/package/font-manager

galkahana avatar Nov 17 '16 11:11 galkahana

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.

aathirag avatar Jul 04 '18 18:07 aathirag

@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.

mohammedabualsoud avatar Oct 02 '19 09:10 mohammedabualsoud