如何在Android中使用unicode支持创建PDF
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Android中使用unicode支持创建PDF相关的知识,希望对你有一定的参考价值。
我想在android中创建一个Pdf文件。我使用“itextpdf”并创建pdf文件,但在放入波斯文本后,并没有向我显示任何内容。下面你可以找到我使用https://medium.com/android-school/exploring-itext-to-create-pdf-in-android-5577881998c8的来源虽然我改变了字体,仍然有问题。
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
document.setPageSize(PageSize.A4);
document.addCreationDate();
document.addAuthor("Sample");
document.addCreator("Sample");
BaseColor mColorAccent = new BaseColor(0, 153, 204, 255);
float mHeadingFontSize = 20.0f;
float mValueFontSize = 26.0f;
BaseFont urName = BaseFont.createFont("assets/fonts/brandon_bold.otf", "UTF-8", BaseFont.EMBEDDED);
LineSeparator lineSeparator = new LineSeparator();
lineSeparator.setLineColor(new BaseColor(0, 0, 0, 68));
// Title Order Details...
// Adding Title....
Font mOrderDetailsTitleFont = new Font(urName, 36.0f, Font.NORMAL, BaseColor.BLACK);
Chunk mOrderDetailsTitleChunk = new Chunk("تست", mOrderDetailsTitleFont);
Paragraph mOrderDetailsTitleParagraph = new Paragraph(mOrderDetailsTitleChunk);
mOrderDetailsTitleParagraph.setAlignment(Element.ALIGN_CENTER);
document.add(mOrderDetailsTitleParagraph);
// Fields of Order Details...
// Adding Chunks for Title and value
Font mOrderIdFont = new Font(urName, mHeadingFontSize, Font.NORMAL, mColorAccent);
Chunk mOrderIdChunk = new Chunk("Order No:", mOrderIdFont);
Paragraph mOrderIdParagraph = new Paragraph(mOrderIdChunk);
document.add(mOrderIdParagraph);
Font mOrderIdValueFont = new Font(urName, mValueFontSize, Font.NORMAL, BaseColor.BLACK);
Chunk mOrderIdValueChunk = new Chunk("#123123", mOrderIdValueFont);
Paragraph mOrderIdValueParagraph = new Paragraph(mOrderIdValueChunk);
document.add(mOrderIdValueParagraph);
// Adding Line Breakable Space....
document.add(new Paragraph(""));
// Adding Horizontal Line...
document.add(new Chunk(lineSeparator));
// Adding Line Breakable Space....
document.add(new Paragraph(""));
// Fields of Order Details...
Font mOrderDateFont = new Font(urName, mHeadingFontSize, Font.NORMAL, mColorAccent);
Chunk mOrderDateChunk = new Chunk("Order Date:", mOrderDateFont);
Paragraph mOrderDateParagraph = new Paragraph(mOrderDateChunk);
document.add(mOrderDateParagraph);
Font mOrderDateValueFont = new Font(urName, mValueFontSize, Font.NORMAL, BaseColor.BLACK);
Chunk mOrderDateValueChunk = new Chunk("06/07/2017", mOrderDateValueFont);
Paragraph mOrderDateValueParagraph = new Paragraph(mOrderDateValueChunk);
document.add(mOrderDateValueParagraph);
document.add(new Paragraph(""));
document.add(new Chunk(lineSeparator));
document.add(new Paragraph(""));
// Fields of Order Details...
Font mOrderAcNameFont = new Font(urName, mHeadingFontSize, Font.NORMAL, mColorAccent);
Chunk mOrderAcNameChunk = new Chunk("Account Name:", mOrderAcNameFont);
Paragraph mOrderAcNameParagraph = new Paragraph(mOrderAcNameChunk);
document.add(mOrderAcNameParagraph);
Font mOrderAcNameValueFont = new Font(urName, mValueFontSize, Font.NORMAL, BaseColor.BLACK);
Chunk mOrderAcNameValueChunk = new Chunk("Pratik Butani", mOrderAcNameValueFont);
Paragraph mOrderAcNameValueParagraph = new Paragraph(mOrderAcNameValueChunk);
document.add(mOrderAcNameValueParagraph);
document.add(new Paragraph(""));
document.add(new Chunk(lineSeparator));
document.add(new Paragraph(""));
document.close();
答案
下面的网站对Unicode PDF https://developers.itextpdf.com/examples/font-examples/examples-font-tutorial#17-f05_russian_correct_encoding.java非常有用
我们必须使用支持Unicode的字体。此外,更改以下代码
BaseFont urName = BaseFont.createFont("assets/fonts/Freesans.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font mOrderDetailsTitleFont = new Font(urName, 36.0f, Font.NORMAL, BaseColor.BLACK);
Chunk mOrderDetailsTitleChunk = new Chunk(new StringBuilder("some unicode character").reverse().toString(),mOrderDetailsTitleFont);
Paragraph mOrderDetailsTitleParagraph = new Paragraph(mOrderDetailsTitleChunk);
mOrderDetailsTitleParagraph.setAlignment(Element.ALIGN_CENTER);
document.add(mOrderDetailsTitleParagraph);
以上是关于如何在Android中使用unicode支持创建PDF的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 的 TextView 中显示 Unicode 字符?
如何在 JSF 的支持 bean 中获取 Unicode 数据