List of usage examples for org.apache.pdfbox.pdmodel PDDocument close
@Override public void close() throws IOException
From source file:org.nuxeo.pdf.PDFPageExtractor.java
License:Open Source License
/** * Return a Blob built from page <code>inStartPage</code> to * <code>inEndPage</code> (inclusive). * <p>/*w w w .j a v a 2s .com*/ * If <code>inEndPage</code> is greater than the number of pages in the * source document, it will go to the end of the document. If * <code>inStartPage</code> is less than 1, it'll start with page 1. If * <code>inStartPage</code> is greater than <code>inEndPage</code> or * greater than the number of pages in the source document, a blank document * will be returned. * <p> * If fileName is null or "", if is set to the original name + the page * range: mydoc.pdf and pages 10-75 +> mydoc-10-75.pdf * <p> * The mimetype is always set to "application/pdf" * <p> * Can set the title, subject and author of the resulting PDF. * <b>Notice</b>: If the value is null or "", it is just ignored * * @param inStartPage * @param inEndPage * @param inFileName * @param inTitle * @param inSubject * @param inAuthor * @return FileBlob * */ public Blob extract(int inStartPage, int inEndPage, String inFileName, String inTitle, String inSubject, String inAuthor) { Blob result = null; PDDocument pdfDoc = null; PDDocument extracted = null; try { pdfDoc = PDDocument.load(pdfBlob.getStream()); PageExtractor pe = new PageExtractor(pdfDoc, inStartPage, inEndPage); extracted = pe.extract(); PDFUtils.setInfos(extracted, inTitle, inSubject, inAuthor); result = PDFUtils.saveInTempFile(extracted); result.setMimeType("application/pdf"); if (inFileName == null || inFileName.isEmpty()) { String originalName = pdfBlob.getFilename(); if (originalName == null || originalName.isEmpty()) { originalName = "extracted"; } else { int pos = originalName.toLowerCase().lastIndexOf(".pdf"); if (pos > 0) { originalName = originalName.substring(0, pos); } } inFileName = originalName + "-" + inStartPage + "-" + inEndPage + ".pdf"; } result.setFilename(inFileName); extracted.close(); } catch (IOException | COSVisitorException e) { throw new ClientException(e); } finally { if (pdfDoc != null) { try { pdfDoc.close(); } catch (IOException e) { log.error("Error closing the PDDocument", e); } } if (extracted != null) { try { extracted.close(); } catch (IOException e) { // Nothing } } } return result; }
From source file:org.nuxeo.pdf.PDFPageNumbering.java
License:Open Source License
/** * Add page numbers and returns a <i>new</i> Blob. Original blob is not * modified. This code assumes://from ww w . j av a 2 s . c o m * <ul> * <li>There is no page numbers already (it always draw the numbers)</li> * <li>The pdf is not rotated</li> * <li>Default values apply: * <ul> * <li><code>inStartAtPage</code> and <code>inStartAtNumber</code> are set * to 1 if they are passed < 1.</li> * <li>If <code>inStartAtPage</code> is > number of pages it also is reset * to 1</li> * <li><code>inFontName</code> is set to "Helvetica" if "" or null</li> * <li><code>inFontSize</code> is <= 0, it is set to 16</li> * <li><code>inHex255Color</code> is set to black if "", null or if its * length < 6. Expected format is 0xrrggbb, #rrggbb or just rrggbb</li> * <li><code>inPosition</code> is set to <code>BOTTOM_RIGHT</code> if null</li> * </ul> * </li> * <li></li> * </ul> * * @param inBlob * @param inStartAtPage * @param inStartAtNumber * @param inFontName * @param inFontSize * @param inHex255Color * @param inPosition * @return Blob * @throws IOException * @throws COSVisitorException * * @since 5.9.5 */ public Blob addPageNumbers(int inStartAtPage, int inStartAtNumber, String inFontName, float inFontSize, String inHex255Color, PAGE_NUMBER_POSITION inPosition) throws IOException, COSVisitorException { Blob result = null; PDDocument doc = null; inStartAtPage = inStartAtPage < 1 ? 1 : inStartAtPage; int pageNumber = inStartAtNumber < 1 ? 1 : inStartAtNumber; inFontSize = inFontSize <= 0 ? DEFAULT_FONT_SIZE : inFontSize; int[] rgb = PDFUtils.hex255ToRGB(inHex255Color); try { doc = PDDocument.load(blob.getStream()); List<?> allPages; PDFont font; int max; if (inFontName == null || inFontName.isEmpty()) { font = PDType1Font.HELVETICA; } else { font = PDType1Font.getStandardFont(inFontName); if (font == null) { font = new PDType1Font(inFontName); } } allPages = doc.getDocumentCatalog().getAllPages(); max = allPages.size(); inStartAtPage = inStartAtPage > max ? 1 : inStartAtPage; for (int i = inStartAtPage; i <= max; i++) { String pageNumAsStr = "" + pageNumber; pageNumber += 1; PDPage page = (PDPage) allPages.get(i - 1); PDPageContentStream footercontentStream = new PDPageContentStream(doc, page, true, true); float stringWidth = font.getStringWidth(pageNumAsStr) * inFontSize / 1000f; float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * inFontSize / 1000; PDRectangle pageRect = page.findMediaBox(); float xMoveAmount, yMoveAmount; if (inPosition == null) { inPosition = PAGE_NUMBER_POSITION.BOTTOM_RIGHT; } switch (inPosition) { case BOTTOM_LEFT: xMoveAmount = 10; yMoveAmount = pageRect.getLowerLeftY() + 10; break; case BOTTOM_CENTER: xMoveAmount = (pageRect.getUpperRightX() / 2) - (stringWidth / 2); yMoveAmount = pageRect.getLowerLeftY() + 10; break; case TOP_LEFT: xMoveAmount = 10; yMoveAmount = pageRect.getHeight() - stringHeight - 10; break; case TOP_CENTER: xMoveAmount = (pageRect.getUpperRightX() / 2) - (stringWidth / 2); yMoveAmount = pageRect.getHeight() - stringHeight - 10; break; case TOP_RIGHT: xMoveAmount = pageRect.getUpperRightX() - 10 - stringWidth; yMoveAmount = pageRect.getHeight() - stringHeight - 10; break; // Bottom-right is the default default: xMoveAmount = pageRect.getUpperRightX() - 10 - stringWidth; yMoveAmount = pageRect.getLowerLeftY() + 10; break; } footercontentStream.beginText(); footercontentStream.setFont(font, inFontSize); footercontentStream.moveTextPositionByAmount(xMoveAmount, yMoveAmount); footercontentStream.setNonStrokingColor(rgb[0], rgb[1], rgb[2]); footercontentStream.drawString(pageNumAsStr); footercontentStream.endText(); footercontentStream.close(); } File tempFile = File.createTempFile("pdfutils-", ".pdf"); doc.save(tempFile); result = new FileBlob(tempFile); Framework.trackFile(tempFile, result); } finally { if (doc != null) { doc.close(); } } return result; }
From source file:org.nuxeo.pdf.PDFTextExtractor.java
License:Open Source License
public String getAllExtractedLines() throws IOException { PDDocument pdfDoc = null; PDFTextStripper stripper = new PDFTextStripper(); if (extractedAllAsString == null) { try {//from www .j ava 2 s . c om pdfDoc = PDDocument.load(pdfBlob.getStream()); extractedAllAsString = stripper.getText(pdfDoc); } catch (IOException e) { throw new ClientException(e); } finally { if (pdfDoc != null) { try { pdfDoc.close(); } catch (IOException e) { log.error("Error closing the PDDocument", e); } } } } return extractedAllAsString; }
From source file:org.nuxeo.pdf.PDFUtils.java
License:Open Source License
public static void closeSilently(PDDocument... inPdfDocs) { for (PDDocument theDoc : inPdfDocs) { if (theDoc != null) { try { theDoc.close(); } catch (IOException e) { // Ignore }// w ww.ja v a 2 s . c om } } }
From source file:org.nuxeo.pdf.test.PDFMergeTest.java
License:Open Source License
protected void checkMergedPDF(Blob inBlob, boolean jutsFirst2Pages) throws IOException { File tempFile = File.createTempFile("testmergepdf", ".pdf"); utils.track(tempFile);/*from ww w .jav a 2s.c om*/ inBlob.transferTo(tempFile); PDDocument doc = PDDocument.load(tempFile); assertNotNull(doc); utils.track(doc); // 2 + 3 + 1 if (jutsFirst2Pages) { assertEquals(5, doc.getNumberOfPages()); } else { assertEquals(6, doc.getNumberOfPages()); } String txt; txt = utils.extractText(doc, 1, 1); assertTrue(txt.indexOf(MERGEPDF_CHECK_PREFIX + "1") > -1); txt = utils.extractText(doc, 3, 3); assertTrue(txt.indexOf(MERGEPDF_CHECK_PREFIX + "2") > -1); if (!jutsFirst2Pages) { txt = utils.extractText(doc, 6, 6); assertTrue(txt.indexOf(MERGEPDF_CHECK_PREFIX + "3") > -1); } doc.close(); utils.untrack(doc); tempFile.delete(); utils.untrack(tempFile); }
From source file:org.nuxeo.pdf.test.PDFPageExtractorTest.java
License:Open Source License
protected void checkPDFBeforeTest() throws IOException { PDDocument doc = PDDocument.load(pdfFile); assertNotNull(doc);/*from w ww.j a v a 2s .com*/ utils.track(doc); assertEquals(13, doc.getNumberOfPages()); doc.close(); utils.untrack(doc); }
From source file:org.nuxeo.pdf.test.PDFPageExtractorTest.java
License:Open Source License
protected void checkExtractedPdf(Blob inBlob, int inExpectedPageCount, String inExpectedTextAtPos0) throws Exception { PDDocument doc = PDDocument.load(inBlob.getStream()); utils.track(doc);//from w w w. j a v a2s . com assertEquals(inExpectedPageCount, doc.getNumberOfPages()); String txt = utils.extractText(doc, 1, 1); assertEquals(0, txt.indexOf(inExpectedTextAtPos0)); doc.close(); utils.untrack(doc); }
From source file:org.nuxeo.pdf.test.PDFPageExtractorTest.java
License:Open Source License
@Test public void testExtractPages_WithSetInfo() throws Exception { Blob extracted;/* ww w .j a v a 2s .co m*/ String originalName = pdfFileBlob.getFilename().replace(".pdf", ""); PDFPageExtractor pe = new PDFPageExtractor(pdfFileBlob); extracted = pe.extract(5, 9, null, "One Upon a Time", "Fairyland", "Cool Author"); assertTrue(extracted instanceof FileBlob); assertEquals(originalName + "-5-9.pdf", extracted.getFilename()); PDDocument doc = PDDocument.load(extracted.getStream()); utils.track(doc); PDDocumentInformation docInfo = doc.getDocumentInformation(); assertEquals("One Upon a Time", docInfo.getTitle()); assertEquals("Fairyland", docInfo.getSubject()); assertEquals("Cool Author", docInfo.getAuthor()); doc.close(); utils.untrack(doc); }
From source file:org.nuxeo.pdf.test.PDFPageNumberingTest.java
License:Open Source License
protected void checkPDFBeforeTest() throws IOException { PDDocument doc = PDDocument.load(pdfFile); assertNotNull(doc);//from w ww . ja va 2s. com utils.track(doc); assertEquals(13, doc.getNumberOfPages()); PDFTextStripper stripper = new PDFTextStripper(); String allTheText = stripper.getText(doc); for (int i = 0; i < 10; i++) { assertEquals(-1, allTheText.indexOf("" + i)); } doc.close(); utils.untrack(doc); }
From source file:org.nuxeo.pdf.test.PDFPageNumberingTest.java
License:Open Source License
protected void checkHasNumberInPage(File inDoc, int inExpected, int inPageNumber, String inPosition) throws IOException { PDDocument doc = PDDocument.load(inDoc); assertNotNull(doc);/*from w w w . j a va 2s. com*/ utils.track(doc); String text = utils.extractText(doc, inPageNumber, inPageNumber); int pos = text.indexOf("" + inExpected); assertTrue(inPosition + ", expecting " + inExpected + " for page " + inPageNumber, pos > -1); doc.close(); utils.untrack(doc); }