Copy link to clipboard
Copied
Hi all,
maybe the answer to my question is so obvious, that I just can't find it, I don't know. So here I am, hoping for some help.
I programmatically like to add annotations. I already know how to do that. I started with the JavaScript for Acrobat API Reference and a Guide called Developing Acrobat Applications Using JavaScript.
Question in short: How can i determine the correct rect size of the freetext annotation, so that the whole text i place in there is shown? Is there a function to calculate the width and heigth of multiline or single line Text?
Example:
var annot = this.addAnnot({
page: 0,
type: "FreeText",
textFont: font.Helv,
textSize: 10,
rect: [200, 300, 200+200, 300+3*12],
contents: "Line1!\nLonger Line 2!\nReeeaaaaalllyyyy loooooong Line3!",
author: "Me",
width: 1,
alignment: 0 });
I already know how to get the quads from Words inside the document. So I thought I add a new page, write the text that should appear in the annotation, get the quads, transform to rect, delete the page and have my rects. But I figured that I can't add simple Text, is that right?
I am new to the Adobe API and I am stuck with this...
Copy link to clipboard
Copied
That is actually a great idea, but implementing it is a bit tricky, since as you said you can't add static text to a PDF using a script, not directly at least. Indirectly you can do it by adding the text to a form field (or even a Free Text annotation) to a page, flatten that page and then search for that word, read its quads and then delete that page (or redact that word). It's a bit involved, but it should work.
Copy link to clipboard
Copied
Big Thanks for pushing me into the right direction! I did not know about page flattening and redaction. That could really do the trick. I will try it out. If it works i will mark this as answer and post the code.
Copy link to clipboard
Copied
There may be a couple of easier ways to do this. First, the size of text on a page can be found with the BBox page box. You may be able to skip searching for word quads by just getting this box. The text will need to be added to a completely blank page.
Another method is to use the text field, which provides an event property that indicates whether the text overflowed the field. Just add a text field of a fixed width and height, add a keystroke event script that tests for the this event property, and calls a callback function that repeats this process, including deleting the previous field, until a correct size is found.
Note: I haven't tested either of these methods, they are just ideas. However, there is another method using the line annot and caption text that I know works.