List of usage examples for org.apache.pdfbox.cos COSDictionary containsKey
public boolean containsKey(String name)
From source file:com.esri.geoportal.commons.pdf.PdfUtils.java
License:Apache License
/** * Extracts the geospatial metadata from a GeoPDF * /*from ww w . j a v a2s .co m*/ * @param page the PDF page to read geospatial metadata from * @param geometryServiceUrl url of a <a href="https://developers.arcgis.com/rest/services-reference/geometry-service.htm">geometry service</a> for reprojecting coordinates. * * @see <a href="https://www.loc.gov/preservation/digital/formats/fdd/fdd000312.shtml">Library of Congress information on GeoPDF</a> * @see <a href="https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf">The PDF specification</a>, section 8, for instructions for translating coordinates. * * @returns the bounding box of the GeoPDF as "yMin xMin, yMax xMax" */ private static String extractGeoPDFProps(PDPage page, String geometryServiceUrl) { // The LGI dictionary is an array, we'll loop through all entries and pull the first one for a bounding box COSArray lgi = (COSArray) page.getCOSObject().getDictionaryObject("LGIDict"); List<String> bBoxes = new ArrayList<>(); lgi.iterator().forEachRemaining(item -> { String currentBbox = null; // Set up the Coordinate Transformation Matrix (used to translate PDF coords to geo coords) Double[][] ctmValues = null; COSDictionary dictionary = (COSDictionary) item; if (dictionary.containsKey("CTM")) { ctmValues = new Double[3][3]; // The last column in the matrix is always constant ctmValues[0][2] = 0.0; ctmValues[1][2] = 0.0; ctmValues[2][2] = 1.0; COSArray ctm = (COSArray) dictionary.getDictionaryObject("CTM"); for (int i = 0; i < ctm.toList().size(); i += 2) { int ctmRow = i / 2; ctmValues[ctmRow][0] = Double.parseDouble(((COSString) ctm.get(i)).getString()); ctmValues[ctmRow][1] = Double.parseDouble(((COSString) ctm.get(i + 1)).getString()); } } // Get the neatline (i.e. the bounding box in *PDF* coordinates) Double[][] neatLineValues = null; int neatLineLength = 0; if (dictionary.containsKey("Neatline")) { COSArray neatline = (COSArray) dictionary.getDictionaryObject("Neatline"); neatLineLength = neatline.toList().size(); neatLineValues = new Double[neatLineLength / 2][3]; for (int i = 0; i < neatline.toList().size(); i += 2) { int neatLineRow = i / 2; neatLineValues[neatLineRow][0] = Double.parseDouble(((COSString) neatline.get(i)).getString()); neatLineValues[neatLineRow][1] = Double .parseDouble(((COSString) neatline.get(i + 1)).getString()); neatLineValues[neatLineRow][2] = 1.0; } } // Translate the PDF coordinates to Geospatial coordintates by multiplying the two matricies MultiPoint mp = new MultiPoint(); if (ctmValues != null && neatLineValues != null) { Double[][] resultCoords = new Double[neatLineLength / 2][3]; for (int z = 0; z < neatLineLength / 2; z++) { for (int i = 0; i < 3; i++) { resultCoords[z][i] = neatLineValues[z][0] * ctmValues[0][i] + neatLineValues[z][1] * ctmValues[1][i] + neatLineValues[z][2] * ctmValues[2][i]; } mp.add(resultCoords[z][0], resultCoords[z][1]); } } // Project the geospatial coordinates to WGS84 for the Dublin-Core metadata if (dictionary.containsKey("Projection")) { COSDictionary projectionDictionary = (COSDictionary) dictionary.getDictionaryObject("Projection"); String projectionType = projectionDictionary.getString("ProjectionType"); try (GeometryService svc = new GeometryService(HttpClients.custom().useSystemProperties().build(), new URL(geometryServiceUrl));) { // UTM projections require slightly different processing if ("UT".equals(projectionType)) { String zone = Integer.toString(projectionDictionary.getInt("Zone")); String hemisphere = projectionDictionary.getString("Hemisphere"); // Get the wkt for the geospatial coordinate system String wkt = datumTranslation(projectionDictionary.getItem("Datum")); if (zone != null && hemisphere != null && wkt != null) { // Generate a list of UTM strings List<String> utmCoords = new ArrayList<>(); for (Point2D pt : mp.getCoordinates2D()) { String coord = String.format("%s%s %s %s", zone, hemisphere, Math.round(pt.x), Math.round(pt.y)); utmCoords.add(coord); } MultiPoint reproj = svc.fromGeoCoordinateString(utmCoords, WGS84_WKID); currentBbox = generateBbox(reproj); } else { LOG.warn("Missing UTM argument: zone: {}, hemisphere: {}, datum: {}", zone, hemisphere, wkt); LOG.debug("Projection dictionary {}", projectionDictionary); } } else { // Generate Well Known Text for projection and re-projects the points to WGS 84 String wkt = getProjectionWKT(projectionDictionary, projectionType); if (wkt != null) { MultiPoint reproj = svc.project(mp, wkt, WGS84_WKID); currentBbox = generateBbox(reproj); } else if (LOG.isDebugEnabled()) { // Print out translated coordinates for debugging purposes LOG.debug("Translated Coordinates"); for (Point2D pt : mp.getCoordinates2D()) { LOG.debug(String.format("\t%s, %s", pt.x, pt.y)); } } } } catch (Exception e) { // If something goes wrong, just try the next set of coordinates LOG.error("Exception reprojecting geometry, skipping this geopdf dictionary instance...", e); } } if (currentBbox != null) { bBoxes.add(currentBbox); } }); return bBoxes.get(0); }
From source file:com.esri.geoportal.commons.pdf.PdfUtils.java
License:Apache License
/** * Generates the list of "PARAMETER" entries in the WKT. * /*from w w w . jav a2 s . c o m*/ * @param projectionDictionary the GeoPDF projection dictionary * * @returns string of WKT parameters */ private static String generateWKTParameters(COSDictionary projectionDictionary) throws IOException { // Set up the projection parameters Properties parameters = new Properties(); COSDictionaryMap<String, Object> dictionaryMap = COSDictionaryMap .convertBasicTypesToMap(projectionDictionary); if (projectionDictionary.containsKey("CentralMeridian")) { parameters.put("Central_Meridian", (String) dictionaryMap.get("CentralMeridian")); } if (projectionDictionary.containsKey("OriginLatitude")) { parameters.put("Latitude_Of_Origin", (String) dictionaryMap.get("OriginLatitude")); } if (projectionDictionary.containsKey("StandardParallelOne")) { parameters.put("Standard_Parallel_1", (String) dictionaryMap.get("StandardParallelOne")); } if (projectionDictionary.containsKey("StandardParallelTwo")) { parameters.put("Standard_Parallel_2", (String) dictionaryMap.get("StandardParallelTwo")); } if (projectionDictionary.containsKey("FalseEasting")) { parameters.put("False_Easting", (String) dictionaryMap.get("FalseEasting")); } if (projectionDictionary.containsKey("FalseNorthing")) { parameters.put("False_Northing", (String) dictionaryMap.get("FalseNorthing")); } if (projectionDictionary.containsKey("ScaleFactor")) { parameters.put("Scale_Factor", (String) dictionaryMap.get("ScaleFactor")); } return parameters.entrySet().stream() .map(entry -> "PARAMETER[\"" + entry.getKey() + "\", " + entry.getValue() + "]") .collect(Collectors.joining(",")); }
From source file:org.apache.fop.render.pdf.pdfbox.DocumentRootModifier.java
License:Apache License
private void checkForMap(COSDictionary structRootDict, String mapName) throws IOException { if (structRootDict.containsKey(mapName)) { COSDictionary addedMapDict = (COSDictionary) structRootDict.getDictionaryObject(mapName); PDFDictionary temp = (PDFDictionary) adapter.cloneForNewDocument(addedMapDict); PDFStructTreeRoot structTreeRoot = pdfDoc.getRoot().getStructTreeRoot(); if (!structTreeRoot.containsKey(mapName)) { structTreeRoot.put(mapName, temp); } else {//from w ww. ja v a 2 s.com PDFDictionary rootMap = (PDFDictionary) structTreeRoot.get(mapName); addMapToStructTreeRoot(rootMap, temp, mapName); } } }
From source file:org.apache.fop.render.pdf.pdfbox.PageParentTreeFinder.java
License:Apache License
private COSArray traverseParentTree(COSDictionary numberTreeNodeDict, int position) { COSArray numberTree;/*ww w.ja v a 2 s . c o m*/ COSArray parentTree; List<COSArray> nums = new ArrayList<COSArray>(); if (numberTreeNodeDict.containsKey(COSName.NUMS)) { numberTree = (COSArray) numberTreeNodeDict.getItem(COSName.NUMS); return extractMarkedContentParents(numberTree, position); } else { parentTree = (COSArray) numberTreeNodeDict.getDictionaryObject(COSName.KIDS); traverseKids(parentTree, position, nums); } return nums.get(0); }
From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java
License:Apache License
private boolean checkPageEntryInAncestorsRecursively(COSDictionary elem) { if (elem.containsKey(COSName.PG)) { COSDictionary pageDict = (COSDictionary) elem.getDictionaryObject(COSName.PG); return srcPage.getCOSObject() == pageDict; } else if (elem.containsKey(COSName.P)) { COSDictionary parent = (COSDictionary) elem.getDictionaryObject(COSName.P); return checkPageEntryInAncestorsRecursively(parent); } else {/* ww w . j a v a2 s.c o m*/ return true; } }
From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java
License:Apache License
private boolean isElementFromSourcePage(COSDictionary mrcDict, COSDictionary parentDict) { if (mrcDict.containsKey(COSName.PG)) { COSDictionary page = (COSDictionary) mrcDict.getDictionaryObject(COSName.PG); return srcPage.getCOSObject() == page; } else {//from ww w . jav a 2 s. c o m return checkPageEntryInAncestorsRecursively(parentDict); } }