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:com.giaybac.traprange.test.TESTPDFBox.java

License:MIT License

@Test
public void test() throws IOException {
    String filePath = "D:\\traprange\\_Docs\\TK0976-AB5-0-2014042211.pdf";
    //String filePath = "C:\\Users\\ThoLuong\\Downloads\\Download\\1986 NL Batting - Sheet1.pdf";
    File pdfFile = new File(filePath);
    PDDocument pdDocument = PDDocument.load(pdfFile);
    //PrintTextLocations printer = new PrinTextLocations();
    List pages = pdDocument.getDocumentCatalog().getAllPages();
    PDPage page = (PDPage) pages.get(0);
    PDStream stream = page.getContents();

    this.processStream(page, page.findResources(), stream.getStream());
    //Print out all text
    ranges.sort(new Comparator<Range>() {
        @Override/* w  w w.  java  2  s. c om*/
        public int compare(Range o1, Range o2) {
            return o1.lowerEndpoint().compareTo(o2.lowerEndpoint());
        }
    });
    for (Range range : ranges) {
        System.out.println("> " + range);
    }
    //Print out all ranges
    List<Range<Integer>> trapRanges = trapRangeBuilder.build();
    for (Range trapRange : trapRanges) {
        System.out.println("TrapRange: " + trapRange);
    }
}

From source file:com.github.joemcintyre.pdffinish.PDFFinish.java

License:Open Source License

/**
 * Show PDF metadata. ToC, and font info.
 * //from   w w  w.  j a  va2 s  . com
 * @param fileInput PDF input file.
 */
public int showInfo(File fileInput) {
    PDDocument document = null;
    try {
        document = PDDocument.load(fileInput);
    } catch (IOException e) {
        System.out.println("Error reading input PDF: " + e);
        return (READ_ERROR_INPUT_PDF);
    }

    try {
        showMetadata(document);
    } catch (IOException e) {
        System.out.println("Error reading metadata: " + e);
        return (READ_ERROR_INPUT_PDF);
    }
    showTOC(document);
    showFonts(document);
    return (NO_ERROR);
}

From source file:com.github.joemcintyre.pdffinish.PDFFinish.java

License:Open Source License

/**
 * Process the PDF input file, producing the output file.
 * /*from  ww  w . j av a  2s.c  o  m*/
 * @param fileInput PDF input file.
 * @param fileOutput PDF output file.
 */
private void processPDF(File fileInput, File fileOutput) {
    PDDocument document = null;
    try {
        document = PDDocument.load(fileInput);
    } catch (IOException e) {
        System.out.println("Error reading PDF: " + e);
    }

    if (document != null) {
        boolean save = true;
        try {
            updateMetadata(document);
            if (fontList != null) {
                updateTOC(document);
            }
        } catch (IOException e) {
            System.out.println("Error processing PDF: " + e);
            save = false;
        }

        if (save) {
            try {
                document.save(fileOutput);
                System.out.println("Write complete");
            } catch (Exception e) {
                System.out.println("Error writing PDF: " + e);
            }
        }

        try {
            document.close();
        } catch (Exception e) {
            System.out.println("Error closing document: " + e);
        }
    }
}

From source file:com.github.somi92.seecsk.gui.FInvoice.java

private void jbtnKreirajUplatnicuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnKreirajUplatnicuActionPerformed
    try {/*from ww w .  j av a 2 s  .  c  om*/
        String uplatilac = jtxtUplatilac.getText().trim();
        String svrhaUplate = jtxtSvrhaUplate.getText().trim();
        String primalac = jtxtPrimalac.getText().trim();
        String ziroRacun = jtxtZiroRacun.getText().trim();
        String model = jtxtModel.getText().trim();
        String poziv = jtxtPozivNaBroj.getText().trim();
        double iznos = Double.parseDouble(jtxtIznos.getText().trim());

        if (uplatilac == null || uplatilac.isEmpty() || svrhaUplate == null || svrhaUplate.isEmpty()
                || primalac == null || primalac.isEmpty() || ziroRacun == null || ziroRacun.isEmpty()
                || model == null || model.isEmpty() || poziv == null || poziv.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Sva polja moraju biti popunjena!");
            return;
        }

        NalogZaUplatu n = new NalogZaUplatu();
        n.setId(clan.getIdClan());
        n.setUplatilac(uplatilac);
        n.setPrimalac(primalac);
        n.setSvrhaUplate(svrhaUplate);
        n.setIznos(iznos);
        n.setRacunPrimaoca(ziroRacun);
        n.setModel(model);
        n.setPozivNaBroj(poziv);

        ReportGenerator.generateInvoice(n);

        //            dispose();

        //            jpnlUplatnica.setVisible(true);
        //            setSize(getSize().width, getSize().height+320);

        uplatnicaFile = Constants.LocationConfigKeys.TEMP_INVOICE_LOCATION + "uplatnica_" + clan.getIdClan()
                + ".pdf";
        File pdfFile = new File(uplatnicaFile);
        PDDocument doc = PDDocument.load(pdfFile);
        List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
        PDPage page = (PDPage) pages.get(0);

        PDFPagePanel pdfPanel = new PDFPagePanel();
        pdfPanel.setPage(page);

        JInternalFrame iFrame = new JInternalFrame();

        BasicInternalFrameUI ui = (BasicInternalFrameUI) iFrame.getUI();
        Component np = ui.getNorthPane();
        Container cont = (Container) np;
        cont.remove(0);
        cont.validate();
        cont.repaint();
        MouseMotionListener[] mmlisteners = (MouseMotionListener[]) np.getListeners(MouseMotionListener.class);
        for (MouseMotionListener mml : mmlisteners) {
            np.removeMouseMotionListener(mml);
        }
        iFrame.setEnabled(false);

        iFrame.setBounds(0, 0, jpnlUplatnica.getWidth(), jpnlUplatnica.getHeight());
        jpnlUplatnica.add(iFrame);
        iFrame.add(pdfPanel);
        iFrame.setVisible(true);
        iFrame.setResizable(false);
        iFrame.setSelected(false);

        pdfPanel.setVisible(true);
        jbtnPosalji.setEnabled(true);

    } catch (NumberFormatException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(this, "Neispravno unet iznos.", "Greka", JOptionPane.ERROR_MESSAGE);
    } catch (PropertyVetoException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this, "Sistem ne moe u?itati uplatnicu (PVE).", "Greka",
                JOptionPane.ERROR_MESSAGE);
    } catch (IOException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this, "Sistem ne moe u?itati uplatnicu (IO).", "Greka",
                JOptionPane.ERROR_MESSAGE);
    } catch (JRException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this, "Sistem ne moe u?itati uplatnicu (JRE).", "Greka",
                JOptionPane.ERROR_MESSAGE);
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this, "Sistem ne moe u?itati uplatnicu (CNF).", "Greka",
                JOptionPane.ERROR_MESSAGE);
    } catch (RuntimeException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this, ex.getMessage(), "Greka", JOptionPane.ERROR_MESSAGE);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.globaldatachain.PDFDocumentTest.java

License:Apache License

@Test
public void embedding() throws IOException {
    PDDocument doc = PDDocument.load(getClass().getResourceAsStream("/sample.pdf"));
    PDFDocument pdfDocument = new PDFDocument(doc);
    assertNull(pdfDocument.retrieveEmbeddedString("linking-to-something"));
    pdfDocument.embed("linking-to-something", "this-is-my-id");
    assertEquals("this-is-my-id", pdfDocument.retrieveEmbeddedString("linking-to-something"));
}

From source file:com.htmlhifive.pitalium.sample.pdf.PDFReadTest.java

License:Apache License

/**
 * PDF?????????<br/>/*from  ww w .ja  v a 2  s .  c  o m*/
 * PDF?1?????1???<br/>
 * PDF??????Apache PDFBox?
 *
 * @param fileName ?PDF???.pdf???
 * @return ?????PDF?
 */
private int savePdfAsImages(String fileName) {
    int numberOfPages = 0;

    try (BufferedInputStream fileToParse = new BufferedInputStream(
            getClass().getResourceAsStream(fileName + ".pdf")); PDDocument pdf = PDDocument.load(fileToParse)) {
        // 1=1?????
        final PDFRenderer pdfRenderer = new PDFRenderer(pdf);
        numberOfPages = pdf.getNumberOfPages();

        for (int i = 0; i < numberOfPages; i++) {
            final BufferedImage image = pdfRenderer.renderImageWithDPI(i, 300, ImageType.RGB);
            if (!saveExportImage(image, fileName + i + ".png")) {
                return -1;
            }
        }
    } catch (IOException e) {
        return -1;
    }

    return numberOfPages;
}

From source file:com.itdhq.poc.ocrsign.CreateVisibleSignature.java

License:Apache License

/**
 * Sign pdf file and create new file that ends with "_signed.pdf".
 *
 * @param inputFile The source pdf document file.
 * @param signedFile The file to be signed.
 * @throws IOException/*from  www. j a v a  2  s .c om*/
 */
public void signPDF(File inputFile, File signedFile) throws IOException {
    if (inputFile == null || !inputFile.exists()) {
        throw new IOException("Document for signing does not exist");
    }

    // creating output document and prepare the IO streams.
    FileOutputStream fos = new FileOutputStream(signedFile);

    // load document
    PDDocument doc = PDDocument.load(inputFile);

    // create signature dictionary
    PDSignature signature = new PDSignature();
    signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter
    // subfilter for basic and PAdES Part 2 signatures
    signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
    signature.setName("signer name");
    signature.setLocation("signer location");
    signature.setReason("reason for signature");

    // the signing date, needed for valid signature
    signature.setSignDate(Calendar.getInstance());

    // register signature dictionary and sign interface
    if (visibleSignatureProperties != null && visibleSignatureProperties.isVisualSignEnabled()) {
        options = new SignatureOptions();
        options.setVisualSignature(visibleSignatureProperties);
        options.setPage(visibleSignatureProperties.getPage() - 1);
        // FIXME
        //doc.addSignature(signature, this, options);
    } else {
        // FIXME
        //doc.addSignature(signature, this);
    }

    // write incremental (only for signing purpose)
    // FIXME
    //doc.saveIncremental(fos);
    doc.close();

    // do not close options before saving, because some COSStream objects within options
    // are transferred to the signed document.
    // FIXME
    //IOUtils.closeQuietly(options);
}

From source file:com.itdhq.poc.ocrsign.ShowSignature.java

License:Apache License

private void showSignature(String[] args) throws IOException, CertificateException {
    if (args.length != 2) {
        usage();//from   ww w . jav  a2s .com
    } else {
        String password = args[0];
        String infile = args[1];
        PDDocument document = null;
        try {
            document = PDDocument.load(new File(infile));
            if (!document.isEncrypted()) {
                System.err.println("Warning: Document is not encrypted.");
            }

            COSDictionary trailer = document.getDocument().getTrailer();
            COSDictionary root = (COSDictionary) trailer.getDictionaryObject(COSName.ROOT);
            COSDictionary acroForm = (COSDictionary) root.getDictionaryObject(COSName.ACRO_FORM);
            COSArray fields = (COSArray) acroForm.getDictionaryObject(COSName.FIELDS);
            for (int i = 0; i < fields.size(); i++) {
                COSDictionary field = (COSDictionary) fields.getObject(i);
                // FIXME
                /*
                COSName type = field.getCOSName( COSName.FT );
                if( COSName.SIG.equals( type ) )
                {
                COSDictionary cert = (COSDictionary)field.getDictionaryObject( COSName.V );
                if( cert != null )
                {
                    System.out.println( "Certificate found" );
                    System.out.println( "Name=" + cert.getDictionaryObject( COSName.NAME ) );
                    System.out.println( "Modified=" + cert.getDictionaryObject( COSName.M ) );
                    COSName subFilter = (COSName)cert.getDictionaryObject( COSName.SUB_FILTER );
                    if( subFilter != null )
                    {
                        if( subFilter.getName().equals( "adbe.x509.rsa_sha1" ) )
                        {
                            COSString certString = (COSString)cert.getDictionaryObject(
                                    COSName.getPDFName( "Cert" ) );
                            byte[] certData = certString.getBytes();
                            CertificateFactory factory = CertificateFactory.getInstance( "X.509" );
                            ByteArrayInputStream certStream = new ByteArrayInputStream( certData );
                            Collection<? extends Certificate> certs = factory.generateCertificates( certStream );
                            System.out.println( "certs=" + certs );
                        }
                        else if( subFilter.getName().equals( "adbe.pkcs7.sha1" ) )
                        {
                            COSString certString = (COSString)cert.getDictionaryObject(
                                    COSName.CONTENTS );
                            byte[] certData = certString.getBytes();
                            CertificateFactory factory = CertificateFactory.getInstance( "X.509" );
                            ByteArrayInputStream certStream = new ByteArrayInputStream( certData );
                            Collection<? extends Certificate> certs = factory.generateCertificates( certStream );
                            System.out.println( "certs=" + certs );
                        }
                        else
                        {
                            System.err.println( "Unknown certificate type:" + subFilter );
                        }
                    }
                    else
                    {
                        throw new IOException( "Missing subfilter for cert dictionary" );
                    }
                }
                else
                {
                    System.out.println( "Signature found, but no certificate" );
                }
                }
                */
            }
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }
}

From source file:com.iwinner.app.pdf.DbProject.java

/**
 * @param args//from   w  ww .  j  ava2  s .c  om
 *            the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    PDDocument pd;
    ArrayList<Student> database = new ArrayList<Student>();
    try {
        File input = new File("E:\\Tech_Learn\\ImpApplications_Source\\student-InfoSystem-master\\fe2012.pdf");
        pd = PDDocument.load(input);

        PDFTextStripper stripper = new PDFTextStripper();
        StringBuilder sb = new StringBuilder();

        stripper.setStartPage(1);
        stripper.setEndPage(4);
        sb.append(stripper.getText(pd));

        Pattern p = Pattern.compile("(\\w)(\\d{9})(.*?)((\\d+)/(\\d{4}))", Pattern.DOTALL);

        Matcher m = p.matcher(sb);

        while (m.find()) {
            //System.out.println(m.group());
            //System.out.println("\n----\n");
            Student s = new Student();
            String student_block, tmp_name;

            student_block = m.group();

            // Regex for roll number
            Pattern p2 = Pattern.compile("\\w\\d{9}", Pattern.DOTALL);
            Matcher m2 = p2.matcher(student_block);
            m2.find();
            s.roll = m2.group().trim();
            //System.out.println(s.roll);

            // Regex for finding name
            p2 = Pattern.compile("\\s{3}(.*?)\\s{2}", Pattern.DOTALL);
            m2 = p2.matcher(student_block);
            m2.find();

            tmp_name = m2.group();

            // Strip leading and trailing whitespace
            s.name = tmp_name.trim();

            // Regex for iterating over individual subjects
            p2 = Pattern.compile("\\d{6}\\s\\w(.*?)\\s[P|F]\\s", Pattern.DOTALL);
            m2 = p2.matcher(student_block);

            while (m2.find()) {
                String tmp;
                tmp = m2.group().trim();
                //System.out.println(tmp);

                // Get subject name
                Pattern p3 = Pattern.compile("[A-Z](.*?)(PP|PR|TW)\\s{2}");
                Matcher m3 = p3.matcher(tmp);
                m3.find();
                String sub_name = m3.group().trim();

                //System.out.println("---" + sub_name);

                // Remove PP/PR/TW from sub_name
                StringBuilder sb_tmp;
                sb_tmp = new StringBuilder(sub_name);
                sb_tmp.deleteCharAt(sub_name.length() - 1);
                sb_tmp.deleteCharAt(sub_name.length() - 2);

                sub_name = sb_tmp.toString().trim();

                // Final subject name
                //System.out.println("---" + sub_name);

                s.subjects.add(sub_name);

            }

            // Add to student db
            database.add(s);

            //System.out.println("\n-------\n");
        }

        for (Student s : database) {
            System.out.println("Name : " + s.name + "\n" + s.roll);
            for (Iterator it = s.subjects.iterator(); it.hasNext();) {
                Object a = it.next();
                System.out.println("--> " + a);
            }
            System.out.println("\n");
        }

        pd.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.izforge.izpack.panels.pdflicence.PDFLicenceConsolePanel.java

License:Open Source License

/**
 * Returns the text to display./*from w w w. j  a  va 2s.  c o  m*/
 *
 * @return the text. A <tt>null</tt> indicates failure
 */
@Override
protected String getText() {
    URL url = null;
    try {
        PDFTextStripper stripper = new PDFTextStripper();
        url = loadLicence();
        return stripper.getText(PDDocument.load(url));
    } catch (IOException e) {
        logger.log(Level.WARNING,
                "Error opening PDF license document from resource" + (url != null ? " " + url.getFile() : ""),
                e);
        return null;
    }
}