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:merge_split.MergeSplit.java

License:Apache License

private void RotateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RotateButtonActionPerformed
    try {//from   w w  w.jav a  2  s.c  o m

        PDDocument samplePdf = new PDDocument();
        File file = new File(RotateFileField.getText());
        PDDocument doc1;
        if (rotatecode.equals("ok")) {
            doc1 = PDDocument.load(file);
        } else {
            doc1 = PDDocument.load(file, rotatecode);

        }
        doc1.setAllSecurityToBeRemoved(true);
        TreeSet tree = findPages(RotatePagesField.getText());
        for (int j = 0; j < doc1.getNumberOfPages(); j++) {
            PDPage page = doc1.getPage(j);

            if (tree.contains(j + 1)) {

                if (Rotate90.isSelected()) {
                    page.setRotation(90);
                    samplePdf.addPage(page);
                } else if (Rotate180.isSelected()) {
                    page.setRotation(180);
                    samplePdf.addPage(page);
                } else if (Rotate270.isSelected()) {
                    page.setRotation(270);
                    samplePdf.addPage(page);
                }
            } else {
                samplePdf.addPage(page);

            }

        }

        System.out.println("Number:" + samplePdf.getNumberOfPages());

        String destination = RotateDestinationField.getText() + "\\" + RotateNameField.getText() + ".pdf";
        PDDocumentInformation info = samplePdf.getDocumentInformation();
        info.setAuthor(RotateAuthorField.getText());
        File output = new File(destination);

        samplePdf.save(output);

        samplePdf.close();
    } catch (IOException ex) {
        Logger.getLogger(MergeSplit.class.getName()).log(Level.SEVERE, null, ex);

        JOptionPane.showMessageDialog(null, "Your input is incorrect. Please fill all the fields.",
                "Input warning", JOptionPane.WARNING_MESSAGE);
    }
}

From source file:merge_split.MergeSplit.java

License:Apache License

private void RotateFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RotateFileButtonActionPerformed
    String fileName;/*from   ww  w. j  a  va2s  . c o m*/
    int returnVal = jFileChooser1.showOpenDialog((Component) evt.getSource());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = jFileChooser1.getSelectedFile();
        fileName = file.toString();
        PDDocument doc = null;
        try {
            doc = PDDocument.load(file);
            if (doc.isEncrypted()) {

                doc.setAllSecurityToBeRemoved(true);

            }
        } catch (IOException ex) {

        }
        rotatecode = "";
        if (doc == null) {
            JFrame frame = new JFrame("Input Dialog Example 3");

            rotatecode = JOptionPane.showInputDialog(frame, "Enter password", "PDF is encrypted",
                    JOptionPane.WARNING_MESSAGE);
            try {
                doc = PDDocument.load(file, rotatecode);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Wrong Password.", "Wrong Password",
                        JOptionPane.WARNING_MESSAGE);

            }

        }

        if (doc != null) {
            int count = doc.getNumberOfPages();

            String currentpages;
            if (count > 1) {
                currentpages = "1 - " + count;
            } else {
                currentpages = "1";
            }
            RotatePagesField.setText(currentpages);
            RotateFileField.setText(fileName);
            String name = file.getName();
            int pos = name.lastIndexOf(".");
            if (pos > 0) {
                name = name.substring(0, pos);
            }
            name = name + "Rotated";
            RotateNameField.setText(name);
            try {
                doc.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Problem finishing process.", "Problem finishing process",
                        JOptionPane.WARNING_MESSAGE);
            }

        }
    } else {
        System.out.println("File access cancelled by user.");
    }
}

From source file:merge_split.MergeSplit.java

License:Apache License

private void ConvertFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConvertFileButtonActionPerformed
    String fileName;/*from  w  w w  . ja  va2 s. c o  m*/
    int returnVal = jFileChooser1.showOpenDialog((Component) evt.getSource());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = jFileChooser1.getSelectedFile();
        fileName = file.toString();
        PDDocument doc = null;
        try {
            doc = PDDocument.load(file);
            if (doc.isEncrypted()) {

                doc.setAllSecurityToBeRemoved(true);

            }
        } catch (IOException ex) {

        }
        convertcode = "";
        if (doc == null) {
            JFrame frame = new JFrame("Input Dialog Example 3");

            convertcode = JOptionPane.showInputDialog(frame, "Enter password", "PDF is encrypted",
                    JOptionPane.WARNING_MESSAGE);
            try {
                doc = PDDocument.load(file, rotatecode);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Wrong Password.", "Wrong Password",
                        JOptionPane.WARNING_MESSAGE);
            }

        }
        if (doc != null) {
            int count = doc.getNumberOfPages();

            String currentpages;
            if (count > 1) {
                currentpages = "1 - " + count;
            } else {
                currentpages = "1";
            }
            ConvertPagesField.setText(currentpages);
            ConvertFileField.setText(fileName);
            String name = file.getName();
            int pos = name.lastIndexOf(".");
            if (pos > 0) {
                name = name.substring(0, pos);
            }
            ConvertNameField.setText(name);
            try {
                doc.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Problem accessing file.", "Problem accessing file",
                        JOptionPane.WARNING_MESSAGE);
            }

        }
    } else {
        System.out.println("File access cancelled by user.");
    }
}

From source file:merge_split.MergeSplit.java

License:Apache License

private void SplitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SplitButtonActionPerformed
    try {//from   w ww  .ja va 2  s.c  o m

        File file = new File(SplitFileField.getText());
        PDDocument doc1;
        if (splitcode.equals("ok")) {
            doc1 = PDDocument.load(file);
        } else {
            doc1 = PDDocument.load(file, splitcode);

        }
        doc1.setAllSecurityToBeRemoved(true);

        if (MultipleButton.isSelected()) {
            PDDocument pdf1 = new PDDocument();
            PDDocument pdf2 = new PDDocument();
            TreeSet tree = findPages(SplitPagesField.getText());
            for (int j = 0; j < doc1.getNumberOfPages(); j++) {
                PDPage page = doc1.getPage(j);
                if (tree.contains(j + 1)) {
                    pdf1.addPage(page);
                } else {
                    pdf2.addPage(page);

                }
            }
            String destination1 = SplitDestinationField.getText() + "\\" + SplitNameField.getText() + "1.pdf";
            String destination2 = SplitDestinationField.getText() + "\\" + SplitNameField.getText() + "2.pdf";

            PDDocumentInformation info = pdf1.getDocumentInformation();
            info.setAuthor(SplitAuthorField.getText());
            PDDocumentInformation info2 = pdf2.getDocumentInformation();
            info2.setAuthor(SplitAuthorField.getText());
            if (pdf1.getNumberOfPages() > 0) {
                File output1 = new File(destination1);
                pdf1.save(output1);
            }
            if (pdf2.getNumberOfPages() > 0) {
                File output2 = new File(destination2);
                pdf2.save(output2);
            }
            pdf1.close();
            pdf2.close();
        } else if (SingleButton.isSelected()) {

            for (int j = 0; j < doc1.getNumberOfPages(); j++) {
                PDDocument pdf1 = new PDDocument();

                PDPage page = doc1.getPage(j);
                pdf1.addPage(page);
                int pagenumber = j + 1;
                String destination1 = SplitDestinationField.getText() + "\\" + SplitNameField.getText()
                        + pagenumber + ".pdf";

                PDDocumentInformation info = pdf1.getDocumentInformation();
                info.setAuthor(SplitAuthorField.getText());

                if (pdf1.getNumberOfPages() > 0) {
                    File output1 = new File(destination1);
                    pdf1.save(output1);
                }

                pdf1.close();
            }

        }
        doc1.close();

    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Your input is incorrect. Please fill all the fields.",
                "Input warning", JOptionPane.WARNING_MESSAGE);
        java.util.logging.Logger.getLogger(MergeSplit.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);

    }
}

From source file:merge_split.MergeSplit.java

License:Apache License

private void SplitFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SplitFileButtonActionPerformed
    String fileName;//from  w w  w.  j a  v a2 s. c o  m
    int returnVal = jFileChooser1.showOpenDialog((Component) evt.getSource());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = jFileChooser1.getSelectedFile();
        fileName = file.toString();
        PDDocument doc = null;
        try {
            doc = PDDocument.load(file);
            if (doc.isEncrypted()) {

                doc.setAllSecurityToBeRemoved(true);

            }
        } catch (IOException ex) {

        }
        splitcode = "";
        if (doc == null) {
            JFrame frame = new JFrame("Input Dialog Example 3");

            splitcode = JOptionPane.showInputDialog(frame, "Enter password", "PDF is encrypted",
                    JOptionPane.WARNING_MESSAGE);
            try {
                doc = PDDocument.load(file, rotatecode);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Wrong Password.", "Wrong Password",
                        JOptionPane.WARNING_MESSAGE);

            }

        }

        if (doc != null) {
            int count = doc.getNumberOfPages();

            String currentpages;
            if (count > 1) {
                currentpages = "1 - " + count;
            } else {
                currentpages = "1";
            }
            SplitPagesField.setText(currentpages);
            SplitFileField.setText(fileName);
            String name = file.getName();
            int pos = name.lastIndexOf(".");
            if (pos > 0) {
                name = name.substring(0, pos);
            }
            name = name + "Split";
            SplitNameField.setText(name);
            try {
                doc.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Problem finishing process.", "Problem finishing process",
                        JOptionPane.WARNING_MESSAGE);
            }

        }
    } else {
        System.out.println("File access cancelled by user.");
    }
}

From source file:model.ParsePDF.java

License:Open Source License

/**
 * Find all passed Exams with and without grade and save them to a vector.
 * // w  w  w.  ja  v  a 2s.co m
 * @param file
 * @throws IOException
 */
public void findPassedCourses(String file) throws IOException {
    PDDocument pddDocument = PDDocument.load(new File(file));
    PDFTextStripper textStripper = new PDFTextStripper();
    String x = textStripper.getText(pddDocument);
    String[] lines = getLines(x);

    // find all passed exams and save them in a Vector called courses.
    for (int i = 0; i < lines.length; i++) {
        //               System.out.println(lines[i]);
        if (lines[i].contains("Fach:")) {
            setSubject(lines[i]);
        }
        if (lines[i].contains("Abschluss:")) {
            setCertificate(lines[i]);
        }
        if (lines[i].contains("BE")) {
            courses.addElement(lines[i]);
        }
    }
    pddDocument.close();
}

From source file:model.util.pdf.XDocument.java

License:Apache License

/**
 * Restore the XDocument file from the path which is saved in the
 * class-variable pathToPDF./*from   ww w  .  j  ava2s .  c  om*/
 * 
 * @see setSerializable()
 */
public final void restoreFormSerializable(final String _pString) {
    try {
        //
        // load the document.
        //
        this.document = PDDocument.load(new File(_pString));

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:model.util.pdf.XDocument.java

License:Apache License

/**
 * Call super-class constructor and initialize extra-
 * settings./*from   w ww  .j a  va  2  s . co  m*/
 * @throws IOException 
 */
public XDocument(final String _pString, final Project _project) throws IOException {

    //
    // call super constructor and save values.
    //
    super();
    this.project = _project;

    //
    // load the document.
    //
    this.document = PDDocument.load(new File(_pString));

}

From source file:module.fileSupport.metadata.parsing.parsers.PDFParser.java

License:Open Source License

@Override
protected String extract(InputStream stream) {
    StringWriter writer = null;/*  ww w. ja va 2s  . com*/
    try {
        PDDocument pdfDocument = PDDocument.load(stream);
        writer = new StringWriter();
        PDFTextStripper stripper = new PDFTextStripper();
        stripper.writeText(pdfDocument, writer);
        pdfDocument.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return writer.toString();
}

From source file:modules.PDFFontDependencyExtractorModule.java

License:Apache License

public PDFFontResults extractFontList(File f) throws IOException, InvalidParameterException {
    PDDocument document;/*from   w w  w .  j  av a  2 s . co  m*/
    try {
        document = PDDocument.load(f);
    } catch (IOException x) {
        throw new InvalidParameterException("Not a PDF file");
    }
    SortedSet<FontInformation> ret = new TreeSet<FontInformation>(new Comparator<FontInformation>() {

        @Override
        public int compare(FontInformation o1, FontInformation o2) {
            int a = o1.fontName.compareTo(o2.fontName);
            if (a != 0)
                return a;
            else
                return o1.fontType.compareTo(o2.fontType);
        }

    });

    document.getDocumentCatalog().getAllPages();
    // The code down here is easier as it gets all the fonts used in the
    // document. Still, this would inlcude unused fonts, so we get the fonts
    // page by page and add them to a Hash table.
    for (COSObject c : document.getDocument().getObjectsByType(COSName.FONT)) {
        if (c == null || !(c.getObject() instanceof COSDictionary)) {
            continue;
            // System.out.println(c.getObject());
        }

        COSDictionary fontDictionary = (COSDictionary) c.getObject();
        // System.out.println(dic.getNameAsString(COSName.BASE_FONT));
        // }
        // }
        // int pagen = document.getNumberOfPages();
        // i=0;
        // for (int p=0;p<pagen;p++){
        // PDPage page = (PDPage)pages.get(p);
        // PDResources res = page.findResources();
        // //for each page resources
        // if (res==null) continue;
        // // get the font dictionary
        // COSDictionary fonts = (COSDictionary)
        // res.getCOSDictionary().getDictionaryObject( COSName.FONT );
        // for( COSName fontName : fonts.keySet() ) {
        // COSObject font = (COSObject) fonts.getItem( fontName );
        // // if the font has already been visited we ingore it
        // long objectId = font.getObjectNumber().longValue();
        // if (ret.get(objectId)!=null)
        // continue;
        // if( font==null || ! (font.getObject() instanceof COSDictionary) )
        // continue;
        // COSDictionary fontDictionary = (COSDictionary)font.getObject();

        // Type MUSt be font
        if (!fontDictionary.getNameAsString(COSName.TYPE).equals("Font")) {
            continue;
        }
        // get the variables
        FontInformation fi = new FontInformation();
        fi.fontType = fontDictionary.getNameAsString(COSName.SUBTYPE);

        String baseFont = fontDictionary.getNameAsString(COSName.BASE_FONT);
        if (baseFont == null) {
            continue;
        }
        if (Arrays.binarySearch(standard14, baseFont) >= 0) {
            continue;
        }
        COSDictionary fontDescriptor = (COSDictionary) fontDictionary.getDictionaryObject(COSName.FONT_DESC);
        COSBase enc = fontDictionary.getItem(COSName.ENCODING);
        COSBase uni = fontDictionary.getItem(COSName.TO_UNICODE);
        fontDictionary.getInt(COSName.FIRST_CHAR);
        fontDictionary.getInt(COSName.LAST_CHAR);
        String encoding;
        boolean toUnicode = uni != null;
        if (enc == null) {
            encoding = "standard14";
        }
        if (enc instanceof COSString) {
            encoding = ((COSString) enc).getString();
        } else {
            encoding = "table";
        }
        fi.isSubset = false;
        boolean t = true;
        // Type one and TT can have subsets defineing the basename see 5.5.3
        // pdfref 1.6
        // if (fi.fontType.lastIndexOf(COSName.TYPE1.getName())!=-1 ||
        // fi.fontType.equals(COSName.TRUE_TYPE.getName()) )
        if (baseFont != null) {
            if (baseFont.length() > 6) {
                for (int k = 0; k < 6; k++)
                    if (!Character.isUpperCase(baseFont.charAt(k))) {
                        t = false;
                    }
                if (baseFont.charAt(6) != '+') {
                    t = false;
                }
            } else {
                t = false;
            }
            fi.isSubset = t;
            if (fi.isSubset) {
                fi.baseName = baseFont.substring(0, 6);
                baseFont = baseFont.substring(7);
            }
        }
        fi.fontFlags = 0;
        if (fi.fontType.equals(COSName.TYPE0.getName()) || fi.fontType.equals(COSName.TYPE3.getName())) {
            fi.isEmbedded = true;
        }

        if (fontDescriptor != null) {
            // in Type1 charset indicates font is subsetted
            if (fontDescriptor.getItem(COSName.CHAR_SET) != null) {
                fi.isSubset = true;
            }
            if (fontDescriptor.getItem(COSName.FONT_FILE) != null
                    || fontDescriptor.getItem(COSName.FONT_FILE3) != null
                    || fontDescriptor.getItem(COSName.FONT_FILE2) != null) {
                fi.isEmbedded = true;
            }
            fi.fontFlags = fontDescriptor.getInt(COSName.getPDFName("Flags"));
            fi.fontFamily = fontDescriptor.getString(COSName.FONT_FAMILY);
            fi.fontStretch = fontDescriptor.getString(COSName.FONT_STRETCH);

        }
        fi.charset = encoding;
        fi.fontName = baseFont;
        fi.isToUnicode = toUnicode;
        fi.encoding = fontDictionary.getNameAsString(COSName.CID_TO_GID_MAP);

        ret.add(fi);

    } // for all fonts

    HashMultimap<String, FontInformation> m = HashMultimap.create();

    for (FontInformation ff : ret) {
        m.put(ff.fontName, ff);
    }
    LinkedList<FontInformation> missing = new LinkedList<FontInformation>();
    Set<String> k = m.keySet();
    for (String kk : k) {
        Set<FontInformation> s = m.get(kk);
        if (s.size() < 1) {
            continue;
        }
        if (s.size() > 1) {
            boolean found = false;
            FontInformation ff = null;
            for (FontInformation fonti : s) {
                if (!fonti.isEmbedded) {
                    ff = fonti;
                } else {
                    found = true;
                }
            }
            if (!found) {
                missing.add(ff);
            }
        } else {
            FontInformation ff = s.iterator().next();
            if (!ff.isEmbedded) {
                missing.add(ff);
            }
        }

    }

    // } // for all pages
    // Iterator<FontInformation> it = ret.iterator();
    // FontInformation prev = null;
    // LinkedList<FontInformation> toDelete = new
    // LinkedList<FontInformation>();
    // while (it.hasNext()) {
    // FontInformation current = it.next();
    //
    // if (prev!= null && prev.fontName.equals(current.fontName) &&
    // (prev.fontType.startsWith("CIDFontType") ||
    // current.fontType.startsWith("CIDFontType")))
    // toDelete.add(current);
    // prev = current;
    // }
    //
    // //ret.removeAll(toDelete);
    // FontInformation[] retArray =toDelete.toArray(new FontInformation[0]);
    //

    if (missing.size() == 0) {
        missing = null;
    } else {
        System.out.println("Found missing fonts: " + f);
        System.out.println(missing);
    }
    return new PDFFontResults(new LinkedList<FontInformation>(ret), missing);
}