javaitextpdfptable

How to add 2 tables top to bottom to a single table using PDFPTable in Java


enter image description here

Please check the code here. Facing issue to merge 2 PDFPtable top to bottom.

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);

I have created two PDFPtable instances one is table and another is table2, i just want to merge these two instances and return from my code. Is it possible? I don't want to add it to the doc from my code and return the document instance. The pdf looks like the attached image.


Solution

  • To summarize, you essentially want to merge those two tables into a single one because your method is designed to return only a single table.

    If possible, the best solution would be to change the design of the method and allow it to return multiple tables, as an Iterable (e.g. a List) or as an array.

    If that is not possible (in your case returning a single table appears to be a project requirement), a solution would be to create another, single-column table and put your first table into its first row and your second one into the second row. For proper appearances you may want to also adapt table properties for margins and borders accordingly.

    Be aware, though, that iText 5 tables were not designed for intense table stacking; having tables inside tables inside tables is likely to cause interesting effects during e.g. page breaks and may be quite resource hungry.