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.olanto.converter.ConverterFactoryPDF.java

License:Open Source License

@Override
public void startConvertion() {
    _logger.debug("Start converting " + source.getName());

    boolean sort = true;
    String fixEncoding = "UTF-8";
    Charset encoding = Charset.forName(fixEncoding);
    int startPage = 1;
    int endPage = Integer.MAX_VALUE;

    Writer output = null;//from  ww w  .  j  ava 2s .c  o m
    Writer outputFile = null;
    PDDocument document = null;
    try {
        document = PDDocument.load(source);

        //            if (document.isEncrypted()) {
        //                _logger.warn("Try to extract text from encrypted document :" + source.getAbsolutePath());
        //                document.decrypt(password);
        //            }

        PDFTextStripper stripper = null;
        if (outputFormat.equalsIgnoreCase(Constants.HTML) || outputFormat.equalsIgnoreCase(Constants.HTM)) {
            stripper = new PDFText2HTML(fixEncoding);
        } else if (outputFormat.equalsIgnoreCase(Constants.TXT)) {
            stripper = new PDFTextStripper(fixEncoding);
        } else {
            _logger.warn("Could not convert PDF file to " + outputFormat);
        }

        if (stripper != null) {
            _logger.debug("open stripper : " + encoding.name());
            stripper.setSortByPosition(sort);
            stripper.setStartPage(startPage);
            stripper.setEndPage(endPage);

            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            output = new OutputStreamWriter(buf, encoding);
            output.write(stripper.getText(document));
            output.flush();

            outputFile = new OutputStreamWriter(new FileOutputStream(target), encoding);
            outputFile.write(buf.toString(encoding.name()));
            buf = null;
            success = true;
            _logger.debug("(writed) file : " + target.getAbsolutePath());
        } else {
            _logger.debug("Destination format not yet implemented");
        }

        //        } catch (CryptographyException ex) {
        //            _logger.error("(Error while decripting) file : " + source.getAbsolutePath(), ex);
        //        } catch (InvalidPasswordException ex) {
        //            _logger.error("(Bad password) file : " + source.getAbsolutePath(), ex);
    } catch (IOException ex) {
        _logger.error("(rejected cannot be opened) file : " + source.getAbsolutePath(), ex);
        success = false;
    } catch (Exception ex) {
        _logger.error("(rejected) file : " + source.getAbsolutePath(), ex);
        success = false;
    } finally {
        try {
            _logger.debug("(closing) file : " + target.getAbsolutePath());
            if (outputFile != null) {
                outputFile.close();
            }
            if (output != null) {
                output.close();
            }
            if (document != null) {
                document.close();
            }
        } catch (IOException e) {
            _logger.error("Could not close document", e);
        }
    }
}

From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java

License:Apache License

@Override
public Size thumbnailPDF(VFSLeaf pdfFile, VFSLeaf thumbnailFile, int maxWidth, int maxHeight) {
    InputStream in = null;/*from   w  ww .  j  a  v  a  2s. c om*/
    PDDocument document = null;
    try {
        WorkThreadInformations.setInfoFiles(null, pdfFile);
        WorkThreadInformations.set("Generate thumbnail VFSLeaf=" + pdfFile);
        in = pdfFile.getInputStream();
        document = PDDocument.load(in);
        if (document.isEncrypted()) {
            try {
                document.decrypt("");
            } catch (Exception e) {
                log.info("PDF document is encrypted: " + pdfFile);
                throw new CannotGenerateThumbnailException("PDF document is encrypted: " + pdfFile);
            }
        }
        List pages = document.getDocumentCatalog().getAllPages();
        PDPage page = (PDPage) pages.get(0);
        BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_BGR, 72);
        Size size = scaleImage(image, thumbnailFile, maxWidth, maxHeight);
        if (size != null) {
            return size;
        }
        return null;
    } catch (CannotGenerateThumbnailException e) {
        return null;
    } catch (Exception e) {
        log.warn("Unable to create image from pdf file.", e);
        return null;
    } finally {
        WorkThreadInformations.unset();
        FileUtils.closeSafely(in);
        if (document != null) {
            try {
                document.close();
            } catch (IOException e) {
                //only a try, fail silently
            }
        }
    }
}

From source file:org.olat.core.commons.services.thumbnail.impl.PDFToThumbnail.java

License:Apache License

@Override
public FinalSize generateThumbnail(VFSLeaf pdfFile, VFSLeaf thumbnailFile, int maxWidth, int maxHeight)
        throws CannotGenerateThumbnailException {
    InputStream in = null;//from   w  ww .j a va  2s .c om
    PDDocument document = null;
    try {
        in = pdfFile.getInputStream();
        document = PDDocument.load(in);
        if (document.isEncrypted()) {
            try {
                document.decrypt("");
            } catch (Exception e) {
                log.info("PDF document is encrypted: " + pdfFile);
                throw new CannotGenerateThumbnailException("PDF document is encrypted: " + pdfFile);
            }
        }
        List pages = document.getDocumentCatalog().getAllPages();
        PDPage page = (PDPage) pages.get(0);
        BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_BGR, 72);
        Size size = ImageHelper.scaleImage(image, thumbnailFile, maxWidth, maxHeight);
        return new FinalSize(size.getWidth(), size.getWidth());

    } catch (CannotGenerateThumbnailException e) {
        throw e;
    } catch (Exception e) {
        log.warn("Unable to create image from pdf file.", e);
        throw new CannotGenerateThumbnailException(e);
    } finally {
        FileUtils.closeSafely(in);
        if (document != null) {
            try {
                document.close();
            } catch (IOException e) {
                // only a try, fail silently
            }
        }
    }
}

From source file:org.olat.search.service.document.file.pdf.PdfBoxExtractor.java

License:Apache License

private FileContent extractTextFromPdf(VFSLeaf leaf) throws IOException, DocumentAccessException {
    if (log.isDebug())
        log.debug("readContent from pdf starts...");
    PDDocument document = null;
    BufferedInputStream bis = null;
    try {//from   w  w w . j ava  2 s .  co m
        bis = new BufferedInputStream(leaf.getInputStream());
        document = PDDocument.load(bis);
        if (document.isEncrypted()) {
            try {
                document.decrypt("");
            } catch (Exception e) {
                log.warn("PDF is encrypted. Can not read content file=" + leaf.getName());
                LimitedContentWriter writer = new LimitedContentWriter(128,
                        FileDocumentFactory.getMaxFileSize());
                writer.append(leaf.getName());
                writer.close();
                return new FileContent(leaf.getName(), writer.toString());
            }
        }
        String title = getTitle(document);
        if (log.isDebug())
            log.debug("readContent PDDocument loaded");
        PDFTextStripper stripper = new PDFTextStripper();
        LimitedContentWriter writer = new LimitedContentWriter(50000, FileDocumentFactory.getMaxFileSize());
        stripper.writeText(document, writer);
        writer.close();
        return new FileContent(title, writer.toString());
    } finally {
        if (document != null) {
            document.close();
        }
        if (bis != null) {
            bis.close();
        }
    }
}

From source file:org.olat.search.service.document.file.PdfDocument.java

License:Apache License

private String extractTextFromPdf(final VFSLeaf leaf) throws IOException, DocumentAccessException {
    if (log.isDebug()) {
        log.debug("readContent from pdf starts...");
    }/*from www  . j  ava 2 s  . c  o  m*/
    PDDocument document = null;
    BufferedInputStream bis = null;
    try {
        bis = new BufferedInputStream(leaf.getInputStream());
        document = PDDocument.load(bis);
        if (document.isEncrypted()) {
            try {
                document.decrypt("");
            } catch (final Exception e) {
                throw new DocumentAccessException(
                        "PDF is encrypted. Can not read content file=" + leaf.getName());
            }
        }
        if (log.isDebug()) {
            log.debug("readContent PDDocument loaded");
        }
        final PDFTextStripper stripper = new PDFTextStripper();
        return stripper.getText(document);
    } finally {
        if (document != null) {
            document.close();
        }
        if (bis != null) {
            bis.close();
        }
    }

}

From source file:org.omegat.filters2.pdf.PdfFilter.java

License:Open Source License

@Override
public BufferedReader createReader(File infile, String encoding) throws IOException {
    PDFTextStripper stripper;/*from   w  w  w .j  a  v  a 2 s.  c om*/
    stripper = new PDFTextStripper();
    stripper.setLineSeparator("\n");
    stripper.setSortByPosition(true);

    PDDocument document = PDDocument.load(infile.getAbsolutePath());
    String text = stripper.getText(document);
    document.close();

    return new BufferedReader(new StringReader(text));
}

From source file:org.opencps.util.ExtractTextLocations.java

License:Open Source License

public ExtractTextLocations(String fullPath) throws IOException {

    PDDocument document = null;

    try {//from w w w  . ja v a2 s. co  m
        File input = new File(fullPath);
        document = PDDocument.load(input);

        if (document.isEncrypted()) {
            try {
                document.decrypt(StringPool.BLANK);
            } catch (Exception e) {
                _log.error(e);
            }
        }

        // ExtractTextLocations printer = new ExtractTextLocations();

        List allPages = document.getDocumentCatalog().getAllPages();
        if (allPages != null && allPages.size() > 0) {
            PDPage page = (PDPage) allPages.get(0);

            PDStream contents = page.getContents();
            if (contents != null) {
                this.processStream(page, page.findResources(), page.getContents().getStream());
            }

            PDRectangle pageSize = page.findMediaBox();
            if (pageSize != null) {
                setPageWidth(pageSize.getWidth());
                setPageHeight(pageSize.getHeight());
                setPageLLX(pageSize.getLowerLeftX());
                setPageURX(pageSize.getUpperRightX());
                setPageLLY(pageSize.getLowerLeftY());
                setPageURY(pageSize.getUpperRightY());
            }
        }
    } catch (Exception e) {
        _log.error(e);
    } finally {
        if (document != null) {
            document.close();
        }
    }
}

From source file:org.opensingular.lib.commons.pdf.TestPDFUtil.java

License:Apache License

private int countPages(File file) {
    try (RandomAccessBufferedFileInputStream in = new RandomAccessBufferedFileInputStream(file)) {
        PDFParser parser = new PDFParser(in);
        parser.parse();/*from  w ww. ja  va 2  s .c  om*/
        PDDocument doc = parser.getPDDocument();
        int pages = doc.getNumberOfPages();
        doc.close();
        return pages;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.oscarehr.document.web.SplitDocumentAction.java

License:Open Source License

public ActionForward split(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    String docNum = request.getParameter("document");
    String[] commands = request.getParameterValues("page[]");

    Document doc = documentDAO.getDocument(docNum);

    //      String docdownload = oscar.OscarProperties.getInstance().getProperty("DOCUMENT_DIR");
    //      new File(docdownload);
    String docdownload = EDocUtil.getDocumentDir(doc.getDocfilename());

    String newFilename = doc.getDocfilename();
    String dbPrefix = EDocUtil.getDocumentPrefix(doc.getDocfilename());

    //      FileInputStream input = new FileInputStream(docdownload + doc.getDocfilename());
    FileInputStream input = new FileInputStream(EDocUtil.getDocumentPath(newFilename));
    PDFParser parser = new PDFParser(input);
    parser.parse();//from   w ww . ja  v a 2  s  .co  m
    PDDocument pdf = parser.getPDDocument();

    PDDocument newPdf = new PDDocument();

    List pages = pdf.getDocumentCatalog().getAllPages();

    if (commands != null) {
        for (String c : commands) {
            String[] command = c.split(",");
            int pageNum = Integer.parseInt(command[0]);
            int rotation = Integer.parseInt(command[1]);

            PDPage p = (PDPage) pages.get(pageNum - 1);
            p.setRotation(rotation);

            newPdf.addPage(p);
        }

    }

    //newPdf.save(docdownload + newFilename);

    if (newPdf.getNumberOfPages() > 0) {
        LoggedInInfo loggedInInfo = LoggedInInfo.loggedInInfo.get();

        //         EDoc newDoc = new EDoc("","", newFilename, "", loggedInInfo.loggedInProvider.getProviderNo(), doc.getDoccreator(), "", 'A', oscar.util.UtilDateUtilities.getToday("yyyy-MM-dd"), "", "", "demographic", "-1",0);
        EDoc newDoc = new EDoc("", "", EDocUtil.getDocumentFileName(newFilename), "",
                loggedInInfo.loggedInProvider.getProviderNo(), doc.getDoccreator(), "", 'A',
                oscar.util.UtilDateUtilities.getToday("yyyy-MM-dd"), "", "", "demographic", "-1", 0);
        newDoc.setFileName(dbPrefix + '.' + newDoc.getFileName());
        newDoc.setDocPublic("0");
        newDoc.setContentType("application/pdf");
        newDoc.setNumberOfPages(newPdf.getNumberOfPages());

        String newDocNo = EDocUtil.addDocumentSQL(newDoc);

        //         //newPdf.save(docdownload + newDoc.getFileName());
        System.gc(); //avoid Windows lock on channel
        newPdf.save(docdownload + EDocUtil.getDocumentFileName(newDoc.getFileName()));
        newPdf.close();

        WebApplicationContext ctx = WebApplicationContextUtils
                .getRequiredWebApplicationContext(request.getSession().getServletContext());
        ProviderInboxRoutingDao providerInboxRoutingDao = (ProviderInboxRoutingDao) ctx
                .getBean("providerInboxRoutingDAO");
        providerInboxRoutingDao.addToProviderInbox("0", newDocNo, "DOC");

        List<ProviderInboxItem> routeList = providerInboxRoutingDao.getProvidersWithRoutingForDocument("DOC",
                docNum);
        for (ProviderInboxItem i : routeList) {
            providerInboxRoutingDao.addToProviderInbox(i.getProviderNo(), newDocNo, "DOC");
        }

        providerInboxRoutingDao.addToProviderInbox(loggedInInfo.loggedInProvider.getProviderNo(), newDocNo,
                "DOC");

        QueueDocumentLinkDao queueDocumentLinkDAO = (QueueDocumentLinkDao) ctx.getBean("queueDocumentLinkDAO");
        Integer qid = 1;
        Integer did = Integer.parseInt(newDocNo.trim());
        queueDocumentLinkDAO.addToQueueDocumentLink(qid, did);

        ProviderLabRoutingDao providerLabRoutingDao = (ProviderLabRoutingDao) SpringUtils
                .getBean("providerLabRoutingDao");

        List<ProviderLabRoutingModel> result = providerLabRoutingDao.getProviderLabRoutingDocuments(docNum);
        if (!result.isEmpty()) {
            new ProviderLabRouting().route(newDocNo, result.get(0).getProviderNo(), "DOC");
        }

        PatientLabRoutingDao patientLabRoutingDao = (PatientLabRoutingDao) SpringUtils
                .getBean("patientLabRoutingDao");
        List<PatientLabRouting> result2 = patientLabRoutingDao.findDocByDemographic(docNum);

        if (!result2.isEmpty()) {
            PatientLabRouting newPatientRoute = new PatientLabRouting();

            newPatientRoute.setDemographicNo(result2.get(0).getDemographicNo());
            newPatientRoute.setLabNo(Integer.parseInt(newDocNo));
            newPatientRoute.setLabType("DOC");

            patientLabRoutingDao.persist(newPatientRoute);
        }

        DocumentDAO documentDao = (DocumentDAO) SpringUtils.getBean("documentDAO");
        CtlDocument result3 = documentDao.getCtrlDocument(Integer.parseInt(docNum));

        if (result3 != null) {
            CtlDocumentPK ctlDocumentPK = new CtlDocumentPK(Integer.parseInt(newDocNo), "demographic");
            CtlDocument newCtlDocument = new CtlDocument(ctlDocumentPK, result3.getModuleId());
            newCtlDocument.setStatus(result3.getStatus());
            documentDao.saveCtlDocument(newCtlDocument);
        }

    }

    pdf.close();
    input.close();

    return mapping.findForward("success");
}

From source file:org.oscarehr.document.web.SplitDocumentAction.java

License:Open Source License

public ActionForward rotate180(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());

    if (doc.getContenttype().equals("application/pdf")) {
        FileInputStream input = null;
        PDDocument pdf = null;
        try {/* ww  w  .  j ava 2  s .  c  om*/
            //      FileInputStream input = new FileInputStream(docdownload + doc.getDocfilename());
            input = new FileInputStream(docdownload);
            PDFParser parser = new PDFParser(input);
            parser.parse();
            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 + 180) % 360);

                ManageDocumentAction.deleteCacheVersion(doc, x);
                x++;
            }

            //      pdf.save(docdownload + doc.getDocfilename());
            pdf.save(docdownload);

        } finally {
            if (pdf != null)
                pdf.close();
            input.close();
        }

    } else if (doc.getContenttype().equals("image/jpg") || doc.getContenttype().equals("image/png")
            || doc.getContenttype().equals("image/gif")) {
        String documentDir = EDocUtil.getDocumentDir(doc.getDocfilename());
        File file = new File(documentDir + doc.getDocfilename());
        BufferedImage image = ImageIO.read(file);
        if (image == null)
            return null;
        BufferedImage rotatedImage = new BufferedImage(image.getHeight(), image.getWidth(),
                BufferedImage.TYPE_INT_ARGB);

        String suffix = null;
        String contentType = doc.getContenttype();
        if (contentType.equalsIgnoreCase("image/jpg") || contentType.equalsIgnoreCase("image/jpeg")) {
            suffix = "jpg";
        } else if (contentType.equalsIgnoreCase("image/png")) {
            suffix = "png";
        } else if (contentType.equalsIgnoreCase("image/gif")) {
            suffix = "gif";
        }
        AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
        tx = AffineTransform.getScaleInstance(-1, -1);
        tx.translate(-image.getWidth(null), -image.getHeight(null));
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        image = op.filter(image, null);
        ImageIO.write(image, suffix, file);
    } else {
        //umknown type - does nothing
    }

    return null;
}