List of usage examples for org.apache.pdfbox.pdmodel PDDocument getNumberOfPages
public int getNumberOfPages()
From source file:io.inkstand.scribble.pdf.PageCountMatcher.java
License:Apache License
@Override protected boolean matchesPDF(final PDDocument doc) { this.actualNumPages = doc.getNumberOfPages(); return doc.getNumberOfPages() == pageCount; }
From source file:main.java.vasolsim.common.GenericUtils.java
License:Open Source License
/** * gets the number of pages in a pdf/* ww w. j a v a2 s. co m*/ * * @param file * * @return * * @throws IOException */ public static int getPDFPages(File file) throws IOException { PDDocument doc = PDDocument.load(file); int pages = doc.getNumberOfPages(); doc.close(); return pages; }
From source file:merge_split.MergeSplit.java
License:Apache License
private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed String fileName;//from ww w. ja va2 s . com int returnVal = jFileChooser1.showOpenDialog((Component) evt.getSource()); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = jFileChooser1.getSelectedFile(); fileName = file.toString(); PDDocument doc = null; String code = ""; try { doc = PDDocument.load(file); if (doc.isEncrypted()) { doc.setAllSecurityToBeRemoved(true); } } catch (IOException ex) { } if (doc == null) { JFrame frame = new JFrame("Input Dialog Example 3"); code = JOptionPane.showInputDialog(frame, "Enter password", "PDF is encrypted", JOptionPane.WARNING_MESSAGE); try { doc = PDDocument.load(file, code); } 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"; } boolean isOriginalDocEncrypted = doc.isEncrypted(); String column4; if (isOriginalDocEncrypted) { column4 = code; } else { column4 = "ok"; } dtm.addRow(new Object[] { fileName, count, currentpages, column4 }); try { doc.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Problem accessing file.", "Problem accessing file", JOptionPane.WARNING_MESSAGE); } arr.add(file); } } else { System.out.println("File access cancelled by user."); } }
From source file:merge_split.MergeSplit.java
License:Apache License
private void MergeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MergeButtonActionPerformed try {//from w ww . j a va2 s . c om PDDocument samplePdf = new PDDocument(); ArrayList<PDDocument> list = new ArrayList<>(); for (int i = 0; i < dtm.getRowCount(); i++) { File file = new File((String) dtm.getValueAt(i, 0)); String code = (String) dtm.getValueAt(i, 3); PDDocument doc1; if (code.equals("ok")) { doc1 = PDDocument.load(file); } else { doc1 = PDDocument.load(file, code); } list.add(doc1); doc1.setAllSecurityToBeRemoved(true); TreeSet tree = findPages((String) dtm.getValueAt(i, 2)); for (int j = 0; j < doc1.getNumberOfPages(); j++) { if (tree.contains(j + 1)) { samplePdf.addPage(doc1.getPage(j)); } } } System.out.println("Number:" + samplePdf.getNumberOfPages()); String destination = jTextField1.getText() + "\\" + jTextField2.getText() + ".pdf"; PDDocumentInformation info = samplePdf.getDocumentInformation(); info.setAuthor(jTextField3.getText()); File output = new File(destination); samplePdf.save(output); samplePdf.close(); for (int i = 0; i < list.size(); i++) { list.get(i).close(); } } catch (IOException 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 RotateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RotateButtonActionPerformed try {/*from ww 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;/* w w w.j av a 2 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) { } 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;//w w w . j a v a 2s .c om 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 ConvertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConvertButtonActionPerformed PDDocument document = null; try {// w w w . j a v a 2 s .c o m document = PDDocument.load(new File((String) ConvertFileField.getText()), convertcode); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Problem opening pdf.", "Problem opening pdf", JOptionPane.WARNING_MESSAGE); } TreeSet tree = findPages((String) ConvertPagesField.getText()); PDFRenderer pdfRenderer = new PDFRenderer(document); for (int page = 0; page < document.getNumberOfPages(); ++page) { BufferedImage bim = null; try { bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Problem rendering image.", "Problem rendering image", JOptionPane.WARNING_MESSAGE); } // suffix in filename will be used as the file format String destination = ConvertDestinationField.getText() + "\\" + ConvertNameField.getText(); String image = ".png"; if (pngbutton.isSelected()) { image = ".png"; } else if (bmpbutton.isSelected()) { image = ".bmp"; } else if (gifbutton.isSelected()) { image = ".gif"; } else if (jpgbutton.isSelected()) { image = ".jpg"; } try { if (tree.contains(page + 1)) { ImageIOUtil.writeImage(bim, destination + "-" + (page + 1) + image, 300); } } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Problem output image.", "Problem output image", JOptionPane.WARNING_MESSAGE); java.util.logging.Logger.getLogger(MergeSplit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } } try { document.close(); } catch (IOException ex) { Logger.getLogger(MergeSplit.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:merge_split.MergeSplit.java
License:Apache License
private void SplitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SplitButtonActionPerformed try {/*www .j a va 2 s . co 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;// w w w .java2 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."); } }