【问题标题】:iText - PdfPTable doesn't show Cyrillic(Russian) symbolsiText - PdfPTable 不显示西里尔文(俄语)符号
【发布时间】:2014-08-28 15:27:23
【问题描述】:

对不起我的英语。 我正在尝试使用 PdfPTable(iText 库)创建表。 表格显示空列![在此处输入图片描述][1]

public class CreatePDF {
File file;
BaseFont bf;
Font f_title;
Font f_text;

public void setFont() throws DocumentException, IOException{
    try{
        bf = BaseFont.createFont("/fonts/Times_New_Roman.ttf", BaseFont.IDENTITY_H , BaseFont.EMBEDDED); 
        f_title = new Font(bf, 14 );
        f_text = new Font(bf);
    }catch(Exception ex){
        ex.printStackTrace();
    }
}

public void make_invoice() throws DocumentException, IOException{
    setFont();
    Document doc = new Document(PageSize.A4);
    Desktop d = Desktop.getDesktop();
    try{
        file = new File("invoice.pdf");
        PdfWriter.getInstance(doc, new FileOutputStream(file));
        doc.open();
        Paragraph title = new Paragraph();
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(f_title);
        title.add("Счет фактура");//this work!

        doc.add(create_table());

        doc.close();

    }catch(Exception ex){
        ex.printStackTrace();
    }
}

public PdfPTable create_table() throws DocumentException{
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    table.setSpacingBefore(5f);

    PdfPCell cell;

    Phrase ph = new Phrase("Номер");//it's doesn't work ((
    ph.setFont(f_text);

    cell = new PdfPCell(ph);
    table.addCell(cell);
    table.addCell("Nuber");

    return table;
}

我尝试使用其他字体,但它对我没有帮助(((

我该如何解决这个问题?

【问题讨论】:

  • 试试new Phrase("Номер", font)
  • 谢谢!!为什么 ph.setFont(f_text);不工作?

标签: java pdf itext


【解决方案1】:

当您使用setFont() 时,您会更改Phrase 的字体,以便在设置字体之后添加所有内容。内容"Номер" 已经存在于Phrase您更改它之前。因此"Номер" 以默认字体 Helvetica 表示。由于 Helvetica 不知道如何表示西里尔字形,因此未呈现文本。

【讨论】:

  • 如果您指出了使用示例的正确方法,它将有助于理解您...仍然不理解您的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
相关资源
最近更新 更多