【问题标题】:How to add new fonts to Itext using java如何使用java向Itext添加新字体
【发布时间】:2015-10-29 05:17:13
【问题描述】:

当我想使用 iText 字体时,我会执行以下操作:

protected final static Font FONT_SIZE_11_BOLD = new Font(Font.HELVETICA, 11f, Font.BOLD);

然后我可以随心所欲地使用它,如下所示:

monthSize11 = new Chunk(month, FONT_SIZE_11_BOLD);

我想使用 Arial 而不是 HELVETICA,但 Arial 不能直接使用。 我的意思是,我做不到

new Font(Font.ARIAL, 11f, Font.BOLD);

因为 Arial 没有在 Font 类中定义,但 Arial.ttf 文件在我的系统中的 C:\WINDOWS\Fonts 下。 问题是如何将 Arial.ttf 文件绑定到 iText 以及如何使用它。

提前很多次。

编辑:我想使用自己的字体。我的意思是,我有一个名为“myCompany.ttf”的文件,其中定义了自己的字体,并且在某些地方我必须使用。问题不仅在于 Arial。

【问题讨论】:

    标签: java fonts itext


    【解决方案1】:
    BaseFont base = BaseFont.createFont("c:/windows/fonts/arial.ttf", BaseFont.WINANSI);
    Font font = new Font(base, 11f, Font.BOLD);
    ....
    

    阅读更多here

    【讨论】:

    • 我创建了自己的字体,字体文件是 gautami.ttf(支持泰卢固语字体),但我需要使用什么编码类型。你能帮我解决这个问题吗?
    【解决方案2】:

    从 JAR 中加载它,使用前导斜杠;否则,请使用字体的绝对路径 (C:\...\fonts\Sansation_Regular.ttf)。例如:

    Font font = FontFactory.getFont("/fonts/Sansation_Regular.ttf",
        BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.NORMAL, BaseColor.BLACK);
    BaseFont baseFont = font.getBaseFont();
    
    • 字体的相对路径为:'src/main/resources/fonts'
    • 使用 Itext 5.4.5
    • Example code

    【讨论】:

      【解决方案3】:

      使用 BaseFont.createFont 创建一个新的 Font 对象。

      您可以传递任何 Type1 或 TTF 字体。您只需要确保您的字体文件随一起分发。 参考 BaseFont API

      【讨论】:

        【解决方案4】:

        使用 itext 创建自定义字体很简单

        我在下面写了同样的代码

        肯定会帮助别人

        public class CustomFontStyle {
            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("CustomFontsStyle.pdf"));
                    final String NEWLINE = "\n";
                    document.open();
                    Phrase phrase = new Phrase();
        
                    BaseFont baseFont3 = BaseFont.createFont("Xenotron.ttf", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
                    Font font2 = new Font(baseFont3, 12);
        
                    document.add(new Paragraph("Custom Xenotron Font: ", font2));
        
                    phrase.add(NEWLINE);
        
                    document.add(phrase);
        
                    document.close();
        
                }
                catch (Exception ex) {
                    System.err.println(ex.getMessage());
                }
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多