List of usage examples for org.apache.pdfbox.pdmodel PDDocument close
@Override public void close() throws IOException
From source file:org.oscarehr.document.web.SplitDocumentAction.java
License:Open Source License
public ActionForward rotate90(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Document doc = documentDAO.getDocument(request.getParameter("document")); FileInputStream input = new FileInputStream(EDocUtil.getDocumentPath(doc.getDocfilename())); PDFParser parser = new PDFParser(input); parser.parse();/*from w w w .j a v a 2 s . co m*/ PDDocument pdf = parser.getPDDocument(); int x = 1; for (Object p : pdf.getDocumentCatalog().getAllPages()) { PDPage pg = (PDPage) p; Integer r = (pg.getRotation() != null ? pg.getRotation() : 0); pg.setRotation((r + 90) % 360); ManageDocumentAction.deleteCacheVersion(doc, x); x++; } pdf.save(EDocUtil.getDocumentPath(doc.getDocfilename())); pdf.close(); input.close(); return null; }
From source file:org.oscarehr.document.web.SplitDocumentAction.java
License:Open Source License
public ActionForward removeFirstPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Document doc = documentDAO.getDocument(request.getParameter("document")); // String docdownload = oscar.OscarProperties.getInstance().getProperty("DOCUMENT_DIR"); String docdownload = EDocUtil.getDocumentPath(doc.getDocfilename()); // FileInputStream input = new FileInputStream(docdownload + doc.getDocfilename()); FileInputStream input = new FileInputStream(docdownload); PDFParser parser = new PDFParser(input); parser.parse();/*from w ww .j a v a 2s. c o m*/ PDDocument pdf = parser.getPDDocument(); // Documents must have at least 2 pages, for the first page to be removed. if (pdf.getNumberOfPages() <= 1) { return null; } int x = 1; for (Object p : pdf.getDocumentCatalog().getAllPages()) { ManageDocumentAction.deleteCacheVersion(doc, x); x++; } pdf.removePage(0); EDocUtil.subtractOnePage(request.getParameter("document")); // pdf.save(docdownload + doc.getDocfilename()); System.gc(); //avoid Win channel lock problem pdf.save(docdownload); pdf.close(); input.close(); return null; }
From source file:org.paxle.parser.pdf.impl.PdfParser.java
License:Open Source License
public IParserDocument parse(URI location, String charset, InputStream fileIn) throws ParserException, UnsupportedEncodingException, IOException { IParserDocument parserDoc = null;/*from ww w . j a v a 2 s. com*/ PDDocument pddDoc = null; try { final IParserContext pc = this.contextLocal.getCurrentContext(); final ICommandProfile cmdProfile = pc.getCommandProfile(); // create an empty document parserDoc = pc.createDocument(); // parse it final PDFParser parser = new PDFParser(fileIn); parser.parse(); pddDoc = parser.getPDDocument(); // check document encryption if (pddDoc.isEncrypted()) { if (this.logger.isDebugEnabled()) { this.logger.debug(String.format("Document '%s' is encrypted.", location)); } // determine the decryption password String pwd = ""; if (cmdProfile != null) { String tmp = (String) cmdProfile.getProperty("org.paxle.parser.pdf.impl.decryptionPassword"); if (tmp != null) pwd = tmp; } // try to open document with the given password try { final StandardDecryptionMaterial dm = new StandardDecryptionMaterial(pwd); pddDoc.openProtection(dm); final AccessPermission accessPermission = pddDoc.getCurrentAccessPermission(); if (accessPermission == null || !accessPermission.canExtractContent()) { if (this.logger.isInfoEnabled()) { this.logger.debug( String.format("No permission to extract content of document '%s'.", location)); } parserDoc.setStatus(IParserDocument.Status.FAILURE, "PDF Document is encrypted."); return parserDoc; } } catch (Throwable e) { this.logger.error(String.format("Unable to decrypt document '%s'.", location), e); parserDoc.setStatus(IParserDocument.Status.FAILURE, String .format("Unable to decrypt document. %s: %s", e.getClass().getName(), e.getMessage())); return parserDoc; } } // extract metadata this.extractMetaData(parserDoc, pddDoc); // extract text final PDFTextStripper stripper = new PDFTextStripper(); // XXX: we could limit the amount of parsed pages via crawling-profile properties? // stripper.setStartPage(startPageValue); // stripper.setEndPage(endPageValue); final Writer pdocWriter = parserDoc.getTextWriter(); stripper.writeText(pddDoc, pdocWriter); pdocWriter.flush(); // extracting URIs this.extractURLs(parserDoc, pddDoc); // extracting embedded files this.extractEmbeddedFiles(location, parserDoc, pddDoc); parserDoc.setStatus(IParserDocument.Status.OK); return parserDoc; } catch (Throwable e) { throw new ParserException("Error parsing pdf document. " + e.getMessage(), e); } finally { if (pddDoc != null) try { pddDoc.close(); } catch (Exception e) { this.logger.error(e); } } }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public List<String> split(final String inputUri, final String outputUri, final List<Integer> pages) throws IOException, COSVisitorException { final List<String> result = new ArrayList<String>(); if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && CollectionUtils.isNotEmpty(pages)) { final PDDocument doc = PDDocument.load(inputUri); final List<PDDocument> splittedDocs = new ArrayList<PDDocument>(); @SuppressWarnings("unchecked") final List<PDPage> pagesList = doc.getDocumentCatalog().getAllPages(); // This section creates a new document for each split // indicated into the list, except the last one. Integer currentPage = 0;/*from w w w . j a v a2s .c o m*/ for (final Integer page : pages) { final PDDocument document = new PDDocument(); for (Integer i = currentPage; i <= page - 2; i++) { document.addPage(pagesList.get(i)); } splittedDocs.add(document); currentPage = page - 1; document.close(); } // This section splits the last document final PDDocument lastDocument = new PDDocument(); for (Integer i = currentPage; i < pagesList.size(); i++) { lastDocument.addPage(pagesList.get(i)); } splittedDocs.add(lastDocument); lastDocument.close(); Integer subIndex = 1; for (final PDDocument document : splittedDocs) { final String extension = this.converterUtils.addSubIndexBeforeExtension(outputUri, subIndex++); document.save(extension); result.add(extension); } doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } return result; }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public List<String> split(final String inputUri, final String outputUri, final Integer pages) throws IOException, COSVisitorException { final List<String> result = new ArrayList<String>(); if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && pages != null) { final PDDocument doc = PDDocument.load(inputUri); final Splitter splitter = new Splitter(); splitter.setSplitAtPage(pages);/*from w w w . j a va 2 s .c o m*/ final List<PDDocument> splittedDocs = splitter.split(doc); Integer subIndex = 1; for (final PDDocument document : splittedDocs) { final String extension = this.converterUtils.addSubIndexBeforeExtension(outputUri, subIndex++); document.save(extension); result.add(extension); document.close(); } doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } return result; }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void protect(final String inputUri, final String outputUri, final String password) throws IOException, BadSecurityHandlerException, COSVisitorException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && StringUtils.isNotBlank(password)) { final PDDocument doc = PDDocument.load(inputUri); final StandardProtectionPolicy pp = new StandardProtectionPolicy(password, password, new AccessPermission()); doc.protect(pp);// w w w.jav a 2s. co m doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void unProtect(final String inputUri, final String outputUri, final String password) throws IOException, COSVisitorException, BadSecurityHandlerException, CryptographyException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && StringUtils.isNotBlank(password)) { final PDDocument doc = PDDocument.load(inputUri); final DecryptionMaterial decryptionMaterial = new StandardDecryptionMaterial(password); doc.openProtection(decryptionMaterial); final StandardProtectionPolicy pp = new StandardProtectionPolicy(null, null, new AccessPermission()); doc.protect(pp);/*w w w .j a v a 2 s . co m*/ doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void putWatermark(final String inputUri, final String outputUri, final String text, final Color color, final Float alpha, final WatermarkPosition watermarkPosition, final List<Integer> pages) throws IOException, COSVisitorException, WatermarkOutOfLengthException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && StringUtils.isNotBlank(text) && color != null && alpha != null && watermarkPosition != null) { // If watermark position is not centered, then max length is the // same for landscape and portrait, text length is tested here so // there is no need to continue. if (!WatermarkPosition.CENTER.equals(watermarkPosition) && text.length() > watermarkPosition.getMaxLengthPortrait()) { throw new WatermarkOutOfLengthException(Constants.WATERMARK_OUT_OF_LENGTH_EXCEPTION_MESSAGE); }// www. ja va 2 s .co m final PDDocument doc = PDDocument.load(inputUri); final List<?> allPages = doc.getDocumentCatalog().getAllPages(); this.converterUtils.deleteNonSelectedPositions(allPages, pages); if (CollectionUtils.isNotEmpty(allPages)) { for (final Object object : allPages) { final PDPage page = (PDPage) object; // The transparency, opacity of graphic objects can be set // directly // on the drawing commands but need to be set to a graphic // state // which will become part of the resources. Graphic state is // set // up. this.watermarkUtils.setUpGraphicState(page, alpha); // Now we will be able to call the state definition before // doing // the // drawing try { this.watermarkUtils.addWatermark(doc, page, color, text, watermarkPosition); } catch (final WatermarkOutOfLengthException e) { doc.close(); throw e; } } } doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void addBookmarks(final String inputUri, final String outputUri, final String title, final List<PDFGalBookmark> pdfGalBookmarksList) throws IOException, COSVisitorException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && StringUtils.isNotEmpty(title) && CollectionUtils.isNotEmpty(pdfGalBookmarksList)) { final PDDocument doc = PDDocument.load(inputUri); final PDDocumentOutline outline = new PDDocumentOutline(); doc.getDocumentCatalog().setDocumentOutline(outline); final PDOutlineItem pagesOutline = new PDOutlineItem(); pagesOutline.setTitle(title);/*from w w w.j av a 2 s. c om*/ @SuppressWarnings("unchecked") final List<PDPage> pages = doc.getDocumentCatalog().getAllPages(); outline.appendChild(pagesOutline); for (final PDFGalBookmark pdfGalBookmark : pdfGalBookmarksList) { if (pdfGalBookmark != null && pdfGalBookmark.isInitializated()) { final PDPage page = pages.get(pdfGalBookmark.getPage() - 1); final PDPageFitWidthDestination dest = new PDPageFitWidthDestination(); dest.setPage(page); final PDOutlineItem bookmark = new PDOutlineItem(); bookmark.setDestination(dest); bookmark.setTitle(pdfGalBookmark.getText()); pagesOutline.appendChild(bookmark); } } pagesOutline.openNode(); outline.openNode(); doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void reIndexPageNumbers(final String inputUri, final String outputUri, final List<PDFGalPageNumbering> pdfGalPageNumberingList) throws IOException, COSVisitorException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && CollectionUtils.isNotEmpty(pdfGalPageNumberingList)) { final PDDocument doc = PDDocument.load(inputUri); final PDPageLabels pdPageLabels = new PDPageLabels(doc); for (final PDFGalPageNumbering pageNumbering : pdfGalPageNumberingList) { if (pageNumbering.isInitializated()) { final PDPageLabelRange pdPageLabelRange = new PDPageLabelRange(); pdPageLabelRange.setStyle(pageNumbering.getNumberingStyle().getValue()); pdPageLabels.setLabelItem(pageNumbering.getPageNumber() - 1, pdPageLabelRange); }/*w w w.j a va 2 s .c o m*/ } doc.getDocumentCatalog().setPageLabels(pdPageLabels); doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }