【问题标题】:How can I display i subscript in pdf using itext5?如何使用 itext 5 在 pdf 中显示下标?
【发布时间】:2021-12-27 14:06:58
【问题描述】:

我正在使用 unicode 字符 \u1D62(https://unicode-table.com/en/1D62/) 来显示 i 下标,但这不起作用我尝试使用 symbola.ttf 文件、arial.ttf 文件、FreeSans.ttf 文件以及 CardoRegular.otf 文件的字体 但没有一个字体文件显示下标。请帮助我在这里使用哪个 ttf 文件可以显示此下标。

    final String FONT1 = "./StaticContent/FreeSans.ttf";
    final String FONT2 = "./StaticContent/Symbola.ttf";
    final String FONT3 = "./StaticContent/CardoRegular.otf";
    final String arialFont = "./StaticContent/Arial.ttf";
    BaseFont bf = null;
    BaseFont bf1=null;
    BaseFont bf2=null;
    BaseFont arialBf=null;
    try {
        bf = BaseFont.createFont(FONT1, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        bf1=BaseFont.createFont(FONT2, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
         bf2 = BaseFont.createFont(FONT3, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
         arialBf = BaseFont.createFont(arialFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    } catch (DocumentException | IOException e1) {
        
    }
    Font font3 = new Font(bf2 ,9.6f);
    Font af=new Font(arialBf,9.6f);
    Font specialFont = new Font(bf, 9.6f);
    Font specialFont1=new Font(bf1,9.6f);
     Font font2 = new Font(Font.FontFamily.COURIER , 18,Font.ITALIC );


    Phrase summationLine=new Phrase();
    summationLine.add(new Chunk("∑",specialFont));
    summationLine.add(new Chunk("(Gᵢ" +" X "+"Vᵢ)",font3));
    summationLine.add(new Chunk("G\u1D62",af));
    summationLine.add(new Chunk("(Gᵢ",af));
    summationLine.add(new Chunk("ᵢ",af));

【问题讨论】:

    标签: java unicode itext pdfptable underscore-java


    【解决方案1】:

    您可以使用 Noto Sans 字体 (https://fonts.google.com/noto/specimen/Noto+Sans) - 它具有您需要的 Unicode 字符 \u1D62 的字形。

    这是代码示例:

    BaseFont bf = BaseFont.createFont("path/to/NotoSans-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    FileOutputStream fs = new FileOutputStream("path/to/out.pdf");
    Document doc = new Document();
    PdfWriter writer = PdfWriter.getInstance(doc, fs);
    Paragraph p;
    writer.setCompressionLevel(0);
    doc.open();
    
    Font font = new Font(bf ,9.6f);
    p = new Paragraph(new Chunk("(Gᵢ" +" X "+"Vᵢ)", font));
    doc.add(p);
    
    doc.close();
    fs.close();
    

    结果如下:

    同时,我强烈建议立即切换到 iText 7,因为 iText 5 早已被弃用。

    【讨论】:

      猜你喜欢
      • 2010-10-10
      • 2014-08-03
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 2013-02-22
      • 1970-01-01
      相关资源
      最近更新 更多