List of usage examples for org.apache.pdfbox.pdmodel PDDocument getDocumentCatalog
public PDDocumentCatalog getDocumentCatalog()
From source file:uk.ac.leeds.ccg.andyt.rdl.web.RDL_ParsePDF.java
private static XMPMetadata getXMPMetadata(PDDocument document) throws IOException { PDDocumentCatalog catalog = document.getDocumentCatalog(); //tPDPageNode. PDMetadata metaRaw = catalog.getMetadata(); if (metaRaw == null) { return null; }/* w ww . j ava 2 s.com*/ XMPMetadata meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream())); //meta.addXMLNSMapping(XMPSchemaBibtex.NAMESPACE, XMPSchemaBibtex.class); return meta; }
From source file:uk.ac.liverpool.thumbnails.PDFService.java
License:Open Source License
public BufferedImage generateThumb(URI u, File f, int w, int h, int pn) throws IOException { PDDocument document = getPages(u, f); List pages = document.getDocumentCatalog().getAllPages(); int pagen = document.getNumberOfPages(); int i = 0;/*from ww w .j a v a2s . co m*/ if (pn < pages.size()) i = pn; PDPage page = (PDPage) pages.get(i); PDRectangle mBox = page.findMediaBox(); float widthPt = mBox.getWidth(); float heightPt = mBox.getHeight(); float sx = widthPt / (float) w; float sy = heightPt / (float) h; BufferedImage bi = page.convertToImage(BufferedImage.TYPE_INT_ARGB, Math.round(72 / Math.max(sx, sy))); return bi; }
From source file:uk.ac.liverpool.thumbnails.PDFService.java
License:Open Source License
private void displaySVG(URI u, int i) throws MalformedURLException, IOException { PDDocument doc = getPages(u, null); List pages = doc.getDocumentCatalog().getAllPages(); int pagen = doc.getNumberOfPages(); PDPage page = (PDPage) pages.get(i); PDRectangle mBox = page.findMediaBox(); float widthPt = mBox.getWidth(); float heightPt = mBox.getHeight(); float sx = widthPt / (float) 600; float sy = heightPt / (float) 800; // Get a DOMImplementation DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Create an instance of org.w3c.dom.Document // String svgNS = "http://www.w3.org/2000/svg"; // org.w3c.dom.Document document = domImpl.createDocument(svgNS, "svg", // null); DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; document = (SVGDocument) impl.createDocument(svgNS, "svg", null); // Create an instance of the SVG Generator SVGGraphics2D svgGenerator = new SVGGraphics2D(document); svgGenerator.getGeneratorContext().setComment("Test"); svgGenerator.getGeneratorContext().setEmbeddedFontsOn(true); // Ask the test to render into the SVG Graphics2D implementation Dimension pageDimension = new Dimension((int) widthPt, (int) heightPt); svgGenerator.setBackground(new Color(255, 255, 255, 0)); svgGenerator.scale(sx, sy);/*from w ww .j a v a 2 s . c o m*/ svgGenerator.setSVGCanvasSize(pageDimension); PageDrawer drawer = new PageDrawer(); drawer.drawPage(svgGenerator, page, pageDimension); JSVGCanvas canvas = new JSVGCanvas(); JFrame f = new JFrame(); f.getContentPane().add(canvas); canvas.setSVGDocument(document); f.pack(); f.setVisible(true); }
From source file:uk.ac.liverpool.thumbnails.PDFService.java
License:Open Source License
@Override public FontInformation[] extractFontList(URI u, File fff) throws MalformedURLException, IOException { SortedSet<FontInformation> ret = new TreeSet<FontInformation>(); PDDocument document = getPages(u, fff); List pages = document.getDocumentCatalog().getAllPages(); int i = 0;//from w w w. j av a 2s .c o m // The code down here is easier as it gets all the fonts used in the document. Still, this would inlcude unused fonts, so we get the fonts page by page and add them to a Hash table. for (COSObject c : document.getDocument().getObjectsByType(COSName.FONT)) { if (c == null || !(c.getObject() instanceof COSDictionary)) continue; //System.out.println(c.getObject()); COSDictionary fontDictionary = (COSDictionary) c.getObject(); // System.out.println(dic.getNameAsString(COSName.BASE_FONT)); // } // } // int pagen = document.getNumberOfPages(); // i=0; // for (int p=0;p<pagen;p++){ // PDPage page = (PDPage)pages.get(p); // PDResources res = page.findResources(); // //for each page resources // if (res==null) continue; // // get the font dictionary // COSDictionary fonts = (COSDictionary) res.getCOSDictionary().getDictionaryObject( COSName.FONT ); // for( COSName fontName : fonts.keySet() ) { // COSObject font = (COSObject) fonts.getItem( fontName ); // // if the font has already been visited we ingore it // long objectId = font.getObjectNumber().longValue(); // if (ret.get(objectId)!=null) // continue; // if( font==null || ! (font.getObject() instanceof COSDictionary) ) // continue; // COSDictionary fontDictionary = (COSDictionary)font.getObject(); // Type MUSt be font if (!fontDictionary.getNameAsString(COSName.TYPE).equals("Font")) continue; // get the variables FontInformation fi = new FontInformation(); fi.fontType = fontDictionary.getNameAsString(COSName.SUBTYPE); String baseFont = fontDictionary.getNameAsString(COSName.BASE_FONT); if (baseFont == null) continue; if (Arrays.binarySearch(standard14, baseFont) >= 0) continue; COSDictionary fontDescriptor = (COSDictionary) fontDictionary.getDictionaryObject(COSName.FONT_DESC); COSBase enc = fontDictionary.getItem(COSName.ENCODING); COSBase uni = fontDictionary.getItem(COSName.TO_UNICODE); int firstChar = fontDictionary.getInt(COSName.FIRST_CHAR); int lastChar = fontDictionary.getInt(COSName.LAST_CHAR); String encoding; boolean toUnicode = uni != null; if (enc == null) { encoding = "standard14"; } if (enc instanceof COSString) { encoding = ((COSString) enc).getString(); } else { encoding = "table"; } fi.isSubset = false; boolean t = true; // Type one and TT can have subsets defineing the basename see 5.5.3 pdfref 1.6 // if (fi.fontType.lastIndexOf(COSName.TYPE1.getName())!=-1 || fi.fontType.equals(COSName.TRUE_TYPE.getName()) ) if (baseFont != null) { if (baseFont.length() > 6) { for (int k = 0; k < 6; k++) if (!Character.isUpperCase(baseFont.charAt(k))) t = false; if (baseFont.charAt(6) != '+') t = false; } else t = false; fi.isSubset = t; if (fi.isSubset) baseFont = baseFont.substring(7); } fi.fontFlags = 0; if (fi.fontType.equals(COSName.TYPE0) || fi.fontType.equals(COSName.TYPE3)) fi.isEmbedded = true; if (fontDescriptor != null) { // in Type1 charset indicates font is subsetted if (fontDescriptor.getItem(COSName.CHAR_SET) != null) fi.isSubset = true; if (fontDescriptor.getItem(COSName.FONT_FILE) != null || fontDescriptor.getItem(COSName.FONT_FILE3) != null || fontDescriptor.getItem(COSName.FONT_FILE2) != null) fi.isEmbedded = true; fi.fontFlags = fontDescriptor.getInt(COSName.getPDFName("Flags")); fi.fontFamily = fontDescriptor.getString(COSName.FONT_FAMILY); fi.fontStretch = fontDescriptor.getString(COSName.FONT_STRETCH); } fi.charset = encoding; fi.fontName = baseFont; fi.isToUnicode = toUnicode; ret.add(fi); } // for all fonts // } // for all pages Iterator<FontInformation> it = ret.iterator(); FontInformation prev = null; LinkedList<FontInformation> toDelete = new LinkedList<FontInformation>(); while (it.hasNext()) { FontInformation current = it.next(); if (prev != null && prev.fontName.equals(current.fontName) && prev.fontType.startsWith("CIDFontType")) toDelete.add(current); prev = current; } ret.removeAll(toDelete); FontInformation[] retArray = ret.toArray(new FontInformation[0]); return retArray; }
From source file:uk.ac.liverpool.thumbnails.PDFService.java
License:Open Source License
@Override public void generateSVG(URI u, File f, int w, int h, int pn, Writer out) throws MalformedURLException, IOException { PDDocument doc = getPages(u, f); List pages = doc.getDocumentCatalog().getAllPages(); int pagen = doc.getNumberOfPages(); int i = 0;//from w w w .j ava 2 s .c o m if (pn < pages.size()) i = pn; PDPage page = (PDPage) pages.get(i); PDRectangle mBox = page.findMediaBox(); float widthPt = mBox.getWidth(); float heightPt = mBox.getHeight(); float sx = widthPt / (float) w; float sy = heightPt / (float) h; // Get a DOMImplementation DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Create an instance of org.w3c.dom.Document // String svgNS = "http://www.w3.org/2000/svg"; // org.w3c.dom.Document document = domImpl.createDocument(svgNS, "svg", // null); DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; document = (SVGDocument) impl.createDocument(svgNS, "svg", null); // Create an instance of the SVG Generator SVGGraphics2D svgGenerator = new SVGGraphics2D(document); svgGenerator.getGeneratorContext().setComment("Test"); svgGenerator.getGeneratorContext().setEmbeddedFontsOn(true); // Ask the test to render into the SVG Graphics2D implementation Dimension pageDimension = new Dimension((int) widthPt, (int) heightPt); svgGenerator.setBackground(new Color(255, 255, 255, 0)); svgGenerator.scale(sx, sy); svgGenerator.setSVGCanvasSize(pageDimension); PageDrawer drawer = new PageDrawer(); drawer.drawPage(svgGenerator, page, pageDimension); // Element root = document.getDocumentElement(); // svgGenerator.getRoot(root); // Finally, stream out SVG to the standard output using UTF-8 // character to byte encoding boolean useCSS = true; // we want to use CSS style attribute svgGenerator.stream(out, useCSS, false); return; }
From source file:uk.bl.wa.tika.parser.pdf.pdfbox.PDFParser.java
License:Apache License
private void extractMetadata(PDDocument document, Metadata metadata) throws TikaException { PDDocumentInformation info = document.getDocumentInformation(); metadata.set(PagedText.N_PAGES, document.getNumberOfPages()); addMetadata(metadata, Metadata.TITLE, info.getTitle()); addMetadata(metadata, Metadata.AUTHOR, info.getAuthor()); addMetadata(metadata, Metadata.KEYWORDS, info.getKeywords()); addMetadata(metadata, "pdf:creator", info.getCreator()); addMetadata(metadata, "pdf:producer", info.getProducer()); addMetadata(metadata, Metadata.SUBJECT, info.getSubject()); addMetadata(metadata, "trapped", info.getTrapped()); addMetadata(metadata, "created", info.getCreationDate()); addMetadata(metadata, Metadata.CREATION_DATE, info.getCreationDate()); Calendar modified = info.getModificationDate(); addMetadata(metadata, Metadata.LAST_MODIFIED, modified); // All remaining metadata is custom // Copy this over as-is List<String> handledMetadata = Arrays.asList(new String[] { "Author", "Creator", "CreationDate", "ModDate", "Keywords", "Producer", "Subject", "Title", "Trapped" }); if (info.getCOSObject() != null && info.getCOSObject().keySet() != null) { for (COSName key : info.getCOSObject().keySet()) { String name = key.getName(); if (!handledMetadata.contains(name)) { addMetadata(metadata, name, info.getCOSObject().getDictionaryObject(key)); }//from w ww. ja va 2s .c om } } // ANJ Extensions: // // // Add other data of interest: metadata.set("pdf:version", "" + document.getDocument().getVersion()); metadata.set("pdf:numPages", "" + document.getNumberOfPages()); //metadata.set("pdf:cryptoMode", ""+getCryptoModeAsString(reader)); //metadata.set("pdf:openedWithFullPermissions", ""+reader.isOpenedWithFullPermissions()); metadata.set("pdf:encrypted", "" + document.isEncrypted()); //metadata.set("pdf:metadataEncrypted", ""+document.isMetadataEncrypted()); //metadata.set("pdf:128key", ""+reader.is128Key()); //metadata.set("pdf:tampered", ""+reader.isTampered()); try { if (document.getDocumentCatalog().getMetadata() != null) { XMPMetadata xmp = XMPMetadata.load(document.getDocumentCatalog().getMetadata().exportXMPMetadata()); // There is a special class for grabbing data in the PDF schema - not sure it will add much here: // Could parse xmp:CreatorTool and pdf:Producer etc. etc. out of here. XMPSchemaPDF pdfxmp = xmp.getPDFSchema(); // Added a PDF/A schema class: xmp.addXMLNSMapping(XMPSchemaPDFA.NAMESPACE, XMPSchemaPDFA.class); XMPSchemaPDFA pdfaxmp = (XMPSchemaPDFA) xmp.getSchemaByClass(XMPSchemaPDFA.class); if (pdfaxmp != null) { metadata.set("pdfaid:part", pdfaxmp.getPart()); metadata.set("pdfaid:conformance", pdfaxmp.getConformance()); String version = "A-" + pdfaxmp.getPart() + pdfaxmp.getConformance().toLowerCase(); //metadata.set("pdfa:version", version ); metadata.set("pdf:version", version); } // TODO WARN if this XMP version is inconsistent with document header version? } } catch (IOException e) { log.error("XMP Parsing failed: " + e); metadata.set("pdf:metadata-xmp-parse-failed", "" + e); } // Attempt to determine Adobe extension level, if present: COSDictionary root = document.getDocumentCatalog().getCOSObject(); COSDictionary extensions = (COSDictionary) root.getDictionaryObject(COSName.getPDFName("Extensions")); if (extensions != null) { for (COSName extName : extensions.keySet()) { // If it's an Adobe one, interpret it to determine the extension level: if (extName.equals(COSName.getPDFName("ADBE"))) { COSDictionary adobeExt = (COSDictionary) extensions.getDictionaryObject(extName); if (adobeExt != null) { String baseVersion = adobeExt.getNameAsString(COSName.getPDFName("BaseVersion")); int el = adobeExt.getInt(COSName.getPDFName("ExtensionLevel")); metadata.set("pdf:version", baseVersion + " Adobe Extension Level " + el); } // TODO WARN if this embedded version is inconsistent with document header version? } else { // WARN that there is an Extension, but it's not Adobe's, and so is a 'new' format'. metadata.set("pdf:foundNonAdobeExtensionName", extName.getName()); } } } // End Of ANJ Extensions. }
From source file:uk.org.openeyes.PDFFunctions.java
/** * * @param PDFDoc/*from w ww. ja v a 2s. c o m*/ * @throws IOException */ public void dumpPDFContent(PDDocument PDFDoc) throws IOException { PDPageTree allPages; allPages = PDFDoc.getDocumentCatalog().getPages(); PDPage currentPage; Iterator pages = allPages.iterator(); while (pages.hasNext()) { currentPage = (PDPage) pages.next(); String pageTitle = getPageTitleIOLM700(currentPage).trim(); System.out.println(pageTitle); if (pageTitle.equals("IOL calculation") || pageTitle.equals("IOL calculation (Multiformula)")) { System.out.println("Extracting calculation values"); System.out.println(getTopLensFormulaNameIOLM700(currentPage)); for (int i = 1; i <= 4; i++) { System.out.println("Right " + i); System.out.println(getMultiLensFormulaNamesIOLM700(currentPage, "R", i)); System.out.println(getMultiLensAValuesIOLM700(currentPage, "R", i)); System.out.println(dumpIOLREFValuesIOLM700(currentPage, "R", i)); System.out.println("Left " + i); System.out.println(getMultiLensFormulaNamesIOLM700(currentPage, "L", i)); System.out.println(getMultiLensAValuesIOLM700(currentPage, "L", i)); System.out.println(dumpIOLREFValuesIOLM700(currentPage, "L", i)); } } } }
From source file:upload.LeerPdf.java
public void lecturaPDF() { String ln = System.getProperty("line.separator"); File dir = new File("C:\\Users\\mmercadoco\\Desktop\\imagenes");//CREO UN OBJETO CON TODOS LOS ARCHIVOS QUE CONTIENE LA CARPETA QUE CONTIENE LOS PDFS. String[] ficheros = dir.list();//ARREGLO QUE ALMACENAR? TODOS LOS NOMBRES DE LOS ARCHIVOS QUE ESTAN DENTRO DEL OBJETO. if (ficheros == null)//EXCEPCION System.out.println("No hay archivos en la carpeta especificada"); else {/*from ww w . ja va2 s . co m*/ for (int x = 0; x < ficheros.length; x++) {//RECORREMOS EL ARREGLO CON LOS NOMBRES DE ARCHIVO String ruta = new String();//VARIABLE QUE DETERMINARA LA RUTA DEL ARCHIVO A LEER. ruta = ("C:\\Users\\mmercadoco\\Desktop\\imagenes\\" + ficheros[x]); //SE ALMACENA LA RUTA DEL ARCHIVO A LEER. System.out.println("ruta" + ruta); try { PDDocument pd = PDDocument.load(ruta); //CARGAR EL PDF List l = pd.getDocumentCatalog().getAllPages();//NUMERO LAS PAGINAS DEL ARCHIVO System.out.println("Paginas: " + l); Object[] obj = l.toArray();//METO EN UN OBJETO LA LISTA DE PAGINAS PARA MANIPULARLA PDPage page = (PDPage) obj[9];//PAGE ES LA PAGINA 1 DE LA QUE CONSTA EL ARCHIVO PageFormat pageFormat = pd.getPageFormat(0);//PROPIEDADES DE LA PAGINA (FORMATO) Double d1 = new Double(pageFormat.getHeight());//ALTO Double d2 = new Double(pageFormat.getWidth());//ANCHO int width = d1.intValue();//ANCHO int eigth = 1024;//ALTO PDFTextStripperByArea stripper = new PDFTextStripperByArea();//COMPONENTE PARA ACCESO AL TEXTO Rectangle rect = new Rectangle(0, 0, width, eigth);//DEFNIR AREA DONDE SE BUSCARA EL TEXTO stripper.addRegion("area1", rect);//REGISTRAMOS LA REGION CON UN NOMBRE stripper.extractRegions(page);//EXTRAE TEXTO DEL AREA System.out.println("Texto del area: " + page); contenido = new String();//CONTENIDO = A LO QUE CONTENGA EL AREA O REGION contenido = (stripper.getTextForRegion("area1")); System.out.println("rect: " + rect); System.out.println("stripper: " + stripper.getTextForRegion("area1")); File archivo = new File(ruta + ".txt");//CREAMOS ARCHIVO CON NOMBRE ORIGINAL PERO EN TXT System.out.println("Nuevo nombre de txt: " + ficheros[x]); BufferedWriter writer = new BufferedWriter(new FileWriter(archivo));//CREAMOS EL ESCRITOR //writer.write(ruta);//IMPRIMIMOS LA RUTA writer.write(contenido);//IMPRIMIMOS EL CONTENIDO System.out.println("Contenido: " + contenido); writer.close();//CERRAMOS EL ESCRITOR pd.close();//CERRAMOS OBJETO ACROBAT } catch (IOException e) { if (e.toString() != null) { File archivo = new File("daado_" + ficheros[x] + ".txt");//SEPARA LOS DAADOS } System.out.println("Archivo daado " + ficheros[x]);// INDICA EN CONSOLA CUALES SON LOS DAADOS e.printStackTrace(); } //CATCH } //FOR } //ELSE }
From source file:Utilities.BatchInDJMSHelper.java
public void generateProcessedAndRejectPDFs(String preProcPdfFileName) throws IOException, COSVisitorException { PDDocument pdf = PDDocument.load(preProcPdfFileName); PDDocument rejectPdf = new PDDocument(); PDDocument auditPdf = new PDDocument(); String rejectPdfFileName = preProcPdfFileName.replace(".pdf", "_forReject.pdf"); String auditPdfFileName = preProcPdfFileName.replace(".pdf", "_forAudit.pdf"); int pageNum = pdf.getNumberOfPages(); // add reject page into rejectPdf PDFTextStripper pdfStripper = new PDFTextStripper(); boolean isLastReject = true; // last page status for (int i = 0; i < pageNum; i++) { PDPage page = (PDPage) pdf.getDocumentCatalog().getAllPages().get(i); int pageIndex = i + 1; pdfStripper.setStartPage(pageIndex); pdfStripper.setEndPage(pageIndex); String res = pdfStripper.getText(pdf); System.out.println(res);/*from w w w. j a va2s . c o m*/ if (res.contains(GlobalVar.PRE_PROC_KEY_SYMBOL)) { String[] data = GlobalVar.getCtrlNumAndfullSSN(res); String ctrlNum = data[0]; String fullSSN = data[1]; System.out.println("full ssn:" + fullSSN + ". ctrl num:" + ctrlNum); if (LEGIT_LV_MAP.containsKey(fullSSN)) { System.out.println("ctrl num: " + LEGIT_LV_MAP.get(fullSSN)); } if (LEGIT_LV_MAP.containsKey(fullSSN) && LEGIT_LV_MAP.get(fullSSN).containsKey(ctrlNum)) { System.out.println("Good leave"); auditPdf.addPage(page); isLastReject = false; } else { rejectPdf.addPage(page); drawComments(rejectPdf, page, fullSSN, ctrlNum); isLastReject = true; } } else { // add the supporting documents to the last pdf file if (isLastReject) { rejectPdf.addPage(page); } else { auditPdf.addPage(page); } } } if (rejectPdf.getNumberOfPages() > 0 && auditPdf.getNumberOfPages() > 0) { auditPdf.save(auditPdfFileName); rejectPdf.save(rejectPdfFileName); JOptionPane.showMessageDialog(null, "The ready-for-aduit and the rejected leave forms are saved in *_forAudit.pdf and *_forReject.pdf, respectively."); numberPDFFile(auditPdfFileName); } else if (rejectPdf.getNumberOfPages() > 0) { rejectPdf.save(rejectPdfFileName); JOptionPane.showMessageDialog(null, "The rejected leave forms are saved in *_forReject.pdf."); } else if (auditPdf.getNumberOfPages() > 0) { auditPdf.save(auditPdfFileName); JOptionPane.showMessageDialog(null, "The ready-for-aduit leave forms are saved in *_forAduit.pdf."); numberPDFFile(auditPdfFileName); } rejectPdf.close(); auditPdf.close(); pdf.close(); }
From source file:Utilities.BatchInDJMSHelper.java
public void generateReadyForAuditPDF(String preProcPdfFileName) throws IOException, COSVisitorException { PDDocument pdf = PDDocument.load(preProcPdfFileName); PDDocument auditPdf = new PDDocument(); String auditPdfFileName = preProcPdfFileName.replace(".pdf", "_forAudit.pdf"); int pageNum = pdf.getNumberOfPages(); // add reject page into rejectPdf PDFTextStripper pdfStripper = new PDFTextStripper(); for (int i = 0; i < pageNum; i++) { PDPage page = (PDPage) pdf.getDocumentCatalog().getAllPages().get(i); int pageIndex = i + 1; pdfStripper.setStartPage(pageIndex); pdfStripper.setEndPage(pageIndex); String res = pdfStripper.getText(pdf); System.out.println(res);//from w w w . jav a 2 s . c o m boolean isLastReject = true; // last page status if (res.contains(GlobalVar.PRE_PROC_KEY_SYMBOL)) { String[] data = GlobalVar.getCtrlNumAndfullSSN(res); String ctrlNum = data[0]; String fullSSN = data[1]; System.out.println("full ssn:" + fullSSN + ". ctrl num:" + ctrlNum); if (LEGIT_LV_MAP.containsKey(fullSSN)) { System.out.println("ctrl num: " + LEGIT_LV_MAP.get(fullSSN)); } if (LEGIT_LV_MAP.containsKey(fullSSN) && LEGIT_LV_MAP.get(fullSSN).containsKey(ctrlNum)) { System.out.println("Good leave"); auditPdf.addPage(page); isLastReject = false; } } else { // add the supporting documents to the last pdf file if (!isLastReject) { auditPdf.addPage(page); } } } if (auditPdf.getNumberOfPages() > 0) { auditPdf.save(auditPdfFileName); JOptionPane.showMessageDialog(null, "The ready-for-aduit leave forms are saved in *_forAduit.pdf."); numberPDFFile(auditPdfFileName); } auditPdf.close(); pdf.close(); }