Package com.lowagie.examples.objects.bookmarks

Source Code of com.lowagie.examples.objects.bookmarks.Layers

/*
* $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.objects.bookmarks;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.LwgDocument;
import com.lowagie.text.DocumentException;
import com.lowagie.text.LwgElement;
import com.lowagie.text.LwgFont;
import com.lowagie.text.LwgPhrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfLayer;
import com.lowagie.text.pdf.PdfLayerMembership;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfOCProperties;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PdfString;
import com.lowagie.text.pdf.PdfWriter;

/**
* Demonstrates how layers work.
*
* @author blowagie
*/

public class Layers {

  /**
   * Demonstrates some Layer functionality.
   *
   * @param args
   *            no arguments needed here
   */
  public static void main(String[] args) {

    System.out.println("layers");

    // step 1: creation of a document-object
    LwgDocument document = new LwgDocument();
    try {
      // step 2:
      PdfWriter writer = PdfWriter.getInstance(document,
          new FileOutputStream("Layers.pdf"));
      writer.setPdfVersion(PdfWriter.VERSION_1_5);
      // step 3:
      writer.setViewerPreferences(PdfWriter.PageModeUseOC);
      document.open();
      // step 4:
      PdfContentByte cb = writer.getDirectContent();
      LwgPhrase explanation = new LwgPhrase("Layer grouping", new LwgFont(
          LwgFont.HELVETICA, 20, 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);
      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, 0);
      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();
      PdfOCProperties p = writer.getOCProperties();
      PdfArray order = new PdfArray();
      order.add(l1.getRef());
      PdfArray group = new PdfArray();
      group.add(new PdfString("A group of two", PdfObject.TEXT_UNICODE));
      group.add(l2.getRef());
      group.add(l3.getRef());
      order.add(group);
      PdfDictionary d = new PdfDictionary();
      d.put(PdfName.ORDER, order);
      p.put(PdfName.D, d);
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }

    // step 5: we close the document
    document.close();
  }
}
TOP

Related Classes of com.lowagie.examples.objects.bookmarks.Layers

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.