iText 中的 TIMES NEW ROMAN 字体(Java 版)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4474125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TIMES NEW ROMAN font in iText (Java version)
提问by Lucas
I need to do something like this in iText:
我需要在 iText 中做这样的事情:
Font fuente=new Font(Font.getFamily("ARIAL"),30,Font.BOLD);
But instead using ARIAL font, I need to use the TIMES NEW ROMAN font over a Paragraph. How could I accomplish this?
但是,我需要在段落上使用 TIMES NEW ROMAN 字体,而不是使用 ARIAL 字体。我怎么能做到这一点?
Thanks.
谢谢。
回答by Mark Storer
You have a problem.
你有问题。
Font.getFamily(string) only works for the "Base 14" fonts, Helvetica, Times Roman, Courier (plus their bold & italic variants), plus Symbol and ZapfDingbats. Asking it for anything but one of those fonts will return FontFamily.UNDEFINED.
Font.getFamily(string) 仅适用于“Base 14”字体、Helvetica、Times Roman、Courier(以及它们的粗体和斜体变体),以及 Symbol 和 ZapfDingbats。除了这些字体中的一种之外,要求它提供任何东西将返回 FontFamily.UNDEFINED。
Your code isn't working the way you think it does. If you happen to be ending up with Arial, that's only because it's the default font.
您的代码没有按照您认为的方式工作。如果您碰巧最终使用 Arial,那只是因为它是默认字体。
What you want is FontFactory.getFont(...)
. Before FontFactory can return a given font by name, you need to register that font's file. The easiest way to do that is to call FontFactory.registerDirectories()
which will enumerate all the directories known operating systems use to store fonts and register all the fonts found there. This will take longer depending on how many fonts you have there, and how big those fonts are. A lot of CJKV fonts will take a significant amount of time to register... under 30 seconds in all likelyhood, but my beast of a system can take 10-20 seconds to do so (but I have a LOT of Big Fonts).
你想要的是FontFactory.getFont(...)
. 在 FontFactory 可以按名称返回给定字体之前,您需要注册该字体的文件。最简单的方法是调用FontFactory.registerDirectories()
which 将枚举已知操作系统用于存储字体的所有目录并注册在那里找到的所有字体。这将需要更长的时间,具体取决于您有多少字体以及这些字体有多大。许多 CJKV 字体将花费大量时间来注册……很可能不到 30 秒,但我的系统野兽可能需要 10-20 秒才能注册(但我有很多大字体)。
You can also register individual files with FontFactory.register(fontPath)
, but that requires that you know where to find them in the first place.
您也可以使用 注册单个文件FontFactory.register(fontPath)
,但这首先要求您知道在哪里可以找到它们。
At any rate, your code could read something like:
无论如何,您的代码可以是这样的:
FontFactory.registerDirectories();
Font fuente = FontFactory.getFont("Times New Roman");
And FontFactory does indeed ignore case when looking up fonts. Font.getFamily()
does not.
FontFactory 在查找字体时确实会忽略大小写。 Font.getFamily()
才不是。