无法为 PdfContentByte 的 pdf 设置自定义字体 - DroidText/iText 库



我正在尝试使用自定义字体,我没有收到任何错误,但它没有考虑到这一点。

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();

我已经检查过了,字体确实是注册的,只是它不适用于PDF。

我偶然发现了它,我试图通过添加BaseFont.IDENTITY_H 来显示重音

这是我改的线路:

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

同样的问题。

因为使用泰语字体

我下定决心,

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();

为我工作。

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

最新更新