Search code examples
javaitextborder-layout

How to apply double line bottom border to a cell in iText


I need your help in applying the double line bottom border only to a cell and to remove the other top, right and left borders. I am able to implement the dotted line cell borders by using the below code:

class DoubleCell implements PdfPCellEvent {

   public void cellLayout(PdfPCell cell, Rectangle position,

      PdfContentByte[] canvases) {
      PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
      canvas.setLineDash(5f, 5f);
      canvas.rectangle(position.getLeft(), position.getBottom(),
                       position.getWidth(), position.getHeight());
      canvas.stroke();

           }
   }

And the PDF code is:

Paragraph tableParagraph = new Paragraph();
tableParagraph.setAlignment(Element.ALIGN_CENTER);
PdfPTable table = new PdfPTable(2);
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
Font total = new Font(Font.TIMES_ROMAN, 16);
PdfPCell cel3a = new PdfPCell(new Paragraph("Total",total));
PdfPCell cel3b = new PdfPCell(new Paragraph("Cell 1",total));
cel3a.setBorder(Rectangle.NO_BORDER);
cel3b.setBorder(Rectangle.NO_BORDER );
cel3b.setCellEvent(new DoubleCell());
table.addCell(cel3a);
table.addCell(cel3b);
tableParagraph.add(table);

So please assist in applying the double line bottom border only without the other borders.


Solution

  • In your code, you are drawing a rectangle:

    canvas.rectangle(position.getLeft(), position.getBottom(),
        position.getWidth(), position.getHeight());
    canvas.stroke();
    

    If you want to draw two lines, you need to draw two lines:

    // construct first line:
    canvas.moveTo(position.getLeft(), position.getBottom());
    canvas.lineTo(position.getRight(), position.getBottom());
    // construct second line (4 user units lower):
    canvas.moveTo(position.getLeft(), position.getBottom() - 4);
    canvas.lineTo(position.getRight(), position.getBottom() - 4);
    // draw lines
    canvas.stroke();
    

    Please adapt position.getBottom() and position.getBottom() - 4 if you want the lines to be at a different position. You may also want to introduce some extra padding to your cells to accommodate for the extra lines.