iText显示Unicode字符的问题

时间:2018-09-25 06:28:15

标签: 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));

当我在视图中显示从其他活动获得的字符串时,文本看起来很正常,但是在创建PDF时,所有特殊字符都消失了。

3 个答案:

答案 0 :(得分:1)

请按照以下步骤操作:

  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;
}

答案 1 :(得分:0)

答案实际上非常简单。我要做的就是在资产文件夹中放置一个支持Unicode字符的字体,然后进行更改:

library(colorspace)

df <- matrix(c(20, 14, 26, 18, 14, 4, 19, 21, 13, 1, 5, 4), ncol = 4, byrow = TRUE)
rownames(df) <- c("Character", "Tree", "Distance")
colnames(df) <- c("nrITS", "trnH-\npsbA", "matK", "rbcL")
graph.dat <- as.table(df)
italic_latin2 <- c(expression(atop(italic("nrITS"), (104))), 
                   expression(atop(italic("trnH-\npsbA"), (82))), 
                   expression(atop(italic("matK"), (42))), 
                   expression(atop(italic("rbcL"), (28))))
barplot(graph.dat, beside = TRUE, ylab = "Percent Identified", 
        xlab = "Locus", ylim = c(0, 30), col = rainbow_hcl(3), 
        names.arg = italic_latin2)

bf = BaseFont.createFont("c:/windows/fonts/CAMBRIA.TTC", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

答案 2 :(得分:0)

我在调用 BaseFont.createFont API 时在编码参数中使用了 BaseFont.IDENTITY_H 并且它起作用了。