2014-02-26 18 views
0

我是iTextSharp的新手。在下面的代碼設置PdfPCell VerticalAlignment(我認爲在文本模式)不做任何事情,文本仍然對齊到單元格的底部。 根據多個在線示例,它應該工作,但沒有。設置Horizo​​ntalAlignment的作品。 請幫忙。謝謝iTextSharp PdfPCell.VerticalAlignment在文本模式下不起作用

table = new PdfPTable(5); 

Document document = new Document(PageSize.A4); 
document.Open(); 

PdfPTable headerTable = new PdfPTable(3); 
float[] headerWidths = { 2f, 5f, 2f }; 
headerTable.SetWidths(headerWidths); 

myFont = FontFactory.GetFont("Arial", 8, Font.NORMAL); 
PdfPCell myCell = setupHeaderCell("My Text", myFont); 


// ADD headerTable to MainTable 
// Creates a PdfPCell that accepts the headerTable as a parameter and then adds that cell to the main PdfPTable. 
PdfPCell cellHeader = new PdfPCell(headerTable); 
cellHeader.Border = PdfPCell.NO_BORDER; 
// Sets the column span of the header cell to dataColumNb. 
cellHeader.Colspan = 5; 
// Adds the above header cell to the table. 
table.AddCell(cellHeader); 
................ 

} 

private PdfPCell setupHeaderCell(string lineText, Font myFont) 
{    
    var cl = new PdfPCell(new Phrase(lineText, myFont); 
    cl.VerticalAlignment = Element.ALIGN_MIDDLE; // does Not change vertical alignment 

    return cl; 
} 
+0

你正在使用什麼iText版本? –

回答

2

你有嵌套表,所以我不完全確定你想要做什麼。 VerticalAlignment與單元格的內容相關,而不是單元格本身,但我不確定這是否是您的問題。在對此進行故障排除時,我們只需簡單地通過消除複雜性並找到仍然導致問題的最簡單版本。我已經針對iTextSharp 5.4.4測試了下面的代碼,它正確地垂直對齊了最左邊的列。從此開始,慢慢添加您的邏輯,並可能包含一些您期望得到的圖紙和/或屏幕截圖。

var t2 = new PdfPTable(3); 
float[] headerWidths = { 2f, 5f, 2f }; 
t2.SetWidths(headerWidths); 

var cl = new PdfPCell(new Phrase("Test")); 
cl.VerticalAlignment = Element.ALIGN_MIDDLE; 

t2.AddCell(cl); 

t2.AddCell("This\nIs\nMore\nText"); 
t2.AddCell("This\nIs\nMore\nText"); 
+1

只是提出了:OP需要提供iText版本,最近有一些嵌套表的問題。 –

相關問題