是否向Apache Pdfbox添加字体? [英] Adding fonts to Apache Pdfbox?

查看:208
本文介绍了是否向Apache Pdfbox添加字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Apache Pdfbox中添加其他字体样式?

Is there a way to add additional font styles into Apache Pdfbox?

我们目前正在尝试在系统中打印PDF(当前使用 PDF-Renderer .)我一直在研究各种替代方法( pdfbox jPDFPrint )

We're currently trying to work around printing PDFs in our system (currently being done with PDF-Renderer.) I have been looking at various alternatives (pdfbox, jpedal, jPDFPrint)

我们希望可以使用免费的GPL兼容库,因此,我们倾向于pdfbox.我已经能够编写一些示例代码来打印出有效"的pdf文件.见下文:

Our hope is for a free GPL compatible library to use, and as such we're leaning towards pdfbox. I have been able to write some sample code to print out the pdf which 'works'. See below:

PDDocument doc;
try {
        doc = PDDocument.load("test.pdf");
        doc.print();
    } catch (Exception e) {
        // Come up with better thing to do on fail.
        e.printStackTrace();
    }

正如我提到的那样,这可行,但是我遇到的问题是PdfBox似乎无法识别pdf中使用的字体,因此改变了所使用的字体.结果,文档看起来很奇怪(间距和字符大小不同并且看起来很奇怪).我通常会看到以下日志消息或类似的消息:

As I mentioned, this works but the problem I'm running into is that PdfBox doesn't seem to be recognizing the fonts used in the pdf, and as such changes the font being used. As a result the document looks very odd (spacing and character size are different and look bizarre). I routinely see the following log message, or things like it:

2014年4月16日下午2点56:21 org.apache.pdfbox.pdmodel.font.PDSimpleFont drawString警告:更改<上的字体>来自<NimbusMono>更改为默认字体

有人知道如何将新字体添加到pdfbox的方法(或参考)吗?或禁止这种情况,如何更改默认字体类型?

Does anyone know of a way (or a reference) on how to approach adding a new fonttype into pdfbox? Or barring that, how to change the default font type?

据我所知,pdfbox支持 14种标准字体.不幸的是NimbusMono不是其中之一.任何指导将不胜感激.

From what I can tell, pdfbox supports 14 standard fonts. Unfortunately NimbusMono is not one of them. Any guidance would be appreciated.

推荐答案

未发布的2.0版本支持呈现嵌入式字体.您可以将其作为快照 https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/或通过"svn结帐 http://svn.apache.org/repos/asf/pdfbox/trunk/".该API与1.8.x版本略有不同,并且可能会有所变化,最好是看一下代码示例.要查看您的文件是否可以正确呈现,一项快速的测试是下载"pdfbox-app" https://存储库.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox-app/2.0.0-SNAPSHOT/然后运行查看器:java -jar pdfbox-app-2.0.0-20140416.173452-273.jar PDFReader您的文件名.pdf还有一个打印功能.

The unreleased 2.0 version supports the rendering of embedded fonts. You can get it as a snapshot https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/ or through "svn checkout http://svn.apache.org/repos/asf/pdfbox/trunk/". The API is slightly different from the 1.8.x versions and might change, the best is to look at the code examples. A quick test to see whether your file will be rendered properly is to download the "pdfbox-app" https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox-app/2.0.0-SNAPSHOT/ and then run the viewer: java -jar pdfbox-app-2.0.0-20140416.173452-273.jar PDFReader your-file-name.pdf There's also a print feature.

祝你好运!

2016年更新:2.0版本已发布,请此处下载.如果您使用的是1.8版本,请阅读迁移指南.

Update 2016: 2.0 release is out, download it here. If you have used the 1.8 version, read the migration guide.

这篇关于是否向Apache Pdfbox添加字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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