?? pdfptable.java
字號:
/* * $Id: PdfPTable.java,v 1.29 2002/11/19 08:33:38 blowagie Exp $ * $Name: $ * * Copyright 2001, 2002 Paulo Soares * * 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/ */package com.lowagie.text.pdf;import java.util.ArrayList;import com.lowagie.text.Phrase;import com.lowagie.text.Element;import com.lowagie.text.ElementListener;import com.lowagie.text.DocumentException;/** This is a table that can be put at an absolute position but can also * be added to the document as the class <CODE>Table</CODE>. * In the last case when crossing pages the table always break at full rows; if a * row is bigger than the page it is dropped silently to avoid infinite loops. * <P> * A PdfPTableEvent can be associated to the table to do custom drawing * when the table is rendered. * @author Paulo Soares (psoares@consiste.pt) */public class PdfPTable implements Element{ /** The index of the original <CODE>PdfcontentByte</CODE>. */ public static final int BASECANVAS = 0; /** The index of the duplicate <CODE>PdfContentByte</CODE> where the backgroung will be drawn. */ public static final int BACKGROUNDCANVAS = 1; /** The index of the duplicate <CODE>PdfContentByte</CODE> where the border lines will be drawn. */ public static final int LINECANVAS = 2; /** The index of the duplicate <CODE>PdfContentByte</CODE> where the text will be drawn. */ public static final int TEXTCANVAS = 3; protected ArrayList rows = new ArrayList(); protected float totalHeight = 0; protected PdfPCell currentRow[]; protected int currentRowIdx = 0; protected PdfPCell defaultCell = new PdfPCell((Phrase)null); protected float totalWidth = 0; protected float relativeWidths[]; protected float absoluteWidths[]; protected PdfPTableEvent tableEvent; /** Holds value of property headerRows. */ protected int headerRows; /** Holds value of property widthPercentage. */ protected float widthPercentage = 80; /** Holds value of property horizontalAlignment. */ private int horizontalAlignment = Element.ALIGN_CENTER; /** Holds value of property skipFirstHeader. */ private boolean skipFirstHeader = false; protected boolean isColspan = false; protected int runDirection = PdfWriter.RUN_DIRECTION_DEFAULT; /** Constructs a <CODE>PdfPTable</CODE> with the relative column widths. * @param relativeWidths the relative column widths */ public PdfPTable(float relativeWidths[]) { if (relativeWidths == null) throw new NullPointerException("The widths array in PdfPTable constructor can not be null."); if (relativeWidths.length == 0) throw new IllegalArgumentException("The widths array in PdfPTable constructor can not have zero length."); this.relativeWidths = new float[relativeWidths.length]; System.arraycopy(relativeWidths, 0, this.relativeWidths, 0, relativeWidths.length); absoluteWidths = new float[relativeWidths.length]; calculateWidths(); currentRow = new PdfPCell[absoluteWidths.length]; } /** Constructs a <CODE>PdfPTable</CODE> with <CODE>numColumns</CODE> columns. * @param numColumns the number of columns */ public PdfPTable(int numColumns) { if (numColumns <= 0) throw new IllegalArgumentException("The number of columns in PdfPTable constructor must be greater than zero."); relativeWidths = new float[numColumns]; for (int k = 0; k < numColumns; ++k) relativeWidths[k] = 1; absoluteWidths = new float[relativeWidths.length]; calculateWidths(); currentRow = new PdfPCell[absoluteWidths.length]; } /** Constructs a copy of a <CODE>PdfPTable</CODE>. * @param table the <CODE>PdfPTable</CODE> to be copied */ public PdfPTable(PdfPTable table) { relativeWidths = new float[table.relativeWidths.length]; absoluteWidths = new float[table.relativeWidths.length]; System.arraycopy(table.relativeWidths, 0, relativeWidths, 0, relativeWidths.length); System.arraycopy(table.absoluteWidths, 0, absoluteWidths, 0, relativeWidths.length); totalWidth = table.totalWidth; totalHeight = table.totalHeight; currentRowIdx = table.currentRowIdx; tableEvent = table.tableEvent; runDirection = table.runDirection; defaultCell = new PdfPCell(table.defaultCell); currentRow = new PdfPCell[table.currentRow.length]; isColspan = table.isColspan; for (int k = 0; k < currentRow.length; ++k) { if (table.currentRow[k] == null) break; currentRow[k] = new PdfPCell(table.currentRow[k]); } for (int k = 0; k < table.rows.size(); ++k) { rows.add(new PdfPRow((PdfPRow)(table.rows.get(k)))); } } /** Sets the relative widths of the table. * @param relativeWidths the relative widths of the table. * @throws DocumentException if the number of widths is different than tne number * of columns */ public void setWidths(float relativeWidths[]) throws DocumentException { if (relativeWidths.length != this.relativeWidths.length) throw new DocumentException("Wrong number of columns."); this.relativeWidths = new float[relativeWidths.length]; System.arraycopy(relativeWidths, 0, this.relativeWidths, 0, relativeWidths.length); absoluteWidths = new float[relativeWidths.length]; totalHeight = 0; calculateWidths(); calculateHeights(); } /** Sets the relative widths of the table. * @param relativeWidths the relative widths of the table. * @throws DocumentException if the number of widths is different than tne number * of columns */ public void setWidths(int relativeWidths[]) throws DocumentException { float tb[] = new float[relativeWidths.length]; for (int k = 0; k < relativeWidths.length; ++k) tb[k] = relativeWidths[k]; setWidths(tb); } private void calculateWidths() { if (totalWidth <= 0) return; float total = 0; for (int k = 0; k < absoluteWidths.length; ++k) { total += relativeWidths[k]; } for (int k = 0; k < absoluteWidths.length; ++k) { absoluteWidths[k] = totalWidth * relativeWidths[k] / total; } } /** Sets the full width of the table. * @param totalWidth the full width of the table. */ public void setTotalWidth(float totalWidth) { if (this.totalWidth == totalWidth) return; this.totalWidth = totalWidth; totalHeight = 0; calculateWidths(); calculateHeights(); } /** Gets the full width of the table. * @return the full width of the table */ public float getTotalWidth() { return totalWidth; } void calculateHeights() { if (totalWidth <= 0) return; totalHeight = 0; for (int k = 0; k < rows.size(); ++k) { PdfPRow row = (PdfPRow)rows.get(k); row.setWidths(absoluteWidths); totalHeight += row.getMaxHeights(); } } /** Gets the default <CODE>PdfPCell</CODE> that will be used as * reference for all the <CODE>addCell</CODE> methods except * <CODE>addCell(PdfPCell)</CODE>. * @return default <CODE>PdfPCell</CODE> */ public PdfPCell getDefaultCell() { return defaultCell; } /** Adds a cell element. * @param cell the cell element */ public void addCell(PdfPCell cell) { PdfPCell ncell = new PdfPCell(cell); int colspan = ncell.getColspan(); colspan = Math.max(colspan, 1); colspan = Math.min(colspan, currentRow.length - currentRowIdx); ncell.setColspan(colspan); if (colspan != 1) isColspan = true; int rdir = ncell.getRunDirection(); if (rdir == PdfWriter.RUN_DIRECTION_DEFAULT) ncell.setRunDirection(runDirection); currentRow[currentRowIdx] = ncell; currentRowIdx += colspan; if (currentRowIdx >= currentRow.length) { if (runDirection == PdfWriter.RUN_DIRECTION_RTL) { PdfPCell rtlRow[] = new PdfPCell[absoluteWidths.length]; int rev = currentRow.length; for (int k = 0; k < currentRow.length; ++k) { PdfPCell rcell = currentRow[k]; int cspan = rcell.getColspan(); rev -= cspan; rtlRow[rev] = rcell; k += cspan - 1; } currentRow = rtlRow; } PdfPRow row = new PdfPRow(currentRow); if (totalWidth > 0) { row.setWidths(absoluteWidths); totalHeight += row.getMaxHeights(); } rows.add(row); currentRow = new PdfPCell[absoluteWidths.length]; currentRowIdx = 0; } } /** Adds a cell element. * @param text the text for the cell */ public void addCell(String text) { addCell(new Phrase(text)); } /** Adds a cell element. * @param table the table to be added to the cell */ public void addCell(PdfPTable table) { defaultCell.setTable(table); addCell(defaultCell); defaultCell.setTable(null); } /** Adds a cell element. * @param phrase the <CODE>Phrase</CODE> to be added to the cell */ public void addCell(Phrase phrase) { defaultCell.setPhrase(phrase); addCell(defaultCell); defaultCell.setPhrase(null); } /** * Writes the selected rows to the document. * <P> * <CODE>canvases</CODE> is obtained from <CODE>beginWrittingRows()</CODE>. * @param rowStart the first row to be written, zero index
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -