Example usage for com.itextpdf.text.pdf PdfWriter setUserProperties

List of usage examples for com.itextpdf.text.pdf PdfWriter setUserProperties

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfWriter setUserProperties.

Prototype

public void setUserProperties(final boolean userProperties) 

Source Link

Document

Sets the flag indicating the presence of structure elements that contain user properties attributes.

Usage

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

License:Open Source License

/**
 * Create the PDF document and a the geospatial tags
 *
 * @param out//from   w ww  .j  a va2 s. c  om
 * @param mt
 * @param pm
 * @throws IOException
 */
public void createPDF(OutputStream out, MapTransform mt, ProgressMonitor pm) throws IOException {
    Document document = new Document(new Rectangle(width, height));
    try {
        PdfWriter writer = PdfWriter.getInstance(document, out);
        writer.setTagged();
        writer.setUserProperties(true);
        document.open();

        PdfContentByte cb = writer.getDirectContent();

        int numLayers = rootLayer.getLayerCount();
        for (int i = numLayers - 1; i >= 0; i--) {
            ILayer layer = rootLayer.getLayer(i);
            processSubLayer(layer, mt, writer, cb, pm, null);
        }
        georefPdf(writer, mt);

    } catch (DocumentException ex) {
        throw new IOException("Cannot create the pdf", ex);
    }
    document.close();
}

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

License:Open Source License

@Override
public void run() {
    try {//from w  w w . ja va  2s . co  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());
    }
}