0

i want know how can i append the custom border to the iTextSharp pdfdcell.

like

enter image description here

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

so the result is something like this

enter image description here

but i need to add one more line under the border.

SKLTFZ
  • 841
  • 2
  • 10
  • 30
  • http://stackoverflow.com/questions/21939280/itextsharp-multiple-lines-in-pdfpcell-one-under-another – Vinoth Raj Mar 15 '17 at 10:33
  • @VinothRaj I think the OP does not mean text lines but instead border lines. – mkl Mar 15 '17 at 12:48
  • 1
    [http://stackoverflow.com/questions/31588087](http://stackoverflow.com/questions/31588087) – kuujinbo Mar 15 '17 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 – SKLTFZ Mar 16 '17 at 02:04

1 Answers1

0

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();
        }
    }
}
SKLTFZ
  • 841
  • 2
  • 10
  • 30