Example usage for org.apache.pdfbox.pdmodel PDDocument close

List of usage examples for org.apache.pdfbox.pdmodel PDDocument close

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

This will close the underlying COSDocument object.

Usage

From source file:org.nuxeo.pdf.test.PDFTextExtractorTest.java

License:Open Source License

protected void checkPDFBeforeTest() throws IOException {

    PDDocument doc = PDDocument.load(pdfFile);
    assertNotNull(doc);//from  w w  w  .  j a  v  a2 s  . c om
    utils.track(doc);

    assertEquals(6, doc.getNumberOfPages());

    doc.close();
    utils.untrack(doc);
}

From source file:org.nuxeo.pdf.test.PDFUtilsTest.java

License:Open Source License

@Test
public void test_saveInTempFile() throws Exception {

    PDDocument doc = PDDocument.load(pdfFile);
    utils.track(doc);//from   w ww  .j  a  v a  2s .c  o m

    FileBlob fb = PDFUtils.saveInTempFile(doc);
    assertNotNull(fb);
    assertEquals("application/pdf", fb.getMimeType());

    doc.close();
    utils.untrack(doc);
}

From source file:org.nuxeo.pdf.test.PDFUtilsTest.java

License:Open Source License

@Test
public void test_setInfos() throws Exception {

    PDDocument doc = PDDocument.load(pdfFile);
    utils.track(doc);/*from w w w .  j  a va  2  s  .  c  o  m*/

    PDDocumentInformation docInfoOriginal = doc.getDocumentInformation();
    // Check original document has the expected values
    assertEquals("Untitled 3", docInfoOriginal.getTitle());
    assertNull(docInfoOriginal.getSubject());
    assertNull(docInfoOriginal.getAuthor());
    // Now, modify
    // First, actually, don't modify
    PDFUtils.setInfos(doc, null, "", null);
    PDDocumentInformation newDocInfo = doc.getDocumentInformation();
    assertEquals(docInfoOriginal.getTitle(), newDocInfo.getTitle());
    assertEquals(docInfoOriginal.getSubject(), newDocInfo.getSubject());
    assertEquals(docInfoOriginal.getAuthor(), newDocInfo.getAuthor());
    // Now, modify
    PDFUtils.setInfos(doc, "The Title", "The Subject", "The Author");
    newDocInfo = doc.getDocumentInformation();
    assertEquals("The Title", newDocInfo.getTitle());
    assertEquals("The Subject", newDocInfo.getSubject());
    assertEquals("The Author", newDocInfo.getAuthor());

    doc.close();
    utils.untrack(doc);
}

From source file:org.nuxeo.pdf.test.PDFWatermarkingTest.java

License:Open Source License

protected void checkHasWatermarkOnAllPages(Blob inBlob, String inWatermark) throws Exception {

    PDDocument doc = PDDocument.load(inBlob.getStream());
    utils.track(doc);/*from  w  w w  .java2s. c om*/

    int count = doc.getNumberOfPages();
    for (int i = 1; i <= count; i++) {
        String txt = utils.extractText(doc, i, i);
        int pos = txt.indexOf(inWatermark);
        assertTrue("for page " + i + ", found pos is " + pos, pos > -1);
    }

    doc.close();
    utils.untrack(doc);
}

From source file:org.nuxeo.pdf.test.PDFWatermarkingTest.java

License:Open Source License

protected void checkHasImage(Blob inBlob, int inExpectedWidth, int inExpectedHeight) throws Exception {

    PDDocument doc = PDDocument.load(inBlob.getStream());
    utils.track(doc);/*  w  w  w .  ja v a2  s .  c  om*/

    List<?> allPages = doc.getDocumentCatalog().getAllPages();
    int max = allPages.size();
    for (int i = 1; i < max; i++) {
        PDPage page = (PDPage) allPages.get(i);

        PDResources pdResources = page.getResources();
        Map<String, PDXObject> allXObjects = pdResources.getXObjects();
        assertNotNull(allXObjects);

        boolean gotIt = false;
        for (Map.Entry<String, PDXObject> entry : allXObjects.entrySet()) {
            PDXObject xobject = entry.getValue();
            if (xobject instanceof PDXObjectImage) {
                PDXObjectImage pdxObjectImage = (PDXObjectImage) xobject;
                if (inExpectedWidth == pdxObjectImage.getWidth()
                        && inExpectedHeight == pdxObjectImage.getHeight()) {
                    gotIt = true;
                    break;
                }
            }
        }
        assertTrue("Page " + i + "does not have the image", gotIt);
    }

    doc.close();
    utils.untrack(doc);
}

From source file:org.nuxeo.pdf.test.TestUtils.java

License:Open Source License

public void cleanup() {

    for (PDDocument pdfDoc : createdPDDocs) {
        try {/*from  w  ww  .ja v a2  s  . c  om*/
            pdfDoc.close();
        } catch (Exception e) {
            // Nothing
        }
    }
    createdPDDocs = new ArrayList<PDDocument>();

    for (File f : createdTempFiles) {
        try {
            f.delete();
        } catch (Exception e) {
            // Nothing
        }
    }
    createdTempFiles = new ArrayList<File>();
}

From source file:org.nuxeo.typeDocPkg.TestPdfBoxN.java

License:Apache License

public void create(String message, String outfile) throws IOException, COSVisitorException {
    PDDocument doc = null;
    try {//from w w  w.  jav  a 2  s  . co  m
        doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);
        PDPageContentStream contentStream = new PDPageContentStream(doc, page);
        PDFont font = PDType1Font.HELVETICA;
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.moveTextPositionByAmount(100, 700);
        contentStream.drawString(message);
        contentStream.endText();
        contentStream.close();
        doc.save(outfile);
        doc.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.nuxeo.webpage.archiver.test.Utils.java

License:Open Source License

public static boolean hasText(Blob inBlob, String inText) throws IOException {

    boolean hasIt = false;
    PDDocument pdfDoc = null;

    try {/*from   w  ww  . j  av a  2  s . c o m*/
        pdfDoc = PDDocument.load(inBlob.getFile());
        PDFTextStripper stripper = new PDFTextStripper();
        String txt;
        int max = pdfDoc.getNumberOfPages();
        for (int i = 1; i <= max; ++i) {
            stripper.setStartPage(i);
            stripper.setEndPage(i);
            txt = stripper.getText(pdfDoc);
            if (txt.indexOf(inText) > -1) {
                hasIt = true;
                break;
            }
        }
    } finally {
        if (pdfDoc != null) {
            pdfDoc.close();
        }
    }

    return hasIt;
}

From source file:org.nuxeo.webpage.archiver.WebpageToBlob.java

License:Apache License

protected boolean pdfLooksValid(File inPdf) {

    boolean valid = false;

    if (inPdf.exists() && inPdf.length() > 0) {
        PDDocument pdfDoc = null;
        try {//from ww  w.  jav  a2s . c  o  m
            pdfDoc = PDDocument.load(inPdf);
            if (pdfDoc.getNumberOfPages() > 0) {
                valid = true;
            }
        } catch (IOException e) {
            // Nothing
        } finally {
            if (pdfDoc != null) {
                try {
                    pdfDoc.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }
    }

    return valid;
}

From source file:org.ochan.control.ThumbnailController.java

License:Open Source License

@SuppressWarnings("unchecked")
private BufferedImage takeCaptureOfPDFPage1(byte[] data) {
    try {/*from   w  w w. j a  v a 2  s. co  m*/
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        PDDocument document = PDDocument.load(bais);
        // get the first page.
        List<PDPage> pages = (List<PDPage>) document.getDocumentCatalog().getAllPages();
        PDPage page = pages.get(0);
        BufferedImage image = page.convertToImage();
        document.close();
        return image;
    } catch (Exception e) {
        LOG.error("Unable to convert pdf page 1 into godlike image", e);
    }
    return null;
}