请在此处检查代码。从顶部到底部合并2个PDFPtable面临的问题。
PdfPTable table = new PdfPTable(new float[] { 1.0f, 2.0f, 5.0f, 1.0f, 2.0f });
table.setWidthPercentage(100.0f);
ArrayList<String> lHeaders = new ArrayList<>();
lHeaders.add("S. No.");
lHeaders.add("Course Code");
lHeaders.add("Course Name");
lHeaders.add("Credit");
lHeaders.add("Letter Grade");
ArrayList<String> lData = new ArrayList<>();
lData.add("1");
lData.add("D 210");
lData.add("COMPUTER & INFORMATION TECHNOLOGY FUNDAMENTAL LAB-II");
lData.add("1.0");
lData.add("BB");
// Create and add a title across both columns.
Font headerfont = new Font(Font.TIMES_ROMAN, 15, Font.BOLD);
// PdfPCell cell = new PdfPCell (new Paragraph ("New Mustang
// Features"));
PdfPCell cell;
for (String i : lHeaders) {
cell = new PdfPCell(new Paragraph(i, headerfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(new Color(255, 0, 0));
// cell.setPadding (10.0f);
table.addCell(cell);
}
// Add header cells for these columns.
// cell = new PdfPCell (new Paragraph ("Feature"));
// PdfPCell cell1;
Font datafont = new Font(Font.HELVETICA, 10, Font.NORMAL);
for (String j : lData) {
cell = new PdfPCell(new Paragraph(j, datafont));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setPadding (10.0f);
table.addCell(cell);
}
Font headerfont2 = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
PdfPTable table2 = new PdfPTable(8);
table2.setWidthPercentage(100.0f);
PdfPCell cell2;
cell2 = new PdfPCell(new Paragraph("CURRENT SEMESTER RECORD", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setColspan(4);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CUMULATIVE SEMESTER RECORD", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setColspan(4);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT POINTS", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CREDITS EARNED", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("SGPA", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT POINTS", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CREDITS EARNED", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CGPA", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("Result declared on :", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
cell2.setPaddingTop(40);
cell2.setBorderWidthLeft(0.5f);
cell2.setBorderWidthBottom(0.5f);
cell2.setColspan(4);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("Controller of Examinations", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setPaddingTop(40);
cell2.setBorderWidthRight(0.5f);
cell2.setBorderWidthBottom(0.5f);
cell2.setColspan(4);
table2.addCell(cell2);
我创建了两个PDFPtable实例,一个是table,另一个是table2,我只想合并这两个实例并从我的代码中返回。可能吗?我不想将其从代码添加到文档中并返回文档实例。 pdf看起来像附件的图像。
答案 0 :(得分:0)
总而言之,您实质上希望将这两个表合并为一个表,因为您的方法旨在仅返回一个表。
如果可能的话,最好的解决方案是更改方法的设计,并允许它以Iterable
(例如List
)或数组的形式返回多个表。
如果这不可能(在您的情况下,返回单个表似乎是项目要求),则解决方案是创建另一个单列表,并将第一个表放入第一行,将第二个表放入第二行。为了获得适当的外观,您可能还需要相应地调整表格属性的边距和边框。
但是请注意,iText 5表不是为密集表堆叠而设计的。在表格内部的表格中放置表格可能会在例如分页符,可能会占用大量资源。