用IText在PDF中嵌入非嵌入字体 [英] Embed non-embedded fonts in PDF with IText

查看:582
本文介绍了用IText在PDF中嵌入非嵌入字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下问题。我收到一个包含一组字体的 PDF 文件。这些字体没有嵌入到文件中。这是一个简单的例子:





但是,与其他示例一样,我需要字体的源文件为了嵌入它。在我的情况下,我没有源文件。但是我可能会把它们放在系统上。所以我想在系统上查询我的可用字体,看它是否与给定的字体相对应。

类似的东西

  GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
java.awt.Font [] fonts = e.getAllFonts();
for(java.awt.Font f:fonts){
System.out.println(f.getFontName());
}

但是我无法转换给定的 java.awt。字体转换为 RandomAccessFile 字节[] 来嵌入字体文件本身。有没有另外一种方法将字体嵌入到 PDF 中,而不需要字体本身的源文件?

解决方案对于Windows C:\ Windows \ Fonts 等包含所有字体文件,并在资源管理器中显示字体名称。在Java中,你有 GraphicsEnvironment.getAvailableFontFamilyNames() Font.getFamilyName()来检查PDF中的名字,比如Arial MT。

然而文件的getter从字体丢失。



因此,列出字体目录的所有文件,并连续加载每个文件为Font。

  GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
Font font = Font.createFont(Font.TRUETYPE_FONT,ttfFile);
ge.registerFont(font); //如果你想加载字体(pdfFontName.startsWith(font.getFamilyName()){
System.out.printf(%s - %s /%s%n,ttfFile.getName(),

) font.getFamilyName(),
font.getName());
}


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:

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.

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 the source file. But I might have them on the system however. So I'd like to query my available fonts on the system and see if it corresponds to the given font.

Something of the likes as

GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
java.awt.Font[] fonts = e.getAllFonts();
for(java.awt.Font f : fonts){
    System.out.println(f.getFontName());
}

But I cannot transform the given java.awt.Font into a RandomAccessFile or a byte[] to be used in order to embed the font file itself. Is there another way for embedding fonts into a PDF, without having the source file of the font itself?

解决方案

For Windows C:\Windows\Fonts or such contain all font files, and in the explorer shows also font names. So a manual search is feasible.

In java, you have GraphicsEnvironment.getAvailableFontFamilyNames() and Font.getFamilyName() to check for a name from the PDF like "Arial MT."

However a getter for the file is missing from Font.

So list all files of the font directory, and load each file consecutively as Font.

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font font = Font.createFont(Font.TRUETYPE_FONT, ttfFile);
ge.registerFont(font); // If you want to load the font.

if (pdfFontName.startsWith(font.getFamilyName()) {
    System.out.printf("%s - %s / %s%n", ttfFile.getName(), font.getFamilyName(),
            font.getName());
}

这篇关于用IText在PDF中嵌入非嵌入字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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