List of usage examples for org.apache.pdfbox.pdmodel PDDocument close
@Override public void close() throws IOException
From source file:com.ackpdfbox.app.Decrypt.java
License:Apache License
private void decrypt() throws IOException { PDDocument document = null; try {/*from w w w. ja va 2s . c o m*/ InputStream keyStoreStream = null; if (keyStore != null) { keyStoreStream = new FileInputStream(keyStore); } document = PDDocument.load(new File(infile), password, keyStoreStream, alias); if (document.isEncrypted()) { AccessPermission ap = document.getCurrentAccessPermission(); if (ap.isOwnerPermission()) { document.setAllSecurityToBeRemoved(true); document.save(outfile); } else { throw new IOException( "Error: You are only allowed to decrypt a document with the owner password."); } } else { System.err.println("Error: Document is not encrypted."); } } finally { if (document != null) { document.close(); } } }
From source file:com.ackpdfbox.app.Encrypt.java
License:Apache License
private void encrypt(String[] args) throws IOException, CertificateException { if (args.length < 1) { usage();//from w w w .jav a 2 s . c om } else { AccessPermission ap = new AccessPermission(); String infile = null; String outfile = null; String certFile = null; String userPassword = ""; String ownerPassword = ""; int keyLength = 40; PDDocument document = null; try { for (int i = 0; i < args.length; i++) { String key = args[i]; if (key.equals("-O")) { ownerPassword = args[++i]; } else if (key.equals("-U")) { userPassword = args[++i]; } else if (key.equals("-canAssemble")) { ap.setCanAssembleDocument(args[++i].equalsIgnoreCase("true")); } else if (key.equals("-canExtractContent")) { ap.setCanExtractContent(args[++i].equalsIgnoreCase("true")); } else if (key.equals("-canExtractForAccessibility")) { ap.setCanExtractForAccessibility(args[++i].equalsIgnoreCase("true")); } else if (key.equals("-canFillInForm")) { ap.setCanFillInForm(args[++i].equalsIgnoreCase("true")); } else if (key.equals("-canModify")) { ap.setCanModify(args[++i].equalsIgnoreCase("true")); } else if (key.equals("-canModifyAnnotations")) { ap.setCanModifyAnnotations(args[++i].equalsIgnoreCase("true")); } else if (key.equals("-canPrint")) { ap.setCanPrint(args[++i].equalsIgnoreCase("true")); } else if (key.equals("-canPrintDegraded")) { ap.setCanPrintDegraded(args[++i].equalsIgnoreCase("true")); } else if (key.equals("-certFile")) { certFile = args[++i]; } else if (key.equals("-keyLength")) { try { keyLength = Integer.parseInt(args[++i]); } catch (NumberFormatException e) { throw new NumberFormatException( "Error: -keyLength is not an integer '" + args[i] + "'"); } } else if (infile == null) { infile = key; } else if (outfile == null) { outfile = key; } else { usage(); } } if (infile == null) { usage(); } if (outfile == null) { outfile = infile; } document = PDDocument.load(new File(infile)); if (!document.isEncrypted()) { if (certFile != null) { PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy(); PublicKeyRecipient recip = new PublicKeyRecipient(); recip.setPermission(ap); CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream inStream = null; try { inStream = new FileInputStream(certFile); X509Certificate certificate = (X509Certificate) cf.generateCertificate(inStream); recip.setX509(certificate); } finally { if (inStream != null) { inStream.close(); } } ppp.addRecipient(recip); ppp.setEncryptionKeyLength(keyLength); document.protect(ppp); } else { StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerPassword, userPassword, ap); spp.setEncryptionKeyLength(keyLength); document.protect(spp); } document.save(outfile); } else { System.err.println("Error: Document is already encrypted."); } } finally { if (document != null) { document.close(); } } } }
From source file:com.ackpdfbox.app.PDFToImage.java
License:Apache License
/** * Infamous main method.//from ww w . j a v a2 s .co 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:com.amolik.misc.ExtractTextByArea.java
License:Apache License
/** * This will print the documents text in a certain area. * * @param args The command line arguments. * * @throws IOException If there is an error parsing the document. *//*w w w.jav a2s. c o m*/ public static void main(String[] args) throws IOException { //args[0]= "E:\\Automation\\uphillit\\Fiscal_demo_data.pdf"; // if( args.length != 1 ) // { // usage(); // } // else // { PDDocument document = null; try { document = PDDocument.load(new File("E:\\Automation\\uphillit\\Fiscal_demo_data.pdf")); int numberOfPages = document.getNumberOfPages(); if (numberOfPages > 0) { PDPage page = (PDPage) document.getPages().get(0); System.out.println(page.getContents()); } PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition(true); Rectangle rect = new Rectangle(3, 1, 600, 6000); stripper.addRegion("class1", rect); PDPage firstPage = document.getPage(0); stripper.extractRegions(firstPage); System.out.println("Text in the area:" + rect); System.out.println(stripper.getTextForRegion("class1")); } finally { if (document != null) { document.close(); } } // } }
From source file:com.apache.pdfbox.ocr.tesseract.BadScan.java
License:Apache License
@Test public void textBadScan() { try {/*from w w w . j ava 2 s . c om*/ PDDocument document = PDDocument.load(new File("src/test/resources/samples/scansmpl.pdf")); PDFRenderer renderer = new PDFRenderer(document); BufferedImage image = renderer.renderImage(0, 3); TessBaseAPI api = new TessBaseAPI(); boolean init = api.init("eng"); api.setBufferedImage(image); String text = api.getUTF8Text(); System.out.println(text); api.end(); assertEquals(init, true); document.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.apache.pdfbox.ocr.tesseract.TessBaseAPITest.java
License:Apache License
@Test public void textBadScan() { try {/* w w w . ja v a2 s . co m*/ PDDocument document = PDDocument.load(new File("src/test/resources/samples/scansmpl.pdf")); PDFRenderer renderer = new PDFRenderer(document); BufferedImage image = renderer.renderImage(0, 3); TessBaseAPI api = new TessBaseAPI(); boolean init = api.init("eng"); api.setBufferedImage(image); String text = api.getUTF8Text(); System.out.println(text); api.end(); assertEquals(true, true); document.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.apache.pdfbox.ocr.tesseract.TessBaseAPITest.java
License:Apache License
@Test public void testBufferedImage() { try {/*ww w . j a v a2 s . c o m*/ PDDocument document = PDDocument.load(new File("src/test/resources/samples/pdf1.pdf")); PDFRenderer renderer = new PDFRenderer(document); BufferedImage image = renderer.renderImage(0, 3); TessBaseAPI api = new TessBaseAPI(); boolean init = api.init("eng"); api.setBufferedImage(image); String text = api.getUTF8Text(); System.out.println(text); api.end(); assertEquals(init, true); document.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.apache.pdfbox.ocr.tesseract.TessBaseAPITest.java
License:Apache License
@Test public void testByteStream() { try {//from www. j a v a 2 s .com PDDocument document = PDDocument.load(new File("src/test/resources/samples/pdf2.pdf")); PDFRenderer renderer = new PDFRenderer(document); BufferedImage image = renderer.renderImage(0, 3); int width = image.getWidth(); int height = image.getHeight(); int bpp = 3; int bpl = width * 3; TessBaseAPI api = new TessBaseAPI(); byte data[] = api.getByteStream(image); boolean init = api.init("eng"); api.setImage(data, width, height, bpp, bpl); String text = api.getUTF8Text(); System.out.println(text); api.end(); assertEquals(init, true); document.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.apache.pdfbox.ocr.tesseract.TessBaseAPITest.java
License:Apache License
@Test public void testIterator() { try {/*from w ww . jav a2s .c om*/ PDDocument document = PDDocument.load(new File("src/test/resources/samples/pdf3.pdf")); PDFRenderer renderer = new PDFRenderer(document); BufferedImage image = renderer.renderImage(0, 3); TessBaseAPI api = new TessBaseAPI(); boolean init = api.init("eng"); api.setBufferedImage(image); api.getResultIterator(); if (api.isResultIteratorAvailable()) { do { System.out.println(api.getWord().trim()); String result = api.getBoundingBox(); System.out.println(result); } while (api.resultIteratorNext()); } api.end(); assertEquals(init, true); document.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.aurel.track.lucene.index.associatedFields.textExctractor.PdfExtractor.java
License:Open Source License
/** * Gets the text from file content /*from w w w . j a va 2s .c o m*/ * @param file * @param fileExtension * @return */ @Override public String getText(File file, String fileExtension) { FileInputStream fis = null; PDDocument pdDoc = null; StringWriter stringWriter = null; try { fis = new FileInputStream(file); PDFParser parser = new PDFParser(fis); parser.parse(); pdDoc = parser.getPDDocument(); PDFTextStripper stripper = new PDFTextStripper(); stripper.setLineSeparator("\n"); stringWriter = new StringWriter(); stripper.writeText(pdDoc, stringWriter); return stringWriter.toString(); } catch (Exception e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug( "Extracting text from the .pdf file " + file.getName() + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } finally { try { if (stringWriter != null) { stringWriter.close(); } } catch (Exception e) { } try { if (pdDoc != null) { pdDoc.close(); } } catch (Exception e) { LOGGER.info("Closing pdDoc for " + file + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } try { if (fis != null) { fis.close(); } } catch (Exception e) { LOGGER.info("Closing the FileInputStream for " + file + " failed with " + e.getMessage()); } } return null; }