📄 multicolumntext.java
字号:
/* * $Id: MultiColumnText.java 3282 2008-04-22 11:06:26Z blowagie $ * $Name$ * * Copyright 2004 Steve Appling * * 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-2005 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000-2005 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.Chunk;import com.lowagie.text.DocumentException;import com.lowagie.text.Element;import com.lowagie.text.ElementListener;import com.lowagie.text.Phrase;import com.lowagie.text.Rectangle;/** * Formats content into one or more columns bounded by a * rectangle. The columns may be simple rectangles or * more complicated shapes. Add all of the columns before * adding content. Column continuation is supported. A MultiColumnText object may be added to * a document using <CODE>Document.add</CODE>. * @author Steve Appling */public class MultiColumnText implements Element { /** special constant for automatic calculation of height */ public static final float AUTOMATIC = -1f; /** * total desiredHeight of columns. If <CODE>AUTOMATIC</CODE>, this means fill pages until done. * This may be larger than one page */ private float desiredHeight; /** * total height of element written out so far */ private float totalHeight; /** * true if all the text could not be written out due to height restriction */ private boolean overflow; /** * Top of the columns - y position on starting page. * If <CODE>AUTOMATIC</CODE>, it means current y position when added to document */ private float top; /** * used to store the y position of the bottom of the page */ private float pageBottom; /** * ColumnText object used to do all the real work. This same object is used for all columns */ private ColumnText columnText; /** * Array of <CODE>ColumnDef</CODE> objects used to define the columns */ private ArrayList columnDefs; /** * true if all columns are simple (rectangular) */ private boolean simple = true; private int currentColumn = 0; private float nextY = AUTOMATIC; private boolean columnsRightToLeft = false; private PdfDocument document; /** * Default constructor. Sets height to <CODE>AUTOMATIC</CODE>. * Columns will repeat on each page as necessary to accommodate content length. */ public MultiColumnText() { this(AUTOMATIC); } /** * Construct a MultiColumnText container of the specified height. * If height is <CODE>AUTOMATIC</CODE>, fill complete pages until done. * If a specific height is used, it may span one or more pages. * * @param height */ public MultiColumnText(float height) { columnDefs = new ArrayList(); desiredHeight = height; top = AUTOMATIC; // canvas will be set later columnText = new ColumnText(null); totalHeight = 0f; } /** * Construct a MultiColumnText container of the specified height * starting at the specified Y position. * * @param height * @param top */ public MultiColumnText(float top, float height) { columnDefs = new ArrayList(); desiredHeight = height; this.top = top; nextY = top; // canvas will be set later columnText = new ColumnText(null); totalHeight = 0f; } /** * Indicates that all of the text did not fit in the * specified height. Note that isOverflow will return * false before the MultiColumnText object has been * added to the document. It will always be false if * the height is AUTOMATIC. * * @return true if there is still space left in the column */ public boolean isOverflow() { return overflow; } /** * Copy the parameters from the specified ColumnText to use * when rendering. Parameters like <CODE>setArabicOptions</CODE> * must be set in this way. * * @param sourceColumn */ public void useColumnParams(ColumnText sourceColumn) { // note that canvas will be overwritten later columnText.setSimpleVars(sourceColumn); } /** * Add a new column. The parameters are limits for each column * wall in the format of a sequence of points (x1,y1,x2,y2,...). * * @param left limits for left column * @param right limits for right column */ public void addColumn(float[] left, float[] right) { ColumnDef nextDef = new ColumnDef(left, right); simple = nextDef.isSimple(); columnDefs.add(nextDef); } /** * Add a simple rectangular column with specified left * and right x position boundaries. * * @param left left boundary * @param right right boundary */ public void addSimpleColumn(float left, float right) { ColumnDef newCol = new ColumnDef(left, right); columnDefs.add(newCol); } /** * Add the specified number of evenly spaced rectangular columns. * Columns will be separated by the specified gutterWidth. * * @param left left boundary of first column * @param right right boundary of last column * @param gutterWidth width of gutter spacing between columns * @param numColumns number of columns to add */ public void addRegularColumns(float left, float right, float gutterWidth, int numColumns) { float currX = left; float width = right - left; float colWidth = (width - (gutterWidth * (numColumns - 1))) / numColumns; for (int i = 0; i < numColumns; i++) { addSimpleColumn(currX, currX + colWidth); currX += colWidth + gutterWidth; } } /** * Add an element to be rendered in a column. * Note that you can only add a <CODE>Phrase</CODE> * or a <CODE>Chunk</CODE> if the columns are * not all simple. This is an underlying restriction in * {@link com.lowagie.text.pdf.ColumnText} * * @param element element to add * @throws DocumentException if element can't be added */ public void addElement(Element element) throws DocumentException { if (simple) { columnText.addElement(element); } else if (element instanceof Phrase) { columnText.addText((Phrase) element); } else if (element instanceof Chunk) { columnText.addText((Chunk) element); } else { throw new DocumentException("Can't add " + element.getClass() + " to MultiColumnText with complex columns"); } } /** * Write out the columns. After writing, use * {@link #isOverflow()} to see if all text was written. * @param canvas PdfContentByte to write with * @param document document to write to (only used to get page limit info) * @param documentY starting y position to begin writing at * @return the current height (y position) after writing the columns * @throws DocumentException on error */ public float write(PdfContentByte canvas, PdfDocument document, float documentY) throws DocumentException { this.document = document; columnText.setCanvas(canvas); if (columnDefs.isEmpty()) { throw new DocumentException("MultiColumnText has no columns"); } overflow = false; pageBottom = document.bottom(); float currentHeight = 0; boolean done = false; try { while (!done) { if (top == AUTOMATIC) { top = document.getVerticalPosition(true); // RS - 07/07/2005 - Get current doc writing position for top of columns on new page. } else if (nextY == AUTOMATIC) { nextY = document.getVerticalPosition(true); // RS - 07/07/2005 - - Get current doc writing position for top of columns on new page. } ColumnDef currentDef = (ColumnDef) columnDefs.get(getCurrentColumn()); columnText.setYLine(top); float[] left = currentDef.resolvePositions(Rectangle.LEFT); float[] right = currentDef.resolvePositions(Rectangle.RIGHT); if (document.isMarginMirroring() && document.getPageNumber() % 2 == 0){ float delta = document.rightMargin() - document.left(); left = (float[])left.clone(); right = (float[])right.clone(); for (int i = 0; i < left.length; i += 2) { left[i] -= delta; } for (int i = 0; i < right.length; i += 2) { right[i] -= delta; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -