List of usage examples for org.apache.pdfbox.pdmodel PDDocument close
@Override public void close() throws IOException
From source file:com.joowon.returnA.classifier.EbsBookCrawler.java
License:Open Source License
public void run() throws IOException { File destination = new File("/Users/Joowon/Desktop"); File bookFolder = new File(getClass().getClassLoader().getResource("book").getFile()); for (File book : bookFolder.listFiles()) { String outputName = destination.getPath() + "/" + book.getName().replace(".pdf", ".txt"); PDDocument document = PDDocument.load(book); String text = ""; for (PDPage page : document.getPages()) { text += new PdfTextExtractor(page) .addRegion(0, 0, (int) page.getMediaBox().getWidth(), (int) page.getMediaBox().getHeight()) .extract();//w w w. ja va 2s .c o m } new TxtWriter(outputName).write(text); document.close(); } }
From source file:com.jt.tool.pdf.CreateBookmarks.java
License:Apache License
public static void createBookmark(String srcFile, String targetFile, String reg) throws Exception { PDDocument document = null; try {/*ww w . java 2 s .co m*/ document = PDDocument.load(new File(srcFile)); if (document.isEncrypted()) { System.err.println("Error: Cannot add bookmarks to encrypted document."); System.exit(1); } PDDocumentOutline outline = new PDDocumentOutline(); document.getDocumentCatalog().setDocumentOutline(outline); PDOutlineItem pagesOutline = new PDOutlineItem(); pagesOutline.setTitle("All Pages"); // outline.appendChild(pagesOutline); List pages = new ArrayList(); // document.getDocumentCatalog().getAllPages(); for (int i = 12; i < pages.size(); i++) { String pageText = getPageText(document, i + 1, 0); String[] strings = matchTitle(pageText, reg); if (makeBookmark(strings)) { PDPage page = (PDPage) pages.get(i); PDPageFitWidthDestination dest = new PDPageFitWidthDestination(); dest.setPage(page); PDOutlineItem bookmark = new PDOutlineItem(); bookmark.setDestination(dest); bookmark.setTitle(strings[0]); // pagesOutline.appendChild(bookmark); System.out.println("add " + strings[0]); } } pagesOutline.openNode(); outline.openNode(); document.save(targetFile); } finally { if (document != null) { document.close(); } } }
From source file:com.lanacion.adminsiteln.services.PdfIndexerService.PdfIndexerService.java
/** * Metodos privados para la indexacin// w ww. j av a 2 s . c om */ private String pdftoText(String fileName, int pagina) { PDFParser parser; String parsedText = null; ; PDFTextStripper pdfStripper = null; //pdfStripper.setStartPage(0); //pdfStripper.setEndPage(0); PDDocument pdDoc = null; COSDocument cosDoc = null; File file = new File(fileName); if (!file.isFile()) { System.err.println("File " + fileName + " does not exist."); return null; } try { parser = new PDFParser(new FileInputStream(file)); } catch (IOException e) { System.err.println("Unable to open PDF Parser. " + e.getMessage()); return null; } try { parser.parse(); cosDoc = parser.getDocument(); pdfStripper = new PDFTextStripper(); pdDoc = new PDDocument(cosDoc); pdfStripper.setStartPage(pagina); pdfStripper.setEndPage(pagina); parsedText = pdfStripper.getText(pdDoc); } catch (Exception e) { System.err.println("An exception occured in parsing the PDF Document." + e.getMessage()); } finally { try { if (cosDoc != null) { cosDoc.close(); } if (pdDoc != null) { pdDoc.close(); } } catch (Exception e) { e.printStackTrace(); } } return parsedText; }
From source file:com.lanacion.adminsiteln.services.PdfIndexerService.PdfIndexerService.java
private int pdfgetPages(String fileName) { int numero_paginas = 0; PDFParser parser;/*from w ww . j a v a 2 s . c o m*/ String parsedText = null; ; PDFTextStripper pdfStripper = null; //pdfStripper.setStartPage(0); //pdfStripper.setEndPage(0); PDDocument pdDoc = null; COSDocument cosDoc = null; File file = new File(fileName); if (!file.isFile()) { System.err.println("File " + fileName + " does not exist."); return 0; } try { parser = new PDFParser(new FileInputStream(file)); } catch (IOException e) { System.err.println("Unable to open PDF Parser. " + e.getMessage()); return 0; } try { parser.parse(); cosDoc = parser.getDocument(); pdfStripper = new PDFTextStripper(); pdDoc = new PDDocument(cosDoc); numero_paginas = pdDoc.getNumberOfPages(); } catch (Exception e) { System.err.println("An exception occured in parsing the PDF Document." + e.getMessage()); } finally { try { if (cosDoc != null) { cosDoc.close(); } if (pdDoc != null) { pdDoc.close(); } } catch (Exception e) { e.printStackTrace(); } } return numero_paginas; }
From source file:com.liferay.portal.util.LuceneFields.java
License:Open Source License
public static Field getFile(String field, File file, String fileExt) throws IOException { fileExt = fileExt.toLowerCase();/*from w w w .ja va 2 s. c o m*/ FileInputStream fis = new FileInputStream(file); Reader reader = new BufferedReader(new InputStreamReader(fis)); String text = null; if (fileExt.equals(".doc")) { try { WordDocument wordDocument = new WordDocument(fis); StringWriter stringWriter = new StringWriter(); wordDocument.writeAllText(stringWriter); text = stringWriter.toString(); stringWriter.close(); } catch (Exception e) { _log.error(e.getMessage()); } } else if (fileExt.equals(".htm") || fileExt.equals(".html")) { try { DefaultStyledDocument dsd = new DefaultStyledDocument(); HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); htmlEditorKit.read(reader, dsd, 0); text = dsd.getText(0, dsd.getLength()); } catch (Exception e) { _log.error(e.getMessage()); } } else if (fileExt.equals(".pdf")) { try { PDFParser parser = new PDFParser(fis); parser.parse(); PDDocument pdDoc = parser.getPDDocument(); StringWriter stringWriter = new StringWriter(); PDFTextStripper stripper = new PDFTextStripper(); stripper.setLineSeparator("\n"); stripper.writeText(pdDoc, stringWriter); text = stringWriter.toString(); stringWriter.close(); pdDoc.close(); } catch (Exception e) { _log.error(e.getMessage()); } } else if (fileExt.equals(".rtf")) { try { DefaultStyledDocument dsd = new DefaultStyledDocument(); RTFEditorKit rtfEditorKit = new RTFEditorKit(); rtfEditorKit.read(reader, dsd, 0); text = dsd.getText(0, dsd.getLength()); } catch (Exception e) { _log.error(e.getMessage()); } } else if (fileExt.equals(".xls")) { try { XLSTextStripper stripper = new XLSTextStripper(fis); text = stripper.getText(); } catch (Exception e) { _log.error(e.getMessage()); } } if (text != null) { return new Field(field, text, Field.Store.YES, Field.Index.NOT_ANALYZED); } else { return new Field(field, reader); } }
From source file:com.liferay.portlet.documentlibrary.util.LiferayPDFBoxConverter.java
License:Open Source License
public void generateImagesPB() throws Exception { PDDocument pdDocument = null; try {//from www . jav a 2 s . c o m pdDocument = PDDocument.load(_inputFile); PDDocumentCatalog pdDocumentCatalog = pdDocument.getDocumentCatalog(); PDFRenderer pdfRenderer = new PDFRenderer(pdDocument); PDPageTree pdPages = pdDocumentCatalog.getPages(); for (int i = 0; i < pdPages.getCount(); i++) { if (_generateThumbnail && (i == 0)) { _generateImagesPB(pdfRenderer, i, _thumbnailFile, _thumbnailExtension); } if (!_generatePreview) { break; } _generateImagesPB(pdfRenderer, i, _previewFiles[i], _extension); } } finally { if (pdDocument != null) { pdDocument.close(); } } }
From source file:com.liferay.portlet.documentlibrary.util.PDFProcessorImpl.java
License:Open Source License
private void _generateImagesPB(FileVersion fileVersion, InputStream inputStream) throws Exception { boolean generatePreview = _isGeneratePreview(fileVersion); boolean generateThumbnail = _isGenerateThumbnail(fileVersion); PDDocument pdDocument = null; try {//ww w . j a v a 2 s.c o m pdDocument = PDDocument.load(inputStream); PDDocumentCatalog pdDocumentCatalog = pdDocument.getDocumentCatalog(); List<PDPage> pdPages = pdDocumentCatalog.getAllPages(); for (int i = 0; i < pdPages.size(); i++) { PDPage pdPage = pdPages.get(i); if (generateThumbnail && (i == 0)) { _generateImagesPB(fileVersion, pdPage, i); if (_log.isInfoEnabled()) { _log.info("PDFBox generated a thumbnail for " + fileVersion.getFileVersionId()); } } if (!generatePreview) { break; } _generateImagesPB(fileVersion, pdPage, i + 1); } if (_log.isInfoEnabled() && generatePreview) { _log.info("PDFBox generated " + getPreviewFileCount(fileVersion) + " preview pages for " + fileVersion.getFileVersionId()); } } finally { if (pdDocument != null) { pdDocument.close(); } } }
From source file:com.lp.server.system.ejbfac.SystemFacBean.java
License:Open Source License
public byte[][] konvertierePDFFileInEinzelneBilder(String pdfFile, int resolution) { PDDocument document = null; byte[][] oBilder = null; try {// w w w. j a v a 2 s .c om document = PDDocument.load(pdfFile); List pages = document.getDocumentCatalog().getAllPages(); oBilder = new byte[pages.size()][]; for (int i = 0; i < pages.size(); i++) { PDPage page = (PDPage) pages.get(i); BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, resolution); oBilder[i] = Helper.imageToByteArray(image); } } catch (IOException e) { e.printStackTrace(); throw new EJBExceptionLP(EJBExceptionLP.FEHLER, e.getMessage()); } finally { if (document != null) { try { document.close(); } catch (IOException e) { e.printStackTrace(); throw new EJBExceptionLP(EJBExceptionLP.FEHLER, e.getMessage()); } } } return oBilder; }
From source file:com.mirth.connect.connectors.doc.DocumentDispatcher.java
License:Open Source License
private void encryptPDF(InputStream inputStream, OutputStream outputStream, String password) throws Exception { PDDocument document = null; try {/*from w w w . j a va 2 s .com*/ document = PDDocument.load(inputStream); AccessPermission accessPermission = new AccessPermission(); accessPermission.setCanAssembleDocument(false); accessPermission.setCanExtractContent(true); accessPermission.setCanExtractForAccessibility(false); accessPermission.setCanFillInForm(false); accessPermission.setCanModify(false); accessPermission.setCanModifyAnnotations(false); accessPermission.setCanPrint(true); accessPermission.setCanPrintDegraded(true); String ownerPassword = System.currentTimeMillis() + "+" + Runtime.getRuntime().freeMemory() + "+" + (ownerPasswordSeq++); StandardProtectionPolicy policy = new StandardProtectionPolicy(ownerPassword, password, accessPermission); policy.setEncryptionKeyLength(128); document.protect(policy); document.save(outputStream); } catch (Exception e) { throw e; } finally { if (document != null) { document.close(); } } }
From source file:com.mycompany.mavenproject4.NewMain.java
/** * @param args the command line arguments *//* w ww.j a v a 2s . c o m*/ public static void main(String[] args) { PDDocument doc; doc = new PDDocument(); doc.addPage(new PDPage()); try { doc.save("Empty PDF.pdf"); doc.close(); } catch (Exception io) { System.out.println(io); } }