How to set only vertical line of a table in pdf using itext sharp?

早过忘川 提交于 2019-12-08 17:37:39

问题


I have a table with vertical and horizontal lines. But I do not want horizontal line.I want only Vertical lines.How can I set it. My expected o/p is

My Table code

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

ex:

Anybody please help


回答1:


You can change the borders of the cells so that they only show the vertical lines. How to do this, depends on how you add the cells to the table.

These are the two approaches:

1. You create PdfPCell objects explicitly:

PdfPCell cell = new PdfPCell(); cell.AddElement(new Paragraph("my content")); cell.Border = PdfPCell.LEFT; table.AddCell(cell);

In this case, only the left border of the cell will be shown. For the last cell in the row you should also add the right border:

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

2. You create PdfPCell objects implicitly:

In this case, you don't create a PdfPCell object yourself, you let iTextSharp create the cells. These cells will copy the properties of the DefaultCell that is defined at the level of the PdfPTable, so you need to change this:

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

Now all the cells won't have a top or bottom border, only a left and right border. You'll be drawing some extra lines, but nobody will notice as the lines coincide.

See also Hiding table border in iTextSharp

For instance:

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

There is no need to define the properties of the DefaultCell so many times. There is no need to nest Phrase constructors like this: new Phrase(new Phrase("content")).




回答2:


The answer of Bruno didn't helped me but I got an idea maybe because that's in the year 2015 but here's what I did. I declare a table

PdfPTable table2 = new PdfPTable(8);

defines the width..

table2.WidthPercentage = 100;

And finaly put only the border that I liked

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

and of course if you want to use it to display.

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

and of course adding it to the pdf.

doc.Add(table2);

It seems that iTextSharp has built-in column/rows like cell type if you didn't specify it.



来源:https://stackoverflow.com/questions/32774984/how-to-set-only-vertical-line-of-a-table-in-pdf-using-itext-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!