Example usage for com.lowagie.text Element ALIGN_LEFT

List of usage examples for com.lowagie.text Element ALIGN_LEFT

Introduction

In this page you can find the example usage for com.lowagie.text Element ALIGN_LEFT.

Prototype

int ALIGN_LEFT

To view the source code for com.lowagie.text Element ALIGN_LEFT.

Click Source Link

Document

A possible value for paragraph alignment.

Usage

From source file:permit.PermitPdf.java

License:Open Source License

/**
 * handles the letter header//from  w w w .j a  va 2s. c  o m
 */
PdfPTable getHeader() {
    //
    String str = "";
    String spacer = "   ";
    //
    PdfPTable headTable = null;
    try {
        //
        // for http url use
        //
        Image image = Image.getInstance(url + "js/images/city_logo3.jpg");

        Font fnt = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);
        Font fntb = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
        float[] widths = { 25f, 40f, 35f }; // percentages
        headTable = new PdfPTable(widths);
        headTable.setWidthPercentage(100);
        headTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        headTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        // image.setWidthPercentage(33.0f);
        image.scalePercent(15f);
        PdfPCell cell = new PdfPCell(image);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        headTable.addCell(cell);
        //
        float[] width = { 33f };
        PdfPTable midTable = new PdfPTable(width);
        midTable.setWidthPercentage(33);
        midTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        midTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        Phrase phrase = new Phrase();
        Chunk ch = new Chunk("City of Bloomington ", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        midTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("Planning and Transportation Department ", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        midTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("bloomington.in.gov", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        midTable.addCell(cell);
        //
        headTable.addCell(midTable);
        //
        PdfPTable rightTable = new PdfPTable(width);
        rightTable.setWidthPercentage(33);
        rightTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        rightTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        phrase = new Phrase();
        ch = new Chunk("401 N Morton St Suite 130 ", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("PO Box 100 \nBloomington, IN 47404", fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        //
        phrase = new Phrase();
        ch = new Chunk("\n Phone: (812) 349-3423\nFax (812) 349-3520\nEmail: planning@bloomington.in.gov",
                fntb);
        phrase.add(ch);
        cell = new PdfPCell(phrase);
        cell.setBorder(Rectangle.NO_BORDER);
        rightTable.addCell(cell);
        headTable.addCell(rightTable);
    } catch (Exception ex) {
        logger.error(ex);
    }
    return headTable;
}

From source file:permit.PermitPdf.java

License:Open Source License

void writePage(HttpServletResponse res, Permit permit) {
    ////from  w  w w  . ja va2  s  .  c o m
    // paper size legal (A4) 8.5 x 11
    // page 1-inch = 72 points
    //
    String fileName = "row_permit_" + permit.getId() + ".pdf";
    Rectangle pageSize = new Rectangle(612, 792); // 8.5" X 11"
    Document document = new Document(pageSize, 36, 36, 18, 18);
    ServletOutputStream out = null;
    Font fnt = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);
    Font fntb = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
    Font fnts = new Font(Font.TIMES_ROMAN, 8, Font.NORMAL);
    Font fntbs = new Font(Font.TIMES_ROMAN, 8, Font.BOLD);
    String spacer = "   ";
    PdfPTable header = getHeader();
    Bond bond = permit.getBond();
    Invoice invoice = permit.getInvoice();
    if (bond == null)
        bond = new Bond();
    if (invoice == null)
        invoice = new Invoice();
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        String str = "";
        document.open();
        document.add(header);
        //
        // title
        float[] width = { 100f }; // one cell
        PdfPTable table = new PdfPTable(width);
        table.setWidthPercentage(100.0f);
        PdfPCell cell = new PdfPCell(new Phrase("Right Of Way Excavation Permit", fntb));
        //         
        cell.setBorder(Rectangle.BOTTOM);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        document.add(table);
        //
        // we need these later
        Paragraph pp = new Paragraph();
        Chunk ch = new Chunk(" ", fntb);
        Phrase phrase = new Phrase();
        //
        float[] widths = { 14f, 20f, 17f, 18f, 13f, 20f }; // percentages
        table = new PdfPTable(widths);
        table.setWidthPercentage(100.0f);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        //
        // first row
        cell = new PdfPCell(new Phrase("Company", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getCompany().getName(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Status", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getStatus(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Permit", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getPermit_num(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        //
        // 2nd row
        //
        cell = new PdfPCell(new Phrase("Responsible", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getContact().getFullName(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Inspector", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getReviewer().getFullName(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Date Issued", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getDate(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        //
        // 3rd row
        cell = new PdfPCell(new Phrase("Project", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getProject(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Permit Fee", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("$" + permit.getFee(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Start Date", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(permit.getStart_date(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        //
        // 4th row
        cell = new PdfPCell(new Phrase("Bond Amount", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("$" + bond.getAmount(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Expiration Date", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(bond.getExpire_date(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Invoice", fntb));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(invoice.getStatus(), fnt));
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        document.add(table);
        //
        phrase = new Phrase(new Chunk(spacer, fnt));
        document.add(phrase);
        //
        float[] widths2 = { 25f, 15f, 15f, 25f, 10f, 10f };
        table = new PdfPTable(widths2);
        table.setWidthPercentage(100.0f);
        cell = new PdfPCell(new Phrase("Address", fntb));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Cut Type", fntb));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Utility", fntb));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Description", fntb));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Width", fntb));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Length", fntb));
        table.addCell(cell);
        List<Excavation> list = permit.getExcavations();
        if (list != null && list.size() > 0) {
            for (Excavation one : list) {
                cell = new PdfPCell(new Phrase(one.getAddress().getAddress(), fnt));
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(one.getCut_type(), fnt));
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(one.getUtility_type().getName(), fnt));
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(one.getCut_description(), fnt));
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(one.getWidth(), fnt));
                table.addCell(cell);
                cell = new PdfPCell(new Phrase(one.getLength(), fnt));
                table.addCell(cell);
            }
        }
        document.add(table);
        //
        pp = new Paragraph();
        pp.setIndentationLeft(12);
        pp.setAlignment(Element.ALIGN_LEFT);
        pp.setLeading(0f, 1f);
        ch = new Chunk("\nSpecial Provisions\n", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        ch = new Chunk(permit.getNotes() + "\n", fnt);
        phrase.add(ch);
        pp.add(phrase);
        document.add(pp);
        //
        pp = new Paragraph();
        pp.setIndentationLeft(12);
        pp.setAlignment(Element.ALIGN_LEFT);
        ch = new Chunk("Standards Conditions of Approval\n", fntb);
        phrase = new Phrase();
        phrase.add(ch);
        pp.add(phrase);
        document.add(pp);
        //
        pp = new Paragraph();
        pp.setIndentationLeft(12);
        pp.setAlignment(Element.ALIGN_LEFT);
        pp.setLeading(0f, 1f);
        ch = new Chunk("1 - " + conditions[0] + "\n", fntbs);
        phrase = new Phrase();
        phrase.add(ch);
        pp.add(phrase);
        document.add(pp);
        //
        int jj = 1;
        for (int j = 1; j < conditions.length; j++) {
            jj = j + 1;
            pp = new Paragraph();
            pp.setLeading(0f, 1f);
            pp.setIndentationLeft(12);
            pp.setAlignment(Element.ALIGN_LEFT);
            ch = new Chunk(jj + " - " + conditions[j] + "\n", fnts);
            phrase = new Phrase();
            phrase.add(ch);
            pp.add(phrase);
            document.add(pp);
        }

        pp = new Paragraph();
        pp.setIndentationLeft(20);
        pp.setAlignment(Element.ALIGN_RIGHT);
        ch = new Chunk("\n\n___________________\n", fnt);
        phrase = new Phrase();
        phrase.add(ch);
        pp.add(phrase);
        ch = new Chunk(permit.getReviewer().getFullName(), fnt);
        phrase = new Phrase();
        phrase.add(ch);
        pp.add(phrase);
        document.add(pp);
        document.close();
        writer.close();
        res.setHeader("Expires", "0");
        res.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        res.setHeader("Pragma", "public");
        //
        // setting the content type
        res.setContentType("application/pdf");
        //
        // the contentlength is needed for MSIE!!!
        res.setContentLength(baos.size());
        //
        out = res.getOutputStream();
        if (out != null) {
            baos.writeTo(out);
        }
    } catch (Exception ex) {
        logger.error(ex);
    }

}

From source file:pl.exsio.ca.app.report.terraincard.view.TerrainCardsView.java

License:Open Source License

private Element getCreationDate(Date date) throws Exception {
    Paragraph p = new Paragraph(t("creation_date") + new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date),
            this.getFont());
    p.setAlignment(Element.ALIGN_LEFT);
    p.getFont().setSize(7);/*from  w w  w . j  av  a  2  s. c  o  m*/
    return p;
}

From source file:questions.directcontent.InterpretOCR.java

public static void main(String[] args) throws IOException, DocumentException {
    Document document = new Document(PageSize.LETTER);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    document.open();// www . ja va  2s  . c o  m
    PdfContentByte cb = writer.getDirectContent();
    BaseFont font = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    cb.beginText();
    cb.setFontAndSize(font, 12);
    String line;
    String word;
    @SuppressWarnings("unused")
    float llx, lly, urx, ury;
    StringTokenizer tokenizer;
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RESOURCE)));
    while ((line = reader.readLine()) != null) {
        tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {
            tokenizer.nextToken();
            word = tokenizer.nextToken();
            llx = Float.parseFloat(tokenizer.nextToken() + "f") / 10;
            lly = document.top() - Float.parseFloat(tokenizer.nextToken() + "f") / 10;
            urx = Float.parseFloat(tokenizer.nextToken() + "f") / 10;
            ury = document.top() - Float.parseFloat(tokenizer.nextToken() + "f") / 10;
            cb.showTextAligned(Element.ALIGN_LEFT, word, (llx + urx) / 2, lly, 0);
        }
    }
    cb.endText();
    document.close();
}

From source file:questions.forms.RadioButtonsOnDifferentPages.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*  w w  w.j a va  2  s. c o m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();

        PdfContentByte cb = writer.getDirectContent();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        String[] languages = { "English", "French", "Dutch" };
        Rectangle rect;

        // create radio button field and its kids
        PdfFormField language = PdfFormField.createRadioButton(writer, true);
        language.setFieldName("language");
        language.setValueAsName(languages[0]);
        for (int i = 0; i < languages.length; i++) {
            rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40);
            addRadioButton(writer, rect, language, languages[i], i == 0, writer.getPageNumber() + i);
        }
        writer.addAnnotation(language);

        // add the page content
        for (int i = 0; i < languages.length; i++) {
            cb.beginText();
            cb.setFontAndSize(bf, 18);
            cb.showTextAligned(Element.ALIGN_LEFT, languages[i], 70, 790 - i * 40, 0);
            cb.endText();
            document.newPage();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // step 5: we close the document
    document.close();

}

From source file:questions.importpages.ConcatenateWithTOC.java

public static PdfImportedPage getStampedPage(PdfReader reader, PdfCopy copy, int oldP, int newP, BaseFont bf)
        throws IOException {
    PdfImportedPage page = copy.getImportedPage(reader, oldP);
    PageStamp pagestamp = copy.createPageStamp(page);
    PdfContentByte canvas = pagestamp.getOverContent();
    canvas.beginText();// w w w. j  ava2  s.  c o  m
    canvas.setFontAndSize(bf, 12);
    canvas.showTextAligned(Element.ALIGN_LEFT, "page " + newP, 36, 26, 0);
    canvas.endText();
    pagestamp.alterContents();
    return page;
}

From source file:questions.importpages.ConcatenateWithTOC2.java

public static void main(String[] args) {
    try {//from  ww w .ja  v a 2s  .  co  m
        // suppose we have some TEST PDF files
        for (int i = 0; i < TEST.length; i++) {
            createTestPdf(i);
        }
        // and we want to concatenate them
        Document document = new Document();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        PdfCopy copy = new PdfCopy(document, os);
        copy.setViewerPreferences(PdfWriter.PageModeUseOutlines);
        document.open();
        // but we want to create an outline tree
        PdfOutline root = copy.getRootOutline();
        // we also want an extra page with the file name
        Document coverpage;
        ByteArrayOutputStream baos;
        PdfReader reader;
        // we want keep track of the page numbers
        int pagenumber = 0;
        for (int i = 0; i < TEST.length; i++) {
            // we create the coverpage in memory
            coverpage = new Document();
            baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(coverpage, baos);
            coverpage.open();
            coverpage.add(new Paragraph("file: " + TEST[i]));
            coverpage.close();
            // we import that page
            reader = new PdfReader(baos.toByteArray());
            pagenumber++;
            copy.addPage(copy.getImportedPage(reader, 1));
            // we create the bookmark to that page
            PdfDestination dest = new PdfDestination(PdfDestination.FIT);
            new PdfOutline(root, PdfAction.gotoLocalPage(pagenumber, dest, copy), TEST[i]);
            // we import the document itself
            reader = new PdfReader(TEST[i]);
            for (int j = 1; j <= reader.getNumberOfPages(); j++) {
                pagenumber++;
                copy.addPage(copy.getImportedPage(reader, j));
            }
        }
        document.close();

        // we want to add page X of Y
        reader = new PdfReader(os.toByteArray());
        int n = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        BaseFont bf = BaseFont.createFont();
        for (int i = 1; i <= n; i++) {
            PdfContentByte canvas = stamper.getOverContent(i);
            canvas.beginText();
            canvas.setFontAndSize(bf, 12);
            canvas.showTextAligned(Element.ALIGN_LEFT, "page " + i + " of " + n, 36, 26, 0);
            canvas.endText();
        }
        stamper.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (DocumentException de) {
        de.printStackTrace();
    }
}

From source file:questions.javascript.AddJavaScriptToForm.java

public static void createPdf(String filename) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    document.open();/* www  . j  av a 2s.  c  om*/

    BaseFont bf = BaseFont.createFont();
    PdfContentByte directcontent = writer.getDirectContent();
    directcontent.beginText();
    directcontent.setFontAndSize(bf, 12);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "Married?", 36, 770, 0);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "YES", 58, 750, 0);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "NO", 102, 750, 0);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "Name partner?", 36, 730, 0);
    directcontent.endText();

    PdfFormField married = PdfFormField.createRadioButton(writer, true);
    married.setFieldName("married");
    married.setValueAsName("yes");
    Rectangle rectYes = new Rectangle(40, 766, 56, 744);
    RadioCheckField yes = new RadioCheckField(writer, rectYes, null, "yes");
    yes.setChecked(true);
    married.addKid(yes.getRadioField());
    Rectangle rectNo = new Rectangle(84, 766, 100, 744);
    RadioCheckField no = new RadioCheckField(writer, rectNo, null, "no");
    no.setChecked(false);
    married.addKid(no.getRadioField());
    writer.addAnnotation(married);

    Rectangle rect = new Rectangle(40, 710, 200, 726);
    TextField partner = new TextField(writer, rect, "partner");
    partner.setBorderColor(Color.BLACK);
    partner.setBorderWidth(0.5f);
    writer.addAnnotation(partner.getTextField());

    document.close();
}

From source file:questions.ocg.AddOptionalContentToExistingPdf.java

public static void main(String[] args) throws IOException, DocumentException {
    // creating an empty document
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT01));
    writer.setPdfVersion(PdfWriter.VERSION_1_5);
    document.open();//from   w w w. j a  v  a2 s.c  o m
    writer.setPageEmpty(false);
    document.close();

    PdfReader reader = new PdfReader(RESULT01);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT02));
    PdfContentByte cb = stamper.getOverContent(1);
    writer = stamper.getWriter();
    PdfLayer nested = new PdfLayer("Nested Layers", writer);
    PdfLayer nested_1 = new PdfLayer("Nested Layer 1", writer);
    PdfLayer nested_2 = new PdfLayer("Nested Layer 2", writer);
    PdfLayer layer21 = new PdfLayer("Layer 2", writer);
    PdfLayer layer22 = new PdfLayer("Layer 2", writer);
    nested_2.addChild(layer21);
    nested_2.addChild(layer22);
    nested.addChild(nested_1);
    nested.addChild(nested_2);
    cb.beginLayer(nested);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layers"), 50, 775, 0);
    cb.endLayer();
    cb.beginLayer(nested_1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 1"), 100, 800, 0);
    cb.endLayer();
    cb.beginLayer(nested_2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 2"), 100, 750, 0);
    cb.endLayer();
    cb.beginLayer(layer21);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2.1 in the group"), 150, 775, 0);
    cb.endLayer();
    cb.beginLayer(layer22);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2.2 in the group"), 150, 725, 0);
    cb.endLayer();
    PdfLayer group = PdfLayer.createTitle("Grouped layers", writer);
    PdfLayer layer1 = new PdfLayer("Group: layer 1", writer);
    PdfLayer layer2 = new PdfLayer("Group: layer 2", writer);
    group.addChild(layer1);
    group.addChild(layer2);
    cb.beginLayer(layer1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 1 in the group"), 50, 700, 0);
    cb.endLayer();
    cb.beginLayer(layer2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2 in the group"), 50, 675, 0);
    cb.endLayer();
    PdfLayer radiogroup = PdfLayer.createTitle("Radio Group", writer);
    PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", writer);
    radio1.setOn(true);
    PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", writer);
    radio2.setOn(false);
    PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", writer);
    radio3.setOn(false);
    radiogroup.addChild(radio1);
    radiogroup.addChild(radio2);
    radiogroup.addChild(radio3);
    ArrayList<PdfLayer> options = new ArrayList<PdfLayer>();
    options.add(radio1);
    options.add(radio2);
    options.add(radio3);
    writer.addOCGRadioGroup(options);
    cb.beginLayer(radio1);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 1"), 50, 600, 0);
    cb.endLayer();
    cb.beginLayer(radio2);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 2"), 50, 575, 0);
    cb.endLayer();
    cb.beginLayer(radio3);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 3"), 50, 550, 0);
    cb.endLayer();
    PdfLayer not_printed = new PdfLayer("not printed", writer);
    not_printed.setOnPanel(false);
    not_printed.setPrint("Print", false);
    cb.beginLayer(not_printed);
    ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("PRINT THIS PAGE"), 300, 700, 90);
    cb.endLayer();
    PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", writer);
    zoom.setOnPanel(false);
    zoom.setZoom(0.75f, 1.25f);
    cb.beginLayer(zoom);
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,
            new Phrase("Only visible if the zoomfactor is between 75 and 125%"), 30, 530, 90);
    cb.endLayer();
    stamper.close();

    reader = new PdfReader(RESULT02);
    stamper = new PdfStamper(reader, new FileOutputStream(RESULT03));
    Map ocg = stamper.getPdfLayers();
    for (Iterator i = ocg.keySet().iterator(); i.hasNext();) {
        System.out.println(i.next());
    }
    PdfLayer layer = (PdfLayer) ocg.get("Layer 2");
    layer.setOn(false);
    cb = stamper.getOverContent(1);
    cb.beginLayer(layer);
    cb.moveTo(0, 0);
    cb.lineTo(500, 500);
    cb.stroke();
    cb.endLayer();
    PdfLayer parent = (PdfLayer) ocg.get("Layer 2(2)");
    PdfLayer newLayer = new PdfLayer("Child layer", stamper.getWriter());
    newLayer.setOn(false);
    parent.addChild(newLayer);
    cb.beginLayer(newLayer);
    cb.moveTo(0, 500);
    cb.lineTo(500, 0);
    cb.stroke();
    cb.endLayer();
    stamper.close();
}

From source file:questions.ocg.LockedLayers.java

public static void main(String[] args) {
    Document document = new Document();
    try {//from   ww  w. java2s  .c  o  m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        writer.setViewerPreferences(PdfWriter.PageModeUseOC);
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        PdfLayer nested = new PdfLayer("Nested Layers", writer);
        PdfLayer nested_1 = new PdfLayer("Nested Layer 1", writer);
        PdfLayer nested_2 = new PdfLayer("Nested Layer 2", writer);
        nested.addChild(nested_1);
        nested.addChild(nested_2);
        cb.beginLayer(nested);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layers"), 50, 775, 0);
        cb.endLayer();
        cb.beginLayer(nested_1);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 1"), 100, 800, 0);
        cb.endLayer();
        cb.beginLayer(nested_2);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 2"), 100, 750, 0);
        cb.endLayer();

        PdfLayer group = PdfLayer.createTitle("Grouped layers", writer);
        PdfLayer layer1 = new PdfLayer("Group: layer 1", writer);
        PdfLayer layer2 = new PdfLayer("Group: layer 2", writer);
        group.addChild(layer1);
        group.addChild(layer2);
        cb.beginLayer(layer1);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 1 in the group"), 50, 700, 0);
        cb.endLayer();
        cb.beginLayer(layer2);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2 in the group"), 50, 675, 0);
        cb.endLayer();

        PdfLayer radiogroup = PdfLayer.createTitle("Radio Group", writer);
        PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", writer);
        radio1.setOn(true);
        PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", writer);
        radio2.setOn(false);
        PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", writer);
        radio3.setOn(false);
        radiogroup.addChild(radio1);
        radiogroup.addChild(radio2);
        radiogroup.addChild(radio3);
        ArrayList<PdfLayer> options = new ArrayList<PdfLayer>();
        options.add(radio1);
        options.add(radio2);
        options.add(radio3);
        writer.addOCGRadioGroup(options);
        cb.beginLayer(radio1);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 1"), 50, 600, 0);
        cb.endLayer();
        cb.beginLayer(radio2);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 2"), 50, 575, 0);
        cb.endLayer();
        cb.beginLayer(radio3);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 3"), 50, 550, 0);
        cb.endLayer();

        PdfLayer not_printed = new PdfLayer("not printed", writer);
        not_printed.setOnPanel(false);
        not_printed.setPrint("Print", false);
        cb.beginLayer(not_printed);
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("PRINT THIS PAGE"), 300, 700, 90);
        cb.endLayer();

        PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", writer);
        zoom.setOnPanel(false);
        zoom.setZoom(0.75f, 1.25f);
        cb.beginLayer(zoom);
        ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,
                new Phrase("Only visible if the zoomfactor is between 75 and 125%"), 30, 530, 90);
        cb.endLayer();

        writer.lockLayer(nested_1);
        writer.lockLayer(layer2);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    document.close();
}