Example usage for com.lowagie.text.pdf PdfGState PdfGState

List of usage examples for com.lowagie.text.pdf PdfGState PdfGState

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfGState PdfGState.

Prototype

PdfGState

Source Link

Usage

From source file:org.mapfish.print.map.renderers.BitmapTileRenderer.java

License:Open Source License

public void render(Transformer transformer, List<URI> uris, ParallelMapTileLoader parallelMapTileLoader,
        final RenderingContext context, final float opacity, int nbTilesHorizontal, float offsetX,
        float offsetY, final long bitmapTileW, final long bitmapTileH) throws IOException {
    final AffineTransform bitmapTransformer = transformer.getBitmapTransform();
    final double rotation = transformer.getRotation();

    for (int i = 0; i < uris.size(); i++) {
        final URI uri = uris.get(i);
        if (uri == null) {
            continue;
        }/* ww w  .  j a  va2s. c o m*/

        final int line = i / nbTilesHorizontal;
        final int col = i % nbTilesHorizontal;
        final float posX = 0 - offsetX + col * bitmapTileW;
        final float posY = 0 - offsetY + line * bitmapTileH;

        if (rotation != 0.0
                && !isTileVisible(posX, posY, bitmapTileW, bitmapTileH, bitmapTransformer, transformer)) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Not needed: " + uri);
            }
            continue;
        }

        parallelMapTileLoader.addTileToLoad(new MapTileTask() {
            public Image map;

            protected void readTile() throws IOException, DocumentException {
                map = PDFUtils.getImage(context, uri, bitmapTileW, bitmapTileH);
                map.setAbsolutePosition(posX, posY);
            }

            protected void renderOnPdf(PdfContentByte dc) throws DocumentException {
                dc.transform(bitmapTransformer);
                if (opacity < 1.0) {
                    PdfGState gs = new PdfGState();
                    gs.setFillOpacity(opacity);
                    gs.setStrokeOpacity(opacity);
                    dc.setGState(gs);
                }
                dc.addImage(map);
            }
        });
    }
}

From source file:org.mapfish.print.map.renderers.PDFMapRenderer.java

License:Open Source License

public void render(Transformer transformer, List<URI> uris, PdfContentByte dc, RenderingContext context,
        float opacity, int nbTilesHorizontal, float offsetX, float offsetY, long bitmapTileW, long bitmapTileH)
        throws IOException {
    if (uris.size() != 1) {
        //tiling not supported in PDF
        throw new InvalidValueException("format", "application/x-pdf");
    }/*from   w  w  w  .j  av  a 2s  .  c  o  m*/
    final URI uri = uris.get(0);
    LOGGER.debug(uri);
    PdfReader reader = new PdfReader(uri.toURL());
    PdfImportedPage pdfMap = context.getWriter().getImportedPage(reader, 1);

    if (opacity < 1.0) {
        PdfGState gs = new PdfGState();
        gs.setFillOpacity(opacity);
        gs.setStrokeOpacity(opacity);
        //gs.setBlendMode(PdfGState.BM_SOFTLIGHT);
        pdfMap.setGState(gs);
    }

    dc.saveState();
    try {
        dc.transform(transformer.getPdfTransform());
        dc.addTemplate(pdfMap, 0, 0);
    } finally {
        dc.restoreState();
    }
}

From source file:org.mapfish.print.map.renderers.PDFTileRenderer.java

License:Open Source License

public void render(final Transformer transformer, List<URI> uris, ParallelMapTileLoader parallelMapTileLoader,
        final RenderingContext context, final float opacity, int nbTilesHorizontal, float offsetX,
        float offsetY, long bitmapTileW, long bitmapTileH) throws IOException {
    if (uris.size() != 1) {
        //tiling not supported in PDF
        throw new InvalidValueException("format", "application/x-pdf");
    }//from   w w  w  . j  a  va 2s. c o  m
    final URI uri = uris.get(0);

    parallelMapTileLoader.addTileToLoad(new MapTileTask() {
        public PdfImportedPage pdfMap;

        protected void readTile() throws IOException, DocumentException {
            LOGGER.debug(uri);
            PdfReader reader = new PdfReader(uri.toURL());
            synchronized (context.getPdfLock()) {
                pdfMap = context.getWriter().getImportedPage(reader, 1);

                if (opacity < 1.0) {
                    PdfGState gs = new PdfGState();
                    gs.setFillOpacity(opacity);
                    gs.setStrokeOpacity(opacity);
                    //gs.setBlendMode(PdfGState.BM_SOFTLIGHT);
                    pdfMap.setGState(gs);
                }
            }
        }

        protected void renderOnPdf(PdfContentByte dc) throws DocumentException {
            dc.transform(transformer.getPdfTransform());
            dc.addTemplate(pdfMap, 0, 0);
        }
    });
}

From source file:org.mapfish.print.map.renderers.SVGMapRenderer.java

License:Open Source License

public void render(Transformer transformer, java.util.List<URI> uris, PdfContentByte dc,
        RenderingContext context, float opacity, int nbTilesHorizontal, float offsetX, float offsetY,
        long bitmapTileW, long bitmapTileH) throws IOException {
    if (uris.size() != 1) {
        //tiling not supported in SVG
        throw new InvalidValueException("format", "application/x-pdf");
    }/*from  w  w w  .  j  a v  a2s. c  om*/

    dc.saveState();
    try {
        dc.transform(transformer.getSvgTransform());

        if (opacity < 1.0) {
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(opacity);
            gs.setStrokeOpacity(opacity);
            //gs.setBlendMode(PdfGState.BM_SOFTLIGHT);
            dc.setGState(gs);
        }

        Graphics2D g2 = dc.createGraphics(transformer.getRotatedSvgW(), transformer.getRotatedSvgH());

        //avoid a warning from Batik
        System.setProperty("org.apache.batik.warn_destination", "false");
        g2.setRenderingHint(RenderingHintsKeyExt.KEY_TRANSCODING,
                RenderingHintsKeyExt.VALUE_TRANSCODING_PRINTING);
        g2.setRenderingHint(RenderingHintsKeyExt.KEY_AVOID_TILE_PAINTING,
                RenderingHintsKeyExt.VALUE_AVOID_TILE_PAINTING_ON);

        final URI uri = uris.get(0);
        LOGGER.debug(uri);
        final TranscoderInput ti = getTranscoderInput(uri.toURL(), transformer, context);
        if (ti != null) {
            PrintTranscoder pt = new PrintTranscoder();
            pt.transcode(ti, null);
            Paper paper = new Paper();
            paper.setSize(transformer.getRotatedSvgW(), transformer.getRotatedSvgH());
            paper.setImageableArea(0, 0, transformer.getRotatedSvgW(), transformer.getRotatedSvgH());
            PageFormat pf = new PageFormat();
            pf.setPaper(paper);
            pt.print(g2, pf, 0);
        }

        g2.dispose();
    } finally {
        dc.restoreState();
    }

}

From source file:org.mapfish.print.map.renderers.SVGTileRenderer.java

License:Open Source License

public void render(final Transformer transformer, java.util.List<URI> uris,
        ParallelMapTileLoader parallelMapTileLoader, final RenderingContext context, final float opacity,
        int nbTilesHorizontal, float offsetX, float offsetY, long bitmapTileW, long bitmapTileH)
        throws IOException {
    if (uris.size() != 1) {
        //tiling not supported in SVG
        throw new InvalidValueException("format", "application/x-pdf");
    }/* ww w. j  a  va2s  .c o m*/

    final URI uri = uris.get(0);

    parallelMapTileLoader.addTileToLoad(new MapTileTask() {
        public PrintTranscoder pt;

        @Override
        protected void readTile() throws IOException, DocumentException {
            LOGGER.debug(uri);
            final TranscoderInput ti = getTranscoderInput(uri.toURL(), transformer, context);
            if (ti != null) {
                pt = new PrintTranscoder();
                pt.transcode(ti, null);
            }
        }

        @Override
        protected void renderOnPdf(PdfContentByte dc) throws DocumentException {
            dc.transform(transformer.getSvgTransform());

            if (opacity < 1.0) {
                PdfGState gs = new PdfGState();
                gs.setFillOpacity(opacity);
                gs.setStrokeOpacity(opacity);
                //gs.setBlendMode(PdfGState.BM_SOFTLIGHT);
                dc.setGState(gs);
            }

            Graphics2D g2 = dc.createGraphics(transformer.getRotatedSvgW(), transformer.getRotatedSvgH());

            //avoid a warning from Batik
            System.setProperty("org.apache.batik.warn_destination", "false");
            g2.setRenderingHint(RenderingHintsKeyExt.KEY_TRANSCODING,
                    RenderingHintsKeyExt.VALUE_TRANSCODING_PRINTING);
            g2.setRenderingHint(RenderingHintsKeyExt.KEY_AVOID_TILE_PAINTING,
                    RenderingHintsKeyExt.VALUE_AVOID_TILE_PAINTING_ON);

            Paper paper = new Paper();
            paper.setSize(transformer.getRotatedSvgW(), transformer.getRotatedSvgH());
            paper.setImageableArea(0, 0, transformer.getRotatedSvgW(), transformer.getRotatedSvgH());
            PageFormat pf = new PageFormat();
            pf.setPaper(paper);
            pt.print(g2, pf, 0);
            g2.dispose();
        }
    });
}

From source file:org.mapfish.print.map.renderers.vector.LineStringRenderer.java

License:Open Source License

protected void renderImpl(RenderingContext context, PdfContentByte dc, PJsonObject style, LineString geometry) {
    PdfGState state = new PdfGState();
    applyStyle(context, dc, style, state);
    dc.setGState(state);/* w ww .  ja v a  2 s  . com*/
    Coordinate[] coords = geometry.getCoordinates();
    if (coords.length < 2)
        return;
    dc.moveTo((float) coords[0].x, (float) coords[0].y);
    for (int i = 1; i < coords.length; i++) {
        Coordinate coord = coords[i];
        dc.lineTo((float) coord.x, (float) coord.y);
    }
    dc.stroke();
}

From source file:org.mapfish.print.map.renderers.vector.PointRenderer.java

License:Open Source License

protected void renderImpl(RenderingContext context, PdfContentByte dc, PJsonObject style, Point geometry) {
    PdfGState state = new PdfGState();
    final Coordinate coordinate = geometry.getCoordinate();
    float pointRadius = style.optFloat("pointRadius", 4.0f);
    final float f = context.getStyleFactor();

    String graphicName = style.optString("graphicName");
    float width = style.optFloat("graphicWidth", pointRadius * 2.0f);
    float height = style.optFloat("graphicHeight", pointRadius * 2.0f);
    float offsetX = style.optFloat("graphicXOffset", -width / 2.0f);
    float offsetY = style.optFloat("graphicYOffset", -height / 2.0f);
    // See Feature/Vector.js for more information about labels
    String label = style.optString("label");
    String labelAlign = style.optString("labelAlign", "lb");
    /*/* ww  w.  j a va2 s  .  c  o m*/
     * Valid values for horizontal alignment: "l"=left, "c"=center, "r"=right. 
     * Valid values for vertical alignment: "t"=top, "m"=middle, "b"=bottom.
     */
    float labelXOffset = style.optFloat("labelXOffset", (float) 0.0);
    float labelYOffset = style.optFloat("labelYOffset", (float) 0.0);
    String fontColor = style.optString("fontColor", "#000000");
    /* Supported itext fonts: COURIER, HELVETICA, TIMES_ROMAN */
    String fontFamily = style.optString("fontFamily", "HELVETICA");
    String fontSize = style.optString("fontSize", "12");
    String fontWeight = style.optString("fontWeight", "normal");

    if (style.optString("externalGraphic") != null) {
        float opacity = style.optFloat("graphicOpacity", style.optFloat("fillOpacity", 1.0f));
        state.setFillOpacity(opacity);
        state.setStrokeOpacity(opacity);
        dc.setGState(state);
        try {
            Image image = PDFUtils.createImage(context, width * f, height * f,
                    new URI(style.getString("externalGraphic")), 0.0f);
            image.setAbsolutePosition((float) coordinate.x + offsetX * f, (float) coordinate.y + offsetY * f);
            dc.addImage(image);
        } catch (BadElementException e) {
            context.addError(e);
        } catch (URISyntaxException e) {
            context.addError(e);
        } catch (DocumentException e) {
            context.addError(e);
        }

    } else if (graphicName != null && !graphicName.equalsIgnoreCase("circle")) {
        PolygonRenderer.applyStyle(context, dc, style, state);
        float[] symbol = SYMBOLS.get(graphicName);
        if (symbol == null) {
            throw new InvalidValueException("graphicName", graphicName);
        }
        dc.setGState(state);
        dc.moveTo((float) coordinate.x + symbol[0] * width * f + offsetX * f,
                (float) coordinate.y + symbol[1] * height * f + offsetY * f);
        for (int i = 2; i < symbol.length - 2; i += 2) {
            dc.lineTo((float) coordinate.x + symbol[i] * width * f + offsetX * f,
                    (float) coordinate.y + symbol[i + 1] * height * f + offsetY * f);

        }
        dc.closePath();
        dc.fillStroke();

    } else if (label != null && label.length() > 0) {
        BaseFont bf = PDFUtils.getBaseFont(fontFamily, fontSize, fontWeight);
        float fontHeight = (float) Double.parseDouble(fontSize.toLowerCase().replaceAll("px", "")) * f;
        dc.setFontAndSize(bf, fontHeight);
        dc.setColorFill(ColorWrapper.convertColor(fontColor));
        state.setFillOpacity((float) 1.0);
        dc.setGState(state);
        dc.beginText();
        dc.setTextMatrix((float) coordinate.x + labelXOffset * f, (float) coordinate.y + labelYOffset * f);
        dc.setGState(state);
        dc.showTextAligned(PDFUtils.getHorizontalAlignment(labelAlign), label,
                (float) coordinate.x + labelXOffset * f,
                (float) coordinate.y + labelYOffset * f - PDFUtils.getVerticalOffset(labelAlign, fontHeight),
                0);
        dc.endText();
    } else {
        PolygonRenderer.applyStyle(context, dc, style, state);
        dc.setGState(state);

        dc.circle((float) coordinate.x, (float) coordinate.y, pointRadius * f);
        dc.fillStroke();
    }
}

From source file:org.mapfish.print.map.renderers.vector.PolygonRenderer.java

License:Open Source License

protected void renderImpl(RenderingContext context, PdfContentByte dc, PJsonObject style, Polygon geometry) {
    PdfGState state = new PdfGState();
    applyStyle(context, dc, style, state);
    dc.setGState(state);//from   ww w. j a v  a 2 s  .  c om

    final LineString ring = geometry.getExteriorRing();
    renderRing(dc, ring);
    for (int i = 0; i < geometry.getNumInteriorRing(); ++i) {
        renderRing(dc, geometry.getInteriorRingN(i));
    }
    dc.eoFillStroke();
}

From source file:org.meveo.admin.action.billing.BillingAccountBean.java

License:Open Source License

public void generatePDF(long invoiceId) {
    Invoice invoice = invoiceService.findById(invoiceId);
    byte[] invoicePdf = invoice.getPdf();
    FacesContext context = FacesContext.getCurrentInstance();
    String invoiceFilename = null;
    BillingRun billingRun = invoice.getBillingRun();
    invoiceFilename = invoice.getInvoiceNumber() + ".pdf";
    if (billingRun != null && billingRun.getStatus() != BillingRunStatusEnum.VALIDATED) {
        invoiceFilename = "unvalidated-invoice.pdf";
    }/*from   w ww . ja  v  a2s . c  o m*/

    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
    response.setContentType("application/pdf"); // fill in
    response.setHeader("Content-disposition", "attachment; filename=" + invoiceFilename);

    try {
        OutputStream os = response.getOutputStream();
        Document document = new Document(PageSize.A4);
        if (billingRun != null && invoice.getBillingRun().getStatus() != BillingRunStatusEnum.VALIDATED) {
            // Add watemark image
            PdfReader reader = new PdfReader(invoicePdf);
            int n = reader.getNumberOfPages();
            PdfStamper stamp = new PdfStamper(reader, os);
            PdfContentByte over = null;
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.5f);
            int i = 1;
            while (i <= n) {
                over = stamp.getOverContent(i);
                over.setGState(gs);
                over.beginText();
                System.out.println("top=" + document.top() + ",bottom=" + document.bottom());
                over.setTextMatrix(document.top(), document.bottom());
                over.setFontAndSize(bf, 150);
                over.setColorFill(Color.GRAY);
                over.showTextAligned(Element.ALIGN_CENTER, "TEST", document.getPageSize().getWidth() / 2,
                        document.getPageSize().getHeight() / 2, 45);
                over.endText();
                i++;
            }

            stamp.close();
        } else {
            os.write(invoicePdf); // fill in PDF with bytes
        }

        // contentType
        os.flush();
        os.close();
        context.responseComplete();
    } catch (IOException e) {
        log.error("failed to generate PDF ", e);
    } catch (DocumentException e) {
        log.error("error in generation PDF ", e);
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

License:Open Source License

public boolean drawPdfImage(final com.lowagie.text.Image image, final Image img, AffineTransform xform,
        final ImageObserver obs) {
    if (img == null) {
        throw new NullPointerException("Image must not be null.");
    }/*ww  w  . java 2s .  com*/
    if (image == null) {
        throw new NullPointerException("Image must not be null.");
    }
    if (xform == null) {
        xform = AffineTransform.getTranslateInstance(0, 0);
    }

    xform.translate(0, img.getHeight(obs));
    xform.scale(img.getWidth(obs), img.getHeight(obs));

    final AffineTransform inverse = this.normalizeMatrix();
    final AffineTransform flipper = FLIP_TRANSFORM;
    inverse.concatenate(xform);
    inverse.concatenate(flipper);

    try {
        final double[] mx = new double[6];
        inverse.getMatrix(mx);
        if (currentFillGState != 255) {
            PdfGState gs = fillGState[255];
            if (gs == null) {
                gs = new PdfGState();
                gs.setFillOpacity(1);
                fillGState[255] = gs;
            }
            cb.setGState(gs);
        }

        cb.addImage(image, (float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4],
                (float) mx[5]);
    } catch (Exception ex) {
        PdfGraphics2D.logger.error("Failed to draw the image: ", ex);
        // throw new IllegalArgumentException("Failed to draw the image");
    } finally {
        if (currentFillGState != 255) {
            final PdfGState gs = fillGState[currentFillGState];
            cb.setGState(gs);
        }
    }
    return true;
}