Example usage for com.itextpdf.text.pdf PdfContentByte beginLayer

List of usage examples for com.itextpdf.text.pdf PdfContentByte beginLayer

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfContentByte beginLayer.

Prototype

public void beginLayer(final PdfOCG layer) 

Source Link

Document

Begins a graphic block whose visibility is controlled by the layer.

Usage

From source file:com.vectorprint.report.itext.DefaultElementProducer.java

License:Open Source License

@Override
public PdfLayer startLayerInGroup(String groupId, PdfContentByte canvas) {
    PdfLayer pl;//www.ja va2  s  . c o  m
    try {
        pl = new PdfLayer(groupId + CHILD_LAYERSUFFIX, canvas.getPdfWriter());
    } catch (IOException ex) {
        throw new VectorPrintRuntimeException(ex);
    }
    initLayerGroup(groupId, canvas).addChild(pl);
    canvas.beginLayer(pl);
    return pl;
}

From source file:com.vectorprint.report.itext.DefaultElementProducer.java

License:Open Source License

@Override
public PdfLayer initLayerGroup(String layerId, PdfContentByte canvas) {
    if (!layerGroups.containsKey(layerId)) {
        PdfLayer parent;/*  w ww  .jav a2s. c  o  m*/
        try {
            parent = new PdfLayer(layerId, canvas.getPdfWriter());
        } catch (IOException ex) {
            throw new VectorPrintRuntimeException(ex);
        }
        layerGroups.put(layerId, parent);
        canvas.beginLayer(parent);
        canvas.endLayer();
    }
    return layerGroups.get(layerId);
}

From source file:org.orbisgis.core_export.GeoSpatialPDF.java

License:Open Source License

/**
 * Process all layers in the layermodel and build the corresponding pdf tree
 * layer//from   w  ww. j a  v  a  2 s.  c om
 *
 * @param layer
 */
private void processSubLayer(ILayer layer, MapTransform mt, PdfWriter writer, PdfContentByte cb,
        ProgressMonitor pm, PdfLayer mainLayer) {
    if (layer.acceptsChilds() && layer.getLayerCount() > 0) {
        PdfLayer groupLayer = new PdfLayer(layer.getName(), writer);
        //To manage group of layers
        for (ILayer subLayer : layer.getChildren()) {
            processSubLayer(subLayer, mt, writer, cb, pm, groupLayer);
        }

    } else {
        if (layer.isVisible() && layer.getEnvelope().intersects(mt.getAdjustedExtent())) {
            PdfLayer mapLayer = new PdfLayer(layer.getName(), writer);
            if (mainLayer != null) {
                mainLayer.addChild(mapLayer);
            }
            PdfTemplate layerTemplate = cb.createTemplate(width, height);
            Graphics2D g2dLayer = layerTemplate.createGraphics(width, height);
            cb.beginLayer(mapLayer);
            PdfRenderer renderer2 = new PdfRenderer(layerTemplate, width, height);
            renderer2.draw(mt, g2dLayer, (int) width, (int) height, layer, pm);
            cb.addTemplate(layerTemplate, 0, 0);
            g2dLayer.dispose();
            cb.endLayer();
        }
    }
}

From source file:org.orbisgis.mapcomposer.controller.utils.exportThreads.ExportPDFThread.java

License:Open Source License

@Override
public void run() {
    try {//  ww w  .j  a va2  s  .c o m
        Document pdfDocument = null;
        //Find the Document GE to create the BufferedImage where all the GE will be drawn
        for (GraphicalElement ge : geIsVectorMap.keySet()) {
            if (ge instanceof org.orbisgis.mapcomposer.model.graphicalelement.element.Document) {
                pdfDocument = new Document(new Rectangle(ge.getWidth(), ge.getHeight()));
                height = ge.getHeight();
            }
        }
        //If no Document was created, throw an exception
        if (pdfDocument == null) {
            throw new IllegalArgumentException(i18n.tr(
                    "Error on export : The list of GraphicalElement to export does not contain any Document GE."));
        }
        //Open the document
        PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(path));
        writer.setUserProperties(true);
        writer.setRgbTransparencyBlending(true);
        writer.setTagged();
        pdfDocument.open();

        PdfContentByte cb = writer.getDirectContent();

        progressBar.setIndeterminate(true);
        progressBar.setStringPainted(true);
        progressBar.setString(i18n.tr("Exporting the document ..."));

        int geCount = 0;
        int numberOfGe[] = new int[geManager.getRegisteredGEClasses().size()];
        for (int i = 0; i < numberOfGe.length; i++) {
            numberOfGe[i] = 0;
        }
        //Draw each GraphicalElement in the BufferedImage
        for (GraphicalElement ge : geStack) {
            if ((ge instanceof org.orbisgis.mapcomposer.model.graphicalelement.element.Document))
                continue;

            double rad = Math.toRadians(ge.getRotation());
            double newHeight = Math.abs(sin(rad) * ge.getWidth()) + Math.abs(cos(rad) * ge.getHeight());
            double newWidth = Math.abs(sin(rad) * ge.getHeight()) + Math.abs(cos(rad) * ge.getWidth());

            int maxWidth = Math.max((int) newWidth, ge.getWidth());
            int maxHeight = Math.max((int) newHeight, ge.getHeight());

            String layerName = ge.getGEName()
                    + numberOfGe[geManager.getRegisteredGEClasses().indexOf(ge.getClass())];

            if (geIsVectorMap.get(ge)) {
                PdfTemplate pdfTemplate = cb.createTemplate(maxWidth, maxHeight);
                Graphics2D g2dTemplate = pdfTemplate.createGraphics(maxWidth, maxHeight);
                PdfLayer layer = new PdfLayer(layerName, writer);
                cb.beginLayer(layer);
                ((RendererVector) geManager.getRenderer(ge.getClass())).drawGE(g2dTemplate, ge);
                cb.addTemplate(pdfTemplate, ge.getX() + (ge.getWidth() - maxWidth) / 2,
                        -ge.getY() + height - ge.getHeight() + (ge.getHeight() - maxHeight) / 2);
                g2dTemplate.dispose();
                cb.endLayer();
            }

            else {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                BufferedImage bi = ((RendererRaster) geManager.getRenderer(ge.getClass())).createGEImage(ge,
                        null);
                ImageIO.write(bi, "png", baos);
                Image image = Image.getInstance(baos.toByteArray());
                image.setAbsolutePosition(ge.getX() + (ge.getWidth() - maxWidth) / 2,
                        -ge.getY() + height - ge.getHeight() + (ge.getHeight() - maxHeight) / 2);

                PdfTemplate pdfTemplate = cb.createTemplate(maxWidth, maxHeight);
                Graphics2D g2dTemplate = pdfTemplate.createGraphics(maxWidth, maxHeight);
                PdfLayer layer = new PdfLayer(layerName, writer);
                cb.beginLayer(layer);
                g2dTemplate.drawImage(bi, 0, 0, null);
                cb.addTemplate(pdfTemplate, ge.getX() + (ge.getWidth() - maxWidth) / 2,
                        -ge.getY() + height - ge.getHeight() + (ge.getHeight() - maxHeight) / 2);
                g2dTemplate.dispose();
                cb.endLayer();
            }
            numberOfGe[geManager.getRegisteredGEClasses().indexOf(ge.getClass())]++;

            progressBar.setIndeterminate(false);
            progressBar.setValue((geCount * 100) / geIsVectorMap.keySet().size());
            progressBar.revalidate();
            geCount++;
        }

        pdfDocument.close();
        //Wait a bit before erasing the progress bar
        progressBar.setValue(progressBar.getMaximum());
        progressBar.setString(i18n.tr("Document successfully exported."));
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            LoggerFactory.getLogger(ExportPDFThread.class).error(e.getMessage());
        }
        progressBar.setValue(0);
        progressBar.setStringPainted(false);

    } catch (IllegalArgumentException | IOException | DocumentException ex) {
        LoggerFactory.getLogger(ExportPDFThread.class).error(ex.getMessage());
    }
}