Search code examples
unicodeawtitextjfreechartgraphics2d

superscripts does not show properly on an image created by itext on pdf


I have a very confusing problem about unicode support in the generated pie chart in my pdf. Here is what I have: I am generating pie chart (with jfreechart library) that need to add superscripts on the title of the pie chart. I tested and I know that jfreechart is generating correct title (superscripts are fine) and I also tested itext unicode support. There is a Graphics2D (from java awt) in between which turns the jfreechart into a template and then I can print this template into my pdf. according my tests I guess problem should be in between graphics2d and itext template.

PdfContentByte canvas = writer.getDirectContent();

PdfTemplate template = canvas.createTemplate(width, height);

FontMapper mapper = new DefaultFontMapper();

template.setFontAndSize(mapper.awtToPdf(new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 7)) , 7);

Graphics2D graphics2d = template.createGraphics(width, height);

graphics2d.setFont(new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 7));

JFreeChart chart = getPieChart("", title, value);


I found out the problem. Problem is template (PdfTemplate) does not show the unicode character. Although I embedded unicode fonts and set it for template, still not working. any ideas?


Solution

  • Actually finally I fixed the problem by removing superscript from pie chart and print it directly into template. This also eliminates the use of unicode font (which costs extra money):

    template.beginText();

    template.setFontAndSize(arial, 8);

    // the fixed position of the xy coordinate that I want to print superscript template.moveText(97, 142);

    template.showText(superscript);

    template.setTextRise(5f);

    template.endText();