我正在使用iText生成pdf。我想在表格标题中绘制虚线。
现在,我正在尝试这样。

private void createTable(Document document)  throws DocumentException {
              Report_Page app = new Report_Page();
            float[] columnWidths = { 1.5f, 5f, 2f, 1.5f, 2f };
            PdfPTable table = new PdfPTable(columnWidths);

            table.setTotalWidth(300f);

            PdfPCell cell = new PdfPCell(new Phrase("P.No"));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setCellEvent(app.new DottedCell());
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);


            cell = new PdfPCell(new Phrase("Item Name"));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setCellEvent(app.new DottedCell());
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);


            cell = new PdfPCell(new Phrase("Price"));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setCellEvent(app.new DottedCell());
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);


            cell = new PdfPCell(new Phrase("Qty"));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setCellEvent(app.new DottedCell());
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);


            cell = new PdfPCell(new Phrase("Ext Price"));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setCellEvent(app.new DottedCell());
            cell.setBorder(Rectangle.NO_BORDER);
            table.addCell(cell);
            table.setHeaderRows(1);


        }


class DottedCell implements PdfPCellEvent {

            @Override
            public void cellLayout(PdfPCell cell, Rectangle position,
                    PdfContentByte[] canvases) {
                PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
                canvas.setLineDash(3f, 3f);
                canvas.rectangle(position.getLeft(), position.getBottom(),
                    position.getWidth(), position.getHeight());
                canvas.stroke();

            }
        }


现在,O / P就是这样。


但是我想从此删除左右边框。请让我有任何想法删除左边框和右边框。

桌子中心的图片:

最佳答案

请看一下DottedLineHeader标头示例。您已经从DottedLineCell复制/粘贴了代码,并创建了一个完整的矩形而不是两条单独的线。因此,您的问题的答案可能是:画两条线而不是矩形的四边:

class DottedCell implements PdfPCellEvent {
    public void cellLayout(PdfPCell cell, Rectangle position,
        PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
        canvas.setLineDash(3f, 3f);
        canvas.moveTo(position.getLeft(), position.getTop());
        canvas.lineTo(position.getRight(), position.getTop());
        canvas.moveTo(position.getLeft(), position.getBottom());
        canvas.lineTo(position.getRight(), position.getBottom());
        canvas.stroke();
    }
}


虽然您可能会说:“嘿,这行得通!”,但如果您接受并提出这样的答案,那不是最好的答案,我会感到内the。为什么不?因为您将为每个单元格绘制一条单独的线,并且每条线的第一个破折号的起始点都不同。

更好的解决方案是将标题行定义为标题行(使用setHeaderRows()方法),并使用表事件绘制线条:

class DottedHeader implements PdfPTableEvent {
    public void tableLayout(PdfPTable table, float[][] widths,
        float[] heights, int headerRows, int rowStart,
        PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
        canvas.setLineDash(3f, 3f);
        float x1 = widths[0][0];
        float x2 = widths[0][widths.length];
        canvas.moveTo(x1, heights[0]);
        canvas.lineTo(x2, heights[0]);
        canvas.moveTo(x1, heights[headerRows]);
        canvas.lineTo(x2, heights[headerRows]);
        canvas.stroke();
    }
}


在这种情况下,您只为标题写两行,而不是在使用单元格事件时写两行的倍数。

07-28 01:51
查看更多