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

📄 pdfprow.java

📁 iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Id: PdfPRow.java 2752 2007-05-15 14:58:33Z blowagie $
 * $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.awt.Color;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;

/**
 * A row in a PdfPTable.
 * 
 * @author Paulo Soares (psoares@consiste.pt)
 */

public class PdfPRow {

	/** the bottom limit (bottom right y) */
	public static final float BOTTOM_LIMIT = -(1 << 30);

	protected PdfPCell cells[];

	protected float widths[];

	protected float maxHeight = 0;

	protected boolean calculated = false;
    
    private int[] canvasesPos;

	/**
	 * Constructs a new PdfPRow with the cells in the array that was passed as a parameter.
	 * @param cells
	 */
	public PdfPRow(PdfPCell cells[]) {
		this.cells = cells;
		widths = new float[cells.length];
	}

	/**
	 * Makes a copy of an existing row.
	 * @param row
	 */
	public PdfPRow(PdfPRow row) {
		maxHeight = row.maxHeight;
		calculated = row.calculated;
		cells = new PdfPCell[row.cells.length];
		for (int k = 0; k < cells.length; ++k) {
			if (row.cells[k] != null)
				cells[k] = new PdfPCell(row.cells[k]);
		}
		widths = new float[cells.length];
		System.arraycopy(row.widths, 0, widths, 0, cells.length);
	}

	/**
	 * Sets the widths of the columns in the row.
	 * @param widths
	 * @return true if everything went right
	 */
	public boolean setWidths(float widths[]) {
		if (widths.length != cells.length)
			return false;
		System.arraycopy(widths, 0, this.widths, 0, cells.length);
		float total = 0;
		calculated = false;
		for (int k = 0; k < widths.length; ++k) {
			PdfPCell cell = cells[k];
			cell.setLeft(total);
			int last = k + cell.getColspan();
			for (; k < last; ++k)
				total += widths[k];
			--k;
			cell.setRight(total);
			cell.setTop(0);
		}
		return true;
	}

	/**
	 * Calculates the heights of each cell in the row.
	 * @return the maximum height of the row.
	 */
	public float calculateHeights() {
		maxHeight = 0;
		for (int k = 0; k < cells.length; ++k) {
			PdfPCell cell = cells[k];
			if (cell == null)
				continue;
			Image img = cell.getImage();
			if (img != null) {
				img.scalePercent(100);
                float refWidth = img.getScaledWidth();
                if (cell.getRotation() == 90 || cell.getRotation() == 270) {
                    refWidth = img.getScaledHeight();
                }
                float scale = (cell.getRight() - cell.getEffectivePaddingRight()
                    - cell.getEffectivePaddingLeft() - cell.getLeft())
                    / refWidth;
                img.scalePercent(scale * 100);
                float refHeight = img.getScaledHeight();
                if (cell.getRotation() == 90 || cell.getRotation() == 270) {
                    refHeight = img.getScaledWidth();
                }
                cell.setBottom(cell.getTop() - cell.getEffectivePaddingTop()
                    - cell.getEffectivePaddingBottom()
                    - refHeight);
			} else {
                if (cell.getRotation() == 0 || cell.getRotation() == 180) {
                    float rightLimit = cell.isNoWrap() ? 20000 : cell.getRight()
                            - cell.getEffectivePaddingRight();
                    float bry = (cell.getFixedHeight() > 0) ? cell.getTop()
                            - cell.getEffectivePaddingTop()
                            + cell.getEffectivePaddingBottom()
                            - cell.getFixedHeight() : BOTTOM_LIMIT;
                    ColumnText ct = ColumnText.duplicate(cell.getColumn());
                    setColumn(ct,
                            cell.getLeft() + cell.getEffectivePaddingLeft(), bry,
                            rightLimit, cell.getTop() - cell.getEffectivePaddingTop());
                    try {
                        ct.go(true);
                    } catch (DocumentException e) {
                        throw new ExceptionConverter(e);
                    }
                    float yLine = ct.getYLine();
                    if (cell.isUseDescender())
                        yLine += ct.getDescender();
                    cell.setBottom(yLine - cell.getEffectivePaddingBottom());
                }
                else {
                    if (cell.getFixedHeight() > 0) {
                        cell.setBottom(cell.getTop() - cell.getFixedHeight());
                    }
                    else {
                        ColumnText ct = ColumnText.duplicate(cell.getColumn());
                        setColumn(ct, 0, cell.getLeft() + cell.getEffectivePaddingLeft(),
                                20000, cell.getRight() - cell.getEffectivePaddingRight());
                        try {
                            ct.go(true);
                        } catch (DocumentException e) {
                            throw new ExceptionConverter(e);
                        }
                        cell.setBottom(cell.getTop() - cell.getEffectivePaddingTop() 
                            - cell.getEffectivePaddingBottom() - ct.getFilledWidth());
                    }
                }
			}
			float height = cell.getFixedHeight();
			if (height <= 0)
				height = cell.getHeight();
			if (height < cell.getFixedHeight())
				height = cell.getFixedHeight();
			else if (height < cell.getMinimumHeight())
				height = cell.getMinimumHeight();
			if (height > maxHeight)
				maxHeight = height;
		}
		calculated = true;
		return maxHeight;
	}

	/**
	 * Writes the border and background of one cell in the row.
	 * @param xPos
	 * @param yPos
	 * @param cell
	 * @param canvases
	 */
	public void writeBorderAndBackground(float xPos, float yPos, PdfPCell cell,
			PdfContentByte[] canvases) {
		PdfContentByte lines = canvases[PdfPTable.LINECANVAS];
		PdfContentByte backgr = canvases[PdfPTable.BACKGROUNDCANVAS];
		// the coordinates of the border are retrieved
		float x1 = cell.getLeft() + xPos;
		float y2 = cell.getTop() + yPos;
		float x2 = cell.getRight() + xPos;
		float y1 = y2 - maxHeight;

		// the backgroundcolor is set
		Color background = cell.getBackgroundColor();
		if (background != null) {
			backgr.setColorFill(background);
			backgr.rectangle(x1, y1, x2 - x1, y2 - y1);
			backgr.fill();
        }
		// if the element hasn't got any borders, nothing is added
		if (cell.hasBorders()) {
			if (cell.isUseVariableBorders()) {
				Rectangle borderRect = new Rectangle(cell.getLeft() + xPos, cell
						.getTop()
						- maxHeight + yPos, cell.getRight() + xPos, cell.getTop()
						+ yPos);
				borderRect.cloneNonPositionParameters(cell);
                borderRect.setBackgroundColor(null);
				lines.rectangle(borderRect);
			} else {
				// the width is set to the width of the element
				if (cell.getBorderWidth() != Rectangle.UNDEFINED) {
					lines.setLineWidth(cell.getBorderWidth());
				}
				// the color is set to the color of the element
				Color color = cell.getBorderColor();
				if (color != null) {
					lines.setColorStroke(color);
				}

				// if the box is a rectangle, it is added as a rectangle
				if (cell.hasBorder(Rectangle.BOX)) {
					lines.rectangle(x1, y1, x2 - x1, y2 - y1);
				}
				// if the border isn't a rectangle, the different sides are
				// added apart
				else {
					if (cell.hasBorder(Rectangle.RIGHT)) {
						lines.moveTo(x2, y1);
						lines.lineTo(x2, y2);
					}
					if (cell.hasBorder(Rectangle.LEFT)) {
						lines.moveTo(x1, y1);
						lines.lineTo(x1, y2);
					}
					if (cell.hasBorder(Rectangle.BOTTOM)) {
						lines.moveTo(x1, y1);
						lines.lineTo(x2, y1);
					}
					if (cell.hasBorder(Rectangle.TOP)) {
						lines.moveTo(x1, y2);
						lines.lineTo(x2, y2);
					}
				}
				lines.stroke();
				if (color != null) {
					lines.resetRGBColorStroke();
				}
			}
		}
	}

    private void saveAndRotateCanvases(PdfContentByte[] canvases, float a, float b, float c, float d, float e, float f) {
        int last = PdfPTable.TEXTCANVAS + 1;
        if (canvasesPos == null) {
            canvasesPos = new int[last * 2];
        }
        for (int k = 0; k < last; ++k) {
            ByteBuffer bb = canvases[k].getInternalBuffer();
            canvasesPos[k * 2] = bb.size();
            canvases[k].saveState();
            canvases[k].concatCTM(a, b, c, d, e, f);
            canvasesPos[k * 2 + 1] = bb.size();
        }
    }
    
    private void restoreCanvases(PdfContentByte[] canvases) {
        int last = PdfPTable.TEXTCANVAS + 1;
        for (int k = 0; k < last; ++k) {
            ByteBuffer bb = canvases[k].getInternalBuffer();
            int p1 = bb.size();
            canvases[k].restoreState();
            if (p1 == canvasesPos[k * 2 + 1])
                bb.setSize(canvasesPos[k * 2]);
        }
    }
    
    private float setColumn(ColumnText ct, float llx, float lly, float urx, float ury) {
        if (llx > urx)
            urx = llx;
        if (lly > ury)
            ury = lly;
        ct.setSimpleColumn(llx, lly, urx, ury);
        return ury;
    }
    
	/**
	 * Writes a number of cells (not necessarily all cells).
	 * @param colStart
	 * @param colEnd
	 * @param xPos
	 * @param yPos
	 * @param canvases
	 */
	public void writeCells(int colStart, int colEnd, float xPos, float yPos,
			PdfContentByte[] canvases) {
		if (!calculated)
			calculateHeights();

⌨️ 快捷键说明

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