Java iText – Add Anchor / Link to PDF

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

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class AnchorDemo {

	public static void main(String[] args) {
		Document document = new Document();
		try {
			PdfWriter.getInstance(document,
					new FileOutputStream("AnchorDemo.pdf"));
			document.open();

			String content = "You can learn Java programming on the " + "following website: ";
			Paragraph paragraph = new Paragraph(content);

			//
			// Creates a new anchor that link to external website
			// and add this anchor to the paragraph.
			//
			Anchor anchor = new Anchor("javac - Java Examples Source Code");
			anchor.setReference("http://javac.in");
			paragraph.add(anchor);

			document.add(paragraph);
		} catch (DocumentException | FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			document.close();
		}
	}

}

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

Page 1 of 212