NestedTableCellPDF.java Source code

Java tutorial

Introduction

Here is the source code for NestedTableCellPDF.java

Source

import java.io.FileOutputStream;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class NestedTableCellPDF {
    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("NestedTableCellPDF.pdf"));
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            PdfPTable pageTot = new PdfPTable(1);
            pageTot.getDefaultCell().setPadding(0f);
            pageTot.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            pageTot.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
            pageTot.setWidthPercentage(100f);

            PdfPTable cell = new PdfPTable(1);
            cell.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            cell.getDefaultCell().setPadding(0f);

            PdfPCell info = new PdfPCell(new Phrase("Outside"));
            info.setBorder(Rectangle.NO_BORDER);
            pageTot.addCell(info);

            PdfPCell shipment = new PdfPCell(new Phrase(new Chunk("Cell")));
            shipment.setFixedHeight(100);
            shipment.setPaddingTop(5f);
            shipment.setPaddingBottom(10f);
            shipment.setBorder(Rectangle.BOX);
            shipment.setVerticalAlignment(Element.ALIGN_TOP);
            shipment.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.addCell(shipment);

            pageTot.addCell(cell);

            document.add(pageTot);
        } catch (Exception e) {
            e.printStackTrace();
        }
        document.close();
    }

}