1

如何使用 iText 在 PDF 中嵌入 Helvetica 字体?

以下不起作用:

BaseFont helvetica = BaseFont.createFont(BaseFont.HELVETICA,
        BaseFont.CP1252, BaseFont.EMBEDDED);
Font font = new Font(helvetica, 20, Font.BOLD);

也就是说,这将返回 false:

font.getBaseFont().isEmbedded()

如果我自己提供 TrueType 文件作为 createFont() 方法的参数,则嵌入工作。

4

4 回答 4

3

我对源代码进行了一些研究,似乎 iText 明确忽略BaseFont.EMBEDDED了某些字体的标志,而 Helvetica 就是其中之一。

如果您为 Helvetica 提供字体文件(例如 TrueType .ttf),嵌入可能会起作用。

于 2010-01-07T17:13:42.700 回答
2

PDF 规范定义了 8 种字体,预计在 PDF 查看器中可用,因此不需要嵌入。Helvetica 就是其中之一。

于 2010-01-11T08:19:09.150 回答
2

是的,嵌入已定义字体将不起作用。
它不能工作。
iText,为了嵌入字体,必须有权访问字体资源。定义的字体由 PDF 阅读器提供,因此在 PDF 创建过程中对您的库不可用。

更重要的是,每个PDF阅读器都必须提供这种字体,但可以自由选择这种字体的许可方式。他们的许可证可以禁止任何最终用途,除了在该阅读器中显示 PDF 文件。

于 2011-05-24T11:05:45.847 回答
1

该程序将帮助您添加 itext 拥有的所有字体样式。

public class FontStyle{
public static void main(String[] args) {

    // creation of the document with a certain size and certain margins
    // may want to use PageSize.LETTER instead
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);
    try {
        // creation of the different writers
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("SupportedFontsStyle.pdf"));

        final Chunk NEWLINE = new ChunkPF("\n");
        document.open();
        Phrase phrase = new Phrase();

        LineSeparator lineSeperator = new LineSeparator();
        final Font font_h1_normal = FontFactory.getFont("Courier",8F, Font.NORMAL);
        phrase.add(new Chunk("Courier", font_h1_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h2_normal = FontFactory.getFont("Courier-Bold", 8F,Font.BOLD);
        phrase.add(new Chunk("Courier-Bold  ", font_h2_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h3_normal = FontFactory.getFont("Courier-Oblique",8F, Font.NORMAL);
        phrase.add(new Chunk("Courier-Oblique  ", font_h3_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h4_normal = FontFactory.getFont("Courier-BoldOblique", 8F,Font.BOLD);
        phrase.add(new Chunk("Courier-BoldOblique", font_h4_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h5_normal = FontFactory.getFont("Helvetica",8F, Font.NORMAL);
        phrase.add(new Chunk("Helvetica  ", font_h5_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h6_normal = FontFactory.getFont("Helvetica-Bold", 8F,Font.BOLD);
        phrase.add(new Chunk("Helvetica-Bold  ", font_h6_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h7_normal = FontFactory.getFont("Helvetica-BoldOblique",8F, Font.BOLD);
        phrase.add(new Chunk("Helvetica-BoldOblique", font_h7_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h8_normal = FontFactory.getFont("Symbol", 8F,Font.NORMAL);
        phrase.add(new Chunk("Symbol", font_h8_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h9_normal = FontFactory.getFont("Times-Bold",8F, Font.BOLD);
        phrase.add(new Chunk("Times-Bold  ", font_h9_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h10_normal = FontFactory.getFont("Times", 8F,Font.NORMAL);
        phrase.add(new Chunk("Times  ", font_h10_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h12_normal = FontFactory.getFont("Times-BoldItalic", 8F,Font.BOLDITALIC);
        phrase.add(new Chunk("Times-BoldItalic  ", font_h12_normal));
        phrase.add(ChunkPF.NEWLINE);            
        final Font font_h13_normal = FontFactory.getFont("Times-Italic",8F, Font.ITALIC);
        phrase.add(new Chunk("Times-Italic  ", font_h13_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h14_normal = FontFactory.getFont("Times-Roman", 8F,Font.NORMAL);
        phrase.add(new Chunk("Times-Roman ", font_h14_normal));
        phrase.add(ChunkPF.NEWLINE);
        final Font font_h15_normal = FontFactory.getFont("ZapfDingbats",8F, Font.NORMAL);
        phrase.add(new Chunk("ZapfDingbats  ", font_h15_normal));
        phrase.add(ChunkPF.NEWLINE);        

        document.add(phrase);

        document.close();

    } catch (Exception ex) {
        System.err.println(ex.getMessage());
    }
}

}

于 2016-03-22T07:38:56.467 回答