pdfcontentbyte.java

来自「有关对pdf操作的代码」· Java 代码 · 共 1,856 行 · 第 1/5 页

JAVA
1,856
字号
     * @param       y3      y-coordinate of the ending point (= new current point)     */        public void curveFromTo(float x1, float y1, float x3, float y3) {        content.append(x1).append(' ').append(y1).append(' ').append(x3).append(' ').append(y3).append(" y").append_i(separator);    }        /** Draws a circle. The endpoint will (x+r, y).     *     * @param x x center of circle     * @param y y center of circle     * @param r radius of circle     */    public void circle(float x, float y, float r) {        float b = 0.5523f;        moveTo(x + r, y);        curveTo(x + r, y + r * b, x + r * b, y + r, x, y + r);        curveTo(x - r * b, y + r, x - r, y + r * b, x - r, y);        curveTo(x - r, y - r * b, x - r * b, y - r, x, y - r);        curveTo(x + r * b, y - r, x + r, y - r * b, x + r, y);    }                /**     * Adds a rectangle to the current path.     *     * @param       x       x-coordinate of the starting point     * @param       y       y-coordinate of the starting point     * @param       w       width     * @param       h       height     */        public void rectangle(float x, float y, float w, float h) {        content.append(x).append(' ').append(y).append(' ').append(w).append(' ').append(h).append(" re").append_i(separator);    }        private boolean compareColors(Color c1, Color c2) {        if (c1 == null && c2 == null)            return true;        if (c1 == null || c2 == null)            return false;        if (c1 instanceof ExtendedColor)            return c1.equals(c2);        return c2.equals(c1);    }        /**     * Adds a variable width border to the current path.     * Only use if {@link com.lowagie.text.Rectangle#isUseVariableBorders() Rectangle.isUseVariableBorders}     * = true.     * @param rect a <CODE>Rectangle</CODE>     */    public void variableRectangle(Rectangle rect) {        float t = rect.getTop();        float b = rect.getBottom();        float r = rect.getRight();        float l = rect.getLeft();        float wt = rect.getBorderWidthTop();        float wb = rect.getBorderWidthBottom();        float wr = rect.getBorderWidthRight();        float wl = rect.getBorderWidthLeft();        Color ct = rect.getBorderColorTop();        Color cb = rect.getBorderColorBottom();        Color cr = rect.getBorderColorRight();        Color cl = rect.getBorderColorLeft();        saveState();        setLineCap(PdfContentByte.LINE_CAP_BUTT);        setLineJoin(PdfContentByte.LINE_JOIN_MITER);        float clw = 0;        boolean cdef = false;        Color ccol = null;        boolean cdefi = false;        Color cfil = null;        // draw top        if (wt > 0) {            setLineWidth(clw = wt);            cdef = true;            if (ct == null)                resetRGBColorStroke();            else                setColorStroke(ct);            ccol = ct;            moveTo(l, t - wt / 2f);            lineTo(r, t - wt / 2f);            stroke();        }        // Draw bottom        if (wb > 0) {            if (wb != clw)                setLineWidth(clw = wb);            if (!cdef || !compareColors(ccol, cb)) {                cdef = true;                if (cb == null)                    resetRGBColorStroke();                else                    setColorStroke(cb);                ccol = cb;            }            moveTo(r, b + wb / 2f);            lineTo(l, b + wb / 2f);            stroke();        }        // Draw right        if (wr > 0) {            if (wr != clw)                setLineWidth(clw = wr);            if (!cdef || !compareColors(ccol, cr)) {                cdef = true;                if (cr == null)                    resetRGBColorStroke();                else                    setColorStroke(cr);                ccol = cr;            }            boolean bt = compareColors(ct, cr);            boolean bb = compareColors(cb, cr);            moveTo(r - wr / 2f, bt ? t : t - wt);            lineTo(r - wr / 2f, bb ? b : b + wb);            stroke();            if (!bt || !bb) {                cdefi = true;                if (cr == null)                    resetRGBColorFill();                else                    setColorFill(cr);                cfil = cr;                if (!bt) {                    moveTo(r, t);                    lineTo(r, t - wt);                    lineTo(r - wr, t - wt);                    fill();                }                if (!bb) {                    moveTo(r, b);                    lineTo(r, b + wb);                    lineTo(r - wr, b + wb);                    fill();                }            }        }                // Draw Left        if (wl > 0) {            if (wl != clw)                setLineWidth(wl);            if (!cdef || !compareColors(ccol, cl)) {                if (cl == null)                    resetRGBColorStroke();                else                    setColorStroke(cl);            }            boolean bt = compareColors(ct, cl);            boolean bb = compareColors(cb, cl);            moveTo(l + wl / 2f, bt ? t : t - wt);            lineTo(l + wl / 2f, bb ? b : b + wb);            stroke();            if (!bt || !bb) {                if (!cdefi || !compareColors(cfil, cl)) {                    if (cl == null)                        resetRGBColorFill();                    else                        setColorFill(cl);                }                if (!bt) {                    moveTo(l, t);                    lineTo(l, t - wt);                    lineTo(l + wl, t - wt);                    fill();                }                if (!bb) {                    moveTo(l, b);                    lineTo(l, b + wb);                    lineTo(l + wl, b + wb);                    fill();                }            }        }        restoreState();    }    /**     * Adds a border (complete or partially) to the current path..     *     * @param       rectangle       a <CODE>Rectangle</CODE>     */        public void rectangle(Rectangle rectangle) {        // the coordinates of the border are retrieved        float x1 = rectangle.getLeft();        float y1 = rectangle.getBottom();        float x2 = rectangle.getRight();        float y2 = rectangle.getTop();        // the backgroundcolor is set        Color background = rectangle.getBackgroundColor();        if (background != null) {            setColorFill(background);            rectangle(x1, y1, x2 - x1, y2 - y1);            fill();            resetRGBColorFill();        }        // if the element hasn't got any borders, nothing is added        if (! rectangle.hasBorders()) {            return;        }        // if any of the individual border colors are set        // we draw the borders all around using the        // different colors        if (rectangle.isUseVariableBorders()) {            variableRectangle(rectangle);        }        else {            // the width is set to the width of the element            if (rectangle.getBorderWidth() != Rectangle.UNDEFINED) {                setLineWidth(rectangle.getBorderWidth());            }            // the color is set to the color of the element            Color color = rectangle.getBorderColor();            if (color != null) {                setColorStroke(color);            }            // if the box is a rectangle, it is added as a rectangle            if (rectangle.hasBorder(Rectangle.BOX)) {               rectangle(x1, y1, x2 - x1, y2 - y1);            }            // if the border isn't a rectangle, the different sides are added apart            else {                if (rectangle.hasBorder(Rectangle.RIGHT)) {                    moveTo(x2, y1);                    lineTo(x2, y2);                }                if (rectangle.hasBorder(Rectangle.LEFT)) {                    moveTo(x1, y1);                    lineTo(x1, y2);                }                if (rectangle.hasBorder(Rectangle.BOTTOM)) {                    moveTo(x1, y1);                    lineTo(x2, y1);                }                if (rectangle.hasBorder(Rectangle.TOP)) {                    moveTo(x1, y2);                    lineTo(x2, y2);                }            }            stroke();            if (color != null) {                resetRGBColorStroke();            }        }    }    /**     * Closes the current subpath by appending a straight line segment from the current point     * to the starting point of the subpath.     */        public void closePath() {        content.append("h").append_i(separator);    }        /**     * Ends the path without filling or stroking it.     */        public void newPath() {        content.append("n").append_i(separator);    }        /**     * Strokes the path.     */        public void stroke() {        content.append("S").append_i(separator);    }        /**     * Closes the path and strokes it.     */        public void closePathStroke() {        content.append("s").append_i(separator);    }        /**     * Fills the path, using the non-zero winding number rule to determine the region to fill.     */        public void fill() {        content.append("f").append_i(separator);    }        /**     * Fills the path, using the even-odd rule to determine the region to fill.     */        public void eoFill() {        content.append("f*").append_i(separator);    }        /**     * Fills the path using the non-zero winding number rule to determine the region to fill and strokes it.     */        public void fillStroke() {        content.append("B").append_i(separator);    }        /**     * Closes the path, fills it using the non-zero winding number rule to determine the region to fill and strokes it.     */        public void closePathFillStroke() {        content.append("b").append_i(separator);    }        /**     * Fills the path, using the even-odd rule to determine the region to fill and strokes it.     */        public void eoFillStroke() {        content.append("B*").append_i(separator);    }        /**     * Closes the path, fills it using the even-odd rule to determine the region to fill and strokes it.     */        public void closePathEoFillStroke() {        content.append("b*").append_i(separator);    }        /**     * Adds an <CODE>Image</CODE> to the page. The <CODE>Image</CODE> must have     * absolute positioning.     * @param image the <CODE>Image</CODE> object     * @throws DocumentException if the <CODE>Image</CODE> does not have absolute positioning     */    public void addImage(Image image) throws DocumentException {        addImage(image, false);    }        /**     * Adds an <CODE>Image</CODE> to the page. The <CODE>Image</CODE> must have     * absolute positioning. The image can be placed inline.     * @param image the <CODE>Image</CODE> object     * @param inlineImage <CODE>true</CODE> to place this image inline, <CODE>false</CODE> otherwise     * @throws DocumentException if the <CODE>Image</CODE> does not have absolute positioning     */    public void addImage(Image image, boolean inlineImage) throws DocumentException {        if (!image.hasAbsoluteY())            throw new DocumentException("The image must have absolute positioning.");        float matrix[] = image.matrix();        matrix[Image.CX] = image.getAbsoluteX() - matrix[Image.CX];        matrix[Image.CY] = image.getAbsoluteY() - matrix[Image.CY];        addImage(image, matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], inlineImage);    }        /**     * Adds an <CODE>Image</CODE> to the page. The positioning of the <CODE>Image</CODE>     * is done with the transformation matrix. To position an <CODE>image</CODE> at (x,y)     * use addImage(image, image_width, 0, 0, image_height, x, y).     * @param image the <CODE>Image</CODE> object

⌨️ 快捷键说明

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