How can I display i subscript in pdf using itext5?

208 Views Asked by At

I am using unicode character \u1D62(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. Please help me here using which ttf file can I display this subscript.

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

There are 1 best solutions below

0
On

You can use Noto Sans font (https://fonts.google.com/noto/specimen/Noto+Sans) - it has the glyph for Unicode character \u1D62 that you need.

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:

result

Meanwhile, I strongly recommend switching to iText 7 immediately as iText 5 has been long deprecated.