0

I am using the iText (java library) to generate a PDF file. Here I have a paragraph in which I have to put a check mark

PdfPCell cell95 = new PdfPCell(new Paragraph((new Chunk('\u2713', FontFactory.getFont(FontFactory.HELVETICA, 11, Font.BOLD, new BaseColor(0,0,0))))));

I am using this but it is not working.

Tushar Agarwal
  • 521
  • 1
  • 16
  • 39

4 Answers4

0

You cannot use a unicode directly into a PDFPCell.

Instead, create image of checkmark and insert it into PDFPCell.

Suhel
  • 16
0

This stackoverflow post and this item from itext-questions indicate that you need to create the font using the unicode character set instead of the default Windows CP1252 Character Set - try using the overload of getFont with the encoding specifier:

FontFactory.getFont(FontFactory.HELVETICA, BaseFont.IDENTITY_H, 11, Font.BOLD, new BaseColor(0,0,0))
Community
  • 1
  • 1
Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
0

Use this code:

    String FONT = "C:/dev/dejavu-fonts-ttf-2.33/ttf/DejaVuSans.ttf";
    FontSelector selector = new FontSelector();
    BaseFont base = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    selector.addFont(FontFactory.getFont(FontFactory.HELVETICA, 12));
    selector.addFont(new Font(base, 12));
    Phrase ph = selector.process(text);
    document.add(new Paragraph(ph));

Here, the point is 1)set BaseFont.IDENTITY_H; 2) only some font provide "check mark"

fkpwolf
  • 373
  • 4
  • 13
0

try this solved. Download the dejavu-sans from https://www.fontsquirrel.com/fonts/dejavu-sans. and keep it in local.

use the below code to write tick symbol into PDF doc.

    private static void writeUsingIText() {


    Document document = new Document();

    try {

        PdfWriter.getInstance(document, new FileOutputStream(new File(FILE_NAME)));

        //open
        document.open();

        Paragraph p = new Paragraph();
        p.add("This is my paragraph 1");
        p.setAlignment(Element.ALIGN_CENTER);

        document.add(p);

        Paragraph p2 = new Paragraph();
        p2.add("This is my paragraph 2"); //no alignment

        document.add(p2);

        Font f = new Font();
        f.setStyle(Font.BOLD);
        f.setSize(8);

        document.add(new Paragraph("This is my paragraph 3", f));

        document.add(new Paragraph("This is my tick mark ", f));


        String FONT = "C:\\Users\\<username>\\Documents\\DejaVuSans.ttf";
        FontSelector selector = new FontSelector();
        BaseFont base = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        selector.addFont(FontFactory.getFont(FontFactory.HELVETICA, 12));
        selector.addFont(new Font(base, 12));
        char tickSymbol=10003;
        String text = String.valueOf(tickSymbol);
        Phrase ph = selector.process(text);
        document.add(new Paragraph(ph));




        //close
        document.close();

        System.out.println("Done");

    } catch (FileNotFoundException | DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}