Package com.itextpdf.text.pdf

Examples of com.itextpdf.text.pdf.PdfContentByte


        PdfWriter writer = PdfWriter.getInstance(document, byteStream);
        document.setPageSize(PageSize.LETTER);

        document.open();

        PdfContentByte cb = writer.getDirectContent();

        BaseFont font = BaseFont.createFont();

        float x = document.getPageSize().getWidth()/2;
        float y = document.getPageSize().getHeight()/2;
       
        cb.transform(AffineTransform.getTranslateInstance(x, y));

        cb.moveTo(-10, 0);
        cb.lineTo(10, 0);
        cb.moveTo(0, -10);
        cb.lineTo(0, 10);
        cb.stroke();
       
        cb.beginText();
        cb.setFontAndSize(font, 12);
        cb.transform(AffineTransform.getRotateInstance(rotation/180f*Math.PI));
        cb.showText(text1);
        if (moveTextToNextLine)
            cb.moveText(0, moveTextDelta);
        else
            cb.transform(AffineTransform.getTranslateInstance(moveTextDelta, 0));
        cb.showText(text2);
        cb.endText();

        document.close();

        final byte[] pdfBytes = byteStream.toByteArray();

View Full Code Here


    // 3.计算前半部分的foot1的长度,后面好定位最后一部分的'Y页'这俩字的x轴坐标,字体长度也要计算进去 = len
    float len = bf.getWidthPoint(foot1, presentFontSize);

    // 4.拿到当前的PdfContentByte
    PdfContentByte cb = writer.getDirectContent();

    // 5.写入页脚1,x轴就是(右margin+左margin + right() -left()- len)/2.0F
    // 再给偏移20F适合人类视觉感受,否则肉眼看上去就太偏左了
    // ,y轴就是底边界-20,否则就贴边重叠到数据体里了就不是页脚了;注意Y轴是从下往上累加的,最上方的Top值是大于Bottom好几百开外的。
    ColumnText
        .showTextAligned(
            cb,
            Element.ALIGN_CENTER,
            footer,
            (document.rightMargin() + document.right()
                + document.leftMargin() - document.left() - len) / 2.0F + 20F,
            document.bottom() - 20, 0);

    // 6.写入页脚2的模板(就是页脚的Y页这俩字)添加到文档中,计算模板的和Y轴,X=(右边界-左边界 - 前半部分的len值)/2.0F +
    // len , y 轴和之前的保持一致,底边界-20
    cb.addTemplate(total, (document.rightMargin() + document.right()
        + document.leftMargin() - document.left()) / 2.0F + 20F,
        document.bottom() - 20); // 调节模版显示的位置

  }
View Full Code Here

   * @throws BadElementException
   *             on error
   * @throws IOException
   */
  public static Image getInstance(final PdfWriter writer, final java.awt.Image awtImage, final float quality) throws BadElementException, IOException {
    return getInstance(new PdfContentByte(writer), awtImage, quality);
  }
View Full Code Here

        } catch (DocumentException ex) {
            Exceptions.printStackTrace(ex);
        }
        document.open();
        PdfContentByte cb = pdfWriter.getDirectContent();
        cb.saveState();

        props.putValue(PDFTarget.LANDSCAPE, landscape);
        props.putValue(PDFTarget.PAGESIZE, size);
        props.putValue(PDFTarget.MARGIN_TOP, new Float((float) marginTop));
        props.putValue(PDFTarget.MARGIN_LEFT, new Float((float) marginLeft));
        props.putValue(PDFTarget.MARGIN_BOTTOM, new Float((float) marginBottom));
        props.putValue(PDFTarget.MARGIN_RIGHT, new Float((float) marginRight));
        props.putValue(PDFTarget.PDF_CONTENT_BYTE, cb);
        target = (PDFTarget) controller.getRenderTarget(RenderTarget.PDF_TARGET, workspace);
        if (target instanceof LongTask) {
            ((LongTask) target).setProgressTicket(progress);
        }

        try {
            controller.render(target, workspace);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        cb.restoreState();
        document.close();

        Progress.finish(progress);

        props.putValue(PDFTarget.PDF_CONTENT_BYTE, null);
View Full Code Here

        labelElem.appendChild(labelText);
        target.getTopElement(SVGTarget.TOP_EDGE_LABELS).appendChild(labelElem);
    }

    public void renderPDF(PDFTarget target, String label, float x, float y, Color color, float outlineSize, Color outlineColor) {
        PdfContentByte cb = target.getContentByte();
        cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
        BaseFont bf = target.getBaseFont(font);
        float textHeight = getTextHeight(bf, font.getSize(), label);
        if (outlineSize > 0) {
            cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
            cb.setRGBColorStroke(outlineColor.getRed(), outlineColor.getGreen(), outlineColor.getBlue());
            cb.setLineWidth(outlineSize);
            cb.setLineJoin(PdfContentByte.LINE_JOIN_ROUND);
            cb.setLineCap(PdfContentByte.LINE_CAP_ROUND);
            if (outlineColor.getAlpha() < 255) {
                cb.saveState();
                float alpha = outlineColor.getAlpha() / 255f;
                PdfGState gState = new PdfGState();
                gState.setStrokeOpacity(alpha);
                cb.setGState(gState);
            }
            cb.beginText();
            cb.setFontAndSize(bf, font.getSize());
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f);
            cb.endText();
            if (outlineColor.getAlpha() < 255) {
                cb.restoreState();
            }
        }
        cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
        cb.beginText();
        cb.setFontAndSize(bf, font.getSize());
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f);
        cb.endText();
    }
View Full Code Here

        float marginLeft = properties.getFloatValue(PDFTarget.MARGIN_LEFT);
        float marginRight = properties.getFloatValue(PDFTarget.MARGIN_RIGHT);
        float marginTop = properties.getFloatValue(PDFTarget.MARGIN_TOP);
        Rectangle pageSize = properties.getValue(PDFTarget.PAGESIZE);
        boolean landscape = properties.getBooleanValue(PDFTarget.LANDSCAPE);
        PdfContentByte cb = properties.getValue(PDFTarget.PDF_CONTENT_BYTE);
        PDFRenderTargetImpl renderTarget = new PDFRenderTargetImpl(cb, width, height, topLeftX, topLeftY,
                pageSize, marginLeft, marginRight, marginTop, marginBottom, landscape);
        return renderTarget;
    }
View Full Code Here

            arrowElem.setAttribute("fill-opacity", (color.getAlpha() / 255f) + "");
            arrowElem.setAttribute("stroke", "none");
            svgTarget.getTopElement(SVGTarget.TOP_ARROWS).appendChild(arrowElem);
        } else if (target instanceof PDFTarget) {
            PDFTarget pdfTarget = (PDFTarget) target;
            PdfContentByte cb = pdfTarget.getContentByte();
            cb.moveTo(p1.x, -p1.y);
            cb.lineTo(p2.x, -p2.y);
            cb.lineTo(p3.x, -p3.y);
            cb.closePath();
            cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
            if (color.getAlpha() < 255) {
                cb.saveState();
                float alpha = color.getAlpha() / 255f;
                PdfGState gState = new PdfGState();
                gState.setFillOpacity(alpha);
                cb.setGState(gState);
            }
            cb.fill();
            if (color.getAlpha() < 255) {
                cb.restoreState();
            }
        }
    }
View Full Code Here

        }
    }

    public void renderPDF(PDFTarget target, Node node, String label, float x, float y, int fontSize, Color color, float outlineSize, Color outlineColor, boolean showBox, Color boxColor) {
        Font font = fontCache.get(fontSize);
        PdfContentByte cb = target.getContentByte();
        BaseFont bf = target.getBaseFont(font);

        //Box
        if (showBox) {
            cb.setRGBColorFill(boxColor.getRed(), boxColor.getGreen(), boxColor.getBlue());
            if (boxColor.getAlpha() < 255) {
                cb.saveState();
                float alpha = boxColor.getAlpha() / 255f;
                PdfGState gState = new PdfGState();
                gState.setFillOpacity(alpha);
                cb.setGState(gState);
            }
            float textWidth = getTextWidth(bf, fontSize, label);
            float textHeight = getTextHeight(bf, fontSize, label);

            //A height of just textHeight seems to be half the text height sometimes
            //BaseFont getAscentPoint and getDescentPoint may be not very precise
            cb.rectangle(x - textWidth / 2f - outlineSize / 2f, -y - outlineSize / 2f - textHeight, textWidth + outlineSize, textHeight * 2f + outlineSize);

            cb.fill();
            if (boxColor.getAlpha() < 255) {
                cb.restoreState();
            }
        }

        cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
        float textHeight = getTextHeight(bf, fontSize, label);
        if (outlineSize > 0) {
            cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
            cb.setRGBColorStroke(outlineColor.getRed(), outlineColor.getGreen(), outlineColor.getBlue());
            cb.setLineWidth(outlineSize);
            cb.setLineJoin(PdfContentByte.LINE_JOIN_ROUND);
            cb.setLineCap(PdfContentByte.LINE_CAP_ROUND);
            if (outlineColor.getAlpha() < 255) {
                cb.saveState();
                float alpha = outlineColor.getAlpha() / 255f;
                PdfGState gState = new PdfGState();
                gState.setStrokeOpacity(alpha);
                cb.setGState(gState);
            }
            cb.beginText();
            cb.setFontAndSize(bf, font.getSize());
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f);
            cb.endText();
            if (outlineColor.getAlpha() < 255) {
                cb.restoreState();
            }
        }
        cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
        cb.beginText();
        cb.setFontAndSize(bf, font.getSize());
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f);
        cb.endText();
    }
View Full Code Here

        Color color = item.getData(NodeItem.COLOR);
        Color borderColor = ((DependantColor) properties.getValue(PreviewProperty.NODE_BORDER_COLOR)).getColor(color);
        float borderSize = properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH);
        float alpha = properties.getFloatValue(PreviewProperty.NODE_OPACITY) / 100f;

        PdfContentByte cb = target.getContentByte();
        cb.setRGBColorStroke(borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue());
        cb.setLineWidth(borderSize);
        cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
        if (alpha < 1f) {
            cb.saveState();
            PdfGState gState = new PdfGState();
            gState.setFillOpacity(alpha);
            gState.setStrokeOpacity(alpha);
            cb.setGState(gState);
        }
        cb.circle(x, -y, size);
        if (borderSize > 0) {
            cb.fillStroke();
        } else {
            cb.fill();
        }
        if (alpha < 1f) {
            cb.restoreState();
        }
    }
View Full Code Here

            selfLoopElem.setAttribute("stroke-width", Float.toString(thickness * svgTarget.getScaleRatio()));
            selfLoopElem.setAttribute("fill", "none");
            svgTarget.getTopElement(SVGTarget.TOP_EDGES).appendChild(selfLoopElem);
        } else if (renderTarget instanceof PDFTarget) {
            PDFTarget pdfTarget = (PDFTarget) renderTarget;
            PdfContentByte cb = pdfTarget.getContentByte();
            cb.moveTo(x, -y);
            cb.curveTo(v1.x, -v1.y, v2.x, -v2.y, x, -y);
            cb.setRGBColorStroke(color.getRed(), color.getGreen(), color.getBlue());
            cb.setLineWidth(thickness);
            if (color.getAlpha() < 255) {
                cb.saveState();
                float alpha = color.getAlpha() / 255f;
                PdfGState gState = new PdfGState();
                gState.setStrokeOpacity(alpha);
                cb.setGState(gState);
            }
            cb.stroke();
            if (color.getAlpha() < 255) {
                cb.restoreState();
            }
        }
    }
View Full Code Here

TOP

Related Classes of com.itextpdf.text.pdf.PdfContentByte

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.