0

I've created a table using Java and iTextPdf that lays out a bunch of cells. I can't quite figure out how to change the font for the text inside of these cells. It's something for my kids, so I'm trying to use Comic Sans MS. Everything compiles and runs just fine, but I get a generic font (probably Helvetica or something like it).

Anyone know how to generate a font like this? Thanks!

Here is the scaled down version of what I wrote:

import com.itextpdf.text.FontFactory; 
import com.itextpdf.text.pdf.FontSelector;

public static PdfPTable createTable() {
        FontSelector selector = new FontSelector();
        selector.addFont(FontFactory.getFont("Comic Sans MS"));
        PdfPTable table = new PdfPTable(3);
        PdfPCell cell;
        Phrase ph;

        ph = selector.process("My Title");
        cell = new PdfPCell(ph);
        table.addCell(cell);
        return table;
AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • 2
    It could be that your `FontFactory` doesn't know the "Comic Sans MS" font. `FontFactory.getRegisteredFonts()` should return a list of all of the font names that the FontFactory knows how to render. – Roddy of the Frozen Peas Aug 16 '12 at 21:37
  • Thank you. You're right. Running through all of the fonts that getRegisteredFonts() returns, there is no mention of Comic Sans MS. I pretty much knew this already by looking at their documentation. Comic Sans MS is, however, in my system fonts folder. Is there a way to draw upon the fonts on my system or am I limited to the embedded fonts in the FontFactory? Thanks! – AndroidDev Aug 16 '12 at 22:29

1 Answers1

0

You can import your font to FontFactory and then use it as

    String filename = "Comic Sans MS.ttf";
    FontFactory.register(filename, filename);
    Font font = FontFactory.getFont(filename, BaseFont.CP1252, BaseFont.EMBEDDED);

This will also embed the font in the PDF file so it will work even if target PDF reader does not have this font installed.

Erik Henriksson
  • 737
  • 1
  • 6
  • 13