简体   繁体   中英

iTextPdf: Using a non-embedded font

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM