如何将双线底部边框应用于iText中的单元格 [英] How to apply double line bottom border to a cell in iText

查看:353
本文介绍了如何将双线底部边框应用于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();

           }
   }

PDF代码为:

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.

推荐答案

在你的代码中,你正在绘制一个矩形:

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

请调整 position.getBottom() position.getBottom() - 4 如果您希望线条位于不同的位置。您可能还需要为单元格引入一些额外的填充以容纳额外的行。

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.

这篇关于如何将双线底部边框应用于iText中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆