PdfUtils { public static Properties readMetadata(byte[] rawBytes, String defaultTitle, String geometryServiceUrl) throws IOException { Properties ret = new Properties(); try (PDDocument document = PDDocument.load(rawBytes)) { if (!document.isEncrypted()) { PDDocumentInformation info = document.getDocumentInformation(); if (info != null) { if (info.getTitle() != null) { ret.put(PROP_TITLE, info.getTitle()); } else { ret.put(PROP_TITLE, defaultTitle); } if (info.getSubject() != null) { ret.put(PROP_SUBJECT, info.getSubject()); } else { StringBuilder psudoSubject = new StringBuilder(""); psudoSubject.append("\nAuthor: " + info.getAuthor()); psudoSubject.append("\nCreator: " + info.getCreator()); psudoSubject.append("\nProducer: " + info.getProducer()); ret.put(PROP_SUBJECT, psudoSubject.toString()); } if (info.getModificationDate() != null) { ret.put(PROP_MODIFICATION_DATE, info.getModificationDate().getTime()); } else if (info.getCreationDate() != null) { ret.put(PROP_MODIFICATION_DATE, info.getCreationDate().getTime()); } } else { LOG.warn("Got null metadata for PDF file"); return null; } COSObject measure = document.getDocument().getObjectByType(COSName.getPDFName("Measure")); String bBox = null; if (measure != null) { COSDictionary dictionary = (COSDictionary) measure.getObject(); float[] coords = ((COSArray) dictionary.getItem("GPTS")).toFloatArray(); bBox = generateBbox(coords); } else { PDPage page = document.getPage(0); if (page.getCOSObject().containsKey(COSName.getPDFName("LGIDict"))) { bBox = extractGeoPDFProps(page, geometryServiceUrl); } } if (bBox != null) { ret.put(PROP_BBOX, bBox); } } else { LOG.warn("Cannot read encrypted PDF file"); return null; } } catch (IOException ex) { LOG.error("Exception reading PDF", ex); throw ex; } return ret; }  static byte[] generateMetadataXML(byte[] pdfBytes, String fileName, String url); static byte[] generateMetadataXML(byte[] pdfBytes, String fileName, String url, String geometryServiceUrl); static Properties readMetadata(byte[] rawBytes, String defaultTitle, String geometryServiceUrl); static final String PROP_TITLE; static final String PROP_SUBJECT; static final String PROP_MODIFICATION_DATE; static final String PROP_BBOX; }
@Test public void testReadMetadata_readGeoPDF_UT () throws Exception { byte[] bytes = getBytes("src/test/data/geoeyeuscapitalimage.pdf"); Properties props = PdfUtils.readMetadata(bytes, "defaultTitle", DEFAULT_GEOM); assertNotNull("null value returned", props); assertEquals("wrong title", "defaultTitle", props.getProperty(PdfUtils.PROP_TITLE)); assertEquals("wrong description", "\nAuthor: null\nCreator: Adobe Acrobat 9.0\nProducer: Adobe Acrobat 9.0 Image Conversion Plug-in", props.getProperty(PdfUtils.PROP_SUBJECT)); assertEquals("Wrong bbox", "-77.05301318703646 38.88323180425436, -77.00746902033382 38.898891017940734", props.getProperty(PdfUtils.PROP_BBOX)); }
@Test public void testReadMetadata_readGeospatialPDF() throws Exception { byte[] bytes = getBytes("src/test/data/MANAmap1.pdf"); Properties props = PdfUtils.readMetadata(bytes, "defaultTitle", DEFAULT_GEOM); assertNotNull("null value returned", props); assertEquals("wrong title", "MANAmap1", props.getProperty(PdfUtils.PROP_TITLE)); assertEquals("wrong description", "\nAuthor: null\nCreator: Adobe Illustrator CS5\nProducer: Adobe PDF library 9.90", props.getProperty(PdfUtils.PROP_SUBJECT)); assertEquals("Wrong bbox", "-77.54026794433594 38.815128326416016, -77.5213394165039 38.82757568359375", props.getProperty(PdfUtils.PROP_BBOX)); }