我已将Bruno Lowagie的代码转介到垂直和水平的拆分表。但是我还有其他要求,例如设置标头。请在下面找到代码:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable tbl = new PdfPTable(new float[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1});
//loop through 100 rows
for (int r = 1; r <= 100; r++) {
for (int c = 1; c <= 24; c++) {
tbl.addCell(String.format("r%sc%s", r, c));
}
}
PdfContentByte canvas = writer.getDirectContent();
tbl.setTotalWidth(1500);//set table width
float top = document.top() - document.topMargin() - 30;
float yPos = top;
int start = 0;
int stop = 0;
for (int i = 0; i < 100; i++) {
yPos -= tbl.getRowHeight(i);
if (yPos < 0) {
stop = --i;
table = getHeaderRow(table);
tbl.writeSelectedRows(0, 12, start, stop, document.leftMargin(), top, canvas);
document.newPage();
tbl.writeSelectedRows(12, -1, start, stop, 5, top, canvas);
start = stop;
document.newPage();
yPos = top;
}
}
tbl.writeSelectedRows(0, 12, stop, -1, document.leftMargin(), top, canvas);
document.newPage();
tbl.writeSelectedRows(12, -1, stop, -1, 5, top, canvas);
document.close();
}
//Method to create header row
private static PdfPTable getHeaderRow(PdfPTable table) {
BaseColor myColor = WebColors.getRGBColor("#475a6d");
table.setWidthPercentage(100);
Font f = new Font();
f.setFamily("sans-serif");
f.setSize(10);
f.setColor(BaseColor.WHITE);
PdfPCell c1 = new PdfPCell(new Phrase("Id", f));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
c1.setNoWrap(true);
c1.setBackgroundColor(myColor);
table.addCell(c1);
//... and so on
}
这会导致在表列生成的末端添加标头行。谁能帮我吗?
我自己找到了答案。而不是在编写文档时添加标题行,而是将其添加到表创建本身中,然后重写以下代码:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(dest));
document.open();
PdfPTable tbl = new PdfPTable(new float[]
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1});
//loop through 100 rows
for (int r = 1; r <= 100; r++) {
for (int c = 1; c <= 24; c++) {
tbl.addCell(String.format("r%sc%s", r, c));
}
}
PdfContentByte canvas = writer.getDirectContent();
tbl.setTotalWidth(1500);//set table width
float top = document.top() - document.topMargin() - 30;
float yPos = top;
int start = 0;
int stop = 0;
for (int i = 0; i < 100; i++) {
yPos -= tbl.getRowHeight(i);
if (yPos < 0) {
stop = --i;
table = getHeaderRow(table);
if(start==0){
//If it is first row, header is already added
tbl.writeSelectedRows(0, 12, start, stop, document.leftMargin(), top, canvas);
} else {
//Adding first row explicitly
tbl.writeSelectedRows(0, 12, 0, 1, document.leftMargin(), top+table.getRowHeight(0), canvas);
tbl.writeSelectedRows(0, 12, start, stop, document.leftMargin(), top, canvas);
}
document.newPage();
if(start==0){
//If it is first row, header is already added
tbl.writeSelectedRows(12, -1, start, stop, 5, top, canvas);
} else {
//Adding first row explicitly
tbl.writeSelectedRows(12, -1, 0, 1, document.leftMargin(), top+table.getRowHeight(0), canvas);
tbl.writeSelectedRows(12, -1, start, stop, document.leftMargin(), top, canvas);
}
start = stop;
document.newPage();
yPos = top;
}
}
document.close();
}