java 生成pdf文件嵌入字体,用IText在PDF中嵌入非嵌入字体

So I have the following problem. I receive a PDF file which contains a set of fonts. These fonts are not embedded into the file. Here is a simple example:

qGOEl.png

I would like to embed these fonts inside the PDF, so they're self-contained and always available. But things don't seem that simple. I'm using IText to do my PDF processing.

I have read and tried the following questions/answers:

But what had gotten me closest was the following example: EmbedFontPostFacto.java (which comes from the book). I was able to embed the Arial font when providing the Arial.ttf file.

wwQRm.png

But with this, like with other examples, I need the source file of the font in order to embed it. In my case, I don't have

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iText是一个用于生成PDF文件Java库,而iText 5.5.6是其一个版本。在iText 5.5.6,可以通过设置字体来自定义PDF文件字体样式。 首先,你需要导入所需的字体文件iText支持TrueType字体(.ttf)和OpenType字体(.otf)。你可以从官方网站或其他来源下载所需的字体文件。 接下来,你可以使用BaseFont类来创建字体对象。BaseFont类提供了多种方法来加载字体文件,并可以设置字体的样式、大小等属性。例如,你可以使用以下代码创建一个Arial字体对象: ``` BaseFont baseFont = BaseFont.createFont("path/to/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font = new Font(baseFont, 12, Font.NORMAL); ``` 在上述代码,"path/to/arial.ttf"是你下载的Arial字体文件的路径。BaseFont.IDENTITY_H表示使用Unicode编码,BaseFont.EMBEDDED表示将字体文件嵌入生成PDF文件。Font.NORMAL表示正常的字体样式,你还可以根据需要设置其他样式,如粗体、斜体等。 接下来,你可以使用创建的字体对象来设置文本的字体样式。例如,你可以使用以下代码将文本添加到PDF文件,并设置为Arial字体: ``` Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("path/to/output.pdf")); document.open(); Paragraph paragraph = new Paragraph("Hello, World!", font); document.add(paragraph); document.close(); writer.close(); ``` 在上述代码,我们创建了一个Paragraph对象,并将字体对象作为参数传递给它。然后,我们将该段落添加到PDF文件。 这样,你就可以使用iText 5.5.6来生成PDF文件并设置字体样式了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值