在TextField IText中调整文本 [英] Fit text in TextField IText

查看:144
本文介绍了在TextField IText中调整文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果有类似我的帖子,但我是该论坛的新手,但是我没有找到它.

Sorry if there exists similar post like mine but I'm new on this forum and I haven't find it.

我无法动态调整TextField的大小,具体取决于文本的大小.我填写现有的PDF-在AcroForm中填写字段:

I have problem with dynamically resize TextField size depends on text size. I fill existing PDF - fill fields in AcroForm:

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

一切正常,但是我也想设置文本(在TextField中),该文本的大小大于字段的大小.如何动态调整TextField的大小(在setField之后/之前,或者在AcroForm创建过程中设置某些字段属性)以适合文本(大于TextField的文本)?此TextField的大小必须与文本大小完全相同,而不能将字体大小更改为较小的大小,并且在此TextField中不能滚动.

Everything it's OK, but I also want to set text (in TextField) which size is greater than field size. How can I dynamically resize TextField (after/before setField or maybe set some field property in AcroForm creation process) to fit text (text larger than TextField)? This TextField must have size exactly like text size, without changing font size to smaller size and without scrolls in this TextField.

预先感谢您的帮助.

推荐答案

很容易做到与您想要的相反的操作,更改字体大小,使所有文本都可见.您只需将字体大小设置为"0",iText(或Acrobat或其他任何东西)就可以确定要即时使用的字体大小(在合理的范围内).

Well it's easy to do the opposite of what you want, change the font size so all the text is visible. You just set the font size to '0', and iText (or Acrobat, or whatever) determines what font size to use on the fly (within some reasonable limits).

要确定给定文本块的长度,可以调用myBaseFont.getWidthPoint( fieldValToBe, fontSize ).然后,您可以在呼叫setField之前在之前调整字段的大小.默认情况下,iText会为您呈现字段外观,并且在您可以按setField键时完成呈现.除非再次调用setField,否则更改字段大小后缀不会更改字段的外观.

To determine the length of a given chunk of text, you can call myBaseFont.getWidthPoint( fieldValToBe, fontSize ). Then you can size the field before you call setField. iText renders field appearances for you by default, and that rendering is done when you can setField. Changing the field size afterwords won't change the field's appearance unless you call setField again.

好的,那么您如何更改字段的大小? iText不直接支持它,因此您必须使用iText的低级PDF对象来做到这一点.像这样:

Okay, so how do you change the field's size? iText doesn't support that directly, so you have to do it with iText's low-level PDF objects. Something like this:

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需要考虑权利&左边框的厚度.我猜大概有3-5点,具体取决于斜边与平边,线宽等.您可能只需要选择一个值并使用它即可.

FUDGE_FACTOR needs to account for right & left border thicknesses. I'd guess 3-5 points, depending on beveled vs flat borders, line thickness and so forth. You can probably just pick a value and go with it.

循环可能是不必要的,因为很少有多个字段共享一个名称. OTOH,如果这是您要面对的问题,则可能还需要重新计算newWidth,因为不同的实例不需要共享相同的字体大小.

The loop is probably uneccessary, as it's rare for more than one field to share a name. OTOH, if that's what you're up against, you might also need to recalculate newWidth because different instances need not share the same font size.

最后,您可能需要将此新的rectArr写入项目的合并"版本以及小部件版本.在处理字段时,iText几乎可以与合并版本一起使用,因为所有可能的键/值对都在那里,您可能必须在其中使用小部件版本检查父字段值.

And finally, you might need to write this new rectArr into the "merged" version of the item as well as the widget version. iText almost universally works with the merged version when manipulating a field because all the possible key/value pairs are right there, where you might have to check parent field values with the widget version.

OTOH,给定的合并"和小部件"应该共享相同的矩形PdfArray,从而呈现出点意义. "Rect"是一个"leaf"值,并且永远不会从父级继承,因此小部件的数组将被浅复制"到合并的字典中……从而共享它.无论如何,您都应该可以轻松地对其进行检查.

OTOH, a given "merged" and "widget" should share the same rectangle PdfArray, rendering the point moot. "Rect" is a "leaf" value and will never be inherited from a parent, so the widget's array will have been "shallow-copied" into the merged dictionary... thus sharing it. In any case, you should be able to check it fairly easily.

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

请注意,这是==而不是.equals.我认为PdfArray 没有equals(),所以这一点也不是很重要.

Note that this is == not .equals. I don't think PdfArray has an equals(), so this point isn't all that relevant either.

哦,仅因为我真的有工作要做,我就让您找出如何从一个字段中自己获取BaseFont的方法,并向正确的方向轻推.您需要通过BaseFont.createFont(PRIndirectReference fontRef)DocumentFont,并应签出

Oh, and just because I've actually got work to do, I'll let you figure out how to get a BaseFont from a field on your own, with a nudge in the right direction. You'll want a DocumentFont via BaseFont.createFont(PRIndirectReference fontRef), and you should check out The PDF Spec, chapter 12.7 (Interactive Forms) and 9.5-9.10 (various font types... which DocumentFont will largely take care of for you) to find out where to find that indirect reference.

要弄清楚间接引用到底有什么用,您需要阅读第7.3章对象",尤其是第7.3.10节间接对象".

And to figure out what the heck an indirect reference is, you'll need to read chapter 7.3, "Objects", particularly 7.3.10, "Indirect Objects".

这篇关于在TextField IText中调整文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆