Itextsharp Pdfptable How to Make a Border Around Entire Table

Hiding table border in iTextSharp

Although I upvoted the answer by Martijn, I want to add a clarification.

Only cells have borders in iText; tables don't have a border. Martijn's suggestion to set the border of the default cell to NO_BORDER is correct:

table.DefaultCell.Border = Rectangle.NO_BORDER;

But it won't work for the code snippet provided in the question. The properties of the default cell are only used if iText needs to create a PdfPCell instance implicitly (for instance: if you use the addCell() method passing a Phrase as parameter).

In the code snippet provided by @Brown_Dynamite, the PdfPCell objects are created explicitly. This means that you need to set the border of each of these cells to NO_BORDER.

Usually, I write a factory class to create cells. That way, I can significantly reduce the amount of code in the class that creates the table.

How to show both the bottom and the top border of a cell in iTextSharp?

More than likely it is flag based.

Give this a shot:

Border = PdfPCell.BOTTOM_BORDER | PdfPCell.TOP_BORDER

How to make the table border length as per cell data length c#?

iText 7

In iText 7, this is very straightforward, because it's possible out of the box to set borders to any element. In this use case, instead of using table or cell borders, it's easier to set a top and bottom border to the piece of text itself.

Table table = new Table(4);
table.SetWidth(UnitValue.CreatePercentValue(100));
// no border on the table
table.SetBorder(null);

// no border on the cell
Cell cell = new Cell().SetBorder(null);
Text t = new Text("410.40");
// top and bottom border on the Text instance
t.SetBorderTop(new SolidBorder(1.5f));
t.SetBorderBottom(new SolidBorder(1.5f));
Paragraph p = new Paragraph().Add(t);
cell.Add(p);
table.AddCell(cell);

// some test cells
table.AddCell(new Cell().Add(new Paragraph("Column 2")).SetBorder(null));
table.AddCell(new Cell().Add(new Paragraph("C 3")).SetBorder(null));
table.AddCell(new Cell().Add(new Paragraph("C 4")).SetBorder(null));

doc.Add(table);

Sample Image

iText 5 / iTextSharp

A bit more grunt work is involved to get the same effect. A possible approach is to use a page event listener and a Chunk with a "generic tag" to trigger a page event upon rendering. That callback will expose the rendering rectangle of the Chunk, which allows those coordinates to be used to draw the top and bottom line at the correct location.

writer.PageEvent = new BorderEvent();

PdfPTable table = new PdfPTable(4);
table.WidthPercentage = 100;

PdfPCell cell = new PdfPCell();
// Use a Chunk with a "generic tag", which triggers a callback when it's rendered
Chunk c = new Chunk("410.40");
c.SetGenericTag("borders");
cell.AddElement(c);
// no border on the cell
cell.Border = 0;
table.AddCell(cell);

// some test cells
cell = new PdfPCell();
cell.AddElement(new Paragraph("Column 2"));
cell.Border = 0;
table.AddCell(cell);
cell = new PdfPCell();
cell.AddElement(new Paragraph("C 3"));
cell.Border = 0;
table.AddCell(cell);
cell = new PdfPCell();
cell.AddElement(new Paragraph("C 4"));
cell.Border = 0;
table.AddCell(cell);

doc.Add(table);

The callback for the generic tag:

class BorderEvent : PdfPageEventHelper
{
public override void OnGenericTag(PdfWriter writer, Document document,
Rectangle rect, String text)
{
PdfContentByte canvas = writer.DirectContent;
// draw the top border, based on the rendering rectangle
canvas.SetLineWidth(1.5);
canvas.MoveTo(rect.Left, rect.Top);
canvas.LineTo(rect.Right, rect.Top);
// draw the bottom border, based on the rendering rectangle
// the bottom coordinate is the base line of the text,
// so some calculation, probably including the font size, is
// needed to lower it a bit
// I've used a quick and dirty 3 points here
canvas.Stroke();
canvas.MoveTo(rect.Left, rect.Bottom - 3);
canvas.LineTo(rect.Right, rect.Bottom - 3);
canvas.Stroke();
}
}

Sample Image

ITextSharp: Set table cell border color

When you set individual cell border properties you either need to set all border colors and widths individually, or explicitly set the UseVariableBorders property to true. Try this example to see what I mean:

PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("test 1"));
cell.UseVariableBorders = true;
cell.BorderColorLeft = BaseColor.BLUE;
cell.BorderColorRight = BaseColor.ORANGE;
table.AddCell(cell);

cell = new PdfPCell(new Phrase("test 2"));
cell.BorderColorLeft = BaseColor.RED;
cell.BorderColorRight = BaseColor.GREEN;
cell.BorderColorTop = BaseColor.PINK;
cell.BorderColorBottom = BaseColor.YELLOW;
cell.BorderWidthLeft = 1f;
cell.BorderWidthRight = 1f;
cell.BorderWidthTop = 1f;
cell.BorderWidthBottom = 1f;
table.AddCell(cell);

cell = new PdfPCell(new Phrase("test 3"));
cell.BorderColor = BaseColor.GREEN;
table.AddCell(cell);

iTextsharp custom border

Since I am using C# and iTextSharp and the comment is only showing the solution on Java.

I have implemented the similar thing to solve the issue.

The basic fact is that iTextSharp didn't support custom border, but it allows you to draw things on the PDF. Thus the objective is to draw a doubleline at the bottom of the cell.

  1. hide existing border
  2. find out the exact position of the cell
  3. draw lines

the trick is that implementing the CellEvent on the cell, within the cellevent it gave us the exact position of the cell, thus we draw things easily.

below is the code which working in my C# project

public function void DrawACell_With_DOUBLELINE_BOTTOM_BORDER(Document doc, PdfWriter writer){
PdfPTable pt = new PdfPTable(new float[]{1});
Chunk c = new Chunk("A Cell with doubleline bottom border");
int padding = 3;
PdfPCell_DoubleLine cell = new PdfPCell_DoubleLine(PdfPTable pt,new Phrase(c), writer, padding);
pt.AddCell(cell);
doc.Add(pt);
}

public class PdfPCell_DoubleLine : PdfPCell
{
public PdfPCell_DoubleLine(Phrase phrase, PdfWriter writer, int padding) : base(phrase)
{
this.HorizontalAlignment = Element.ALIGN_RIGHT;
//1. hide existing border
this.Border = Rectangle.NO_BORDER;
//2. find out the exact position of the cell
this.CellEvent = new DLineCell(writer, padding);
}
public class DLineCell : IPdfPCellEvent
{
public PdfWriter writer { get; set; }
public int padding { get; set; }
public DLineCell(PdfWriter writer, int padding)
{
this.writer = writer;
this.padding = padding;
}

public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] canvases)
{
//draw line 1
PdfContentByte cb = writer.DirectContent;
cb.MoveTo(rect.GetLeft(0), rect.GetBottom(0) - padding);
cb.LineTo(rect.GetRight(0), rect.GetBottom(0) - padding);
//draw line 2
cb.MoveTo(rect.GetLeft(0), rect.GetBottom(0) - padding - 2);
cb.LineTo(rect.GetRight(0), rect.GetBottom(0) - padding - 2);
cb.Stroke();
}
}
}

Image overwrites table cell border in iTextSharp

Try adding this:

cell.PaddingBottom = 5;

So the updated code would be:

PdfPTable table = new PdfPTable(1);
table.DefaultCell.Border = PdfPCell.NO_BORDER;
table.WidthPercentage = 100;

Image img = Image.GetInstance("Logo.PNG");
PdfPCell cell = new PdfPCell(img, false);
cell.Border = PdfPCell.BOTTOM_BORDER;
cell.PaddingBottom = 5;
table.AddCell(cell);

PdfPCell cell2 = new PdfPCell(new Phrase("Title"));
table.AddCell(cell2);

document.Add(table);
document.Close();

Something else to try:

cell.UseBorderPadding = true;

border of PdfPTable - iText, java

In iText each PdfPCell has its own borders.

You can set the borders needed on the cell, and in case of a nested table do the same for that tables cells.

In your case you probably want to set the bottom border on the entire row that is to be separated.



Related Topics



Leave a reply



Submit