0

I am using iText 4.0.2 version for pdf generation. I have some characters/symbols to print like ∈, ∩, ∑, ∫, ∆ (Mathematical symbols) and many others. My code :

  Document document = new Document(PageSize.A4, 60, 60, 60, 60);
        try
        {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/home/adeel/experiment.pdf"));

            document.open();

            String str = "this string will contains special character like this  ∈, ∩, ∑, ∫, ∆";

        BaseFont bfTimes = null;
        try {
            bfTimes = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Font fontnormal = new Font(bfTimes, 12, Font.NORMAL, Color.BLACK);
         Paragraph para = new Paragraph(str, fontnormal);
         document.add(para);

        document.close();
        writer.close();

        System.out.println("Done!");
    } catch (DocumentException e)
    {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

In the above code I am putting all the symbols in str and generating the pdf file. In place of symbols I tried putting unicode characters in str for the symbols but It didn't worked as well.

Adeel Ahmad
  • 185
  • 1
  • 17
  • 1
    You are making several errors: (1) You are not using the real iText; iText 4.0.2 [never existed](http://itextpdf.com/changelog/50). Who knows what's inside that version? Don't use it. (2) You are using a font that doesn't know the characters you want to use. (3) Even if you used the right font, you are using the wrong encoding. (4) Never write source code containing non-ASCII characters, use a notation such as `\u00a0` instead. – Bruno Lowagie Jul 07 '15 at 12:55
  • 1
    All the problems mentioned above are explained in my answer to the following question: [Can't get Czech characters while generating a PDF](http://stackoverflow.com/questions/26631815/cant-get-czech-characters-while-generating-a-pdf). You can find this question in the [official documentation](http://pages.itextpdf.com/ebook-stackoverflow-questions.html) – Bruno Lowagie Jul 07 '15 at 12:56
  • Which font and encoding should we use to get these mathematical characters? We tried replacing the symbols with unicode notation like "\u2220" - still the character either gets skipped or generates a question mark. – Adeel Ahmad Jul 07 '15 at 14:16

1 Answers1

2

Please take a look at the MathSymbols example.

  • First you need a font that supports the symbols you need. Incidentally, FreeSans.ttf is such a font. Then you need to use the right encoding.
  • You're using UNICODE, so you need Identity-H as the encoding.
  • You should also use notations such as \u2208, \u2229, \u2211, \u222b, \u2206. That's not a must, but it's good practice.

This is how it's done:

public static final String DEST = "results/fonts/math_symbols.pdf";
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static final String TEXT = "this string contains special characters like this  \u2208, \u2229, \u2211, \u222b, \u2206";

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

The result looks like this: math_symbols.pdf

enter image description here

Important: you should always use the most recent, official version of iText. iText 4.0.2 is not an official version.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165