⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 table.java

📁 iText可以制作中文PDF文件的JAVA源程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * $Id: Table.java,v 1.94 2002/07/10 07:22:39 blowagie Exp $ * $Name:  $ * * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above.  If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library 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. See the GNU LIBRARY GENERAL PUBLIC LICENSE for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ * * Some methods in this class were contributed by Geert Poels, Kris Jespers and * Steve Ogryzek. Check the CVS repository. */package com.lowagie.text;import java.awt.Color;import java.awt.Dimension;import java.awt.Point;import java.util.ArrayList;import java.util.Hashtable;import java.util.Iterator;import java.util.Properties;import java.util.StringTokenizer;import com.lowagie.text.markup.*;/** * A <CODE>Table</CODE> is a <CODE>Rectangle</CODE> that contains <CODE>Cell</CODE>s, * ordered in some kind of matrix. * <P> * Tables that span multiple pages are cut into different parts automatically. * If you want a table header to be repeated on every page, you may not forget to * mark the end of the header section by using the method <CODE>endHeaders()</CODE>. * <P> * The matrix of a table is not necessarily an m x n-matrix. It can contain holes * or cells that are bigger than the unit. Believe me or not, but it took some serious * thinking to make this as userfriendly as possible. I hope you wil find the result * quite simple (I love simple solutions, especially for complex problems). * I didn't want it to be something as complex as the Java <CODE>GridBagLayout</CODE>. * <P> * Example: * <BLOCKQUOTE><PRE> * // Remark: You MUST know the number of columns when constructing a Table. * //         The number of rows is not important. * <STRONG>Table table = new Table(3);</STRONG> * <STRONG>table.setBorderWidth(1);</STRONG> * <STRONG>table.setBorderColor(new Color(0, 0, 255));</STRONG> * <STRONG>table.setPadding(5);</STRONG> * <STRONG>table.setSpacing(5);</STRONG> * Cell cell = new Cell("header"); * cell.setHeader(true); * cell.setColspan(3); * <STRONG>table.addCell(cell);</STRONG> * <STRONG>table.endHeaders();</STRONG> * cell = new Cell("example cell with colspan 1 and rowspan 2"); * cell.setRowspan(2); * cell.setBorderColor(new Color(255, 0, 0)); * <STRONG>table.addCell(cell);</STRONG> * <STRONG>table.addCell("1.1");</STRONG> * <STRONG>table.addCell("2.1");</STRONG> * <STRONG>table.addCell("1.2");</STRONG> * <STRONG>table.addCell("2.2");</STRONG> * <STRONG>table.addCell("cell test1");</STRONG> * cell = new Cell("big cell"); * cell.setRowspan(2); * cell.setColspan(2); * <STRONG>table.addCell(cell);</STRONG> * <STRONG>table.addCell("cell test2");</STRONG> * </PRE></BLOCKQUOTE> * The result of this code is a table: *      <TABLE ALIGN="Center" BORDER="1" BORDERCOLOR="#0000ff" CELLPADDING="5" CELLSPACING="5"> *              <TR ALIGN="Left" VALIGN="Left"> *                      <TH ALIGN="Left" COLSPAN="3" VALIGN="Left"> *                              header *                      </TH> *              </TR> *              <TR ALIGN="Left" VALIGN="Left"> *                      <TD ALIGN="Left" BORDERCOLOR="#ff0000" ROWSPAN="2" VALIGN="Left"> *                              example cell with colspan 1 and rowspan 2 *                      </TD> *                      <TD ALIGN="Left" VALIGN="Left"> *                              1.1 *                      </TD> *                      <TD ALIGN="Left" VALIGN="Left"> *                              2.1 *                      </TD> *              </TR> *              <TR ALIGN="Left" VALIGN="Left"> *                      <TD ALIGN="Left" VALIGN="Left"> *                              1.2 *                      </TD> *                      <TD ALIGN="Left" VALIGN="Left"> *                              2.2 *                      </TD> *              </TR> *              <TR ALIGN="Left" VALIGN="Left"> *                      <TD ALIGN="Left" VALIGN="Left"> *                              cell test1 *                      </TD> *                      <TD ALIGN="Left" COLSPAN="2" ROWSPAN="2" VALIGN="Left"> *                              big cell *                      </TD> *              </TR> *              <TR ALIGN="Left" VALIGN="Left"> *                      <TD ALIGN="Left" VALIGN="Left"> *                              cell test2 *                      </TD> *              </TR> *      </TABLE> * * @see         Rectangle * @see         Element * @see         Row * @see         Cell */public class Table extends Rectangle implements Element, MarkupAttributes {    // membervariables    // these variables contain the data of the table/** This is the number of columns in the <CODE>Table</CODE>. */    private int columns;// this is the current Position in the table    private Point curPosition = new Point(0, 0);/** This is the list of <CODE>Row</CODE>s. */    private ArrayList rows = new ArrayList();    // these variables contain the layout of the table/** This Empty Cell contains the DEFAULT layout of each Cell added with the method addCell(String content). */    private Cell defaultLayout = new Cell(true);/** This is the number of the last row of the table headers. */    private int lastHeaderRow = -1;/** This is the horizontal alignment. */    private int alignment = Element.ALIGN_CENTER;/** This is cellpadding. */    private float cellpadding;/** This is cellspacing. */    private float cellspacing;/** This is the width of the table (in percent of the available space). */    private float widthPercentage = 80;    // member variable added by Evelyne De Cordier/** This is the width of the table (in pixels). */    private String absWidth = "";/** This is an array containing the widths (in percentages) of every column. */    private float[] widths;/** Boolean to track errors (some checks will be performed) */    boolean mDebug = false;/** Boolean to track if a table was inserted (to avoid unnecessary computations afterwards) */    boolean mTableInserted = false;/** * Boolean to automatically fill empty cells before a table is rendered *  (takes CPU so may be set to false in case of certainty) */    boolean mAutoFillEmptyCells = false;/** If true this table may not be split over two pages. */    boolean tableFitsPage = false;/** If true cells may not be split over two pages. */    boolean cellsFitPage = false;/** This is the offset of the table. */    float offset = Float.NaN;/** contains the attributes that are added to each odd (or even) row */    protected Hashtable alternatingRowAttributes = null;    // constructors/** * Constructs a <CODE>Table</CODE> with a certain number of columns. * * @param       columns         The number of columns in the table * @throws      BadElementException if the creator was called with less than 1 column */    public Table(int columns) throws BadElementException {        this(columns, 1);    }/** * Constructs a <CODE>Table</CODE> with a certain number of columns * and a certain number of <CODE>Row</CODE>s. * * @param       columns         The number of columns in the table * @param       rows            The number of rows * @throws      BadElementException if the creator was called with less than 1 column */    public Table(int columns, int rows) throws BadElementException {        // a Rectangle is create with BY DEFAULT a border with a width of 1        super(0, 0, 0, 0);        setBorder(BOX);        setBorderWidth(1);        defaultLayout.setBorder(BOX);        // a table should have at least 1 column        if (columns <= 0) {            throw new BadElementException("A table should have at least 1 column.");        }        this.columns = columns;        // a certain number of rows are created        for (int i = 0; i < rows; i++) {            this.rows.add(new Row(columns));        }        curPosition = new Point(0, 0);        // the DEFAULT widths are calculated        widths = new float[columns];        float width = 100f / columns;        for (int i = 0; i < columns; i++) {            widths[i] = width;        }    }/** * Returns a <CODE>Table</CODE> that has been constructed taking in account * the value of some <VAR>attributes</VAR>. * * @param    attributes        Some attributes */    public Table(Properties attributes) {        // a Rectangle is create with BY DEFAULT a border with a width of 1        super(0, 0, 0, 0);        setBorder(BOX);        setBorderWidth(1);        defaultLayout.setBorder(BOX);        String value = (String)attributes.remove(ElementTags.COLUMNS);        if (value == null) {            columns = 1;        }        else {            columns = Integer.parseInt(value);            if (columns <= 0) {                columns = 1;            }        }        rows.add(new Row(columns));        curPosition.setLocation(0, curPosition.y);        if ((value = (String)attributes.remove(ElementTags.LASTHEADERROW)) != null) {            setLastHeaderRow(Integer.parseInt(value));        }        if ((value = (String)attributes.remove(ElementTags.ALIGN)) != null) {            setAlignment(value);        }        if ((value = (String)attributes.remove(ElementTags.CELLSPACING)) != null) {            setSpacing(Float.valueOf(value + "f").floatValue());        }        if ((value = (String)attributes.remove(ElementTags.CELLPADDING)) != null) {            setPadding(Float.valueOf(value + "f").floatValue());        }        if ((value = (String)attributes.remove(ElementTags.OFFSET)) != null) {            setOffset(Float.valueOf(value + "f").floatValue());        }        if ((value = (String)attributes.remove(ElementTags.WIDTH)) != null) {            if (value.endsWith("%"))                setWidth(Float.valueOf(value.substring(0, value.length() - 1) + "f").floatValue());            else                setAbsWidth(value);        }        widths = new float[columns];        for (int i = 0; i < columns; i++) {            widths[i] = 0;        }        if ((value = (String)attributes.remove(ElementTags.WIDTHS)) != null) {            StringTokenizer widthTokens = new StringTokenizer(value, ";");            int i = 0;            while (widthTokens.hasMoreTokens()) {                value = (String) widthTokens.nextToken();                widths[i] = Float.valueOf(value + "f").floatValue();                i++;            }            columns = i;        }        if ((value = (String)attributes.remove(ElementTags.TABLEFITSPAGE)) != null) {            tableFitsPage = new Boolean(value).booleanValue();        }        if ((value = (String)attributes.remove(ElementTags.CELLSFITPAGE)) != null) {            cellsFitPage = new Boolean(value).booleanValue();        }        if ((value = (String)attributes.remove(ElementTags.BORDERWIDTH)) != null) {            setBorderWidth(Float.valueOf(value + "f").floatValue());        }        int border = 0;        if ((value = (String)attributes.remove(ElementTags.LEFT)) != null) {            if (new Boolean(value).booleanValue()) border |= Rectangle.LEFT;        }        if ((value = (String)attributes.remove(ElementTags.RIGHT)) != null) {            if (new Boolean(value).booleanValue()) border |= Rectangle.RIGHT;        }        if ((value = (String)attributes.remove(ElementTags.TOP)) != null) {            if (new Boolean(value).booleanValue()) border |= Rectangle.TOP;        }        if ((value = (String)attributes.remove(ElementTags.BOTTOM)) != null) {            if (new Boolean(value).booleanValue()) border |= Rectangle.BOTTOM;        }        setBorder(border);        String r = (String)attributes.remove(ElementTags.RED);        String g = (String)attributes.remove(ElementTags.GREEN);        String b = (String)attributes.remove(ElementTags.BLUE);        if (r != null || g != null || b != null) {            int red = 0;            int green = 0;            int blue = 0;            if (r != null) red = Integer.parseInt(r);            if (g != null) green = Integer.parseInt(g);            if (b != null) blue = Integer.parseInt(b);            setBorderColor(new Color(red, green, blue));        }        else if ((value = attributes.getProperty(ElementTags.BORDERCOLOR)) != null) {            setBorderColor(MarkupParser.decodeColor(value));        }        r = (String)attributes.remove(ElementTags.BGRED);        g = (String)attributes.remove(ElementTags.BGGREEN);        b = (String)attributes.remove(ElementTags.BGBLUE);        if (r != null || g != null || b != null) {            int red = 0;            int green = 0;            int blue = 0;            if (r != null) red = Integer.parseInt(r);            if (g != null) green = Integer.parseInt(g);            if (b != null) blue = Integer.parseInt(b);            setBackgroundColor(new Color(red, green, blue));        }        else if ((value = (String)attributes.remove(ElementTags.BACKGROUNDCOLOR)) != null) {            setBackgroundColor(MarkupParser.decodeColor(value));        }        if ((value = (String)attributes.remove(ElementTags.GRAYFILL)) != null) {            setGrayFill(Float.valueOf(value + "f").floatValue());        }        if (attributes.size() > 0) setMarkupAttributes(attributes);    }    // implementation of the Element-methods/** * Processes the element by adding it (or the different parts) to an * <CODE>ElementListener</CODE>. * * @param       listener        an <CODE>ElementListener</CODE> * @return <CODE>true</CODE> if the element was processed successfully */    public boolean process(ElementListener listener) {        try {            return listener.add(this);        }        catch(DocumentException de) {            return false;        }    }/** * Performs extra checks when executing table code (currently only when cells are added). */    public void setDebug(boolean aDebug) {        mDebug = aDebug;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -