2016-11-21 23 views
0

我有一個有幾個數字列的表。我可以對齊行列就好了,但我不能右側列標題列。我認爲,原因是因爲PdfPHeaderCell構造函數不接受Phrase對象,所以我必須使用cell.addElement(),它將其置於複合模式,忽略我指定的對齊方式。PdfPHeaderCell在文本模式下對齊內容

我發現實現我所尋找的效果的唯一方法是創建一個段落,爲其分配對齊,然後將其放在標題中。這對我來說似乎有點笨拙,因爲我不需要任何段落的任何特徵,我只需要對齊它。

有沒有更乾淨的方法來做到這一點?

/** 
    * Builds a basic header cell from the given string 
    * 
    * @param content 
    * @param alignment 
    * @return PdfPHeaderCell 
    */ 
    protected static PdfPHeaderCell getGenericHeaderCell(String content, Integer alignment) 
    { 
    PdfPHeaderCell cell = new PdfPHeaderCell(); 

    Paragraph p = new Paragraph(); 
    p.add(new Phrase(content, TABLE_HEADER)); 
    if (alignment != null) 
    { 
     p.setAlignment(alignment); 
    } 

    cell.addElement(p); 
    cell.setBorder(Rectangle.BOTTOM); 
    cell.setBorderColorBottom(TABLE_HEADER_BORDERCOLOR); 
    cell.setBorderWidthBottom(1); 
    cell.setPaddingTop(0); 
    return cell; 
    } 

回答

0

首先這樣的:你需要一個Phrase但是你用一個Paragraph,這樣你就可以更換:

Paragraph p = new Paragraph(); 
p.add(new Phrase(content, TABLE_HEADER)); 
if (alignment != null) 
{ 
    p.setAlignment(alignment); 
} 

有:

Phrase p = new Phrase(content, TABLE_HEADER); 
if (alignment != null) 
{ 
    p.setAlignment(alignment); 
} 

在你的代碼中實際的錯誤,是事實上你使用addElement()。您聲稱您以文本模式創建單元格,但忘記了使用addElement()使單元格切換到複合模式。

您可以通過替換解決這個問題:

cell.addElement(p); 

有:

cell.setPhrase(p); 

這令在文本模式下的單元格。