itext 5使用中文字体,加粗和斜体的文本框

huangapple 未分类评论48阅读模式
英文:

itext 5 TextField using chinese font with bold and italic

问题

我正在尝试在itext 5中使用TextField。
我的字体名称是"微軟正黑體",英文名称是"Microsoft JhengHei"。
我想要使用这种字体,同时加粗和斜体。

初始化字体(3代表Font.BOLD | Font.ITALIC,BC是我的基础颜色)

  1. Font fontZh = FontFactory.getFont(CurrentFont.getFontName(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED,
  2. CurrentFont.getFontSize(), 3, BC);

我有一个textField变量,并将fontZh设置为setFont。

  1. field.setFont(fontZh.getBaseFont());

PDF的结果只有字体样式、大小和颜色是正确的。但是加粗和斜体并没有生效。

英文:

I'am trying to using TextField in itext 5.
My font name is "微軟正黑體" in English is "Microsoft JhengHei".
I want to using this font with bold and itatic.

Init Font ( 3 is Font.BOLD | Font.ITALIC and BC is my base color)

  1. Font fontZh = FontFactory.getFont(CurrentFont.getFontName(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED,
  2. CurrentFont.getFontSize(), 3, BC);

I have a textField variable and set fontZh to setFont.

  1. field.setFont(fontZh.getBaseFont());

The pdf result only the font style, size and color is correct. But the bold and itatic isn't work.

答案1

得分: 0

只能将BaseFont设置给字段。这特别意味着,来自于FontFactory.getFont调用的那些仅成为返回的Font的一部分而不是基础BaseFont的属性,不包含在使用fontZh.getBaseFont()检索到的基础字体中,因此不会被设置到字段中。

在这种情况下丢失的属性包括特定的样式信息,比如斜体粗体(除非引用的字体文件本身已经是斜体或粗体,或者引用的字体系列已经注册了粗体或斜体字体文件),以及颜色。

<sub>(您在测试中提到颜色是正确的;如果是这样,那不是因为通过字体传输的。由于您没有展示足够的代码进行良好的分析,我只能猜测;我想可能颜色要么是默认值,要么是您使用其他指令设置的。)</sub>

因此,如果您想在文本字段中使用粗体或斜体字体,您应该使用引用已经以粗体和斜体的所需组合绘制字符的字体文件的基础字体,例如在Century Gothic的情况下,使用GOTHICBI.TTF代替GOTHIC.TTF。我不知道是否有“微软正黑体”的这种变体。

如果您想设置字段的字体颜色,可以在构建字段字典之前通过以下方式设置:

  1. field.setTextColor(BC);

使用field.getTextField()

英文:

You can only set a BaseFont to a field. This in particular means that those properties from your FontFactory.getFont call which merely become part of the returned Font and not of the underlying BaseFont, are not in the base font you retrieve using fontZh.getBaseFont() and, therefore, are not set to the field.

Among the properties that get lost this way are in particular style information like italic or bold (unless the referenced font file already is italic or bold or the referenced font family is registered with a bold or italic font file) and the color.

<sub>(You say that in your test the color is correct; if it is, that is not because of it being transported via the font. As you don't show enough code for a good analysis, I can only guess; I would surmise that the color either is the default to start with or you set it using some other instruction.)</sub>

Thus, if you want to have a bold or italic font used in a text field, you should use a base font that references a font file which already draws characters in the desired combination of bold and italic, e.g. in case of Century Gothic use GOTHICBI.TTF instead of GOTHIC.TTF. I don't know whether you have such a variant of "微軟正黑體".

If you want to set the color of the font of a field, you can do so by setting it to the field, i.e.

  1. field.setTextColor(BC);

before building the field dictionary using field.getTextField().

huangapple
  • 本文由 发表于 2020年4月7日 12:06:13
  • 转载请务必保留本文链接:https://java.coder-hub.com/61072662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定