pdfptable.java
来自「处理PDF」· Java 代码 · 共 1,164 行 · 第 1/3 页
JAVA
1,164 行
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 nested table. * @param table the table to be added to the cell */ public void addCell(PdfPTable table) { defaultCell.setTable(table); addCell(defaultCell); defaultCell.setTable(null); } /** * Adds an Image as Cell. * @param image the <CODE>Image</CODE> to add to the table. This image will fit in the cell */ public void addCell(Image image) { defaultCell.setImage(image); addCell(defaultCell); defaultCell.setImage(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>beginWritingRows()</CODE>. * @param rowStart the first row to be written, zero index * @param rowEnd the last row to be written + 1. If it is -1 all the * rows to the end are written * @param xPos the x write coordinate * @param yPos the y write coordinate * @param canvases an array of 4 <CODE>PdfContentByte</CODE> obtained from * <CODE>beginWrittingRows()</CODE> * @return the y coordinate position of the bottom of the last row * @see #beginWritingRows(com.lowagie.text.pdf.PdfContentByte) */ public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases) { return writeSelectedRows(0, -1, rowStart, rowEnd, xPos, yPos, canvases); } /** Writes the selected rows and columns to the document. * This method does not clip the columns; this is only important * if there are columns with colspan at boundaries. * <P> * <CODE>canvases</CODE> is obtained from <CODE>beginWritingRows()</CODE>. * <P> * The table event is only fired for complete rows. * @param colStart the first column to be written, zero index * @param colEnd the last column to be written + 1. If it is -1 all the * columns to the end are written * @param rowStart the first row to be written, zero index * @param rowEnd the last row to be written + 1. If it is -1 all the * rows to the end are written * @param xPos the x write coordinate * @param yPos the y write coordinate * @param canvases an array of 4 <CODE>PdfContentByte</CODE> obtained from * <CODE>beginWrittingRows()</CODE> * @return the y coordinate position of the bottom of the last row * @see #beginWritingRows(com.lowagie.text.pdf.PdfContentByte) */ public float writeSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases) { if (totalWidth <= 0) throw new RuntimeException("The table width must be greater than zero."); int size = rows.size(); if (rowEnd < 0) rowEnd = size; rowEnd = Math.min(rowEnd, size); if (rowStart < 0) rowStart = 0; if (rowStart >= rowEnd) return yPos; if (colEnd < 0) colEnd = absoluteWidths.length; colEnd = Math.min(colEnd, absoluteWidths.length); if (colStart < 0) colStart = 0; colStart = Math.min(colStart, absoluteWidths.length); float yPosStart = yPos; for (int k = rowStart; k < rowEnd; ++k) { PdfPRow row = (PdfPRow)rows.get(k); if (row != null) { row.writeCells(colStart, colEnd, xPos, yPos, canvases); yPos -= row.getMaxHeights(); } } if (tableEvent != null && colStart == 0 && colEnd == absoluteWidths.length) { float heights[] = new float[rowEnd - rowStart + 1]; heights[0] = yPosStart; for (int k = rowStart; k < rowEnd; ++k) { PdfPRow row = (PdfPRow)rows.get(k); float hr = 0; if (row != null) hr = row.getMaxHeights(); heights[k - rowStart + 1] = heights[k - rowStart] - hr; } tableEvent.tableLayout(this, getEventWidths(xPos, rowStart, rowEnd, headersInEvent), heights, headersInEvent ? headerRows : 0, rowStart, canvases); } return yPos; } /** * Writes the selected rows to the document. * * @param rowStart the first row to be written, zero index * @param rowEnd the last row to be written + 1. If it is -1 all the * rows to the end are written * @param xPos the x write coordinate * @param yPos the y write coordinate * @param canvas the <CODE>PdfContentByte</CODE> where the rows will * be written to * @return the y coordinate position of the bottom of the last row */ public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas) { return writeSelectedRows(0, -1, rowStart, rowEnd, xPos, yPos, canvas); } /** * Writes the selected rows to the document. * This method clips the columns; this is only important * if there are columns with colspan at boundaries. * <P> * The table event is only fired for complete rows. * * @param colStart the first column to be written, zero index * @param colEnd the last column to be written + 1. If it is -1 all the * @param rowStart the first row to be written, zero index * @param rowEnd the last row to be written + 1. If it is -1 all the * rows to the end are written * @param xPos the x write coordinate * @param yPos the y write coordinate * @param canvas the <CODE>PdfContentByte</CODE> where the rows will * be written to * @return the y coordinate position of the bottom of the last row */ public float writeSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas) { if (colEnd < 0) colEnd = absoluteWidths.length; colEnd = Math.min(colEnd, absoluteWidths.length); if (colStart < 0) colStart = 0; colStart = Math.min(colStart, absoluteWidths.length); if (colStart != 0 || colEnd != absoluteWidths.length) { float w = 0; for (int k = colStart; k < colEnd; ++k) w += absoluteWidths[k]; canvas.saveState(); float lx = 0; float rx = 0; if (colStart == 0) lx = 10000; if (colEnd == absoluteWidths.length) rx = 10000; canvas.rectangle(xPos - lx, -10000, w + lx + rx, 20000); canvas.clip(); canvas.newPath(); } PdfContentByte[] canvases = beginWritingRows(canvas); float y = writeSelectedRows(colStart, colEnd, rowStart, rowEnd, xPos, yPos, canvases); endWritingRows(canvases); if (colStart != 0 || colEnd != absoluteWidths.length) canvas.restoreState(); return y; } /** Gets and initializes the 4 layers where the table is written to. The text or graphics are added to * one of the 4 <CODE>PdfContentByte</CODE> returned with the following order:<p> * <ul> * <li><CODE>PdfPtable.BASECANVAS</CODE> - the original <CODE>PdfContentByte</CODE>. Anything placed here * will be under the table. * <li><CODE>PdfPtable.BACKGROUNDCANVAS</CODE> - the layer where the background goes to. * <li><CODE>PdfPtable.LINECANVAS</CODE> - the layer where the lines go to. * <li><CODE>PdfPtable.TEXTCANVAS</CODE> - the layer where the text go to. Anything placed here * will be over the table. * </ul><p> * The layers are placed in sequence on top of each other. * @param canvas the <CODE>PdfContentByte</CODE> where the rows will * be written to * @return an array of 4 <CODE>PdfContentByte</CODE> * @see #writeSelectedRows(int, int, float, float, PdfContentByte[]) */ public static PdfContentByte[] beginWritingRows(PdfContentByte canvas) { return new PdfContentByte[]{ canvas, canvas.getDuplicate(), canvas.getDuplicate(), canvas.getDuplicate(), }; } /** Finishes writing the table. * @param canvases the array returned by <CODE>beginWritingRows()</CODE> */ public static void endWritingRows(PdfContentByte[] canvases) { PdfContentByte canvas = canvases[BASECANVAS]; canvas.saveState(); canvas.add(canvases[BACKGROUNDCANVAS]); canvas.restoreState(); canvas.saveState(); canvas.setLineCap(2); canvas.resetRGBColorStroke(); canvas.add(canvases[LINECANVAS]); canvas.restoreState(); canvas.add(canvases[TEXTCANVAS]); } /** Gets the number of rows in this table. * @return the number of rows in this table */ public int size() { return rows.size(); } /** Gets the total height of the table. * @return the total height of the table */ public float getTotalHeight() { return totalHeight; } /** Gets the height of a particular row. * @param idx the row index (starts at 0) * @return the height of a particular row */ public float getRowHeight(int idx) { if (totalWidth <= 0 || idx < 0 || idx >= rows.size()) return 0; PdfPRow row = (PdfPRow)rows.get(idx); if (row == null) return 0; return row.getMaxHeights(); } /** Gets the height of the rows that constitute the header as defined by * <CODE>setHeaderRows()</CODE>. * @return the height of the rows that constitute the header and footer */ public float getHeaderHeight() { float total = 0; int size = Math.min(rows.size(), headerRows); for (int k = 0; k < size; ++k) { PdfPRow row = (PdfPRow)rows.get(k); if (row != null) total += row.getMaxHeights(); } return total; } /** Gets the height of the rows that constitute the header as defined by * <CODE>setFooterRows()</CODE>. * @return the height of the rows that constitute the footer * @since 2.1.1 */ public float getFooterHeight() { float total = 0; int start = Math.min(0, headerRows - footerRows); int size = Math.min(rows.size(), footerRows); for (int k = start; k < size; ++k) { PdfPRow row = (PdfPRow)rows.get(k); if (row != null) total += row.getMaxHeights(); } return total; } /** Deletes a row from the table. * @param rowNumber the row to be deleted * @return <CODE>true</CODE> if the row was deleted */ public boolean deleteRow(int rowNumber) { if (rowNumber < 0 || rowNumber >= rows.size()) { return false; } if (totalWidth > 0) { PdfPRow row = (PdfPRow)rows.get(rowNumber); if (row != null) totalHeight -= row.getMaxHeights(); } rows.remove(rowNumber); return true; } /** Deletes the last row in the table. * @return <CODE>true</CODE> if the last row was deleted */ public boolean deleteLastRow() { return deleteRow(rows.size() - 1); } /** * Removes all of the rows except headers */ public void deleteBodyRows() { ArrayList rows2 = new ArrayList(); for (int k = 0; k < headerRows; ++k) rows2.add(rows.get(k)); rows = rows2; totalHeight = 0; if (totalWidth > 0) totalHeight = getHeaderHeight(); } /** Returns the number of columns. * @return the number of columns. * @since 2.1.1 */ public int getNumberOfColumns() { return relativeWidths.length; } /** Gets the number of the rows that constitute the header. * @return the number of the rows that constitute the header */ public int getHeaderRows() { return headerRows; } /** Sets the number of the top rows that constitute the header. * This header has only meaning if the table is added to <CODE>Document</CODE> * and the table crosses pages. * @param headerRows the number of the top rows that constitute the header */ public void setHeaderRows(int headerRows) { if (headerRows < 0) headerRows = 0; this.headerRows = headerRows; } /** * Gets all the chunks in this element. * * @return an <CODE>ArrayList</CODE> */ public ArrayList getChunks() { return new ArrayList(); } /** * Gets the type of the text element. * * @return a type
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?