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 tutorial; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.parser.PdfReaderContentParser; import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy; import com.itextpdf.text.pdf.parser.TextExtractionStrategy; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.StringTokenizer; import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; /** * * @author Zakaria */ public class PDFtoText extends javax.swing.JFrame { public JFileChooser fileChooser = new JFileChooser(); /** * Creates new form PDFtoText */ public PDFtoText() { initComponents(); } /** * 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() { jFileChooser1 = new javax.swing.JFileChooser(); converting = new javax.swing.JButton(); txtDirektori = new javax.swing.JTextField(); tombolBrowse = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setBackground(new java.awt.Color(204, 204, 0)); converting.setText("Converting"); converting.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { convertingActionPerformed(evt); } }); tombolBrowse.setText("Browse.."); tombolBrowse.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { tombolBrowseActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jLabel1.setText("Convert PDF to Text"); jLabel2.setText("zakariandys.com"); jLabel3.setText("Browse file pdf:"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(135, 135, 135).addComponent(jLabel1)) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(converting) .addGroup(layout.createSequentialGroup() .addComponent(txtDirektori, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(tombolBrowse)) .addGroup(layout.createSequentialGroup().addComponent(jLabel3).addGap(134, 134, 134))))) .addGap(0, 22, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10).addComponent(jLabel1).addGap(18, 18, 18) .addComponent(jLabel3).addGap(8, 8, 8) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txtDirektori, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(tombolBrowse)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(converting) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE) .addComponent(jLabel2))); pack(); }// </editor-fold>//GEN-END:initComponents private void convertingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertingActionPerformed try { convertPDFtoText(); } catch (Exception e) { e.printStackTrace(); } }//GEN-LAST:event_convertingActionPerformed private void tombolBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tombolBrowseActionPerformed // TODO add your handling code here: fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Document", "pdf")); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { try { File selectedFile = fileChooser.getSelectedFile(); txtDirektori.setText(selectedFile.getAbsolutePath()); } catch (Exception ex) { System.out.println(ex); } } else { System.out.println("FIle acces cancelled by user"); } }//GEN-LAST:event_tombolBrowseActionPerformed /** * @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(PDFtoText.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(PDFtoText.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(PDFtoText.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(PDFtoText.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new PDFtoText().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton converting; private javax.swing.JFileChooser jFileChooser1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JButton tombolBrowse; private javax.swing.JTextField txtDirektori; // End of variables declaration//GEN-END:variables public void convertPDFtoText() throws IOException { /*variabel "pdf" digunakan untuk menampung alamat direktori tempat file pdf disimpan.*/ String pdf = txtDirektori.getText(); StringBuilder text = new StringBuilder(); String resultText; /*Buat file Text ".txt"*/ File namaFile = new File(txtDirektori.getText().replace("pdf", "txt")); if (namaFile.createNewFile()) { System.out.println("File .txt berhasil dibuat."); } try { /*Panggil class yang ada pada library iText untuk membaca file PDF*/ PdfReader reader = new PdfReader(pdf); PdfReaderContentParser parser = new PdfReaderContentParser(reader); TextExtractionStrategy strategy; for (int i = 1; i <= reader.getNumberOfPages(); i++) { strategy = parser.processContent(i, new SimpleTextExtractionStrategy()); text.append(strategy.getResultantText()); } resultText = text.toString(); /*Code untuk menuliskan hasil pembacaan file PDF ke file Text*/ StringTokenizer stringTokenizer = new StringTokenizer(resultText, "\n"); PrintWriter lineWriter = new PrintWriter(new FileOutputStream(namaFile)); while (stringTokenizer.hasMoreTokens()) { String curToken = stringTokenizer.nextToken(); lineWriter.println(curToken); } lineWriter.flush(); lineWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }