0

Hiyas

I'm trying to display this string:

λλλλλλλλλλλλλλλλλλλλλλλλ

which is read from a RTF file, parsed and put into this variable. It is NOT used as constant in the code.

    Font pdfFont = FontFactory.getFont(font.getFont().getName(), BaseFont.IDENTITY_H, embed, font.getFont().getSize2D(), style);    
Phrase phrase = new Phrase("λλλλλλλλλλλλλλλλλλλλλλλλ", pdfFont);
        ColumnText.showTextAligned(content[i], alignment, phrase, x, y, rotation);

I also tried CP1252 (and basically all the other encodings I found) together with a simple ArialMT.ttf font, but that damn string is never displayed. I can see that the conversion to the byte array inside iText (we use 5.5.0) always returns a null length byte array which explains why the text is not used, but I don't understand why. What encoding would I need to use to make this visible in a PDF?

Thanks a lot

Fuggly
  • 905
  • 1
  • 8
  • 18
  • Have you tried ArialUni? That font contains a wider selection of glyphs. – mkl Aug 16 '16 at 16:20
  • @mkl I just tested, Lambda is known by arial.ttf too. Maybe the cause of the problem is bad coding hygiene. You don't put special characters in your source code; you use the Unicode notation instead. – Bruno Lowagie Aug 16 '16 at 16:52

1 Answers1

2

I suppose that you want to get a result that looks like this:

enter image description here

That's easy. I first tried the SunCharacter example from the official documentation. That example was written in answer to the question: iText : Unable to print mathematical characters like ∈, ∩, ∑, ∫, ∆ √, ∠

I then changed the TEXT to:

public static final String TEXT = "Always use the Unicode notation for special characters: \u03bb";

As you can see, I don't use λ in my source code (that's bad practice). Instead I use \u03bb which is the Unicode notation of λ.

The result looked like this:

enter image description here

That's not what you want; you want ArialMT. So I changed the FONT to:

public static final String FONT = "c:/windows/fonts/arial.ttf";

This gave me the desired PDF.

This is the full code sample:

public class LambdaCharacter {
    public static final String DEST = "results/fonts/lambda_character.pdf";
    public static final String FONT = "c:/windows/fonts/arial.ttf";
    public static final String TEXT = "Always use the Unicode notation for special characters: \u03bb";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new LambdaCharacter().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font f = new Font(bf, 12);
        Paragraph p = new Paragraph(TEXT, f);
        document.add(p);
        document.close();
    }
}

I works just fine.

Maybe you aren't really using Arial. Maybe font.getFont().getName() doesn't give you the correct name of the font. Or maybe it gives you the correct name of the font, but you forgot to register the font. In that case, you will see that Helvetica is used. Helvetica can't render a lambda. You need Arial or Cardo-Regular or Arial Unicode or another font, as long as that font knows how to render a lambda.

If you don't know how to register a font, read:

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • The real issue here is, that we get RTF text that we parse and then use the RTF strings directly on iText. I guess I need to convert them somehow first, but this gave me a pointer... thanks! – Fuggly Aug 17 '16 at 07:48
  • Nevermind.. when I embed the fonts, it works – Fuggly Aug 17 '16 at 08:37