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

眉间皱痕 提交于 2020-01-05 08:48:46

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31588087/how-to-apply-double-line-bottom-border-to-a-cell-in-itext

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