table.java
来自「处理PDF」· Java 代码 · 共 1,490 行 · 第 1/4 页
JAVA
1,490 行
if(Math.abs(wb - wt) < 0.0001) break; } } colMap[cb] = lDummyColumn+ct; // need to change this to work out how many cols to span for (int k=0; k < lDummyTable.getDimension().height; k++) { for (int l=0; l < lDummyTable.getDimension().width; l++) { lDummyElement = lDummyTable.getElement(k,l); if (lDummyElement != null) { int col=lDummyColumn+l; if ( Cell.class.isInstance(lDummyElement) ) { Cell lDummyC = (Cell)lDummyElement; // Find col to add cell in and set col span col = colMap[l]; int ot = colMap[l+lDummyC.getColspan()]; lDummyC.setColspan(ot-col); } ((Row) newRows.get(k + lDummyRow)).addElement(lDummyElement,col); // use addElement to set reserved status ok in row } } } } else // copy others values { Object aElement = getElement(i,j); if ( Cell.class.isInstance(aElement) ) { // adjust spans for cell ((Cell) aElement).setRowspan(((Cell) ((Row) rows.get(i)).getCell(j)).getRowspan() + lDummyHeights[i] - 1); ((Cell) aElement).setColspan(((Cell) ((Row) rows.get(i)).getCell(j)).getColspan() + lDummyWidths[j] - 1); // most likely this cell covers a larger area because of the row/cols splits : define not-to-be-filled cells placeCell(newRows,((Cell) aElement), new Point(lDummyRow,lDummyColumn)); } } lDummyColumn += lDummyWidths[j]; } lDummyRow += lDummyHeights[i]; } // Set our new matrix columns = lTotalColumns; rows = newRows; this.widths = lNewWidths; } } /** * adds new<CODE>Cell</CODE>'s to empty/null spaces. */ private void fillEmptyMatrixCells() { try { for (int i=0; i < rows.size(); i++) { for (int j=0; j < columns; j++) { if (!((Row) rows.get(i)).isReserved(j)) { addCell(defaultCell, new Point(i, j)); } } } } catch(BadElementException bee) { throw new ExceptionConverter(bee); } } /** * check if <CODE>Cell</CODE> 'fits' the table. * <P> * <UL><LI>rowspan/colspan not beyond borders * <LI>spanned cell don't overlap existing cells</UL> * * @param aCell the cell that has to be checked * @param aLocation the location where the cell has to be placed * @return true if the location was valid */ private boolean isValidLocation(Cell aCell, Point aLocation) { // rowspan not beyond last column if ( aLocation.x < rows.size() ) // if false : new location is already at new, not-yet-created area so no check { if ((aLocation.y + aCell.getColspan()) > columns) { return false; } int difx = ((rows.size() - aLocation.x) > aCell.getRowspan()) ? aCell.getRowspan() : rows.size() - aLocation.x; int dify = ((columns - aLocation.y) > aCell.getColspan()) ? aCell.getColspan() : columns - aLocation.y; // no other content at cells targeted by rowspan/colspan for (int i=aLocation.x; i < (aLocation.x + difx); i++) { for (int j=aLocation.y; j < (aLocation.y + dify); j++) { if (((Row) rows.get(i)).isReserved(j)) { return false; } } } } else { if ((aLocation.y + aCell.getColspan()) > columns) { return false; } } return true; } /** * Sets the unset cell properties to be the table defaults. * * @param aCell The cell to set to table defaults as necessary. */ private void assumeTableDefaults(Cell aCell) { if (aCell.getBorder() == Rectangle.UNDEFINED) { aCell.setBorder(defaultCell.getBorder()); } if (aCell.getBorderWidth() == Rectangle.UNDEFINED) { aCell.setBorderWidth(defaultCell.getBorderWidth()); } if (aCell.getBorderColor() == null) { aCell.setBorderColor(defaultCell.getBorderColor()); } if (aCell.getBackgroundColor() == null) { aCell.setBackgroundColor(defaultCell.getBackgroundColor()); } if (aCell.getHorizontalAlignment() == Element.ALIGN_UNDEFINED) { aCell.setHorizontalAlignment(defaultCell.getHorizontalAlignment()); } if (aCell.getVerticalAlignment() == Element.ALIGN_UNDEFINED) { aCell.setVerticalAlignment(defaultCell.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 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(columns); 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 border positions */ 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.setComplete(complete); if (isNotAddedYet()) pdfptable.setSkipFirstHeader(true); 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; } /** * Indicates if this is the first time the section is added. * @since iText2.0.8 * @return true if the section wasn't added yet */ public boolean isNotAddedYet() { return notAddedYet; } /** * Sets the indication if the section was already added to * the document. * @since iText2.0.8 * @param notAddedYet */ public void setNotAddedYet(boolean notAddedYet) { this.notAddedYet = notAddedYet; } /** * @since iText 2.0.8 * @see com.lowagie.text.LargeElement#flushContent() */ public void flushContent() { this.setNotAddedYet(false); ArrayList headerrows = new ArrayList(); for (int i = 0; i < getLastHeaderRow() + 1; i++) { headerrows.add(rows.get(i)); } rows = headerrows; } /** * @since iText 2.0.8 * @see com.lowagie.text.LargeElement#isComplete() */ public boolean isComplete() { return complete; } /** * @since iText 2.0.8 * @see com.lowagie.text.LargeElement#setComplete(boolean) */ public void setComplete(boolean complete) { this.complete = complete; } /** * Gets the default layout of the Table. * @return a cell with all the defaults * @deprecated As of iText 2.0.7, replaced by {@link #getDefaultCell()}, * scheduled for removal at 2.2.0 */ public Cell getDefaultLayout() { return getDefaultCell(); } /** * Sets the default layout of the Table to * the provided Cell * @param value a cell with all the defaults * @deprecated As of iText 2.0.7, replaced by {@link #setDefaultCell(Cell)}, * scheduled for removal at 2.2.0 */ public void setDefaultLayout(Cell value) { defaultCell = value; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?