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

3

我想使用自定义字体,没有出现任何错误,但是它没有被考虑在内。

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文件中。


它不考虑这个”是什么意思?你是否以其他字体收到了这条消息?还是根本没有收到?请提供一个样例结果。 - mkl
消息已经被打印,但是使用了默认字体。 - Mike Bryant
我尝试了多种字体,从其他地方(资产和内部存储)获取字体文件,但结果相同。 - Mike Bryant
请提供一个样例结果 PDF,并且提供您使用的 iText 库版本。 - mkl
我正在使用基于iText 2.1.7的droidText 0.5版本。 - Mike Bryant
2个回答

0

我是偶然发现的,我试图通过添加BaseFont.IDENTITY_H来显示重音符号。

这是我更改的行:

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

0

同样的问题。

是因为使用了泰语字体

我的解决方法如下:

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接