List of usage examples for org.apache.pdfbox.pdmodel PDDocument getNumberOfPages
public int getNumberOfPages()
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 w w.ja v a 2s . 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 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 ww .j a v a2 s .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; // 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 www. j a va 2 s.co m } } // 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 ww w . ja v a 2 s . co m*/ * @throws IOException */ public void dumpPDFStructure(PDDocument PDFDoc) throws IOException { PDFTextStripper stripper = new PDFFunctions(); stripper.setSortByPosition(true); stripper.setStartPage(0); stripper.setEndPage(PDFDoc.getNumberOfPages()); Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream()); stripper.writeText(PDFDoc, dummy); }
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);// w ww .j a va 2 s . 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);/*w w w .j a v a 2 s .co 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(); }