Java iText – Add Image to PDF

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class ImageExample {
  public static void main(String[] args) {
    Document document = new Document();

    try {
        PdfWriter.getInstance(document, new FileOutputStream("Image.pdf"));
        document.open();

        Image image1 = Image.getInstance("watermark.png");
        document.add(image1);

        String imageUrl = "http://javac.in/wp-content/themes/Whitey/images/javac.jpg";

        Image image2 = Image.getInstance(new URL(imageUrl));
        document.add(image2);

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

Java iText – Add Table to PDF

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class TableExample {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                new FileOutputStream("HelloWorld-Table.pdf"));

            document.open();

            PdfPTable table = new PdfPTable(3); // 3 columns.

            PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
            PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
            PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));

            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);

            document.add(table);

            document.close();
        } catch(Exception e){

        }
    }
}

Table Width

table.setWidthPercentage(100);

Spacing Before and After Table

table.setSpacingBefore(10f);
table.setSpacingAfter(10f);

Column Span

cell.setColspan(2);

Cell Alignment

cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

Cell Padding

cell.setPadding(5);

cell.setPaddingLeft(8);
cell.setPaddingRight(8);
cell.setPaddingTop(8);
cell.setPaddingBottom(8);

Java iText – Create PDF

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class HelloWorld {

/** Path to the resulting PDF file. */
public static final String RESULT = "hello.pdf";

    public static void main(String[] args)
    	throws DocumentException, IOException {
    	new HelloWorld().createPdf(RESULT);
    }

    public void createPdf(String filename)
	throws DocumentException, IOException {

        Document document = new Document();

        PdfWriter.getInstance(document, new FileOutputStream(filename));

        document.open();

        document.add(new Paragraph("Hello World!"));

        document.close();
    }
}

Page 1 of 3123