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

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

/*
* $Id$
*
* This code is free software. It may only be copied or modified
* if you include the following copyright notice:
*
* --> Copyright 2001-2005 by G. Martinelli and Bruno Lowagie <--
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://www.lowagie.com/iText/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.LwgFont;
import com.lowagie.text.FontFactory;
import com.lowagie.text.LwgPageSize;
import com.lowagie.text.LwgPhrase;
import com.lowagie.text.pdf.LwgPdfPCell;
import com.lowagie.text.pdf.LwgPdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import org.geoforge.demo.GfrFileOutputStream;

/**
* Adds a table to a page twice.
*/
public class Tables {

  /**
   * Adds a table to a page twice.
   *
   * @param args
   *            no arguments needed
   */
  public static void main(String[] args) {

    LwgFont font8 = FontFactory.getFont(FontFactory.HELVETICA, 8);
   
    // step 1
    LwgDocument document = new LwgDocument(LwgPageSize.A4);

    try {
      // step 2
      PdfWriter writer = PdfWriter.getInstance(document,
          new GfrFileOutputStream("com.lowagie.examples.objects.tables.pdfptable.Tables.pdf"));
      float width = document.getPageSize().getWidth();
      float height = document.getPageSize().getHeight();
      // step 3
      document.open();

      // step 4
      float[] columnDefinitionSize = { 33.33F, 33.33F, 33.33F };

      float pos = height / 2;
      LwgPdfPTable table = null;
      LwgPdfPCell cell = null;

      table = new LwgPdfPTable(columnDefinitionSize);
      table.getDefaultCell().setBorder(0);
      table.setHorizontalAlignment(0);
      table.setTotalWidth(width - 72);
      table.setLockedWidth(true);

      cell = new LwgPdfPCell(new LwgPhrase("Table added with document.add()"));
      cell.setColspan(columnDefinitionSize.length);
      table.add(cell);
      table.add(new LwgPhrase("Louis Pasteur", font8));
      table.add(new LwgPhrase("Albert Einstein", font8));
      table.add(new LwgPhrase("Isaac Newton", font8));
      table.add(new LwgPhrase("8, Rabic street", font8));
      table.add(new LwgPhrase("2 Photons Avenue", font8));
      table.add(new LwgPhrase("32 Gravitation Court", font8));
      table.add(new LwgPhrase("39100 Dole France", font8));
      table.add(new LwgPhrase("12345 Ulm Germany", font8));
      table.add(new LwgPhrase("45789 Cambridge  England", font8));
     
      document.add(table);
     
      table = new LwgPdfPTable(columnDefinitionSize);
      table.getDefaultCell().setBorder(0);
      table.setHorizontalAlignment(0);
      table.setTotalWidth(width - 72);
      table.setLockedWidth(true);

      cell = new LwgPdfPCell(new LwgPhrase("Table added with writeSelectedRows"));
      cell.setColspan(columnDefinitionSize.length);
      table.add(cell);
      table.add(new LwgPhrase("Louis Pasteur", font8));
      table.add(new LwgPhrase("Albert Einstein", font8));
      table.add(new LwgPhrase("Isaac Newton", font8));
      table.add(new LwgPhrase("8, Rabic street", font8));
      table.add(new LwgPhrase("2 Photons Avenue", font8));
      table.add(new LwgPhrase("32 Gravitation Court", font8));
      table.add(new LwgPhrase("39100 Dole France", font8));
      table.add(new LwgPhrase("12345 Ulm Germany", font8));
      table.add(new LwgPhrase("45789 Cambridge  England", font8));
     
      table.writeSelectedRows(0, -1, 50, pos, writer.getDirectContent());
    }

    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.Tables

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.