【问题标题】:Can't set custom font for pdf for PdfContentByte - DroidText/iText library无法为 PdfContentByte 的 pdf 设置自定义字体 - DroidText/iText 库
【发布时间】:2013-07-09 11:34:50
【问题描述】:

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

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。

【问题讨论】:

  • 不考虑是什么意思?您是否收到其他字体的消息?或者根本没有?请提供样本结果。
  • 消息被打印但使用默认字体
  • 我尝试了多种字体,在其他地方(资产和内部存储)获取字体文件,结果相同
  • 请提供示例结果 PDF 并提供您使用的 iText 库的版本。
  • 我正在使用基于 iText 2.1.7 的 droidText 0.5

标签: pdf fonts itext


【解决方案1】:

偶然发现的,我想通过添加BaseFont.IDENTITY_H来显示重音

这是我更改的行:

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

【讨论】:

    【解决方案2】:

    同样的问题。

    因为使用泰文字体

    我下定决心,

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 2014-06-16
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多