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

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

Introduction

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

Prototype

public static PDDocument load(byte[] input) throws IOException 

Source Link

Document

Parses a PDF.

Usage

From source file:indexer.PDFTextExtractor.java

License:Open Source License

private PDDocument getPDDocument(String filename) throws IOException {
    if (stripper == null)
        throw new IOException("ERROR: PDFStripper was not created");
    File file = new File(filename);
    if (!file.exists())
        throw new IOException("ERROR: " + filename + " doesn't exist");
    return PDDocument.load(file);
}

From source file:io.cloudslang.content.utilities.services.PdfParseService.java

License:Open Source License

private static PDDocument getPdfDocument(final Path path, final String password) throws IOException {
    if (isEmpty(password))
        return PDDocument.load(path.toFile());
    return PDDocument.load(path.toFile(), password);
}

From source file:io.inkstand.scribble.pdf.BasePDFMatcher.java

License:Apache License

/**
 * Is invoked by the matches method when the type of the target object is verified. Override this method to add
 * verifications on the raw data instead of a loaded document.
 *
 * @param pdf//  w w  w.  j a  va 2s  .c o m
 *         the handle for the PDF data
 *
 * @return <code>true</code> if the pdf document is a valid PDF document.
 */
protected boolean matches(PDF pdf) {
    try (InputStream inStream = pdf.openStream()) {
        final PDDocument doc = PDDocument.load(inStream);
        return matchesPDF(doc);
    } catch (IOException e) {
        LOG.debug("Could not load PDF document", e);
        return false;
    }
}

From source file:io.konik.carriage.pdfbox.PDFBoxInvoiceAppender.java

License:Open Source License

@Override
public void append(AppendParameter appendParameter) {
    InputStream inputPdf = appendParameter.inputPdf();
    try {/*www . j a  va  2s .  com*/
        PDDocument doc = PDDocument.load(inputPdf);
        setMetadata(doc, appendParameter);
        attachZugferdFile(doc, appendParameter.attachmentFile());
        doc.getDocument().setVersion(1.7f);
        doc.save(appendParameter.resultingPdf());
        doc.close();
    } catch (Exception e) {
        throw new InvoiceAppendError("Error appending Invoice", e);
    }

}

From source file:io.konik.carriage.pdfbox.PDFBoxInvoiceExtractor.java

License:Open Source License

private static final InputStream extractIntern(InputStream pdfStream) throws IOException {
    PDDocument doc = PDDocument.load(pdfStream);
    InputStream inputStream = extractZugferdFileAttachment(doc);
    return new CallBackInputStream(inputStream, doc);
}

From source file:it.myideas.bancamarcheextractor.Distinta.java

public static Distinta parse(Path file) {

    try (PDDocument doc = PDDocument.load(file.toFile())) {

        Distinta distinta = new Distinta();

        PDFTextStripper stripper = new PDFTextStripper();
        String contents = stripper.getText(doc);
        Stream<String> lines = Arrays.stream(contents.split(stripper.getLineSeparator()));

        log.debug("FILE:" + file.toString());
        log.debug(contents);/*  w w w .j a  v a2  s. com*/

        lines.forEach(line -> {

            if (line.startsWith("Tipo disposizione")) {
                distinta.tipoDisposizione = line.replace("Tipo disposizione", "").trim().toLowerCase();
            } else if (line.startsWith("1 Esecuzione")) {
                String[] p = line.split(" ");

                distinta.beneficiario = Arrays.stream(Arrays.copyOfRange(p, 4, p.length))
                        .map(String::toLowerCase).collect(Collectors.joining("_"));

                distinta.data = LocalDate.parse(p[2], DateTimeFormatter.ofPattern("dd/MM/yyyy"));
            }

        });

        if (!isOk(distinta.beneficiario) || !isOk(distinta.tipoDisposizione) || distinta.data == null) {
            throw new IOException("Parser failure for file " + file.toString());
        }

        return distinta;
    } catch (IOException e) {
        log.error("Error parsing PDF", e);
        return null;
    }
}

From source file:javaapplication2.NewJFrame.java

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
    /* JFileChooser chooser = new JFileChooser();
     FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "ficheiros PDF", "pdf");/*ww  w . jav a  2s.  co  m*/
     chooser.setFileFilter(filter);
     int returnVal;
     returnVal = chooser.showOpenDialog(getParent());
     if(returnVal == JFileChooser.APPROVE_OPTION) {*/
    if (!jTextField1.getText().isEmpty()) {

        System.out.println("You chose to open this file: " + jTextField1.getText());
        String text = null;
        String[] linhas = null;

        try {
            PDDocument doc = PDDocument.load(jTextField1.getText());
            PDFTextStripper stripper = new PDFTextStripper();
            text = stripper.getText(doc);
            doc.close();
        } catch (IOException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        linhas = text.split("\n", -1);

        ArrayList<Dia> dias = new ArrayList<>();
        Refeicao r = null;
        Dia d = null;
        Boolean almoco = false;
        Boolean ignorarDia = false; //dias sem refeio ex. feriados
        for (String linha : linhas) {
            if (linha.contains("Segunda-feira") || linha.contains("Tera-feira")
                    || linha.contains("Quarta-feira") || linha.contains("Quinta-feira")
                    || linha.contains("Sexta-feira")) {
                if (d != null) {// && !ignorarDia){
                    dias.add(d);
                    // ignorarDia=false;
                }
                d = new Dia();
                almoco = true;
                r = new Refeicao();
                jTextArea1.setText(jTextArea1.getText() + linha.trim() + " --NOVO DIA \n");
            }
            /* else if(linha.contains("ENCERRADO")){
            ignorarDia=true;
             }*/
            else if (isValidDate(linha)) {
                DateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
                try {
                    d.setDia(format.parse(linha));
                } catch (ParseException ex) {
                    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                jTextArea1.setText(jTextArea1.getText() + linha.trim() + " --DATA \n");
            } else if (linha.contains("Sopa")) {

                if (almoco)
                    jTextArea1.setText(jTextArea1.getText() + " ALMOO \n");
                else
                    jTextArea1.setText(jTextArea1.getText() + " JANTAR \n");

                r.setSopa(linha);
                jTextArea1.setText(jTextArea1.getText() + linha.trim() + " --SOPA \n");
            }

            else if (linha.contains("Carne")) {
                r.setCarne(linha);
                jTextArea1.setText(jTextArea1.getText() + linha.trim() + " --CARNE \n");
            }

            else if (linha.contains("Peixe")) {
                r.setPeixe(linha);
                jTextArea1.setText(jTextArea1.getText() + linha.trim() + " --PEIXE \n");
            }

            else if (linha.contains("Sobremesa")) {

                jTextArea1.setText(jTextArea1.getText() + linha.trim() + " --SOBREMESA \n");
                r.setSobremesa(linha);
                if (almoco) {
                    d.setAlmoco(r);
                    r = new Refeicao();
                    almoco = false;
                } else {
                    d.setJantar(r);
                    r = new Refeicao();
                    almoco = true;
                }

            } else {
                jTextArea1.setText(jTextArea1.getText() + linha.trim() + " --Ignorado \n");
            }

            // jTextArea1.setText(jTextArea1.getText()+t+" a testar\n ");
        }

        if (d != null)
            dias.add(d);

        jTextArea1.setText(" ");
        System.out.println("nmero de dias: " + dias.size());
        getSelectedButtonText();
        getConnection();
        for (Dia dia : dias) {
            //if(dia.dia.compareTo(new Date(2015, 06, 12))==0){
            if (dia.dia != null) {
                System.out.println("dia: " + dia.dia);
                jTextArea1.setText(jTextArea1.getText() + "\n" + dia.dia);
                jTextArea1.setText(jTextArea1.getText() + "\n" + "-----ALMOO-----\n");
                if (dia.almoco != null)
                    jTextArea1
                            .setText(jTextArea1.getText() + dia.almoco.getSopa() + "\n" + dia.almoco.getCarne()
                                    + "\n" + dia.almoco.getPeixe() + "\n" + dia.almoco.getSobremesa() + "\n");
                else
                    jTextArea1.setText(jTextArea1.getText() + "Nao definido" + "\n");

                jTextArea1.setText(jTextArea1.getText() + "\n" + "-----JANTAR-----\n");
                if (dia.jantar != null)
                    jTextArea1
                            .setText(jTextArea1.getText() + dia.jantar.getSopa() + "\n" + dia.jantar.getCarne()
                                    + "\n" + dia.jantar.getPeixe() + "\n" + dia.jantar.getSobremesa() + "\n");
                else
                    jTextArea1.setText(jTextArea1.getText() + "Nao definido" + "\n");

                try {
                    String query = " insert into " + getSelectedButtonText()
                            + " (data,temAlmoco,almoco_sopa,almoco_carne,almoco_peixe,almoco_sobremesa,temJantar,jantar_sopa,jantar_carne,jantar_peixe,jantar_sobremesa)"
                            + " values (?,?,?,?,?,?,?,?,?,?,?)";

                    // create the mysql insert preparedstatement
                    PreparedStatement preparedStmt = conexao.prepareStatement(query);
                    java.sql.Date sqldate = new Date(dia.dia.getTime());
                    preparedStmt.setDate(1, sqldate);
                    if (dia.almoco != null) {
                        preparedStmt.setInt(2, 1);
                        preparedStmt.setString(3, dia.almoco.getSopa());
                        preparedStmt.setString(4, dia.almoco.getCarne());
                        preparedStmt.setString(5, dia.almoco.getPeixe());
                        preparedStmt.setString(6, dia.almoco.getSobremesa());
                    } else {
                        preparedStmt.setInt(2, 0);
                        preparedStmt.setString(3, "No Definido");
                        preparedStmt.setString(4, "No Definido");
                        preparedStmt.setString(5, "No Definido");
                        preparedStmt.setString(6, "No Definido");
                    }
                    if (dia.jantar != null) {
                        preparedStmt.setInt(7, 1);
                        preparedStmt.setString(8, dia.jantar.getSopa());
                        preparedStmt.setString(9, dia.jantar.getCarne());
                        preparedStmt.setString(10, dia.jantar.getPeixe());
                        preparedStmt.setString(11, dia.jantar.getSobremesa());
                    } else {
                        preparedStmt.setInt(7, 0);
                        preparedStmt.setString(8, "No Definido");
                        preparedStmt.setString(9, "No Definido");
                        preparedStmt.setString(10, "No Definido");
                        preparedStmt.setString(11, "No Definido");
                    }
                    // execute the preparedstatement
                    preparedStmt.execute();
                    //System.out.println(text);
                    //jTextArea1.setText(text);

                } catch (SQLException ex) {
                    Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }
}

From source file:jeanderson.br.converte.Converte.java

public static void converter(String nome) {
    File arquivo = new File(
            "/Users/" + System.getProperty("user.name") + "/Google Drive/Notas Fiscais/PDF/" + nome + ".pdf");
    if (arquivo.exists()) {
        try {//www  .j a va2 s .c  o m
            PDDocument documento = PDDocument.load(arquivo);
            List<PDPage> lista = documento.getDocumentCatalog().getAllPages();
            int numeroDePagina = 1;
            for (PDPage paginas : lista) {
                BufferedImage imagem = paginas.convertToImage();
                File saida = new File("/Users/" + System.getProperty("user.name")
                        + "/Google Drive/Notas Fiscais/PNG/" + nome + numeroDePagina + ".png");
                ImageIO.write(imagem, "png", saida);
                numeroDePagina++;
            }
            documento.close();
        } catch (IOException ex) {
            Relatar.bug(Converte.class.getName(), ex.toString());
            Logger.getLogger(Converte.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:khoji.PDFdoc.java

License:Apache License

/**
 *
 * @param doc_path/* www . java  2  s.  c o  m*/
 * @return
 * @throws Exception
 */
public String extractPDF(String doc_path) throws Exception {

    PDDocument document = null;
    try {
        document = PDDocument.load(doc_path);
        if (document.isEncrypted()) {
            try {
                document.decrypt("");
            } catch (InvalidPasswordException e) {
                System.err.println("Error: Document is encrypted with a password.");
                System.exit(1);
            }
        }

        PDFTextStripper stripper = new PDFTextStripper();
        String text = "";
        text = stripper.getText(document);
        //            System.out.println("text:"+text);
        return text;
    } finally {
        if (document != null) {
            document.close();
        }
    }

}

From source file:kz.technovision.technokitap.Book.java

/**
 * Imagify/*from w  w w  . j a  v a2 s  .c o m*/
 *
 * @param pdfFile
 */
public void imagify(String pdfFile) {
    try (PDDocument document = PDDocument.load(pdfFile)) {
        int imageType = BufferedImage.TYPE_INT_RGB;
        PDFImageWriter pw = new PDFImageWriter();
        boolean success = pw.writeImage(document, "jpg", "", 1, document.getNumberOfPages(), big, imageType,
                300);
        if (!success) {
            System.err.println("Error: no writer found for image format");
            System.exit(1);
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }
}