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

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

Introduction

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

Prototype

public PdfArray() 

Source Link

Document

Constructs an empty PdfArray-object.

Usage

From source file:at.laborg.briss.CropManager.java

License:Open Source License

private static void cropMultipliedFile(File source, CropJob cropJob)
        throws FileNotFoundException, DocumentException, IOException {

    PdfReader reader = new PdfReader(source.getAbsolutePath());
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(cropJob.getDestinationFile()));
    stamper.setMoreInfo(cropJob.getSourceMetaInfo());

    PdfDictionary pageDict;//  ww  w. ja  v  a2s  . c o  m
    int newPageNumber = 1;
    for (int origPageNumber = 1; origPageNumber <= cropJob.getSourcePageCount(); origPageNumber++) {
        SingleCluster cluster = cropJob.getClusterCollection().getSingleCluster(origPageNumber);

        // if no crop was selected do nothing
        if (cluster.getRatiosList().size() == 0) {
            newPageNumber++;
            continue;
        }

        for (Float[] ratios : cluster.getRatiosList()) {

            pageDict = reader.getPageN(newPageNumber);

            List<Rectangle> boxes = new ArrayList<Rectangle>();
            boxes.add(reader.getBoxSize(newPageNumber, "media"));
            boxes.add(reader.getBoxSize(newPageNumber, "crop"));
            int rotation = reader.getPageRotation(newPageNumber);

            Rectangle scaledBox = calculateScaledRectangle(boxes, ratios, rotation);

            PdfArray scaleBoxArray = new PdfArray();
            scaleBoxArray.add(new PdfNumber(scaledBox.getLeft()));
            scaleBoxArray.add(new PdfNumber(scaledBox.getBottom()));
            scaleBoxArray.add(new PdfNumber(scaledBox.getRight()));
            scaleBoxArray.add(new PdfNumber(scaledBox.getTop()));

            pageDict.put(PdfName.CROPBOX, scaleBoxArray);
            pageDict.put(PdfName.MEDIABOX, scaleBoxArray);
            // increment the pagenumber
            newPageNumber++;
        }
        int[] range = new int[2];
        range[0] = newPageNumber - 1;
        range[1] = cropJob.getSourcePageCount() + (newPageNumber - origPageNumber);
        SimpleBookmark.shiftPageNumbers(cropJob.getSourceBookmarks(), cluster.getRatiosList().size() - 1,
                range);
    }
    stamper.setOutlines(cropJob.getSourceBookmarks());
    stamper.close();
    reader.close();
}

From source file:at.laborg.briss.utils.DocumentCropper.java

License:Open Source License

private static PdfArray createScaledBoxArray(final Rectangle scaledBox) {
    PdfArray scaleBoxArray = new PdfArray();
    scaleBoxArray.add(new PdfNumber(scaledBox.getLeft()));
    scaleBoxArray.add(new PdfNumber(scaledBox.getBottom()));
    scaleBoxArray.add(new PdfNumber(scaledBox.getRight()));
    scaleBoxArray.add(new PdfNumber(scaledBox.getTop()));
    return scaleBoxArray;
}

From source file:io.konik.carriage.itext.ITextInvoiceAppender.java

License:Open Source License

private static void attachFile(byte[] attachmentFile, PdfAStamper stamper) throws IOException {
    PdfDictionary embeddedFileParams = new PdfDictionary();
    embeddedFileParams.put(PARAMS, new PdfName(ZF_FILE_NAME));
    embeddedFileParams.put(MODDATE, new PdfDate());

    PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(stamper.getWriter(), null, ZF_FILE_NAME,
            attachmentFile, MIME_TYPE, embeddedFileParams, 0);
    fs.put(AFRELATIONSHIP, Alternative);
    stamper.addFileAttachment(ZF_FILE_NAME, fs);

    PdfArray array = new PdfArray();
    array.add(fs.getReference());/*from  w  w w  . j  a  v  a2  s  .co m*/
    stamper.getWriter().getExtraCatalog().put(new PdfName("AF"), array);
}

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

License:Open Source License

@Override
public void beginFeature(long id, DataSource sds) {
    int fieldNameIndex;
    try {//from  ww  w.j  a  v a  2  s .  c om
        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 ww w  .  ja v  a 2s .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 {// www. j av  a 2s  .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);
    }
}