2015-09-25 35 views
4

我有一個垂直和水平線的表。但我不想要水平線。我只想要垂直線。我可以如何設置它。我的預期O/P是如何在pdf中使用itext sharp只設置表格的垂直線?

我的表碼

PdfPTable table = new PdfPTable(5); 
table.TotalWidth = 510f;//table size 
table.LockedWidth = true; 
table.HorizontalAlignment = 0; 
table.SpacingBefore = 10f;//both are used to mention the space from heading 


table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; 
table.AddCell(new Phrase(new Phrase(" SL.NO", font1))); 

table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; 
table.AddCell(new Phrase(new Phrase(" SUBJECTS", font1))); 

table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; 
table.AddCell(new Phrase(new Phrase(" MARKS", font1))); 

table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; 
table.AddCell(new Phrase(new Phrase(" MAX MARK", font1))); 

table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; 
table.AddCell(new Phrase(new Phrase(" CLASS AVG", font1))); 

Doc.Add(table); 

例如:

enter image description here

有人請幫助

+0

@布魯諾Lowegi ..我希望你能幫助我 –

回答

5

您可以更改單元格的邊框,使他們只顯示垂直線。如何做到這一點取決於你如何將單元格添加到表格中。

這是兩種方法:

1.創建PdfPCell對象明確:

PdfPCell電池=新PdfPCell(); cell.AddElement(new Paragraph(「my content」)); cell.Border = PdfPCell.LEFT; table.AddCell(cell);

在這種情況下,只顯示左邊的邊框。對於行中的最後一個單元格,您還應該添加正確的邊框:

cell.Border = PdfPCell.LEFT | PdfPCell.RIGHT;

2.創建PdfPCell隱含對象:

在這種情況下,你沒有自己創建一個PdfPCell對象,你讓iTextSharp的創建細胞。這些細胞會複製是在PdfPTable的級別定義的DefaultCell的屬性,所以你需要改變這一點:

table.DefaultCell.Border = Rectangle.LEFT | Rectangle.RIGHT; 
table.addCell("cell 1"); 
table.addCell("cell 2"); 
table.addCell("cell 3"); 

現在所有的細胞將不會有一個頂部或底部邊框,只有左和右邊界。你會畫出一些額外的線條,但沒有人會注意到線條重合。

Hiding table border in iTextSharp

見例如:

PdfPTable table = new PdfPTable(5); 
table.TotalWidth = 510f;//table size 
table.LockedWidth = true; 
table.SpacingBefore = 10f;//both are used to mention the space from heading 
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; 
table.DefaultCell.Border = PdfPCell.LEFT | PdfPCell.RIGHT; 
table.AddCell(new Phrase(" SL.NO", font1)); 
table.AddCell(new Phrase(" SUBJECTS", font1)); 
table.AddCell(new Phrase(" MARKS", font1)); 
table.AddCell(new Phrase(" MAX MARK", font1)); 
table.AddCell(new Phrase(" CLASS AVG", font1)); 
Doc.Add(table); 

沒有必要界定DefaultCell這麼多次的特性。沒有必要嵌套Phrase這樣的構造函數:new Phrase(new Phrase("content"))

+0

請找到我編輯的問題。這是我創建表的方式。我怎樣才能做到這一點? –

+0

你有太多的代碼。我會更新我的答案。 –

+0

:P,從早上起我就在等着你.. –

0

布魯諾的答案並沒有幫助我,但我有一個想法,也許是因爲那是在2015年,但這是我做的。 我申報表

PdfPTable table2 = new PdfPTable(8); 

定義寬度..

table2.WidthPercentage = 100; 

而且finaly只放,如果你想用它來顯示我喜歡

table2.DefaultCell.Border = Rectangle.RIGHT_BORDER; 
table2.DefaultCell.Border = Rectangle.LEFT_BORDER; 

當然和邊界。

table2.AddCell(new Phrase("Total Amount", ftxt)); 
table2.AddCell(new Phrase("Another text", ftxt)); 

並且當然將其添加到pdf中。

doc.Add(table2); 

看起來像iTextSharp內置列/行像單元格類型如果你沒有指定它。

相關問題