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

📄 rtfrow.java

📁 iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        this.cellspacing = cellspacing;
        this.borders = borders;
        this.borderColor = borderColor;
        this.borderWidth = borderWidth;

        if (this.borderWidth > 2) this.borderWidth = 2;

        int cellLeft = 0;
        for (int i = 0; i < row.getColumns(); i++) {
            Element cell = (Element) row.getCell(i);

            // cellWidth is an absolute argument
            // it's based on the absolute of this row and the proportional
            // width of this column
            int cellWidth = (int) (width * propWidths[i] / 100);
            if (cell != null) {
                if (cell.type() == Element.CELL) {
                    RtfCell rtfCell = (RtfCell) cells.get(i);
                    cellLeft = rtfCell.importCell((Cell) cell, cellLeft, cellWidth, i, y, cellpadding);
                }
            } else {
                RtfCell rtfCell = (RtfCell) cells.get(i);
                cellLeft = rtfCell.importCell(null, cellLeft, cellWidth, i, y, cellpadding);
            }
        }

        // recalculate the cell right border and the cumulative width
        // on col spanning cells.
        // col + row spanning cells are also handled by this loop, because the real cell of
        // the upper left corner in such an col, row matrix is copied as first cell
        // in each row in this matrix
        int columns = row.getColumns();
        for (int i = 0; i < columns; i++) {
            RtfCell firstCell = (RtfCell) cells.get(i);
            Cell cell = firstCell.getStore();
            int cols = 0;
            if(cell != null) {
                cols = cell.getColspan();
            }
            if (cols > 1) {
                RtfCell lastCell = (RtfCell) cells.get(i + cols - 1);
                firstCell.setCellRight(lastCell.getCellRight());
                int width = firstCell.getCellWidth();
                for (int j = i + 1; j < i + cols; j++) {
                    RtfCell cCell = (RtfCell) cells.get(j);
                    width += cCell.getCellWidth();
                }
                firstCell.setCellWidth(width);
                i += cols - 1;
            }
        }
        return true;
    }

    /**
     * Write the <code>RtfRow</code> to the specified <code>OutputStream</code>.
     *
     * @param os The <code>OutputStream</code> to which this <code>RtfRow</code>
     * should be written to.
     * @param rowNum The <code>index</code> of this row in the containing table.
     * @param table The <code>Table</code> which contains the original <code>Row</code>.
     * @return true if writing the row succeeded
     * @throws DocumentException
     * @throws IOException
     */
    public boolean writeRow(ByteArrayOutputStream os, int rowNum, Table table) throws DocumentException,
            IOException {
        os.write(RtfWriter.escape);
        os.write(rowBegin);
        os.write((byte) '\n');
        os.write(RtfWriter.escape);
        os.write(rowWidthStyle);
        os.write(RtfWriter.escape);
        os.write(rowWidth);
        writeInt(os, width);
//        os.write(RtfWriter.escape);
//        os.write(rowAutofit);
        if (mainTable.getOriginalTable().isCellsFitPage()) {
            os.write(RtfWriter.escape);
            os.write(rowKeep);
        }
        // check if this row is a header row
        if (rowNum < table.getLastHeaderRow() + 1) {
            os.write(RtfWriter.escape);
            os.write(rowHeader);
        }
        os.write(RtfWriter.escape);
        switch (this.origRow.getHorizontalAlignment()) {
            case Element.ALIGN_LEFT:
                os.write(rowAlignLeft);
                break;
            case Element.ALIGN_CENTER:
                os.write(rowAlignCenter);
                break;
            case Element.ALIGN_RIGHT:
                os.write(rowAlignRight);
                break;
            default :
                os.write(rowAlignLeft);
                break;
        }
        os.write(RtfWriter.escape);
        os.write(graphLeft);
        writeInt(os, 10);
        if (((borders & Rectangle.LEFT) == Rectangle.LEFT) && (borderWidth > 0)) {
            writeBorder(os, rowBorderLeft);
        }
        if (((borders & Rectangle.TOP) == Rectangle.TOP) && (borderWidth > 0)) {
            writeBorder(os, rowBorderTop);
        }
        if (((borders & Rectangle.BOTTOM) == Rectangle.BOTTOM) && (borderWidth > 0)) {
            writeBorder(os, rowBorderBottom);
        }
        if (((borders & Rectangle.RIGHT) == Rectangle.RIGHT) && (borderWidth > 0)) {
            writeBorder(os, rowBorderRight);
        }
        if (((borders & Rectangle.BOX) == Rectangle.BOX) && (borderWidth > 0)) {
            writeBorder(os, rowBorderInlineHorizontal);
            writeBorder(os, rowBorderInlineVertical);
        }

        if (cellspacing > 0) {
            os.write(RtfWriter.escape);
            os.write(rowSpacingLeft);
            writeInt(os, cellspacing / 2);
            os.write(RtfWriter.escape);
            os.write(rowSpacingLeftStyle);
            os.write(RtfWriter.escape);
            os.write(rowSpacingTop);
            writeInt(os, cellspacing / 2);
            os.write(RtfWriter.escape);
            os.write(rowSpacingTopStyle);
            os.write(RtfWriter.escape);
            os.write(rowSpacingBottom);
            writeInt(os, cellspacing / 2);
            os.write(RtfWriter.escape);
            os.write(rowSpacingBottomStyle);
            os.write(RtfWriter.escape);
            os.write(rowSpacingRight);
            writeInt(os, cellspacing / 2);
            os.write(RtfWriter.escape);
            os.write(rowSpacingRightStyle);
        }
        os.write(RtfWriter.escape);
        os.write(rowPaddingLeft);
        writeInt(os, cellpadding / 2);
        os.write(RtfWriter.escape);
        os.write(rowPaddingRight);
        writeInt(os, cellpadding / 2);
        os.write(RtfWriter.escape);
        os.write(rowPaddingLeftStyle);
        os.write(RtfWriter.escape);
        os.write(rowPaddingRightStyle);
        os.write((byte) '\n');

        Iterator cellIterator = cells.iterator();
        while (cellIterator.hasNext()) {
            RtfCell cell = (RtfCell) cellIterator.next();
            cell.writeCellSettings(os);
        }

        os.write(RtfWriter.escape);
        os.write("intbl".getBytes());

        cellIterator = cells.iterator();
        while (cellIterator.hasNext()) {
            RtfCell cell = (RtfCell) cellIterator.next();
            cell.writeCellContent(os);
        }
        os.write(RtfWriter.delimiter);
        os.write(RtfWriter.escape);
        os.write(rowEnd);
        return true;
    }


    private void writeBorder(ByteArrayOutputStream os, byte[] borderType) throws IOException {
        // horizontal and vertical, top, left, bottom, right
        os.write(RtfWriter.escape);
        os.write(borderType);
        // line style
        os.write(RtfWriter.escape);
        os.write(RtfRow.tableBorder);
        // borderwidth
        os.write(RtfWriter.escape);
        os.write(RtfRow.tableBorderWidth);
        writeInt(os, (int) (borderWidth * RtfWriter.TWIPSFACTOR));
        // border color
        os.write(RtfWriter.escape);
        os.write(RtfRow.tableBorderColor);
        if (borderColor == null) {
            writeInt(os, writer.addColor(new Color(0, 0, 0)));
        } else {
            writeInt(os, writer.addColor(borderColor));
        }
        os.write((byte) '\n');
    }


    /**
     * <code>RtfTable</code>s call this method from their own setMerge() to
     * specify that a certain other cell is to be merged with it.
     *
     * @param x The column position of the cell to be merged
     * @param mergeType The merge type specifies the kind of merge to be applied
     * (MERGE_HORIZ_PREV, MERGE_VERT_PREV, MERGE_BOTH_PREV)
     * @param mergeCell The <code>RtfCell</code> that the cell at x and y is to
     * be merged with
     */
    public void setMerge(int x, int mergeType, RtfCell mergeCell) {
        RtfCell cell = (RtfCell) cells.get(x);
        cell.setMerge(mergeType, mergeCell);
    }

    /*
     * Write an Integer to the Outputstream.
     *
     * @param out The <code>OutputStream</code> to be written to.
     * @param i The int to be written.
     */
    private void writeInt(ByteArrayOutputStream out, int i) throws IOException {
        out.write(Integer.toString(i).getBytes());
    }
}

⌨️ 快捷键说明

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