Example usage for org.apache.pdfbox.cos COSDictionary getObjectFromPath

List of usage examples for org.apache.pdfbox.cos COSDictionary getObjectFromPath

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSDictionary getObjectFromPath.

Prototype

public COSBase getObjectFromPath(String objPath) 

Source Link

Document

Nice method, gives you every object you want Arrays works properly too.

Usage

From source file:ddf.catalog.transformer.input.pdf.GeoPdfParserImpl.java

License:Open Source License

/**
 * Generates a WKT compliant String from a PDF Document if it contains GeoPDF information.
 * Currently, only WGS84 Projections are supported (GEOGRAPHIC GeoPDF ProjectionType).
 *
 * @param pdfDocument - The PDF document
 * @return the WKT String//from  w  ww.  j a v  a  2s.co  m
 * @throws IOException
 */
@Override
public String apply(PDDocument pdfDocument) throws IOException {
    ToDoubleVisitor toDoubleVisitor = new ToDoubleVisitor();
    LinkedList<String> polygons = new LinkedList<>();

    for (PDPage pdPage : pdfDocument.getPages()) {
        COSDictionary cosObject = pdPage.getCOSObject();

        COSBase lgiDictObject = cosObject.getObjectFromPath(LGIDICT);

        // Handle Multiple Map Frames
        if (lgiDictObject instanceof COSArray) {
            for (int i = 0; i < ((COSArray) lgiDictObject).size(); i++) {
                COSDictionary lgidict = (COSDictionary) cosObject.getObjectFromPath(LGIDICT + "/[" + i + "]");

                COSDictionary projectionArray = (COSDictionary) lgidict.getDictionaryObject(PROJECTION);
                if (projectionArray != null) {
                    String projectionType = ((COSString) projectionArray.getItem(PROJECTION_TYPE)).getString();
                    if (GEOGRAPHIC.equals(projectionType)) {
                        COSArray neatlineArray = (COSArray) cosObject
                                .getObjectFromPath(LGIDICT + "/[" + i + "]/" + NEATLINE);
                        String wktString = getWktFromNeatLine(lgidict, neatlineArray, toDoubleVisitor);
                        polygons.add(wktString);
                    } else {
                        LOGGER.debug("Unsupported projection type {}.  Map Frame will be skipped.",
                                projectionType);
                    }
                } else {
                    LOGGER.debug("No projection array found on the map frame.  Map Frame will be skipped.");
                }
            }
            // Handle One Map Frame
        } else if (lgiDictObject instanceof COSDictionary) {
            COSDictionary lgidict = (COSDictionary) lgiDictObject;
            COSDictionary projectionArray = (COSDictionary) lgidict.getDictionaryObject(PROJECTION);
            if (projectionArray != null) {
                String projectionType = ((COSString) projectionArray.getItem(PROJECTION_TYPE)).getString();
                if (GEOGRAPHIC.equals(projectionType)) {
                    COSArray neatlineArray = (COSArray) cosObject.getObjectFromPath(LGIDICT + "/" + NEATLINE);
                    if (neatlineArray == null) {
                        neatlineArray = generateNeatLineFromPDFDimensions(pdPage);

                    }
                    polygons.add(getWktFromNeatLine(lgidict, neatlineArray, toDoubleVisitor));

                } else {
                    LOGGER.debug("Unsupported projection type {}.  Map Frame will be skipped.", projectionType);
                }
            } else {
                LOGGER.debug("No projection array found on the map frame.  Map Frame will be skipped.");
            }
        }
    }

    if (polygons.size() == 0) {
        LOGGER.debug(
                "No GeoPDF information found on PDF during transformation.  Metacard location will not be set.");
        return null;
    }

    if (polygons.size() == 1) {
        return POLYGON + polygons.get(0) + "))";
    } else {
        return polygons.stream().map(polygon -> "((" + polygon + "))")
                .collect(Collectors.joining(",", MULTIPOLYGON, ")"));
    }
}

From source file:ddf.catalog.transformer.input.pdf.GeoPdfParserImpl.java

License:Open Source License

/**
 * Parses a given NeatLine and Transformation matrix into a WKT String
 *
 * @param lgidict         - The PDF's LGIDict object
 * @param neatLineArray   - The NeatLine array of points for the PDF
 * @param toDoubleVisitor - A visitor that converts PDF Strings / Ints / Longs into doubles.
 * @return the generated WKT Lat/Lon set
 * @throws IOException/*  w ww  .  j  a va2  s.  com*/
 */
private String getWktFromNeatLine(COSDictionary lgidict, COSArray neatLineArray, ICOSVisitor toDoubleVisitor)
        throws IOException {
    List<Double> neatline = new LinkedList<>();
    List<String> coordinateList = new LinkedList<>();
    String firstCoordinate = null;

    double[] points = new double[CTM_SIZE];
    for (int i = 0; i < CTM_SIZE; i++) {
        points[i] = (Double) lgidict.getObjectFromPath(CTM + "/[" + i + "]").accept(toDoubleVisitor);
    }
    AffineTransform affineTransform = new AffineTransform(points);

    for (int i = 0; i < neatLineArray.size(); i++) {
        neatline.add((Double) neatLineArray.get(i).accept(toDoubleVisitor));
    }

    for (int i = 0; i < neatline.size(); i += 2) {
        double x = neatline.get(i);
        double y = neatline.get(i + 1);

        Point2D p = new Point2D.Double(x, y);

        Point2D pprime = affineTransform.transform(p, null);

        String xySet = point2dToWkt(pprime);

        if (firstCoordinate == null) {
            firstCoordinate = xySet;
        }
        coordinateList.add(xySet);
    }
    coordinateList.add(firstCoordinate);
    String wktString = StringUtils.join(coordinateList, ", ");
    LOGGER.debug("{}", wktString);
    return wktString.toString();
}