java - iText 显示 unicode 字符的问题

标签 java android pdf itext pdf-generation

我需要一些有关 Android 上 iText 的帮助。我正在创建一个 PDF,需要它显示以下字符:ā、ū、ē、ķ、č。这是我用来创建自定义字体的代码。

BaseFont bf;
{
    try {
        bf = BaseFont.createFont("c:/windows/fonts/CAMBRIA.TTC", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Font smallFont = new Font(bf, 12);

这是我如何使用它的示例。

PdfPTable table_start = new PdfPTable(3);

    PdfPCell name = new PdfPCell(new Paragraph("Objekts: " + view0.getText().toString(), smallFont));

当我在 View 中显示从另一个 Activity 获取的字符串时,文本看起来很正常,但是当创建 PDF 时,所有特殊字符都消失了。

最佳答案

请按照以下步骤操作:

  1. 将 Unicode 文本转换为 ANSI 字体支持的文本
  2. 然后使用 ANSI 字体生成 pdf

首先,您必须像下面这样转换您的 Unicode 文本:

formattedMsg =  ANSIObj.ConvertToANSIText(outletObj.banglaName);

public String Convert(String line) throws UnsupportedEncodingException {

    //Your conversion Code
    return line;
}

然后创建 PDF

public void CreateBanglaMemoTableFormat(List<PDFTableData> items) {
    Document doc = new Document(PageSize.A4.rotate());

    try {
        setYourFont();
        doc = new Document(new Rectangle(818.3f, 609f));


        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyPDF";

        File dir = new File(path);
        if (!dir.exists())
            dir.mkdirs();

        File file = new File(dir, "newPDFFile.pdf");
        FileOutputStream fOut = new FileOutputStream(file);

        PdfWriter.getInstance(doc, fOut);

        //open the document
        doc.open();

         Paragraph p1 = new Paragraph();
         p1.setAlignment(Paragraph.ALIGN_LEFT);
         p1.setFont(f_textFont);
         p1.add(formattedMsg);

         PdfPTable tt = masteraTableCreation(p1);

         doc.add(tt);
        }

    } catch (DocumentException de) {
        Log.e("PDFCreator", "DocumentException:" + de);
    } catch (IOException e) {
        Log.e("PDFCreator", "ioException:" + e);
    } finally {
        doc.close();
    }
}

设置字体

 public void setYourFont() throws DocumentException, IOException {
    try {
        bf = BaseFont.createFont("/fontname", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        f_textFont = new Font(bf, 10);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

为 PDF 创建单元格

private PdfPTable masteraTableCreation(Paragraph celV1) {
    float[] columnWidths = {236};
    PdfPTable mainTable = new PdfPTable(columnWidths);
    mainTable.getDefaultCell().setPaddingRight(5);
    mainTable.setTotalWidth(786f);
    mainTable.setLockedWidth(true);

    PdfPCell cell;

    //region Row 1
    // column 1
    cell = new PdfPCell(new Phrase(celV1));
    cell.setBorder(Rectangle.NO_BORDER);
    mainTable.addCell(cell);

    return mainTable;
}

关于java - iText 显示 unicode 字符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52491882/

相关文章:

java - 使用ajax将参数传递给Controller

android - 在APK文件中插入音频

java - Servlet 使用 Apache FOP 抛出 Stackoverflow 错误

java - 编译器没有发现实例变量和构造函数初始化期间传递的参数之间存在歧义?

java - Integer set ADT的摊销分析(Java考试)

android - Android 在哪里存储关机日志?

android - getFrameAtTime() 返回同一帧

ios - 在 iOS CoreGraphics 中减小渲染 PDF 的文件大小

cocoa-touch - 在 iPad 上的外部应用程序中打开 PDF 文件

Java subList ConcurrentModificationException();