【问题标题】:iText, What's going on with Font, BaseFont and createFont()?iText,Font、BaseFont 和 createFont() 是怎么回事?
【发布时间】:2012-07-06 16:44:24
【问题描述】:

对我来说,字体和basefont 发生了什么对我来说有很多谜团。尤其是在涉及到构造函数时。 iText 网站将此行作为新字体的示例代码

 BaseFont unicode = BaseFont.createFont("c:/windows/fonts/arialuni.ttf", 
                        BaseFont.IDENTITY_H, 
                        BaseFont.EMBEDDED);

我可以让这个电话工作:

BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1257, 
                  BaseFont.EMBEDDED);

但如果我将 BaseFont.CP1257 替换为 BaseFont.HELVETICA 则它不起作用,并且我得到一个页面显示“无法加载 pdf 文档”。

我尝试查看类文件,但似乎无法弄清楚第二个参数是什么(我假设它类似于备用字体,以防第一个字体不起作用,例如在 HTML 中)而且我不明白为什么有些字体会起作用而其他字体不起作用。

【问题讨论】:

    标签: itext


    【解决方案1】:

    从您的 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();
    

    【讨论】:

    • 0.8f float 在这里做什么?这是我见过的大约 30 个参考文献(包括官方文档)中的第一个,它实际上向您展示了如何在 itext 中引用和使用字体。谢谢。
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      这对我有用。旧帖子,但我找不到简单的答案。

      //Here you setup the font that you want. I put it under the root/Content/fonts folder in my project
      Font font = FontFactory.GetFont("~/Content/fonts/ARIALN.ttf", BaseFont.CP1252,false, 9);
      
      //Here I create the paragraph then assign the font to it at the end
      var addressAttn = new Paragraph("Attn:  Manager, Billing Services", font);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-07
        • 2013-11-28
        • 1970-01-01
        • 1970-01-01
        • 2011-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多