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:de.catma.document.source.contenthandler.PDFContentHandler.java

License:Open Source License

public void load(InputStream is) throws IOException {
    PDDocument document = null;
    try {/*  ww  w.  j a  v  a2s. com*/
        document = PDDocument.load(is, false);

        if (document.isEncrypted()) {
            throw new IOException("can not open pdf document because it is encrypted");
        }

        AccessPermission ap = document.getCurrentAccessPermission();
        if (!ap.canExtractContent()) {
            throw new IOException("You do not have permission to extract text");
        }

        PDFTextStripper stripper = new PDFTextStripper("UTF-8");

        stripper.setForceParsing(false);
        stripper.setSortByPosition(false);
        stripper.setShouldSeparateByBeads(true);
        stripper.setStartPage(1);
        stripper.setEndPage(Integer.MAX_VALUE);

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Writer w = new OutputStreamWriter(os);
        try {
            stripper.writeText(document, w);
        } finally {
            w.close();
        }
        // some pdfs seem to include non valid unicode characters
        // and this causes problems when converting text to HTML
        // for GUI delivery and during indexing 
        setContent(os.toString().replaceAll("[^\\x09\\x0A\\x0D\\x20-\\uD7FF\\uE000-\\uFFFD\\u10000-\\u10FFFF]",
                "?"));
    } finally {
        if (document != null) {
            document.close();
        }
    }
}

From source file:de.csw.linkgenerator.plugin.lucene.textextraction.PDFTextExtractor.java

License:Apache License

public String getText(byte[] data) throws Exception {
    PDDocument pdfDocument = null;
    try {//  w w w.jav  a 2  s.  co  m
        PDFParser parser = new PDFParser(new ByteArrayInputStream(data));
        parser.parse();

        pdfDocument = parser.getPDDocument();

        Writer writer = new CharArrayWriter();
        PDFTextStripper stripper = new PDFTextStripper();
        stripper.writeText(pdfDocument, writer);

        return writer.toString();
    } finally {
        if (pdfDocument != null)
            pdfDocument.close();
    }
}

From source file:de.dominicscheurer.quicktxtview.view.FileViewerController.java

License:Open Source License

private String getFileContentsInDirectoryHTML(File directory) {
    final StringBuilder sb = new StringBuilder();
    final File[] files = listFiles(directory);

    if (files == null) {
        return "";
    }/*from  w ww .j a v a 2s.  c  o m*/

    sb.append("<html>").append("<body>").append("<style type=\"text/css\">").append(fileTreeViewerCSS)
            .append("</style>");
    for (File file : files) {
        try {
            String contentsString;

            if (file.getName().endsWith(".pdf")) {
                final PDDocument doc = PDDocument.load(file);
                final StringWriter writer = new StringWriter();
                new PDFText2HTML("UTF-8").writeText(doc, writer);

                contentsString = writer.toString();

                writer.close();
                doc.close();
            } else {
                byte[] encoded = Files.readAllBytes(file.toPath());
                contentsString = new String(encoded, charset);

                contentsString = contentsString.replace("<", "&lt;");
                contentsString = contentsString.replace(">", "&gt;");
                contentsString = contentsString.replace("\n", "<br/>");
            }

            sb.append("<div class=\"entry\"><h3>").append(file.getName()).append("</h3>")
                    .append("<div class=\"content\">").append(contentsString).append("</div>").append("</div>");
        } catch (IOException e) {
        }
    }
    sb.append("</body></html>");

    return sb.toString();
}

From source file:de.fau.amos4.util.ZipGenerator.java

License:Open Source License

public void generate(OutputStream out, Locale locale, float height, Employee employee, int fontSize,
        String zipPassword) throws ZipException, NoSuchMessageException, IOException, COSVisitorException,
        CloneNotSupportedException {
    final ZipOutputStream zout = new ZipOutputStream(out);

    if (zipPassword == null) {
        // Use default password if none is set.
        zipPassword = "fragebogen";
    }//from ww w.  j a  v a 2  s.  c o m

    ZipParameters params = new ZipParameters();
    params.setFileNameInZip("employee.txt");
    params.setCompressionLevel(Zip4jConstants.COMP_DEFLATE);
    params.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA);
    params.setEncryptFiles(true);
    params.setReadHiddenFiles(false);
    params.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
    params.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
    params.setPassword(zipPassword);
    params.setSourceExternalStream(true);

    zout.putNextEntry(null, params);
    zout.write((AppContext.getApplicationContext().getMessage("HEADER", null, locale) + "\n\n").getBytes());

    zout.write(
            (AppContext.getApplicationContext().getMessage("print.section.personalData", null, locale) + "\n\n")
                    .getBytes());

    Iterator it = employee.getPersonalDataFields().entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry) it.next();
        zout.write((pair.getKey() + ": " + pair.getValue() + '\n').getBytes());
        it.remove(); // avoids a ConcurrentModificationException
    }

    zout.write(("\n\n" + AppContext.getApplicationContext().getMessage("print.section.taxes", null, locale)
            + "\n\n").getBytes());

    it = employee.getTaxesFields().entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry) it.next();
        zout.write((pair.getKey() + ": " + pair.getValue() + '\n').getBytes());
        it.remove(); // avoids a ConcurrentModificationException
    }
    zout.closeEntry();

    // Create a document and add a page to it
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    float y = -1;
    int margin = 100;

    // Create a new font object selecting one of the PDF base fonts
    PDFont font = PDType1Font.TIMES_ROMAN;

    // Start a new content stream which will "hold" the to be created content
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
    contentStream.beginText();

    y = page.getMediaBox().getHeight() - margin + height;
    contentStream.moveTextPositionByAmount(margin, y);
    /*
    List<String> list = StringUtils.splitEqually(fileContent, 90);
    for (String e : list) {
        contentStream.moveTextPositionByAmount(0, -15);
        contentStream.drawString(e);
    }
    */

    contentStream.setFont(PDType1Font.TIMES_BOLD, 36);
    contentStream.drawString(AppContext.getApplicationContext().getMessage("HEADER", null, locale));
    contentStream.setFont(PDType1Font.TIMES_BOLD, 14);
    contentStream.moveTextPositionByAmount(0, -4 * height);
    contentStream.drawString(
            AppContext.getApplicationContext().getMessage("print.section.personalData", null, locale));
    contentStream.moveTextPositionByAmount(0, -2 * height);
    contentStream.setFont(font, fontSize);

    it = employee.getPersonalDataFields().entrySet().iterator();
    while (it.hasNext()) {
        StringBuffer nextLineToDraw = new StringBuffer();
        Map.Entry pair = (Map.Entry) it.next();
        nextLineToDraw.append(pair.getKey());
        nextLineToDraw.append(": ");
        nextLineToDraw.append(pair.getValue());

        contentStream.drawString(nextLineToDraw.toString());
        contentStream.moveTextPositionByAmount(0, -height);
        it.remove(); // avoids a ConcurrentModificationException
    }
    contentStream.setFont(PDType1Font.TIMES_BOLD, 14);
    contentStream.moveTextPositionByAmount(0, -2 * height);
    contentStream
            .drawString(AppContext.getApplicationContext().getMessage("print.section.taxes", null, locale));
    contentStream.moveTextPositionByAmount(0, -2 * height);
    contentStream.setFont(font, fontSize);
    it = employee.getTaxesFields().entrySet().iterator();
    while (it.hasNext()) {
        StringBuffer nextLineToDraw = new StringBuffer();
        Map.Entry pair = (Map.Entry) it.next();
        nextLineToDraw.append(pair.getKey());
        nextLineToDraw.append(": ");
        nextLineToDraw.append(pair.getValue());

        contentStream.drawString(nextLineToDraw.toString());
        contentStream.moveTextPositionByAmount(0, -height);
        it.remove(); // avoids a ConcurrentModificationException
    }
    contentStream.endText();

    // Make sure that the content stream is closed:
    contentStream.close();

    // Save the results and ensure that the document is properly closed:
    ByteArrayOutputStream pdfout = new ByteArrayOutputStream();
    document.save(pdfout);
    document.close();

    ZipParameters params2 = (ZipParameters) params.clone();
    params2.setFileNameInZip("employee.pdf");

    zout.putNextEntry(null, params2);
    zout.write(pdfout.toByteArray());
    zout.closeEntry();

    // Write the zip to client
    zout.finish();
    zout.flush();
    zout.close();
}

From source file:de.fau.amos4.web.PrintDataController.java

License:Open Source License

@RequestMapping("/employee/download/zip")
public void EmployeeDownloadZip(HttpServletResponse response,
        @RequestParam(value = "id", required = true) long employeeId) throws IOException {
    int fontSize = 12;
    float height = 1;
    height = height * fontSize * 1.05f;//from  www . j a v a2  s.c  o m

    //Prepare textfile contents
    Employee employee = employeeRepository.findOne(employeeId);
    Locale locale = LocaleContextHolder.getLocale();
    Map<String, String> fields = employee.getFields();

    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment;filename=employee.zip");

    final ZipOutputStream zout = new ZipOutputStream(response.getOutputStream());

    try {
        ZipParameters params = new ZipParameters();
        params.setFileNameInZip("employee.txt");
        params.setCompressionLevel(Zip4jConstants.COMP_DEFLATE);
        params.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_ULTRA);
        params.setEncryptFiles(true);
        params.setReadHiddenFiles(false);
        params.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
        params.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
        params.setPassword("AMOS");
        params.setSourceExternalStream(true);

        zout.putNextEntry(null, params);
        zout.write((AppContext.getApplicationContext().getMessage("EmployeeForm.header", null, locale) + "\n\n")
                .getBytes());
        //zout.println();
        zout.write((AppContext.getApplicationContext().getMessage("print.section.personalData", null, locale)
                + "\n\n").getBytes());
        //zout.println();
        Iterator it = fields.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            zout.write((pair.getKey() + ": " + pair.getValue() + '\n').getBytes());
            it.remove(); // avoids a ConcurrentModificationException
        }
        zout.closeEntry();

        try {
            // Create a document and add a page to it
            PDDocument document = new PDDocument();
            PDPage page = new PDPage();
            document.addPage(page);
            float y = -1;
            int margin = 100;
            float maxStringLength = page.getMediaBox().getWidth() - 2 * margin;

            // Create a new font object selecting one of the PDF base fonts
            PDFont font = PDType1Font.TIMES_ROMAN;

            // Start a new content stream which will "hold" the to be created content
            PDPageContentStream contentStream = new PDPageContentStream(document, page);

            // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
            contentStream.beginText();

            y = page.getMediaBox().getHeight() - margin + height;
            contentStream.moveTextPositionByAmount(margin, y);
            /*
            List<String> list = StringUtils.splitEqually(fileContent, 90);
            for (String e : list) {
            contentStream.moveTextPositionByAmount(0, -15);
            contentStream.drawString(e);
            }
            */

            fields = employee.getFields();

            contentStream.setFont(PDType1Font.TIMES_BOLD, 36);
            contentStream.drawString(
                    AppContext.getApplicationContext().getMessage("EmployeeForm.header", null, locale));
            contentStream.setFont(PDType1Font.TIMES_BOLD, 14);
            contentStream.moveTextPositionByAmount(0, -4 * height);
            contentStream.drawString(
                    AppContext.getApplicationContext().getMessage("print.section.personalData", null, locale));
            contentStream.moveTextPositionByAmount(0, -2 * height);
            contentStream.setFont(font, fontSize);
            it = fields.entrySet().iterator();
            while (it.hasNext()) {
                StringBuffer nextLineToDraw = new StringBuffer();
                Map.Entry pair = (Map.Entry) it.next();
                nextLineToDraw.append(pair.getKey());
                nextLineToDraw.append(": ");
                nextLineToDraw.append(pair.getValue());

                contentStream.drawString(nextLineToDraw.toString());
                contentStream.moveTextPositionByAmount(0, -height);
                it.remove(); // avoids a ConcurrentModificationException
            }
            contentStream.endText();

            // Make sure that the content stream is closed:
            contentStream.close();

            // Save the results and ensure that the document is properly closed:
            ByteArrayOutputStream pdfout = new ByteArrayOutputStream();
            document.save(pdfout);
            document.close();

            ZipParameters params2 = (ZipParameters) params.clone();
            params2.setFileNameInZip("employee.pdf");

            zout.putNextEntry(null, params2);
            zout.write(pdfout.toByteArray());
            zout.closeEntry();
        } catch (CloneNotSupportedException | COSVisitorException e) {
            e.printStackTrace();
        }

        // Write the zip to client
        zout.finish();
        zout.flush();
        zout.close();
    } catch (ZipException e) {
        e.printStackTrace();
    }
}

From source file:de.haber.pdfbox.CountPages.java

License:Apache License

/**
 * Counts the number of pages from a given <b>input</b> file.
 * /*from   www .ja  v  a 2 s .  c  om*/
 * @param input
 *            input pdf file that has to exist and must be a file.
 * @return number of pages from the given pdf file.
 * @throws IOException
 *             If there is an error reading from the given file.
 * @throws IllegalArgumentException
 *             If the <b>file</b> does not exist or is not a file.
 */
public int count(File input) throws IOException {
    checkArgument(input.exists() && input.isFile(), "The input pdf has to exist and must be a file.");
    PDDocument doc = PDDocument.load(input);
    int res = doc.getNumberOfPages();
    doc.close();
    return res;
}

From source file:de.hrogge.CompactPDFExport.PDFGenerator.java

License:Apache License

public void exportierePDF(JFrame frame, File output, Document input, Konfiguration k, boolean speichernDialog)
        throws IOException, COSVisitorException, JAXBException {
    PDDocument doc = null;

    /* JAXB Reprsentation des XML-Dokuments erzeugen */
    JAXBContext jaxbContext = JAXBContext.newInstance(jaxbGenerated.datenxml.Daten.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Daten daten = (Daten) jaxbUnmarshaller.unmarshal(input.getDocumentElement());

    try {//w  ww.  ja  v  a 2  s  .c  om
        doc = internErzeugePDFDokument(k, daten);

        if (output == null) {
            String ordner = k.getTextDaten(Konfiguration.GLOBAL_ZIELORDNER);
            if (speichernDialog) {
                output = waehlePDFFile(frame, daten, ordner);
                if (output == null) {
                    return;
                }
            } else {
                output = new File(ordner, daten.getAngaben().getName() + ".pdf");
            }
        }

        if (output.exists()) {
            int result = JOptionPane.showConfirmDialog(frame,
                    "Die Datei " + output.getAbsolutePath()
                            + " existiert schon.\nSoll sie berschrieben werden?",
                    "Datei berschreiben?", JOptionPane.YES_NO_OPTION);

            if (result != JOptionPane.YES_OPTION) {
                return;
            }
        }

        doc.save(new FileOutputStream(output));
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}

From source file:de.hrogge.CompactPDFExport.PDFGenerator.java

License:Apache License

private PDDocument internErzeugePDFDokument(Konfiguration k, Daten daten) throws IOException {
    String[] guteEigenschaften;//from  w  ww . j av a2  s . com
    List<PDFSonderfertigkeiten> sflist;
    List<Gegenstand> ausruestung;
    boolean tzm;
    PDDocument doc;
    String pfad;
    PDJpeg charakterBild;
    PDJpeg hintergrundBild;
    Hausregeln hausregeln;
    List<String> commands;
    doc = null;

    hausregeln = new Hausregeln(k);

    charakterBild = null;
    hintergrundBild = null;
    tzm = daten.getConfig().getRsmodell().equals("zone");

    /*
     * Gute Eigenschaften auslesen, da sie seitenbergreifend gebraucht
     * werden
     */
    Eigenschaften eigenschaften = daten.getEigenschaften();
    guteEigenschaften = new String[8];
    guteEigenschaften[0] = eigenschaften.getMut().getAkt().toString();
    guteEigenschaften[1] = eigenschaften.getKlugheit().getAkt().toString();
    guteEigenschaften[2] = eigenschaften.getIntuition().getAkt().toString();
    guteEigenschaften[3] = eigenschaften.getCharisma().getAkt().toString();
    guteEigenschaften[4] = eigenschaften.getFingerfertigkeit().getAkt().toString();
    guteEigenschaften[5] = eigenschaften.getGewandtheit().getAkt().toString();
    guteEigenschaften[6] = eigenschaften.getKonstitution().getAkt().toString();
    guteEigenschaften[7] = eigenschaften.getKoerperkraft().getAkt().toString();

    sflist = new ArrayList<PDFSonderfertigkeiten>();
    for (Sonderfertigkeit sf : daten.getSonderfertigkeiten().getSonderfertigkeit()) {
        if (sf.getAuswahlen() != null && sf.getAuswahlen().getAuswahl().size() > 0) {
            for (Sonderfertigkeit.Auswahlen.Auswahl a : sf.getAuswahlen().getAuswahl()) {
                sflist.add(new PDFSonderfertigkeiten(sf, a.getName()));
            }
        } else {
            sflist.add(new PDFSonderfertigkeiten(sf));
        }
    }

    ausruestung = new ArrayList<>(daten.getGegenstaende().getGegenstand());

    /* Kommandos aus Notizen extrahieren */
    Notizen n = daten.getAngaben().getNotizen();
    commands = new ArrayList<>();
    extrahiereKommandos(commands, n.getN0());
    extrahiereKommandos(commands, n.getN1());
    extrahiereKommandos(commands, n.getN2());
    extrahiereKommandos(commands, n.getN3());
    extrahiereKommandos(commands, n.getN4());
    extrahiereKommandos(commands, n.getN5());
    extrahiereKommandos(commands, n.getN6());
    extrahiereKommandos(commands, n.getN7());
    extrahiereKommandos(commands, n.getN8());
    extrahiereKommandos(commands, n.getN9());
    extrahiereKommandos(commands, n.getN10());
    extrahiereKommandos(commands, n.getN11());

    try {
        /* PDF erzeugen */
        doc = new PDDocument();

        /*
         * Bilder mssen bei PDFBox geladen werden bevor die Content-Streams
         * erzeugt werden
         */
        pfad = daten.getAngaben().getBildPfad();
        if (pfad != null && pfad.length() > 0) {
            try {
                BufferedImage img = ImageIO.read(new File(pfad));
                charakterBild = new PDJpeg(doc, img);
            } catch (Exception e) {
                System.err.println("Konnte das Bild '" + pfad + "' nicht laden.");
            }
        }

        pfad = k.getTextDaten(Konfiguration.GLOBAL_HINTERGRUND);
        if (pfad != null && pfad.length() > 0) {
            try {
                BufferedImage img = ImageIO.read(new File(pfad));
                hintergrundBild = new PDJpeg(doc, img);
            } catch (Exception e) {
                System.err.println("Konnte das Bild '" + pfad + "' nicht laden.");
            }
        }

        /* globale Settings fr Seite festlegen */
        PDFSeite.init(marginX, marginY, textMargin, hintergrundBild,
                k.getOptionsDaten(Konfiguration.GLOBAL_HINTERGRUND_VERZERREN));

        /* Sonderfertigkeiten sortieren */
        Collections.sort(sflist);

        /* Seiten erzeugen */
        FrontSeite page1 = new FrontSeite(doc);
        page1.erzeugeSeite(daten, charakterBild, hintergrundBild, guteEigenschaften, sflist, hausregeln,
                commands, tzm, k);

        TalentSeite page2 = new TalentSeite(doc);
        page2.erzeugeSeite(daten, hintergrundBild, guteEigenschaften, sflist, hausregeln, commands, k);

        if (daten.getAngaben().isMagisch()) {
            ZauberSeite page3 = new ZauberSeite(doc);
            page3.erzeugeSeite(daten, hintergrundBild, guteEigenschaften, sflist, hausregeln, commands, k);
        }

        /* Leerzeilen zu Sonderfertigkeitsliste hinzufgen */
        for (int i = 1; i < sflist.size(); i++) {
            if (sflist.get(i - 1).getKategorie() != sflist.get(i).getKategorie()) {
                sflist.add(i, null);
                i++;
            }
        }

        while (hatNichtGedruckteSonderfertigkeit(sflist) || ausruestung.size() > 0) {
            SonstigesSeite page4 = new SonstigesSeite(doc);
            page4.erzeugeSeite(hintergrundBild, guteEigenschaften, sflist, ausruestung);
        }
    } catch (IOException e) {
        if (doc != null) {
            doc.close();
            doc = null;
        }
        throw e;
    }
    return doc;
}

From source file:de.hsmannheim.ss15.alr.searchengine.PDFParser.java

public String getTextOfPDF(byte[] in) throws Exception {

    ByteArrayInputStream input = new ByteArrayInputStream(in);

    org.apache.pdfbox.pdfparser.PDFParser parser;
    String parsedText = null;//from   ww  w .  j ava  2  s . c  o m
    ;
    PDFTextStripper pdfStripper = null;
    PDDocument pdDoc = null;
    COSDocument cosDoc = null;

    parser = new NonSequentialPDFParser(input);

    //parse PDF
    try {
        parser.parse();
        cosDoc = parser.getDocument();
        pdfStripper = new PDFTextStripper();
        pdDoc = new PDDocument(cosDoc);

        parsedText = pdfStripper.getText(pdDoc);
    } catch (Exception e) {
        throw (e);

    } finally {
        if (cosDoc != null) {
            cosDoc.close();
        }
        if (pdDoc != null) {
            pdDoc.close();
        }

    }
    return parsedText;
}

From source file:de.ilias.services.lucene.index.file.PDFBoxPDFHandler.java

License:Open Source License

/**
 * @throws IOException /*w  ww. ja v  a  2 s . c o m*/
 * @see de.ilias.services.lucene.index.file.FileHandler#getContent(java.io.InputStream)
 */
public String getContent(InputStream is) throws FileHandlerException {

    PDDocument pddo = null;
    PDFTextStripper stripper = null;
    String str = new String("");

    try {

        pddo = PDDocument.load(is);

        if (pddo.isEncrypted()) {
            logger.warn("PDF Document is encrypted. Trying empty password...");
            return "";
        }
        stripper = new PDFTextStripper();
        str = stripper.getText(pddo);
    } catch (NumberFormatException e) {
        logger.warn("Invalid PDF version number given. Aborting");
    } catch (IOException e) {
        logger.warn(e.getMessage());
        throw new FileHandlerException(e);
    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new FileHandlerException(e);
    } finally {
        try {
            if (pddo != null)
                pddo.close();
        } catch (IOException e) {
            ;
        }
    }
    return str;
}