Package com.lowagie.examples.directcontent.optionalcontent

Source Code of com.lowagie.examples.directcontent.optionalcontent.OptionalContent

/*
* $Id$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
package com.lowagie.examples.directcontent.optionalcontent;

import java.awt.Color;
import java.io.FileOutputStream;
import java.util.ArrayList;

import com.lowagie.text.Chunk;
import com.lowagie.text.LwgDocument;
import com.lowagie.text.LwgElement;
import com.lowagie.text.LwgFont;
import com.lowagie.text.LwgImage;
import com.lowagie.text.LwgPageSize;
import com.lowagie.text.LwgPhrase;
import com.lowagie.text.LwgRectangle;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfBorderDictionary;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfLayer;
import com.lowagie.text.pdf.PdfLayerMembership;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.TextField;

/**
* Demonstrates the use of layers.
*/
public class OptionalContent {
  /**
   * Demonstrates the use of layers.
   * @param args no arguments needed
   */
  public static void main(String[] args) {
        try {
            System.out.println("Optional content");       
            // step 1: creation of a document-object
            LwgDocument document = new LwgDocument(LwgPageSize.A4, 50, 50, 50, 50);
            // step 2: creation of the writer
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("optionalcontent.pdf"));
            writer.setPdfVersion(PdfWriter.VERSION_1_5);
            writer.setViewerPreferences(PdfWriter.PageModeUseOC);
            // step 3: opening the document
            document.open();
            // step 4: content
            PdfContentByte cb = writer.getDirectContent();
            LwgPhrase explanation = new LwgPhrase("Automatic layers, form fields, images, templates and actions", new LwgFont(LwgFont.HELVETICA, 18, LwgFont.BOLD, Color.red));
            ColumnText.showTextAligned(cb, LwgElement.ALIGN_LEFT, explanation, 50, 650, 0);
            PdfLayer l1 = new PdfLayer("Layer 1", writer);
            PdfLayer l2 = new PdfLayer("Layer 2", writer);
            PdfLayer l3 = new PdfLayer("Layer 3", writer);
            PdfLayer l4 = new PdfLayer("Form and XObject Layer", writer);
            PdfLayerMembership m1 = new PdfLayerMembership(writer);
            m1.addMember(l2);
            m1.addMember(l3);
            LwgPhrase p1 = new LwgPhrase("Text in layer 1");
            LwgPhrase p2 = new LwgPhrase("Text in layer 2 or layer 3");
            LwgPhrase p3 = new LwgPhrase("Text in layer 3");
            cb.beginLayer(l1);
            ColumnText.showTextAligned(cb, LwgElement.ALIGN_LEFT, p1, 50, 600, 0f);
            cb.endLayer();
            cb.beginLayer(m1);
            ColumnText.showTextAligned(cb, LwgElement.ALIGN_LEFT, p2, 50, 550, 0);
            cb.endLayer();
            cb.beginLayer(l3);
            ColumnText.showTextAligned(cb, LwgElement.ALIGN_LEFT, p3, 50, 500, 0);
            cb.endLayer();
            TextField ff = new TextField(writer, new LwgRectangle(200, 600, 300, 620), "field1");
            ff.setBorderColor(Color.blue);
            ff.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
            ff.setBorderWidth(TextField.BORDER_WIDTH_THIN);
            ff.setText("I'm a form field");
            PdfFormField form = ff.getTextField();
            form.setLayer(l4);
            writer.addAnnotation(form);
            LwgImage img = LwgImage.getInstance("pngnow.png");
            img.setLayer(l4);
            img.setAbsolutePosition(200, 550);
            cb.addImage(img);
            PdfTemplate tp = cb.createTemplate(100, 20);
            LwgPhrase pt = new LwgPhrase("I'm a template", new LwgFont(LwgFont.HELVETICA, 12, LwgFont.NORMAL, Color.magenta));
            ColumnText.showTextAligned(tp, LwgElement.ALIGN_LEFT, pt, 0, 0, 0);
            tp.setLayer(l4);
            tp.setBoundingBox(new LwgRectangle(0, -10, 100, 20));
            cb.addTemplate(tp, 200, 500);
            ArrayList state = new ArrayList();
            state.add("toggle");
            state.add(l1);
            state.add(l2);
            state.add(l3);
            state.add(l4);
            PdfAction action = PdfAction.setOCGstate(state, true);
            Chunk ck = new Chunk("Click here to toggle the layers", new LwgFont(LwgFont.HELVETICA, 18, LwgFont.NORMAL, Color.yellow)).setBackground(Color.blue).setAction(action);
            ColumnText.showTextAligned(cb, LwgElement.ALIGN_CENTER, new LwgPhrase(ck), 250, 400, 0);
            cb.sanityCheck();
           
            // step 5: closing the document
            document.close();
        }
        catch(Exception de) {
            de.printStackTrace();
        }
    }
}
TOP

Related Classes of com.lowagie.examples.directcontent.optionalcontent.OptionalContent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.