iTextsharp custom border

enter image description here

i want know how can i append the custom border to the iTextSharp pdfdcell. like the problem is that i cant find a way to add border with 2 line. i can only create a normal bottom border in the pdfdcell by

PdfPCell tmp = new PdfPCell(new Phrase(c)); tmp.HorizontalAlignment = Element.ALIGN_RIGHT; tmp.Border = c.borderPosition; tmp.BorderWidth = c.borderWidth; pt.AddCell(tmp); 

enter image description here

so the result is something like this but i need to add one more line under the border.

asked Mar 15, 2017 at 9:48 920 2 2 gold badges 11 11 silver badges 35 35 bronze badges Commented Mar 15, 2017 at 10:33 @VinothRaj I think the OP does not mean text lines but instead border lines. Commented Mar 15, 2017 at 12:48 Commented Mar 15, 2017 at 16:45

i solved it by myself by drawing one more line below the bottom border. and this implementation is pretty weird, i think the link given by @kuujinbo could help, let me try that. thanks

Commented Mar 16, 2017 at 2:04

1 Answer 1

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[]); 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(); >> >