Paste Text into TextField IText

Sorry if there is the same mail as mine, but I am new to this forum and I have not found it.

I have a problem with dynamically resizing TextField depending on the size of the text. I fill in the existing PDF fill fields in AcroForm:

form.setField ("field", "value"); (etc.)

Everything is fine, but I also want to set the text (in the TextField) that is larger than the size of the field. How can I dynamically resize a TextField (after / before setField, or possibly set some field property during the creation of AcroForm) to fit the text (the text is larger than the TextField)? This TextField should have the size just like the size of the text, without changing the font size to a smaller size and without scrolling in this text box.

Thanks in advance for your help.

+4
source share
1 answer

Well, it's easy to do the opposite of what you want, to change the font size so that all the text is visible. You simply set the font size to “0”, and iText (or Acrobat or something else) determines which font size to use on the fly (within some reasonable range).


To determine the length of a given piece of text, you can call myBaseFont.getWidthPoint( fieldValToBe, fontSize ) . Then you can change the field to which you call setField . iText displays the fields for you by default, and rendering is done when you can setField . Changing the delays to the size of the field will not change the appearance of the field unless you call setField .

Ok, so how do you resize the field? iText does not support this directly, so you need to do this using low level iText PDFs. Something like that:

 AcroFields.Item fldItem = myAcroFields.getFieldItem("fieldName"); for (int i =0; i < fldItem.size(); ++i) { // "widget" is the visible portion of the field PdfDictionary widgetDict = fldItem.getwidget(0); // pdf rectangles are stored as [llx, lly, urx, ury] PdfArray rectArr = widgetDict.getAsArray(PdfName.RECT); // should never be null float origX = rectArr.getAsNumber(0).floatValue(); // overwrite the old value. rectArr.set( 2, new PdfNumber( origX + newWidth + FUDGE_FACTOR ) ); } 

FUDGE_FACTOR should consider the thickness of the right and left borders. I would suggest 3-5 points, depending on the beveled and flat borders, line thickness, etc. You can probably just pick a value and go with it.

The loop is probably not needed, since it is rarely used for more than one field name. OTOH, if this is what you are facing, you might also need to recount newWidth , because different instances should not have the same font size.

And finally, you may need to write this new rectArr in the "merged" version of the element, as well as the version of the widget. iText works almost everywhere with the merged version when manipulating the field, because there are all possible key / value pairs, where you may have to check the values ​​of the parent field with the version of the widget.

OTOH, this “merged” and “widget” must have the same PdfArray rectangle representing the point. "Rect" is the meaning of "leaf" and will never be inherited from the parent, so the array of widgets will be "finely copied" into the combined dictionary ... so it will share it. In any case, you should be able to verify this quite easily.

 assert item.getWidget(0).getAsArray(PdfName.RECT) == item.getMerged(0).getAsArray(PdfName.RECT); 

Note that these are == not .equals . I don’t think PdfArray has equals() , so this point doesn’t all matter.

Oh, and just because I really have a job, I will let you know how to get BaseFont from the field on my own, with a push in the right direction. You will need a DocumentFont through BaseFont.createFont(PRIndirectReference fontRef) , and you should check the PDF Specification , section 12.7 (Interactive Forms) and 9.5-9.10 (various types of fonts ... that DocumentFont will basically take care of you) to figure out where to find this indirect link.

And to find out what the feature of an indirect link is, you need to read Chapter 7.3, “Objects,” in particular 7.3.10, “Indirect Objects.”

+7
source

All Articles