Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
912 views
in Technique[技术] by (71.8m points)

itext - Rupee symbol is not showing in android

Friends am using itextpdf-5.3.4.jar for creating pdf. For showing rupee symbol am using custom font. I tried arial.ttf,arialbd.ttf both this font but no luck rupee symbol is not showing. For showing the rupee symbol i have followed these links but it's not working for me. How to display indian rupee symbol in iText PDF in MVC3. This is the code I have used.

BaseFont rupee =BaseFont.createFont( "assets/arial .ttf", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
createHeadings(cb,495,60,": " +edt_total.getText().toString(),12,rupee);

  private void createHeadings(PdfContentByte cb, float x, float y, String text, int   size,BaseFont fb){

        cb.beginText();
        cb.setFontAndSize(fb, size);
        cb.setTextMatrix(x,y);
        cb.showText(text.trim());
        cb.endText();
    } 

Please help me guys.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the comment section, Funkystein wrote that the problem you describe is typical when

  1. you are using a font which doesn't have that glyph. or
  2. you aren't using the right encoding.

I have written an example that illustrates this: RupeeSymbol

public static final String DEST = "results/fonts/rupee.pdf";
public static final String FONT1 = "resources/fonts/PlayfairDisplay-Regular.ttf";
public static final String FONT2 = "resources/fonts/PT_Sans-Web-Regular.ttf";
public static final String FONT3 = "resources/fonts/FreeSans.ttf";
public static final String RUPEE = "The Rupee character u20B9 and the Rupee symbol u20A8";

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(DEST));
    document.open();
    Font f1 = FontFactory.getFont(FONT1, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
    Font f2 = FontFactory.getFont(FONT2, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
    Font f3 = FontFactory.getFont(FONT3, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
    Font f4 = FontFactory.getFont(FONT3, BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
    document.add(new Paragraph(RUPEE, f1));
    document.add(new Paragraph(RUPEE, f2));
    document.add(new Paragraph(RUPEE, f3));
    document.add(new Paragraph(RUPEE, f4));
    document.close();
}

The RUPEE constant is a String that contains the Rupee character as well as the Rupee symbol: "The Rupee character ? and the Rupee symbol ?". The characters are stored as Unicode values, because if we store the characters otherwise, they may not be rendered correctly. For instance: if you retrieve the values from a database as Winansi, you will end up with incorrect characters.

I test three different fonts (PlayfairDisplay-Regular.ttf, PT_Sans-Web-Regular.ttf and FreeSans.ttf) and I use IDENTITY_H as encoding three times. I also use WINANSI a fourth time to show that it goes wrong if you do.

The result is a file named rupee.pdf:

Rupee character vs Rupee Symbol

As you can see, the first two fonts know how to draw the Rupee character. The third one doesn't. The first two fonts don't know how to draw the Rupee symbol. The third one does. However, if you use the wrong encoding, none of the fonts draw the correct character or symbol.

In short: you need to find a font that knows how to draw the characters or symbols you need, then you have to make sure that you are using the correct encoding (for the String as well as the Font).

You can download the full sample code here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...