英文:
Unable to add rows in a PDF table
问题
public class report {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\report.pdf"));
document.open();
PdfPTable table = new PdfPTable(4); // 4列
table.setWidthPercentage(100); // 宽度100%
table.setSpacingBefore(10f); // 表格前间距
table.setSpacingAfter(10f); // 表格后间距
// 设置列宽
float[] columnWidths = {1f, 1f, 1f , 1f};
table.setWidths(columnWidths);
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
cell1.setBorderColor(BaseColor.RED);
cell1.setBackgroundColor(BaseColor.GRAY);
cell1.setPaddingLeft(10);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
cell2.setBorderColor(BaseColor.RED);
cell2.setBackgroundColor(BaseColor.GRAY);
cell2.setPaddingLeft(10);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
cell3.setBorderColor(BaseColor.RED);
cell3.setBackgroundColor(BaseColor.GRAY);
cell3.setPaddingLeft(10);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
cell4.setBorderColor(BaseColor.WHITE);
cell4.setBackgroundColor(BaseColor.GRAY);
cell4.setPaddingLeft(10);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
document.add(table);
document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
英文:
I tried adding rows under the column. It must display 2 rows which consists of 4 columns each.
The below code provides 1 row and 4 column respectively.
I tried adding cell1.setRowSpan(1)
under each cell but that doesn't seems to work.
public class report {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\report.pdf"));
document.open();
PdfPTable table = new PdfPTable(4); // 4 columns.
table.setWidthPercentage(100); //Width 100%
table.setSpacingBefore(10f); //Space before table
table.setSpacingAfter(10f); //Space after table
//Set Column widths
float[] columnWidths = {1f, 1f, 1f , 1f};
table.setWidths(columnWidths);
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
cell1.setBorderColor(BaseColor.RED);
cell1.setBackgroundColor(BaseColor.GRAY);
cell1.setPaddingLeft(10);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
cell2.setBorderColor(BaseColor.RED);
cell2.setBackgroundColor(BaseColor.GRAY);
cell2.setPaddingLeft(10);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
cell3.setBorderColor(BaseColor.RED);
cell3.setBackgroundColor(BaseColor.GRAY);
cell3.setPaddingLeft(10);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
cell4.setBorderColor(BaseColor.WHITE);
cell4.setBackgroundColor(BaseColor.GRAY);
cell4.setPaddingLeft(10);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
document.add(table);
document.close();
writer.close();
} catch (Exception e) {`enter code here`
e.printStackTrace();
}
}
}
答案1
得分: 1
你已经定义了你的表格有4列:
PdfPTable table = new PdfPTable(4); // 4列。
而且你已经添加了4个单元格:
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
所以,只需继续添加更多的单元格。你添加的第5个单元格会自动成为第二行的第一个单元格。
如果你想看看我的意思,可以重复你的四个 addCell()
语句:
这只是重复了整个第一行。
英文:
You have already defined your table to have 4 columns:
PdfPTable table = new PdfPTable(4); // 4 columns.
And you have already added 4 cells:
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
So, just keep adding more cells. The 5th cell you add will automatically become the first cell of row 2.
You can repeat your four addCell()
statements if you want to see what I mean:
This just repeats the entire first row.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论