iText 顶部和底部的虚线
Dotted Line in top and bottom in iText
我正在使用 iText 生成 pdf。我想在 table header 中画虚线。
现在我正在尝试这样。
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是这样的。
但我想从中删除左右边框。请让我知道如何删除左右边框。
table 中心的图像:
请查看 DottedLineHeader header example. You have copy/pasted code from the 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();
}
}
虽然您可能会说:嘿,这行得通!,如果您接受并点赞这样的答案,我会感到内疚,因为它不是最佳答案。为什么不?因为您将为每个单元格绘制一条单独的线,每条线的第一条破折号的起点不同。
更好的解决方案是将 header 行定义为 header 行(使用 setHeaderRows()
方法)并使用 table 事件绘制线条:
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();
}
}
在这种情况下,您只需为 header 编写两行,而不是在使用单元格事件时编写两行的倍数。
我正在使用 iText 生成 pdf。我想在 table header 中画虚线。 现在我正在尝试这样。
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是这样的。
但我想从中删除左右边框。请让我知道如何删除左右边框。
table 中心的图像:
请查看 DottedLineHeader header example. You have copy/pasted code from the 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();
}
}
虽然您可能会说:嘿,这行得通!,如果您接受并点赞这样的答案,我会感到内疚,因为它不是最佳答案。为什么不?因为您将为每个单元格绘制一条单独的线,每条线的第一条破折号的起点不同。
更好的解决方案是将 header 行定义为 header 行(使用 setHeaderRows()
方法)并使用 table 事件绘制线条:
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();
}
}
在这种情况下,您只需为 header 编写两行,而不是在使用单元格事件时编写两行的倍数。