【发布时间】:2017-01-02 10:50:20
【问题描述】:
我正在使用 iText PDF 在 android 中创建 PDF 文件。我需要在 PDF 中写下“₹”符号。 我有以下代码:-
public static final String FONT1 = "/main/assets//PlayfairDisplay-Regular.ttf";
public static final String FONT2 = "/main/assets/PT_Sans-Web-Regular.ttf";
public static final String FONT3 = "/main/assets/FreeSans.ttf";
public static final String RUPEE = "The Rupee character \u20B9 and the Rupee symbol \u20A8";
这些被声明为类变量。 我有一个函数 createPDF() 写入 PDF 文档。 我在函数中有以下代码行:-
File pdfFile = new File(filePath);
OutputStream output = new FileOutputStream(pdfFile);
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, output);
document.open();
Font f1 = FontFactory.getFont(FONT1, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f2 = FontFactory.getFont(FONT2, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f3 = FontFactory.getFont(FONT3, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
Font f4 = FontFactory.getFont(FONT3, BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
document.add(new Paragraph(RUPEE, f1));
document.add(new Paragraph(RUPEE, f2));
document.add(new Paragraph(RUPEE, f3));
document.add(new Paragraph(RUPEE, f4));
document.close();
但是,字体既不会反映在 PDF 中,也不会反映在卢比符号中。 我的 PDF 如下所示:-
我已使用以下链接执行步骤:- iText Developers Tutorial 和StackOverflow Question on where to place Assets folder
我知道关于 SO 的另一个类似问题:- Rupee symbol is not showing in android 但是这也没有帮助我。
我在这里做错了吗?我的字体是不是放错了位置?
【问题讨论】:
-
字体路径错误。我怎么知道? PDF 中显示的所有文本均采用 Helvetica(这是默认字体)。
-
尝试,删除所有 /main/ 只保留资产/PlayfairDisplay-Regular.ttf 等...并且在您拥有的第一个字体中 // 也删除那个并尝试
-
@BrunoLowagie 。谢谢帮助:)
-
@Raghavendra 对此表示感谢。这有帮助。 :) 对我来说很傻。
-
@Raghavendra 请给出 Pratik 可以接受的实际答案。
标签: java android pdf fonts itext