Example usage for com.itextpdf.text.pdf PdfStructureElement PdfStructureElement

List of usage examples for com.itextpdf.text.pdf PdfStructureElement PdfStructureElement

Introduction

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

Prototype

public PdfStructureElement(PdfStructureTreeRoot root, PdfName structureType) 

Source Link

Document

Creates a new instance of PdfStructureElement.

Usage

From source file:org.orbisgis.core.renderer.PdfRendererWithAttributes.java

License:Open Source License

@Override
public void beginFeature(long id, DataSource sds) {
    int fieldNameIndex;
    try {// w  ww . j  av a2s .  co  m
        fieldNameIndex = sds.getFieldIndexByName(fieldName);
    } catch (DriverException ex) {
        Logger.getLogger(PdfRendererWithAttributes.class.getName()).log(Level.SEVERE, null, ex);
        Logger.getLogger("Field name used for attributes does not exist: by default, we use feature + id");
        fieldNameIndex = -1;

    }

    try {
        String attributeName;

        if (fieldNameIndex > -1) {
            attributeName = sds.getFieldValue(id, fieldNameIndex).toString();
        } else {
            attributeName = "feature " + (id + 1);
        }

        PdfStructureElement e = new PdfStructureElement(top, new PdfName(attributeName));
        PdfDictionary userProperties = new PdfDictionary();
        userProperties.put(PdfName.O, PdfName.USERPROPERTIES);
        PdfArray properties = new PdfArray();

        for (int i = 0; i < sds.getFieldCount(); i++) {
            if ((sds.getFieldType(i).getTypeCode() & Type.GEOMETRY) == 0) {
                PdfDictionary property = new PdfDictionary();
                property.put(PdfName.N, new PdfString(sds.getFieldName(i)));
                Value v = sds.getFieldValue(id, i);
                property.put(PdfName.V, new PdfString(v.toString()));
                properties.add(property);
            }
        }

        userProperties.put(PdfName.P, properties);
        e.put(PdfName.A, userProperties);

        pTemp = cb.createTemplate(width, height);
        cb.beginMarkedContentSequence(e);

    } catch (DriverException ex) {
        Logger.getLogger(PdfRendererWithAttributes.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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

License:Open Source License

/**
 * This method is used to georeference the pdf.
 * Note : The CRS is not yet supported.//from   www.  ja  va 2 s.  c o  m
 *
 * @param writer
 * @param mt
 * @throws IOException
 * @throws DocumentException
 */
public void georefPdf(PdfWriter writer, MapTransform mt) throws IOException, DocumentException {

    PdfStructureTreeRoot tree = writer.getStructureTreeRoot();

    //the part of the document where maps are displayed
    float mapWidth = width;
    float mapHeight = height;
    float mapLX = 0;
    float mapLY = 0;

    //ViewPort Dictionary
    PdfDictionary viewPortDict = new PdfDictionary();
    viewPortDict.put(PdfName.TYPE, new PdfName("Viewport"));
    viewPortDict.put(PdfName.BBOX, new PdfRectangle(mapLX, mapLY, mapLX + mapWidth, mapLY + mapHeight));
    viewPortDict.put(PdfName.NAME, new PdfString("Layers"));

    //Measure dictionary
    PdfDictionary measureDict = new PdfDictionary();
    measureDict.put(PdfName.TYPE, new PdfName("Measure"));
    measureDict.put(PdfName.SUBTYPE, new PdfName("GEO"));

    //Bounds
    PdfArray bounds = new PdfArray();
    bounds.add(new PdfNumber(0));
    bounds.add(new PdfNumber(0));
    bounds.add(new PdfNumber(1));
    bounds.add(new PdfNumber(0));
    bounds.add(new PdfNumber(1));
    bounds.add(new PdfNumber(1));
    bounds.add(new PdfNumber(0));
    bounds.add(new PdfNumber(1));

    measureDict.put(new PdfName("Bounds"), bounds);

    //GPTS
    Envelope adjustedBbox = mt.getAdjustedExtent();

    if (!adjustedBbox.isNull()) {

        //ly lx ly ux uy ux uy lx
        PdfArray gptsTable = new PdfArray();
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinY()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinX()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinY()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxX()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxY()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxX()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxY()).toString()));
        gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinX()).toString()));

        measureDict.put(new PdfName("GPTS"), gptsTable);

        //The CRS will be added when the mapcontext will support it.

        //            //GCS Geospatial Coordinate system
        //            PdfDictionary gcsDict = new PdfDictionary();
        //            if (context.getBbox().getCrs() != null) {
        //
        //                if (context.getBbox().getCrs().getType() != null) {
        //                    gcsDict.put(PdfName.TYPE, new PdfName(context.getBbox().getCrs().getType()));
        //                } else {
        //                    LOGGER.warn("No type of crs : the pdf cannot be georeferenced");
        //                    return;
        //                }
        //
        //                if (context.getBbox().getCrs().getEpsg() != 0) {
        //                    gcsDict.put(new PdfName("EPSG"), new PdfNumber(context.getBbox().getCrs().getEpsg()));
        //                } else {
        //                    LOGGER.warn("No epsg : the pdf cannot be georeferenced");
        //                    return;
        //                }
        //
        //
        //            } else {
        //                LOGGER.warn("No crs :  the pdf cannot be georeferenced");
        //
        //            }
        //
        //            measureDict.put(new PdfName("GCS"), gcsDict);
    } else {
        LOGGER.warn("Envelope of bbox null : the pdf cannot be georeferenced");

    }

    //PDU : array of units
    PdfArray pdu = new PdfArray();
    pdu.add(new PdfName("KM"));
    pdu.add(new PdfName("SQKM"));
    pdu.add(new PdfName("DEG"));

    measureDict.put(new PdfName("PDU"), pdu);

    //LPTS
    PdfArray lptsTable = new PdfArray();
    lptsTable.add(new PdfNumber(0));
    lptsTable.add(new PdfNumber(0));
    lptsTable.add(new PdfNumber(1));
    lptsTable.add(new PdfNumber(0));
    lptsTable.add(new PdfNumber(1));
    lptsTable.add(new PdfNumber(1));
    lptsTable.add(new PdfNumber(0));
    lptsTable.add(new PdfNumber(1));

    measureDict.put(new PdfName("LPTS"), lptsTable);

    viewPortDict.put(new PdfName("Measure"), measureDict);

    PdfStructureElement top = new PdfStructureElement(tree, new PdfName("VP"));
    top.putAll(viewPortDict);
}

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

License:Open Source License

@Override
public void beginFeature(long id, ResultSet rs) {
    try {//  w w  w.  j  av a  2  s . c o  m
        String attributeName;
        attributeName = rs.getString(fieldName);
        PdfStructureElement e = new PdfStructureElement(top, new PdfName(attributeName));
        PdfDictionary userProperties = new PdfDictionary();
        userProperties.put(PdfName.O, PdfName.USERPROPERTIES);
        PdfArray properties = new PdfArray();
        SpatialResultSetMetaData metaData = rs.getMetaData().unwrap(SpatialResultSetMetaData.class);

        int geometryField = metaData.getFirstGeometryFieldIndex();
        PdfDictionary property = new PdfDictionary();
        property.put(PdfName.N, new PdfString(metaData.getColumnName(geometryField)));
        property.put(PdfName.V, new PdfString(rs.getString(geometryField)));
        properties.add(property);

        userProperties.put(PdfName.P, properties);
        e.put(PdfName.A, userProperties);

        pTemp = cb.createTemplate(width, height);
        cb.beginMarkedContentSequence(e);

    } catch (SQLException ex) {
        Logger.getLogger(PdfRendererWithAttributes.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:pdfpruefbericht.PdfPruefbericht.java

License:Open Source License

public static void main(String[] args)
        throws IOException, DocumentException, SAXException, ParserConfigurationException, URISyntaxException {

    String RESULT = args[1];//  www.jav  a2  s.co m
    String RESOURCE = args[0];
    ContentParser cp;
    float compression;

    try {
        compression = Float.parseFloat(args[2]);
    } catch (NumberFormatException e) {
        System.out.println("Kompresion konnte nicht geparsed werden ... : " + e.getMessage());
        compression = 0.5f;
    }

    try {
        Document document = new Document(PageSize.A4);
        document.setMargins(36, 72, 36, 144);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setTagged();
        document.open();
        PdfStructureTreeRoot root = writer.getStructureTreeRoot();
        root.mapRole(new PdfName("Report"), PdfName.ROOT);
        root.mapRole(new PdfName("Norm"), PdfName.SECT);
        root.mapRole(new PdfName("Test"), PdfName.SEPARATION);
        root.mapRole(new PdfName("Comment"), PdfName.CO);
        root.mapRole(new PdfName("img"), PdfName.PI);
        root.mapRole(new PdfName("log"), PdfName.L);
        root.mapRole(new PdfName("Setup"), PdfName.SETTINGS);
        root.mapRole(new PdfName("device"), PdfName.D);
        root.mapRole(new PdfName("parameter"), PdfName.P);

        PdfStructureElement top = new PdfStructureElement(root, new PdfName("Report"));
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        List<PdfStructureElement> elements = new ArrayList<>();
        cp = new ContentParser(document, writer, elements);
        parser.parse(new InputSource(new FileInputStream(RESOURCE)), new StructureParser(top, elements));
        parser.parse(new InputSource(new FileInputStream(RESOURCE)), cp);
        PdfBuilder pdf = new PdfBuilder(document, writer, cp.getReport(), compression);
        pdf.build();
        document.close();
    } catch (IOException | DocumentException | SAXException | ParserConfigurationException e) {
        System.out.println(e.getMessage());
    }
    //watermark watermark = new watermark();
}

From source file:xml.StructureParser.java

License:Open Source License

/**
 * @throws org.xml.sax.SAXException//w  ww.  ja v a2  s.co  m
 * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
 *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
 */
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    if ("Report".equals(qName))
        return;
    elements.add(new PdfStructureElement(top, new PdfName(qName)));
}