table.java

来自「iText是一个能够快速产生PDF文件的java类库。iText的java类对于」· Java 代码 · 共 1,619 行 · 第 1/4 页

JAVA
1,619
字号
        }
        if (aCell.getBorderWidth() == Rectangle.UNDEFINED) {
            aCell.setBorderWidth(defaultLayout.getBorderWidth());
        }
        if (aCell.getBorderColor() == null) {
            aCell.setBorderColor(defaultLayout.getBorderColor());
        }
        if (aCell.getBackgroundColor() == null) {
            aCell.setBackgroundColor(defaultLayout.getBackgroundColor());
        }
        if (aCell.getHorizontalAlignment() == Element.ALIGN_UNDEFINED) {
            aCell.setHorizontalAlignment(defaultLayout.getHorizontalAlignment());
        }
        if (aCell.getVerticalAlignment() == Element.ALIGN_UNDEFINED) {
            aCell.setVerticalAlignment(defaultLayout.getVerticalAlignment());
        }
    }
    
    /**
     * Inserts a Cell in a cell-array and reserves cells defined by row-/colspan.
     *
     * @param   someRows    some rows
     * @param   aCell       the cell that has to be inserted
     * @param   aPosition   the position where the cell has to be placed
     */
    private void placeCell(ArrayList someRows, Cell aCell, Point aPosition) {
        int i;
        Row row = null;
        int lColumns = ((Row) someRows.get(0)).getColumns();
        int rowCount = aPosition.x + aCell.getRowspan() - someRows.size();
        assumeTableDefaults(aCell);
        if ( (aPosition.x + aCell.getRowspan()) > someRows.size() ) {
            for (i = 0; i < rowCount; i++) {
                row = new Row(lColumns);
                someRows.add(row);
            }
        }
        
        // reserve cell in rows below
        for (i = aPosition.x + 1; i < (aPosition.x  + aCell.getRowspan()); i++) {
            if ( !((Row) someRows.get(i)).reserve(aPosition.y, aCell.getColspan())) {
                
                // should be impossible to come here :-)
                throw new RuntimeException("addCell - error in reserve");
            }
        }
        row = (Row) someRows.get(aPosition.x);
        row.addElement(aCell, aPosition.y);
        
    }
    
    /**
     *  Sets current col/row to valid(empty) pos after addCell/Table
     * @param aLocation a location in the Table
     */
    private void setCurrentLocationToNextValidPosition(Point aLocation)    {
        // set latest location to next valid position
        int i, j;
        i = aLocation.x;
        j = aLocation.y;
        do {
            if ( (j + 1)  == columns ) {    // goto next row
                i++;
                j = 0;
            }
            else {
                j++;
            }
        }
        while (
        (i < rows.size()) && (j < columns) && (((Row) rows.get(i)).isReserved(j))
        );
        curPosition = new Point(i, j);
    }
    
    // public helper methods
    
    /**
     * Gets an array with the positions of the borders between every column.
     * <P>
     * This method translates the widths expressed in percentages into the
     * x-coordinate of the borders of the columns on a real document.
     *
     * @param       left            this is the position of the first border at the left (cellpadding not included)
     * @param       totalWidth      this is the space between the first border at the left
     *                                              and the last border at the right (cellpadding not included)
     * @return      an array with borderpositions
     */
    public float[] getWidths(float left, float totalWidth) {
        // for x columns, there are x+1 borders
        float[] w = new float[columns + 1];
        float wPercentage;
        if (locked) {
        	wPercentage = 100 * width / totalWidth;
        }
        else {
        	wPercentage = width;
        }
        // the border at the left is calculated
        switch(alignment) {
            case Element.ALIGN_LEFT:
                w[0] = left;
                break;
            case Element.ALIGN_RIGHT:
                w[0] = left + (totalWidth * (100 - wPercentage)) / 100;
                break;
            case Element.ALIGN_CENTER:
            default:
                w[0] = left + (totalWidth * (100 - wPercentage)) / 200;
        }
        // the total available width is changed
        totalWidth = (totalWidth * wPercentage) / 100;
        // the inner borders are calculated
        for (int i = 1; i < columns; i++) {
            w[i] = w[i - 1] + (widths[i - 1] * totalWidth / 100);
        }
        // the border at the right is calculated
        w[columns] = w[0] + totalWidth;
        return w;
    }
    
    /**
     * Gets an <CODE>Iterator</CODE> of all the <CODE>Row</CODE>s.
     *
     * @return      an <CODE>Iterator</CODE>
     */
    public Iterator iterator() {
        return rows.iterator();
    }

    /**
     * Create a PdfPTable based on this Table object.
     * @return a PdfPTable object
     * @throws BadElementException
     */
    public PdfPTable createPdfPTable() throws BadElementException {
    	if (!convert2pdfptable) {
    		throw new BadElementException("No error, just an old style table");
    	}
        setAutoFillEmptyCells(true);
    	complete();
    	PdfPTable pdfptable = new PdfPTable(widths);
    	pdfptable.setTableEvent(SimpleTable.getDimensionlessInstance(this, cellspacing));
    	pdfptable.setHeaderRows(lastHeaderRow + 1);
    	pdfptable.setSplitLate(cellsFitPage);
    	pdfptable.setKeepTogether(tableFitsPage);
    	if (!Float.isNaN(offset)) {
    		pdfptable.setSpacingBefore(offset);
    	}
    	pdfptable.setHorizontalAlignment(alignment);
    	if (locked) {
    		pdfptable.setTotalWidth(width);
    		pdfptable.setLockedWidth(true);
    	}
    	else {
    		pdfptable.setWidthPercentage(width);
    	}
    	Row row;
        for (Iterator iterator = iterator(); iterator.hasNext(); ) {
            row = (Row) iterator.next();
            Element cell;
            PdfPCell pcell;
            for (int i = 0; i < row.getColumns(); i++) {
                if ((cell = (Element)row.getCell(i)) != null) {
                	if (cell instanceof Table) {
                		pcell = new PdfPCell(((Table)cell).createPdfPTable());
                	}
                	else if (cell instanceof Cell) {
                		pcell = ((Cell)cell).createPdfPCell();
                		 pcell.setPadding(cellpadding + cellspacing / 2f);
                         pcell.setCellEvent(SimpleCell.getDimensionlessInstance((Cell)cell, cellspacing));
                	}
                	else {
                		pcell = new PdfPCell();
                	}
                	pdfptable.addCell(pcell);
                }
            }
        }
    	return pdfptable;
    }
    
    // deprecated stuff
    
    /**
     * Returns a <CODE>Table</CODE> that has been constructed taking in account
     * the value of some <VAR>attributes</VAR>.
     *
     * @param    attributes        Some attributes
     * @throws BadElementException 
     * @deprecated 
     */
    public Table(java.util.Properties attributes) {
        this(com.lowagie.text.factories.ElementFactory.getTable(attributes));
    } 
    
    /**
	 * Gets the number of columns.
	 *
	 * @return    a value
	 * @deprecated Use {@link #getColumns()} instead
	 */
	public int columns() {
		return getColumns();
	}
    
    /**
	 * Gets the horizontal alignment.
	 *
	 * @return  a value
	 * @deprecated Use {@link #getAlignment()} instead
	 */
	public int alignment() {
		return getAlignment();
	}
    
    /**
	 * Gets the cellpadding.
	 *
	 * @return  a value
	 * @deprecated Use {@link #getPadding()} instead
	 */
	public float cellpadding() {
		return getPadding();
	}
    
    /**
	 * Gets the cellspacing.
	 *
	 * @return  a value
	 * @deprecated Use {@link #getSpacing()} instead
	 */
	public float cellspacing() {
		return getSpacing();
	}
    
    /**
     * Sets the cellpadding.
     *
     * @param       value   the new value
     * @deprecated	use setPadding
     */
    public void setSpaceInsideCell(float value) {
        cellpadding = value;
    }
    
    /**
     * Sets the cellspacing.
     *
     * @param       value   the new value
     * @deprecated	use setSpacing
     */
    public void setSpaceBetweenCells(float value) {
        cellspacing = value;
    }
    
    /**
	 * Gets the last number of the rows that contain headers.
	 *  
	 * @return a rownumber
	 * @deprecated Use {@link #getLastHeaderRow()} instead
	 */
	public int lastHeaderRow() {
		return getLastHeaderRow();
	}
    
    /**
	 * Gets the table width (a percentage).
	 *
	 * @return      the table width
	 * @deprecated Use {@link #getWidth()} instead
	 */
	public float widthPercentage() {
		return getWidth();
	}
    
    /**
     * Sets the width of this table (in percentage of the available space).
     *
     * @param   width           the width
     * @deprecated setTotalWidth(int width)
     */
    public void setAbsWidth(String width) {
    	setWidth(Float.parseFloat(width + "f"));
        setLocked(true);
    }
    
    /**
     * Gets the table width (in pixels).
     *
     * @return  the table width
     * @deprecated use getWidth
     */
    public String absWidth() {
    	if (isLocked())
    		return String.valueOf(width);
    	else
    		return "";
    }
    
    // setters for default cell
    
    /**
     * Changes the border in the default layout of the <CODE>Cell</CODE>s
     * added with method <CODE>addCell(String content)</CODE>.
     *
     * @param       value   the new border value
     * @deprecated	use getDefaultCell.setBorder
     */
    public void setDefaultCellBorder(int value) {
        defaultLayout.setBorder(value);
    }
    
    /**
     * Changes the width of the borders in the default layout of the <CODE>Cell</CODE>s
     * added with method <CODE>addCell(String content)</CODE>.
     *
     * @param       value   the new width
     * @deprecated	use getDefaultCell.setBorderWidth
     */
    public void setDefaultCellBorderWidth(float value) {
        defaultLayout.setBorderWidth(value);
    }
    
    /**
     * Changes the bordercolor in the default layout of the <CODE>Cell</CODE>s
     * added with method <CODE>addCell(String content)</CODE>.
     *
     * @param       color   the new color
     * @deprecated	getDefaultCell.setBorderColor
     */
    public void setDefaultCellBorderColor(Color color) {
        defaultLayout.setBorderColor(color);
    }
    
    /**
     * Changes the backgroundcolor in the default layout of the <CODE>Cell</CODE>s
     * added with method <CODE>addCell(String content)</CODE>.
     *
     * @param       color   the new color
     * @deprecated	use getDefaultCell.setBackgroundColor
     */
    public void setDefaultCellBackgroundColor(Color color) {
        defaultLayout.setBackgroundColor(color);
    }
    
    /**
     * Changes the grayfill in the default layout of the <CODE>Cell</CODE>s
     * added with method <CODE>addCell(String content)</CODE>.
     *
     * @param       value   the new value
     * @deprecated	use getDefaultCell.setGrayFill
     */
    public void setDefaultCellGrayFill(float value) {
        if (value >= 0 && value <= 1) {
            defaultLayout.setGrayFill(value);
        }
    }
    
    /**
     * Changes the horizontalAlignment in the default layout of the <CODE>Cell</CODE>s
     * added with method <CODE>addCell(String content)</CODE>.
     *
     * @param       value   the new alignment value
     * @deprecated	use getDefaultCell.setHorizontalAlignment
     */
    public void setDefaultHorizontalAlignment(int value) {
        defaultLayout.setHorizontalAlignment(value);
    }
    
    /**
     * Changes the verticalAlignment in the default layout of the <CODE>Cell</CODE>s
     * added with method <CODE>addCell(String content)</CODE>.
     *
     * @param       value   the new alignment value
     * @deprecated	use getDefaultCell.setVerticalAlignment
     */
    public void setDefaultVerticalAlignment(int value) {
        defaultLayout.setVerticalAlignment(value);
    }
    
    /**
     * Changes the rowspan in the default layout of the <CODE>Cell</CODE>s
     * added with method <CODE>addCell(String content)</CODE>.
     *
     * @param       value   the new rowspan value
     * @deprecated	use getDefaultCell.setRowspan
     */
    public void setDefaultRowspan(int value) {
        defaultLayout.setRowspan(value);
    }
    
    /**
     * Changes the colspan in the default layout of the <CODE>Cell</CODE>s
     * added with method <CODE>addCell(String content)</CODE>.
     *
     * @param       value   the new colspan value
     * @deprecated	use getDefaultCell.setColspan
     */
    public void setDefaultColspan(int value) {
        defaultLayout.setColspan(value);
    }
    
}

⌨️ 快捷键说明

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