2014-08-27 106 views
1

我很抱歉我的英語。 我想用PdfPTable(iText庫)創建表。 表給我空列![在這裏輸入的形象描述] [1]iText-PdfPTable不顯示西里爾文(俄語)符號

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

我嘗試使用其他字體,但它並不能幫助我(((

我怎樣才能解決這個問題?

+0

嘗試'新短語(「Номер」,字體)' – VahidN 2014-08-27 17:31:36

+0

謝謝!爲什麼ph.setFont(f_text);不工作? – inoob 2014-08-27 17:38:36

回答

0

當您使用setFont(),你改變了Phrase的字體爲所有被字體設置後添加的內容。內容"Номер"已經存在於Phrase前你改變了它。因此"Номер"以默認字體Helvetica表示。由於Helvetica不知道如何表示西里爾字形,所以文字沒有呈現。

+0

謝謝你的解釋! – inoob 2014-08-28 11:26:34