如何使用 iText5 在 table 中指定行背景
How to specify a row background in table with iText5
使用 iText5 创建 PDF table 时,可以通过实现 PdfPTableEvent
创建 table 背景,通过实现 PdfPCellEvent
创建单元格背景。
但是行背景呢?我该如何创建它?
原因是因为我想创建一个如下图所示的日历table:
实际上,您已经在问题中包含了答案:您必须使用 table 事件。看一下 RowBackground 示例。它包含一个 table 事件 RowBackgroundEvent
,允许您创建一个 table 事件来绘制单行的背景。
public class RowBackgroundEvent implements PdfPTableEvent {
// the row number of the row that needs a background
protected int row;
// creates a background event for a specific row
public RowBackgroundEvent(int row) {
this.row = row;
}
/**
* Draws the background of a row.
*/
@Override
public void tableLayout(PdfPTable table, float[][] widths, float[] heights,
int headerRows, int rowStart, PdfContentByte[] canvases) {
float llx = widths[row][0];
float lly = heights[row];
float urx = widths[row][widths[row].length - 1];
float ury = heights[row - 1];
float h = ury - lly;
PdfContentByte canvas = canvases[PdfPTable.BASECANVAS];
canvas.saveState();
canvas.arc(llx - h / 2, lly, llx + h / 2, ury, 90, 180);
canvas.lineTo(urx, lly);
canvas.arc(urx - h / 2, lly, urx + h / 2, ury, 270, 180);
canvas.lineTo(llx, ury);
canvas.setColorFill(BaseColor.LIGHT_GRAY);
canvas.fill();
canvas.restoreState();
}
}
这个事件是这样使用的:
public void createPdf(String filename) throws SQLException, DocumentException, IOException {
// step 1
Document document = new Document(PageSize.A4.rotate());
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
PdfPTableEvent event = new RowBackgroundEvent(3);
PdfPTable table = new PdfPTable(7);
table.setTableEvent(event);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
for (int i = 0; i < 10; i++) {
for (int j = 1; j < 8; j++) {
table.addCell(String.valueOf(j));
}
}
document.add(table);
// step 5
document.close();
}
如您所见,我们需要第三行的背景。结果如下所示:
如果您想调整背景栏的大小,您可以调整 llx
、lly
、urx
和 ury
值。
如果可以绘制单行背景,可以扩展代码绘制多行背景。
使用 iText5 创建 PDF table 时,可以通过实现 PdfPTableEvent
创建 table 背景,通过实现 PdfPCellEvent
创建单元格背景。
但是行背景呢?我该如何创建它?
原因是因为我想创建一个如下图所示的日历table:
实际上,您已经在问题中包含了答案:您必须使用 table 事件。看一下 RowBackground 示例。它包含一个 table 事件 RowBackgroundEvent
,允许您创建一个 table 事件来绘制单行的背景。
public class RowBackgroundEvent implements PdfPTableEvent {
// the row number of the row that needs a background
protected int row;
// creates a background event for a specific row
public RowBackgroundEvent(int row) {
this.row = row;
}
/**
* Draws the background of a row.
*/
@Override
public void tableLayout(PdfPTable table, float[][] widths, float[] heights,
int headerRows, int rowStart, PdfContentByte[] canvases) {
float llx = widths[row][0];
float lly = heights[row];
float urx = widths[row][widths[row].length - 1];
float ury = heights[row - 1];
float h = ury - lly;
PdfContentByte canvas = canvases[PdfPTable.BASECANVAS];
canvas.saveState();
canvas.arc(llx - h / 2, lly, llx + h / 2, ury, 90, 180);
canvas.lineTo(urx, lly);
canvas.arc(urx - h / 2, lly, urx + h / 2, ury, 270, 180);
canvas.lineTo(llx, ury);
canvas.setColorFill(BaseColor.LIGHT_GRAY);
canvas.fill();
canvas.restoreState();
}
}
这个事件是这样使用的:
public void createPdf(String filename) throws SQLException, DocumentException, IOException {
// step 1
Document document = new Document(PageSize.A4.rotate());
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
PdfPTableEvent event = new RowBackgroundEvent(3);
PdfPTable table = new PdfPTable(7);
table.setTableEvent(event);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
for (int i = 0; i < 10; i++) {
for (int j = 1; j < 8; j++) {
table.addCell(String.valueOf(j));
}
}
document.add(table);
// step 5
document.close();
}
如您所见,我们需要第三行的背景。结果如下所示:
如果您想调整背景栏的大小,您可以调整 llx
、lly
、urx
和 ury
值。
如果可以绘制单行背景,可以扩展代码绘制多行背景。