itext android,itext android hindi pdf creation

First, put the your FreeSans.ttf file in the assets forlder. iText library takes the file path of the font file so you cannot just give the assets path to it since you cannot directly access assets folder. Following is the code to copy your font file from the assets folder to your app's local directory.

public static final String HINDI_FONT = "FreeSans.ttf";

try {

File file = new File(getFilesDir(), HINDI_FONT);

if (file.length() == 0) {

InputStream fs = getAssets().open(HINDI_FONT);

FileOutputStream os = new FileOutputStream(file);

int i;

while ((i = fs.read()) != -1) {

os.write(i);

}

os.flush();

os.close();

fs.close();

}

} catch (IOException e) {

e.printStackTrace();

}

Execute the try block code in a separate thread, do not do this in the main thread.

The if statement insures that this happens only once. Please write the stream close statements in finally block instead of try block as shown above.

Next is the itext statements to use the font

Font f = FontFactory.getFont(getFilesDir() + "/" + HINDI_FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

PdfPCell eCell = new PdfPCell(new Phrase(entry, f));

eCell.setPadding(5);

table.addCell(eCell);

Here, the varaible entry is your hindi text and the code is written in an Activity so getFilesDir() works. I have added a phrase in a table cell. You can use it similarly in a paragraph as well. I have checked this code and it works.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值