package examples.tables;

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TableImage2 {

    public static void main(String[] args) throws IOException,
                                                  DocumentException {
        // step 1
        Document document = new Document();
       
        // step 2
        PdfWriter writer =
            PdfWriter.getInstance(document, new FileOutputStream("image_table.pdf"));
        writer.setFullCompression();
        // step 3
        document.open();
        // step 4
        Image img = Image.getInstance("smiley.png");
        img.scalePercent(200);
        
        PdfPTable outerTable = new PdfPTable(1);
        PdfPCell outerCell;
        PdfPTable innerTable;
        PdfPCell innerCell;
        
        Paragraph para =
            new Paragraph("Java is a general-purpose, concurrent, class-based, object-oriented computer programming language that is specifically designed to have as few implementation dependencies as possible. It is intended to let application developers \"write once, run anywhere\" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another. Java applications are typically compiled to bytecode (class file) that can run on any Java virtual machine (JVM) regardless of computer architecture. Java is, as of 2012, one of the most popular programming languages in use, particularly for client-server web applications, with a reported 10 million users. Java was originally developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation) and released in 1995 as a core component of Sun Microsystems Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.The original and reference implementation Java compilers, virtual machines, and class libraries were developed by Sun from 1991 and first released in 1995. As of May 2007, in compliance with the specifications of the Java Community Process, Sun relicensed most of its Java technologies under the GNU General Public License. Others have also developed alternative implementations of these Sun technologies, such as the GNU Compiler for Java and GNU Classpath. Java is a general-purpose, concurrent, class-based, object-oriented computer programming language that is specifically designed to have as few implementation dependencies as possible. It is intended to let application developers \"write once, run anywhere\" (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another. Java applications are typically compiled to bytecode (class file) that can run on any Java virtual machine (JVM) regardless of computer architecture. Java is, as of 2012, one of the most popular programming languages in use, particularly for client-server web applications, with a reported 10 million users. Java was originally developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation) and released in 1995 as a core");
        
        outerCell = new PdfPCell();
        outerCell.addElement(para);
        outerTable.addCell(outerCell);

        document.add(outerTable);

        para = new Paragraph("<Some Paragraph>");
        document.add(para);
        para = new Paragraph(" ");
        document.add(para);

        outerTable = new PdfPTable(1);
        for (int i = 0; i < 5; i++) {
            
            innerTable = new PdfPTable(1);
            innerTable.setWidthPercentage(100);

            innerCell = new PdfPCell();
          
            innerCell.addElement(new Paragraph("<Image Heading>"));
            innerTable.addCell(innerCell);

            innerCell = new PdfPCell();
            
            innerCell.addElement(img);
            innerTable.addCell(innerCell);

            outerCell = new PdfPCell();
            
            outerCell.addElement(innerTable);
            outerTable.addCell(outerCell);
        }
        document.add(outerTable);
        // step 5
        document.close();
    }
}
