List of usage examples for org.apache.pdfbox.pdmodel PDDocument getNumberOfPages
public int getNumberOfPages()
From source file:com.poscoict.license.service.CertificateService.java
public boolean extractPagesAsImage(String sourceFile, String fileName, int resolution, String password) { boolean result = false; //? ?//from w w w . j a v a 2 s. c om String imageFormat = Consts.IMG_FORMAT; int pdfPageCn = 0; PDDocument pdfDoc = null; try { //PDF? ? pdfDoc = PDDocument.load(sourceFile); //PDF? ?? ? pdfPageCn = pdfDoc.getNumberOfPages(); } catch (IOException ioe) { logger.error("PDF ? : ", ioe); } PDFImageWriter imageWriter = new PDFImageWriter(); try { result = imageWriter.writeImage(pdfDoc, imageFormat, password, 1, //? ? pdfPageCn, //? ? //? ? ? ?+? "?1.gif" ? Consts.IMG_PATH + fileName, BufferedImage.TYPE_INT_RGB, resolution //? 300 ); pdfDoc.close(); } catch (IOException ioe) { logger.error("PDF ? : ", ioe); } return result; }
From source file:com.qwazr.library.pdfbox.PdfBoxParser.java
License:Apache License
private void extractMetaData(final PDDocument pdf, final ParserFieldsBuilder metas) { metas.set(MIME_TYPE, DEFAULT_MIMETYPES[0]); final PDDocumentInformation info = pdf.getDocumentInformation(); if (info != null) { metas.add(TITLE, info.getTitle()); metas.add(SUBJECT, info.getSubject()); metas.add(AUTHOR, info.getAuthor()); metas.add(PRODUCER, info.getProducer()); metas.add(KEYWORDS, info.getKeywords()); metas.add(CREATION_DATE, info.getCreationDate()); metas.add(MODIFICATION_DATE, info.getModificationDate()); }/*from w ww. ja va 2 s . c om*/ int pages = pdf.getNumberOfPages(); metas.add(NUMBER_OF_PAGES, pages); PDDocumentCatalog catalog = pdf.getDocumentCatalog(); if (catalog != null) metas.add(LANGUAGE, catalog.getLanguage()); }
From source file:com.testautomationguru.utility.PDFUtil.java
License:Apache License
/** * Get the page count of the document./*from ww w. j a va 2s . co m*/ * * @param file Absolute file path * @return int No of pages in the document. * @throws java.io.IOException when file is not found. */ public int getPageCount(String file) throws IOException { logger.info("file :" + file); PDDocument doc = PDDocument.load(new File(file)); int pageCount = doc.getNumberOfPages(); logger.info("pageCount :" + pageCount); doc.close(); return pageCount; }
From source file:com.testautomationguru.utility.PDFUtil.java
License:Apache License
private void updateStartAndEndPages(String file, int start, int end) throws IOException { PDDocument document = PDDocument.load(new File(file)); int pagecount = document.getNumberOfPages(); logger.info("Page Count : " + pagecount); logger.info("Given start page:" + start); logger.info("Given end page:" + end); if ((start > 0 && start <= pagecount)) { this.startPage = start; } else {//from www . ja va 2s . co m this.startPage = 1; } if ((end > 0 && end >= start && end <= pagecount)) { this.endPage = end; } else { this.endPage = pagecount; } document.close(); logger.info("Updated start page:" + this.startPage); logger.info("Updated end page:" + this.endPage); }
From source file:com.wintindustries.pdffilter.pdfcore.PDFTester.java
static public void printMetadata(PDDocument document) throws IOException { PDDocumentInformation info = document.getDocumentInformation(); PDDocumentCatalog cat = document.getDocumentCatalog(); PDMetadata metadata = cat.getMetadata(); System.out.println("Page Count=" + document.getNumberOfPages()); System.out.println("Title=" + info.getTitle()); System.out.println("Author=" + info.getAuthor()); System.out.println("Subject=" + info.getSubject()); System.out.println("Keywords=" + info.getKeywords()); System.out.println("Creator=" + info.getCreator()); System.out.println("Producer=" + info.getProducer()); System.out.println("Creation Date=" + formatDate(info.getCreationDate())); System.out.println("Modification Date=" + formatDate(info.getModificationDate())); System.out.println("Trapped=" + info.getTrapped()); if (metadata != null) { System.out.println("Metadata=" + metadata.getInputStreamAsString()); }// ww w .jav a 2s. c o m }
From source file:com.yiyihealth.tools.test.DrawPrintTextLocations.java
License:Apache License
/** * This will print the documents data.// www.j ava2 s.c om * * @param args The command line arguments. * * @throws IOException If there is an error parsing the document. */ public static void main(String[] args) throws IOException { if (args.length != 1) { usage(); } else { PDDocument document = null; try { document = PDDocument.load(new File(args[0])); DrawPrintTextLocations stripper = new DrawPrintTextLocations(document, args[0]); stripper.setSortByPosition(true); for (int page = 0; page < document.getNumberOfPages(); ++page) { stripper.stripPage(page); } } finally { if (document != null) { document.close(); } } } }
From source file:com.yiyihealth.util.PDF2Image.java
License:Apache License
/** * Infamous main method.//from w w w . j a v a 2 s .c o m * * @param args Command line arguments, should be one and a reference to a file. * * @throws IOException If there is an error parsing the document. */ public static void main(String[] args) throws IOException { // suppress the Dock icon on OS X System.setProperty("apple.awt.UIElement", "true"); String password = ""; String pdfFile = null; String outputPrefix = null; String imageFormat = "jpg"; int startPage = 1; int endPage = Integer.MAX_VALUE; String color = "rgb"; int dpi; float cropBoxLowerLeftX = 0; float cropBoxLowerLeftY = 0; float cropBoxUpperRightX = 0; float cropBoxUpperRightY = 0; boolean showTime = false; try { dpi = Toolkit.getDefaultToolkit().getScreenResolution(); } catch (HeadlessException e) { dpi = 96; } for (int i = 0; i < args.length; i++) { if (args[i].equals(PASSWORD)) { i++; if (i >= args.length) { usage(); } password = args[i]; } else if (args[i].equals(START_PAGE)) { i++; if (i >= args.length) { usage(); } startPage = Integer.parseInt(args[i]); } else if (args[i].equals(END_PAGE)) { i++; if (i >= args.length) { usage(); } endPage = Integer.parseInt(args[i]); } else if (args[i].equals(PAGE)) { i++; if (i >= args.length) { usage(); } startPage = Integer.parseInt(args[i]); endPage = Integer.parseInt(args[i]); } else if (args[i].equals(IMAGE_TYPE) || args[i].equals(FORMAT)) { i++; imageFormat = args[i]; } else if (args[i].equals(OUTPUT_PREFIX) || args[i].equals(PREFIX)) { i++; outputPrefix = args[i]; } else if (args[i].equals(COLOR)) { i++; color = args[i]; } else if (args[i].equals(RESOLUTION) || args[i].equals(DPI)) { i++; dpi = Integer.parseInt(args[i]); } else if (args[i].equals(CROPBOX)) { i++; cropBoxLowerLeftX = Float.valueOf(args[i]); i++; cropBoxLowerLeftY = Float.valueOf(args[i]); i++; cropBoxUpperRightX = Float.valueOf(args[i]); i++; cropBoxUpperRightY = Float.valueOf(args[i]); } else if (args[i].equals(TIME)) { showTime = true; } else { if (pdfFile == null) { pdfFile = args[i]; } } } if (pdfFile == null) { usage(); } else { if (outputPrefix == null) { outputPrefix = pdfFile.substring(0, pdfFile.lastIndexOf('.')); } PDDocument document = null; try { document = PDDocument.load(new File(pdfFile), password); ImageType imageType = null; if ("bilevel".equalsIgnoreCase(color)) { imageType = ImageType.BINARY; } else if ("gray".equalsIgnoreCase(color)) { imageType = ImageType.GRAY; } else if ("rgb".equalsIgnoreCase(color)) { imageType = ImageType.RGB; } else if ("rgba".equalsIgnoreCase(color)) { imageType = ImageType.ARGB; } if (imageType == null) { System.err.println("Error: Invalid color."); System.exit(2); } //if a CropBox has been specified, update the CropBox: //changeCropBoxes(PDDocument document,float a, float b, float c,float d) if (cropBoxLowerLeftX != 0 || cropBoxLowerLeftY != 0 || cropBoxUpperRightX != 0 || cropBoxUpperRightY != 0) { changeCropBox(document, cropBoxLowerLeftX, cropBoxLowerLeftY, cropBoxUpperRightX, cropBoxUpperRightY); } long startTime = System.nanoTime(); // render the pages boolean success = true; endPage = Math.min(endPage, document.getNumberOfPages()); PDFRenderer renderer = new PDFRenderer(document); for (int i = startPage - 1; i < endPage; i++) { BufferedImage image = renderer.renderImageWithDPI(i, dpi, imageType); String fileName = outputPrefix + "_" + (i + 1) + "." + imageFormat; success &= ImageIOUtil.writeImage(image, fileName, dpi); } // performance stats long endTime = System.nanoTime(); long duration = endTime - startTime; int count = 1 + endPage - startPage; if (showTime) { System.err.printf("Rendered %d page%s in %dms\n", count, count == 1 ? "" : "s", duration / 1000000); } if (!success) { System.err.println("Error: no writer found for image format '" + imageFormat + "'"); System.exit(1); } } finally { if (document != null) { document.close(); } } } }
From source file:Control.MyiReportVisor.java
/** * si se desea imprimir solo una hoja, se llama al objeto "printr" * si se desea imprimir hoja + copia, se llama al objeto "pages" *//*from ww w . j a va 2 s . c o m*/ public void exportarAPdfConCopia() throws IOException { try { //EXPORTANDO PDF System.out.println("Exportando a pdf"); final JRPdfExporter exp = new JRPdfExporter(); exp.setParameter(JRExporterParameter.JASPER_PRINT_LIST, pages); exp.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); exp.setParameter(JRExporterParameter.OUTPUT_FILE, new File("D:\\pdfGenerados\\" + nombreArchivo + ".pdf")); exp.exportReport(); PrinterJob job = PrinterJob.getPrinterJob(); PDDocument pdc = PDDocument.load(new File("D:\\pdfGenerados\\" + nombreArchivo + ".pdf")); System.out.println("" + pdc.getNumberOfPages()); job.setPageable(new PDFPageable(pdc)); try { job.print(); } catch (PrinterException ex) { Logger.getLogger(MyiReportVisor.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Mostrando PDF"); //Abriendo PDF //Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler "+"D:\\pdfGenerados\\" + nombreArchivo + ".pdf"); } catch (JRException ex) { ex.printStackTrace(); } }
From source file:cx.fbn.nevernote.gui.PDFPreview.java
License:Open Source License
public int getPageCount(String filePath) { try {// ww w . j a v a 2 s . co m String whichOS = System.getProperty("os.name"); if (whichOS.contains("Windows")) { filePath = filePath.replace("\\", "/"); } PDDocument document = null; document = PDDocument.load(filePath); return document.getNumberOfPages(); } catch (Exception e) { return 0; } }
From source file:cx.fbn.nevernote.gui.PDFPreview.java
License:Open Source License
public boolean setupPreview(String filePath, String appl, int pageNumber) { // Fix stupid Windows file separation characters String whichOS = System.getProperty("os.name"); if (whichOS.contains("Windows")) { filePath = filePath.replace("\\", "/"); }/*from w w w. j a va2s .com*/ if (appl.equals("pdf")) { PDDocument document = null; try { document = PDDocument.load(filePath); if (document.getNumberOfPages() <= pageNumber) return false; if (document.getDocumentCatalog().getAllPages().size() <= pageNumber) return false; PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(pageNumber); BufferedImage bi = page.convertToImage(); File outputfile; outputfile = new File(filePath + ".png"); ImageIO.write(bi, "png", outputfile); return true; } catch (IOException e1) { return false; } } return false; }