Package com.lowagie.examples.objects.tables.pdfptable

Source Code of com.lowagie.examples.objects.tables.pdfptable.FloatingBoxes

/*
* $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.tables.pdfptable;

import java.io.IOException;

import com.lowagie.text.LwgDocument;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.LwgRectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.LwgPdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.LwgPdfPTable;
import com.lowagie.text.pdf.PdfPTableEvent;
import com.lowagie.text.pdf.PdfWriter;
import org.geoforge.demo.GfrFileOutputStream;

/**
* General example using TableEvents and CellEvents.
*/
public class FloatingBoxes implements PdfPCellEvent, PdfPTableEvent {

  /**
   * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable,
   *      float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[])
   */
  public void tableLayout(LwgPdfPTable table, float[][] width, float[] height,
      int headerRows, int rowStart, PdfContentByte[] canvases) {
    float widths[] = width[0];
    float x1 = widths[0];
    float x2 = widths[widths.length - 1];
    float y1 = height[0];
    float y2 = height[height.length - 1];
    PdfContentByte canvas = canvases[LwgPdfPTable.LINECANVAS];
    canvas.setRGBColorStroke(0x00, 0x00, 0xFF);
    canvas.rectangle(x1, y1, x2 - x1, y2 - y1);
    canvas.stroke();
    canvas.resetRGBColorStroke();
  }

  /**
   * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell,
   *      com.lowagie.text.LwgRectangle, com.lowagie.text.pdf.PdfContentByte[])
   */
  public void cellLayout(LwgPdfPCell cell, LwgRectangle position,
      PdfContentByte[] canvases) {
    float x1 = position.getLeft() + 2;
    float x2 = position.getRight() - 2;
    float y1 = position.getTop() - 2;
    float y2 = position.getBottom() + 2;
    PdfContentByte canvas = canvases[LwgPdfPTable.LINECANVAS];
    canvas.setRGBColorStroke(0xFF, 0x00, 0x00);
    canvas.rectangle(x1, y1, x2 - x1, y2 - y1);
    canvas.stroke();
    canvas.resetRGBColorStroke();
  }

  /**
   * Example originally written by Wendy Smoak to generate a Table with
   * 'floating boxes'. Adapted by Bruno Lowagie.
   *
   * @param args
   */
  public static void main(String[] args) {
    FloatingBoxes floatingBoxes = new FloatingBoxes();
    // step 1
    LwgDocument document = new LwgDocument();
    try {
      // step 2
      PdfWriter.getInstance(document,
                 new GfrFileOutputStream("com.lowagie.examples.objects.tables.pdfptable.FloatingBoxes.pdf"));
      // step 3
      document.open();
      // step 4
      LwgPdfPTable table = new LwgPdfPTable(2);
      table.setTableEvent(floatingBoxes);
      table.getDefaultCell().setBorder(LwgRectangle.NO_BORDER);
      table.getDefaultCell().setCellEvent(floatingBoxes);
      table.getDefaultCell().setPadding(5f);
      table.add("value");
      table.add("name");
      table.add(new Paragraph("dog"));
      table.add(new Paragraph("cat"));
      table.add(new Paragraph("bird"));
      table.add(new Paragraph("horse"));
      document.add(table);

    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
  }
}
TOP

Related Classes of com.lowagie.examples.objects.tables.pdfptable.FloatingBoxes

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.