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;
4

1 回答 1

0

您可以将字体导入到FontFactory然后将其用作

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

这也会将字体嵌入到 PDF 文件中,因此即使目标 PDF 阅读器没有安装此字体,它也能正常工作。

于 2013-03-01T02:09:57.690 回答