简体   繁体   English

得到DocumentException Arial与Cp1252无法识别

[英]Got DocumentException Arial with Cp1252 is not recognized

the last months I generates thousends of pdfs (on Windows 7 while developing, OpenSuse while test & production) with font Courier. 在过去的几个月里,我使用字体Courier生成了pdfs(在开发时使用Windows 7,在测试和生产时使用OpenSuse)。

Now I got the goal to use Arial. 现在我的目标是使用Arial。 So I switched the font name in our property files and i got this exception: 所以我在属性文件中切换了字体名称,我得到了这个例外:

Stacktrace snippet: com.itextpdf.text.DocumentException: Font 'Arial' with 'Cp1252' is not recognized. Stacktrace片段:com.itextpdf.text.DocumentException:无法识别带有'Cp1252'的字体'Arial'。 at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:708) at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:615) at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:450) at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:708)at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:615)at com.itextpdf.text.pdf.BaseFont.createFont (BaseFont.java:450)

After googleing a while I found a solution. 谷歌一段时间后我找到了解决方案。 I just have to use "Arial.ttf" (like the filename) in the settings. 我只需要在设置中使用“Arial.ttf”(如文件名)。 But after that, I got another exception: 但在那之后,我又得到了一个例外:

 java.io.IOException: Arial.ttf not found as file or resource. at com.itextpdf.text.io.RandomAccessSourceFactory.createByReadingToMemory(RandomAccessSourceFactory.java:224) at com.itextpdf.text.io.RandomAccessSourceFactory.createBestSource(RandomAccessSourceFactory.java:173) at com.itextpdf.text.pdf.RandomAccessFileOrArray.<init>(RandomAccessFileOrArray.java:147) at com.itextpdf.text.pdf.TrueTypeFont.process(TrueTypeFont.java:625) at com.itextpdf.text.pdf.TrueTypeFont.<init>(TrueTypeFont.java:369) at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:699) at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:615) at com.itextpdf.text.pdf.BaseFont.createFont(BaseFont.java:450) 

So the hint there is, that itextpdf is looking for a file right now. 所以有提示,itextpdf正在寻找一个文件。 So I just typed the filename C:\\\\Windows\\\\Fonts\\\\Arial.ttf into our config. 所以我只输入文件名C:\\\\Windows\\\\Fonts\\\\Arial.ttf到我们的配置中。 Now it works! 现在它有效!

But the goal is just to use "Arial" as the fontname. 但目标只是使用“Arial”作为字体名称。 Is it possible? 可能吗?

Thanx a lot! 非常感谢!

It's not possible to just use the name Arial, because Arial isn't listed as one of the 14 standard Type 1 fonts (whereas Courier is one of those 14). 不可能仅使用名称Arial,因为Arial未被列为14种标准Type 1字体之一(而Courier是其中之一)。

If you want to use Arial, you'll always need to refer to the arial.ttf file, and yes, that can mean that you need to provide c:/windows/fonts/arial.ttf as a path. 如果你想使用Arial,你总是需要引用arial.ttf文件,是的,这可能意味着你需要提供c:/windows/fonts/arial.ttf作为路径。

If you want to make your application platform-independent, you can ship the font with your code as a resource, and use the path to that resource. 如果要使应用程序与平台无关,可以将代码作为资源发送到字体,并使用该资源的路径。

Another solution, is to use the FontFactory and to register all fonts. 另一种解决方案是使用FontFactory并注册所有字体。 Note that the initial call to register fonts can take some time, as FontFactory will search your hard disk for as many fonts as it can find looking at directories that may contains font files (depending on the OS). 请注意,对注册字体的初始调用可能需要一些时间,因为FontFactory将在您的硬盘上搜索尽可能多的字体,因为它可以查找可能包含字体文件的目录(取决于操作系统)。 Take a look at the FontFactoryExample and experiment with the register() , registerDirectory() and registerDirectories() methods. 查看FontFactoryExample并尝试使用register()registerDirectory()registerDirectories()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM