Reputation: 9
I already created table using iText in java. But I want to create two parallel tables for one of my projects. Can anyone tell me the solution to create two parallel tables using iText please?
Upvotes: 0
Views: 6536
Reputation: 77528
Please take a look at the column_table.pdf document. Starting on page 3, you'll see that the pages have a layout with two columns next to each other. This is achieved by using the ColumnText
class. See ColumnTable for the source code or look at other code samples by going to the keyword page for ColumnText
.
Upvotes: 3
Reputation: 2052
Here is the code snippet
package com.example;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class PdfTest {
private Document document;
public PdfTest() {
document = new Document();
}
public boolean openPdf() {
boolean status = false;
try {
File pdfFile = new File("test.pdf");
if (pdfFile != null) {
PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
status = true;
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (DocumentException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return status;
}
public void closePdf() {
document.close();
}
public void generatePdf() throws DocumentException {
Paragraph paragraph = new Paragraph();
PdfPCell cell = null;
// Main table
PdfPTable mainTable = new PdfPTable(2);
mainTable.setWidthPercentage(100.0f);
// First table
PdfPCell firstTableCell = new PdfPCell();
firstTableCell.setBorder(PdfPCell.NO_BORDER);
PdfPTable firstTable = new PdfPTable(2);
firstTable.setWidthPercentage(50.0f);
cell = new PdfPCell(new Phrase("T1R1C1"));
firstTable.addCell(cell);
cell = new PdfPCell(new Phrase("T1R1C2"));
firstTable.addCell(cell);
cell = new PdfPCell(new Phrase("T1R2C1"));
firstTable.addCell(cell);
cell = new PdfPCell(new Phrase("T1R2C2"));
firstTable.addCell(cell);
firstTableCell.addElement(firstTable);
mainTable.addCell(firstTableCell);
// Second table
PdfPCell secondTableCell = new PdfPCell();
secondTableCell.setBorder(PdfPCell.NO_BORDER);
PdfPTable secondTable = new PdfPTable(2);
secondTable.setWidthPercentage(50.0f);
cell = new PdfPCell(new Phrase("T2R1C1"));
secondTable.addCell(cell);
cell = new PdfPCell(new Phrase("T2R1C2"));
secondTable.addCell(cell);
cell = new PdfPCell(new Phrase("T2R2C1"));
secondTable.addCell(cell);
cell = new PdfPCell(new Phrase("T2R2C2"));
secondTable.addCell(cell);
secondTableCell.addElement(secondTable);
mainTable.addCell(secondTableCell);
paragraph.add(mainTable);
document.add(paragraph);
}
}
Test this with
PdfTest pdfTest = new PdfTest();
try {
if (pdfTest.openPdf()) {
pdfTest.generatePdf();
pdfTest.closePdf();
}
} catch (Exception ex) {
ex.printStackTrace();
}
Hope this helps.
Upvotes: 3