Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.mycompany.mavenproject1.ragaiproject; import java.util.List; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.DefaultListModel; import javax.swing.JFileChooser; import javax.swing.JList; import javax.swing.JTextArea; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; import org.apache.pdfbox.pdmodel.interactive.form.PDField; import org.apache.pdfbox.pdmodel.interactive.form.PDNonTerminalField; /** * * @author helios */ public class RagaiGUI extends javax.swing.JFrame { /** * Creates new form PDFToExcel */ static JFileChooser fc = new JFileChooser(); File[] files; int index = 0; PDDocument pdf; List<PDDocument> pdfList = new ArrayList<PDDocument>(); //PDFManipulation[] pdfArray; String[] columnHeaders; PDFManipulation formatter = new PDFManipulation(); //public listModel = new DefaultListModel(); public RagaiGUI() { initComponents(); } public void clearTextArea(JTextArea jTextArea) { jTextArea.setText(null); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { loadFileButton = new javax.swing.JButton(); convertButton = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); selectedFiles = new javax.swing.JTextArea(); jScrollPane1 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList<>(); jLabel3 = new javax.swing.JLabel(); outputFileName = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("PDF to Excel"); setResizable(false); loadFileButton.setText("Load Files"); loadFileButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loadFile(evt); } }); convertButton.setText("Convert"); convertButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { convertToExcel(evt); } }); jLabel1.setText("Selected Files"); jLabel2.setText("Form Type"); selectedFiles.setEditable(false); selectedFiles.setColumns(20); selectedFiles.setRows(5); jScrollPane2.setViewportView(selectedFiles); jList1.setModel(new DefaultListModel()); jScrollPane1.setViewportView(jList1); jLabel3.setText("Available Fields"); jLabel4.setText("Excel File Name"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(loadFileButton).addGap(18, 18, 18).addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(155, 155, 155) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2).addComponent(jLabel1)))) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(74, 74, 74) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(18, 18, 18) .addComponent(outputFileName, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(34, 34, 34) .addComponent(jLabel4)) .addGroup(layout.createSequentialGroup().addGap(55, 55, 55) .addComponent(convertButton)))) .addGroup( layout.createSequentialGroup().addGap(119, 119, 119).addComponent(jLabel3))) .addContainerGap(102, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(jLabel3)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE) .addComponent(jScrollPane2) .addGroup(layout.createSequentialGroup().addGap(33, 33, 33) .addComponent(jLabel4) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(outputFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(convertButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)) .addGroup(layout.createSequentialGroup().addGap(103, 103, 103) .addComponent(loadFileButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addComponent(jLabel2).addGap(78, 78, 78))); pack(); }// </editor-fold>//GEN-END:initComponents private void loadFile(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadFile // TODO add your handling code here: String filePath; List<File> fileList = new ArrayList<File>(); files = null; columnHeaders = null; clearTextArea(selectedFiles); ((DefaultListModel) (jList1.getModel())).clear(); int returnVal = fc.showOpenDialog(jLabel1); if (returnVal == JFileChooser.APPROVE_OPTION) { //File[]files = fc.getSelectedFiles(); files = fc.getSelectedFiles(); for (File file : files) { selectedFiles.append(file.getName() + "\n"); } for (int i = 0; i < files.length; i++) { filePath = files[i].getAbsolutePath(); fileList.add(new File(filePath)); } //String filePath = files[0].getAbsolutePath(); // File file = new File(filePath); PDDocument formatterPDF = null; try { formatterPDF = PDDocument.load(fileList.get(0)); //PDFManipulation formatter = new PDFManipulation(); for (int i = 0; i < files.length; i++) { pdfList.add(PDDocument.load(fileList.get(i))); } columnHeaders = formatter.getFieldNames(formatterPDF, jList1); } catch (IOException ex) { System.out.println("PDF Failed to Load"); } } }//GEN-LAST:event_loadFile private void convertToExcel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertToExcel // TODO add your handling code here: System.out.println("files = " + Arrays.toString(files)); String filePath = files[0].getAbsolutePath(); File file = new File(filePath); PDDocument pdf1 = null; List<String> selectedFields = jList1.getSelectedValuesList(); try { pdf1 = PDDocument.load(file); //PDFManipulation exporter = new PDFManipulation(); //pdfArray[0] = exporter.powderMETForm(pdf1); formatter.convert(pdfList, selectedFields, outputFileName.getText()); } catch (IOException ex) { Logger.getLogger(RagaiGUI.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_convertToExcel /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(RagaiGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(RagaiGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(RagaiGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(RagaiGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new RagaiGUI().setVisible(true); fc.setMultiSelectionEnabled(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton convertButton; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JList<String> jList1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JButton loadFileButton; private javax.swing.JTextField outputFileName; private javax.swing.JTextArea selectedFiles; // End of variables declaration//GEN-END:variables }