3
votes

I'm trying to use a custom font, I don't get any error but it doesn't take it into account.

Rectangle pageSize = basePdf.getPageSize(i);

PdfContentByte pdfContentByte = stamper.getOverContent(i);

// Use custom font
if (!FontFactory.isRegistered(FUTURA_LIGHT)) {
    FileHelper.copyFileFromAssetsToInternalStorage(mContext, FONT_PATH_IN_ASSETS);
    FontFactory.register(mContext.getFilesDir() + "/" + FONT_PATH_IN_ASSETS, FUTURA_LIGHT);
}
Font myFont = FontFactory.getFont(FUTURA_LIGHT);
BaseFont font = myFont.getBaseFont();

pdfContentByte.saveState();

pdfContentByte.stroke();
pdfContentByte.restoreState();
// Start to add text
pdfContentByte.beginText();
pdfContentByte.setFontAndSize(font, 6);
if (fontColor != null) {
    pdfContentByte.setColorFill(fontColor);
}

pdfContentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, message, pageSize.getWidth() / 2, 40, 0);
pdfContentByte.endText();

I've checked and the font is indeed registered, it just doesn't apply it to the PDF.

2
What does it doesn't take it into account mean? Do you get the message in some other font? Or not at all? Please supply a sample result.mkl
the message is printed but with the default fontMike Bryant
i've tried multiple fonts, fetching the font file elsewhere(assets and internal storage) with the same resultMike Bryant
Please supply a sample result PDF and also provide the version of the iText library you use.mkl
I'm using droidText 0.5 which is based off iText 2.1.7Mike Bryant

2 Answers

0
votes

I found it by accident, I was trying to display accents by adding BaseFont.IDENTITY_H

here's the line that I changed:

Font myFont = FontFactory.getFont(FUTURA_LIGHT, BaseFont.IDENTITY_H);
0
votes

Same problem.

because using Thai font.

My resolve to below,

String FONT_DEFAULT = ROOT_PATH + "/assets/THSarabunNew/THSarabunNew.ttf";

PdfContentByte pdfContentByte = pdfStamper.getOverContent(1);

pdfContentByte.beginText();
pdfContentByte.setFontAndSize(
        BaseFont.createFont(
                FONT_DEFAULT,
                BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED
        ), 12);
pdfContentByte.setTextMatrix(50, 760); // set x and y co-ordinates
pdfContentByte.showText("สวัสดีครับ"); // add the text
pdfContentByte.endText();

Work for me.

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.2</version>
</dependency>