开发者

How to embed Helvetica font in PDF using iText?

How to embed Helvetica font in PDF using iText?

Following does not work:

B开发者_JAVA百科aseFont helvetica = BaseFont.createFont(BaseFont.HELVETICA,
        BaseFont.CP1252, BaseFont.EMBEDDED);
Font font = new Font(helvetica, 20, Font.BOLD);

That is, this will return false:

font.getBaseFont().isEmbedded()

Embedding works if I provide the TrueType file myself as a parameter to createFont() method.


I did some digging into source code and it seems that iText explicitly ignores BaseFont.EMBEDDED flag for certain fonts and Helvetica is one of them.

Embedding probably works if you provide font file (e.g. TrueType .ttf) for Helvetica.


Yes, embedding of defined fonts will not work.
It cannot work.
iText, in order to embed font, must have the access to font resource. The defined fonts are provided by PDF reader and therefore are not available to your library during PDF creating process.

What is more, each PDF reader must provide this fonts, but have freedom of choice of licencing method for this fonts. Their licence can forbid any usage for final usage except the displaying of PDF files in that reader.


The PDF specification defines 8 fonts which are expected to be available in a PDF Viewer so they would not need to be embedded. Helvetica is one of these.


This program will help you to add all font style that itext is having.

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());
    }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜