2010-01-07 84 views
1

如何使用iText在PDF中嵌入Helvetica字體?如何使用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()方法。

回答

2

是的,嵌入定義的字體將不起作用。
它不能工作。
iText爲了嵌入字體,必須能夠訪問字體資源。所定義的字體由PDF閱讀器提供,因此在PDF創建過程中您的圖書館無法使用。更有甚者,每個PDF閱讀器都必須提供這種字體,但可以自由選擇這種字體的授權方式。除了在該閱讀器中顯示PDF文件之外,其許可證可以禁止任何最終用法的使用。

3

我做了一些挖掘源代碼,似乎iText明確忽略BaseFont.EMBEDDED標誌爲某些字體和Helvetica是其中之一。

如果您爲Helvetica提供字體文件(例如TrueType .ttf),嵌入可能適用。

1

PDF規範定義了8種字體,這些字體預計可在PDF查看器中使用,因此不需要嵌入。 Helvetica就是其中之一。

+1

我的問題是,我需要嵌入到PDF文件中使用*所有*字體,包括8防止在渲染任何差異。 – 2010-01-11 09:10:03

+3

PDF/A一致性需要嵌入所有字體,包括標準PDF字體。 – rwong 2010-11-12 01:45:59

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

}

+0

這不是問題所在。 – mkl 2016-03-22 08:15:33

相關問題