PDF Java Tutorial

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfLayer;
import com.lowagie.text.pdf.PdfLayerMembership;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfLayer layer1 = new PdfLayer("layer 1", writer);
    PdfLayer layer2 = new PdfLayer("layer 2", writer);
    PdfLayer layer3 = new PdfLayer("layer 3", writer);
    PdfLayerMembership cat = new PdfLayerMembership(writer);
    cat.addMember(layer2);
    cat.addMember(layer3);
    PdfLayerMembership no_cat = new PdfLayerMembership(writer);
    no_cat.addMember(layer2);
    no_cat.addMember(layer3);
    no_cat.setVisibilityPolicy(PdfLayerMembership.ALLOFF);
    cb.beginLayer(layer1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("A"), 50, 775, 0);
    cb.endLayer();
    cb.beginLayer(layer2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("B"), 50, 750, 0);
    cb.endLayer();
    cb.beginLayer(layer3);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("C"), 50, 725, 0);
    cb.endLayer();
    cb.beginLayer(cat);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("D"), 50, 700, 0);
    cb.endLayer();
    cb.beginLayer(no_cat);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("E"), 50, 700, 0);
    cb.endLayer();
    document.close();
  }
}