简体   繁体   English

如何使用 itext5 在 pdf 中显示 i 下标?

[英]How can I display i subscript in pdf using itext5?

I am using unicode character \ᵢ( https://unicode-table.com/en/1D62/ ) to display i subscript but this is not working I have tried using fonts of symbola.ttf file,arial.ttf file ,FreeSans.ttf file and also CardoRegular.otf file but none of the font file is displaying the subscript.我正在使用 unicode 字符 \ᵢ( https://unicode-table.com/en/1D62/ ) 来显示 i 下标,但这不起作用我尝试使用 symbola.ttf 文件、arial.ttf 文件、FreeSans 的字体。 ttf 文件和 CardoRegular.otf 文件,但没有一个字体文件显示下标。 Please help me here using which ttf file can I display this subscript.请在这里帮助我使用哪个 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));

You can use Noto Sans font ( https://fonts.google.com/noto/specimen/Noto+Sans ) - it has the glyph for Unicode character \ᵢ that you need.您可以使用 Noto Sans 字体 ( https://fonts.google.com/noto/specimen/Noto+Sans ) - 它具有您需要的 Unicode 字符\ᵢ的字形。

Here is the code sample:这是代码示例:

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

And here is how result looks like:结果如下所示:

结果

Meanwhile, I strongly recommend switching to iText 7 immediately as iText 5 has been long deprecated.同时,我强烈建议立即切换到iText 7 ,因为 iText 5 早已被弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM