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 pdf; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.GraphicsEnvironment; import java.awt.LayoutManager; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.AdjustmentEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.BorderFactory; import javax.swing.JFileChooser; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.SpringLayout; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.filechooser.FileNameExtensionFilter; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; /** * * @author thomas.sauvajon, loic.thiawwingkai */ public class PDFWindow extends javax.swing.JFrame { static PDDocument actualFile = null; static HashMap<Integer, Integer> offset = new HashMap(); static float zoom = 0.25f; int currentPage = 0; int nbPages = 0; int i = 0; static HashMap<Integer, JPanel> panels = new HashMap(); int selectedTool = 0; int dragging = 0; /** * Creates new form PDFWindow */ public PDFWindow() { initComponents(); setIcon(); jScrollPaneImageContainer.getVerticalScrollBar() .addAdjustmentListener(new java.awt.event.AdjustmentListener() { @Override public void adjustmentValueChanged(AdjustmentEvent e) { if (nbPages != 0) { ScrollChanged(e); } } }); jSpinner1.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { if (nbPages != 0) { if ((int) jSpinner1.getValue() < 1) { jSpinner1.setValue(1); } if ((int) jSpinner1.getValue() > nbPages) { jSpinner1.setValue(nbPages); } currentPage = (int) jSpinner1.getValue() - 1; jScrollPaneImageContainer.getVerticalScrollBar().setValue(offset.get(currentPage)); } } }); } /** * 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() { bindingGroup = new org.jdesktop.beansbinding.BindingGroup(); jPopupMenu1 = new javax.swing.JPopupMenu(); jPopupMenu2 = new javax.swing.JPopupMenu(); buttonGroup1 = new javax.swing.ButtonGroup(); FrameJoin = new javax.swing.JFrame(); jPanel2 = new javax.swing.JPanel(); jLabel5 = new javax.swing.JLabel(); jRadioButton3 = new javax.swing.JRadioButton(); joinStartInput = new javax.swing.JSpinner(); jRadioButton2 = new javax.swing.JRadioButton(); jRadioButton1 = new javax.swing.JRadioButton(); buttonAddJoin = new javax.swing.JButton(); fileUrlJoin = new javax.swing.JTextField(); addJoin = new javax.swing.JButton(); cancelJoin = new javax.swing.JButton(); jPanelBody = new javax.swing.JPanel(); jToolBar1 = new javax.swing.JToolBar(); jPanel1 = new javax.swing.JPanel(); jSlider1 = new javax.swing.JSlider(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); Ouvrir = new javax.swing.JButton(); jButtonExtract = new javax.swing.JButton(); jButtonJoin = new javax.swing.JButton(); fromPage = new javax.swing.JSpinner(); toPage = new javax.swing.JSpinner(); jTabbedPane2 = new javax.swing.JTabbedPane(); jScrollPaneImageContainer = new javax.swing.JScrollPane(); jLabel3 = new javax.swing.JLabel(); jSpinner1 = new javax.swing.JSpinner(); jLabel4 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButtonPreviousPage = new javax.swing.JButton(); jButtonNextPage = new javax.swing.JButton(); jMenuBar1 = new javax.swing.JMenuBar(); jMenuFile = new javax.swing.JMenu(); jMenuItemOpen = new javax.swing.JMenuItem(); jMenuItemSave = new javax.swing.JMenuItem(); jMenuItemSaveAs = new javax.swing.JMenuItem(); jMenuItemClose = new javax.swing.JMenuItem(); jMenuItemQuit = new javax.swing.JMenuItem(); jMenuEdit = new javax.swing.JMenu(); jMenuItemJoin = new javax.swing.JMenuItem(); jMenuItemExtract = new javax.swing.JMenuItem(); jLabel5.setText("Insrer aprs :"); buttonGroup1.add(jRadioButton3); jRadioButton3.setMnemonic('\u0003'); jRadioButton3.setText("Aprs la page :"); buttonGroup1.add(jRadioButton2); jRadioButton2.setMnemonic('\u0002'); jRadioButton2.setSelected(true); jRadioButton2.setText("A la fin"); jRadioButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton2ActionPerformed(evt); } }); buttonGroup1.add(jRadioButton1); jRadioButton1.setMnemonic('\u0001'); jRadioButton1.setText("Au dbut"); jRadioButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton1ActionPerformed(evt); } }); buttonAddJoin.setText("Parcourir"); buttonAddJoin.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonAddJoinActionPerformed(evt); } }); fileUrlJoin.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fileUrlJoinActionPerformed(evt); } }); addJoin.setText("Ajouter"); addJoin.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addJoinActionPerformed(evt); } }); cancelJoin.setText("Annuler"); cancelJoin.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelJoinActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap(28, Short.MAX_VALUE) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup().addComponent(buttonAddJoin) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fileUrlJoin, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jRadioButton1) .addGroup(jPanel2Layout.createSequentialGroup().addGap(12, 12, 12) .addComponent(jLabel5).addGap(31, 31, 31) .addComponent(joinStartInput, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jRadioButton2).addComponent(jRadioButton3)) .addGap(144, 144, 144))) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(addJoin) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cancelJoin))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(buttonAddJoin).addComponent(fileUrlJoin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(joinStartInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(addJoin).addComponent(cancelJoin)) .addContainerGap())); javax.swing.GroupLayout FrameJoinLayout = new javax.swing.GroupLayout(FrameJoin.getContentPane()); FrameJoin.getContentPane().setLayout(FrameJoinLayout); FrameJoinLayout.setHorizontalGroup(FrameJoinLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(FrameJoinLayout.createSequentialGroup().addContainerGap().addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(27, 27, 27))); FrameJoinLayout.setVerticalGroup(FrameJoinLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(FrameJoinLayout.createSequentialGroup().addContainerGap().addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(16, 16, 16))); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setName("Fentre principale"); // NOI18N jToolBar1.setRollover(true); jToolBar1.setFocusable(false); jLabel1.setText("zoom"); jLabel1.setToolTipText(""); org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding( org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jSlider1, org.jdesktop.beansbinding.ELProperty.create("${value}%"), jLabel2, org.jdesktop.beansbinding.BeanProperty.create("text")); bindingGroup.addBinding(binding); Ouvrir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pdf/ouvrir.jpg"))); // NOI18N Ouvrir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { OuvrirActionPerformed(evt); } }); jButtonExtract.setText("Extraire"); jButtonExtract.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonExtractActionPerformed(evt); } }); jButtonJoin.setText("Joindre un autre PDF ..."); jButtonJoin.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonJoinActionPerformed(evt); } }); fromPage.setVerifyInputWhenFocusTarget(false); toPage.setVerifyInputWhenFocusTarget(false); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2).addGap(18, 18, 18) .addComponent(Ouvrir, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(46, 46, 46) .addComponent(fromPage, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(toPage, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonExtract) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonJoin).addContainerGap(26, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1).addComponent(jLabel2))) .addGroup(jPanel1Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(Ouvrir) .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonExtract, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButtonJoin, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(fromPage, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(toPage, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))))) .addGap(18, 18, 18))); jScrollPaneImageContainer.setBackground(new java.awt.Color(80, 80, 80)); jScrollPaneImageContainer.setEnabled(false); jTabbedPane2.addTab("PDF", jScrollPaneImageContainer); jLabel3.setText("Page :"); jSpinner1.setVerifyInputWhenFocusTarget(false); jLabel4.setText("/ " + nbPages); jButton1.setText("Ajouter du texte"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Enregistrer"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButtonPreviousPage.setText("<- Page prcedente"); jButtonPreviousPage.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonPreviousPageActionPerformed(evt); } }); jButtonNextPage.setText("Page suivante ->"); jButtonNextPage.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonNextPageActionPerformed(evt); } }); javax.swing.GroupLayout jPanelBodyLayout = new javax.swing.GroupLayout(jPanelBody); jPanelBody.setLayout(jPanelBodyLayout); jPanelBodyLayout.setHorizontalGroup(jPanelBodyLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelBodyLayout.createSequentialGroup() .addContainerGap() .addGroup(jPanelBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelBodyLayout.createSequentialGroup() .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4)) .addGroup(jPanelBodyLayout.createSequentialGroup() .addComponent(jTabbedPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 750, Short.MAX_VALUE) .addGap(30, 30, 30) .addGroup(jPanelBodyLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonPreviousPage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonNextPage, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap())); jPanelBodyLayout.setVerticalGroup(jPanelBodyLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelBodyLayout.createSequentialGroup().addGroup(jPanelBodyLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4))) .addGroup(jPanelBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelBodyLayout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTabbedPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE) .addGap(32, 32, 32)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelBodyLayout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonPreviousPage) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonNextPage).addGap(390, 390, 390))))); jMenuFile.setText("Fichier"); jMenuItemOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK)); jMenuItemOpen.setText("Ouvrir ..."); jMenuItemOpen.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemOpenActionPerformed(evt); } }); jMenuFile.add(jMenuItemOpen); jMenuItemSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK)); jMenuItemSave.setText("Enregistrer"); jMenuItemSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemSaveActionPerformed(evt); } }); jMenuFile.add(jMenuItemSave); jMenuItemSaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK)); jMenuItemSaveAs.setText("Enregistrer sous ..."); jMenuItemSaveAs.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemSaveAsActionPerformed(evt); } }); jMenuFile.add(jMenuItemSaveAs); jMenuItemClose.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK)); jMenuItemClose.setText("Fermer"); jMenuItemClose.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemCloseActionPerformed(evt); } }); jMenuFile.add(jMenuItemClose); jMenuItemQuit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK)); jMenuItemQuit.setText("Quitter"); jMenuItemQuit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemQuitActionPerformed(evt); } }); jMenuFile.add(jMenuItemQuit); jMenuBar1.add(jMenuFile); jMenuEdit.setText("Editer"); jMenuItemJoin.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_J, java.awt.event.InputEvent.CTRL_MASK)); jMenuItemJoin.setText("Joindre ..."); jMenuEdit.add(jMenuItemJoin); jMenuItemExtract.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK)); jMenuItemExtract.setText("Extraire ..."); jMenuItemExtract.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemExtractActionPerformed(evt); } }); jMenuEdit.add(jMenuItemExtract); jMenuBar1.add(jMenuEdit); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanelBody, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jPanelBody, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); bindingGroup.bind(); pack(); }// </editor-fold>//GEN-END:initComponents private void jMenuItemOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemOpenActionPerformed openDocument = importFile(evt); jTabbedPane2.setTitleAt(0, openDocumentPath); actualFile = openDocument; getImages(openDocument); }//GEN-LAST:event_jMenuItemOpenActionPerformed private void displayPage() { nbPages = images.size(); int totalPagesHeight = 0; int maxWidth = 0; jLabel4.setText("/ " + String.valueOf(nbPages)); SpringLayout layout = new SpringLayout(); JPanel canvasContainer = new JPanel(); canvasContainer.setBackground(new Color(80, 80, 80)); for (int i = 0; i < nbPages; i++) { PDFCanvas canvas = new PDFCanvas(images.get(i)); canvas.setPreferredSize(new Dimension(Math.round(images.get(i).getWidth() * PDFWindow.zoom), Math.round(images.get(i).getHeight() * PDFWindow.zoom))); offset.put(i, Math.round(totalPagesHeight * PDFWindow.zoom) + 50); canvas.setLayout(layout); panels.put(i, canvas); totalPagesHeight += images.get(i).getHeight(); maxWidth = (maxWidth < images.get(i).getWidth()) ? images.get(i).getWidth() : maxWidth; canvas.revalidate(); canvasContainer.add(canvas); } canvasContainer.setPreferredSize(new Dimension(Math.round(maxWidth * PDFWindow.zoom), Math.round(totalPagesHeight * PDFWindow.zoom))); jScrollPaneImageContainer.setViewportView(canvasContainer); jScrollPaneImageContainer.revalidate(); } private void jMenuItemQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemQuitActionPerformed // TODO add your handling code here: System.exit(0); }//GEN-LAST:event_jMenuItemQuitActionPerformed private void jMenuItemExtractActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExtractActionPerformed // TODO add your handling code here: // TODO : demander page de dbut, page de fin (ou nb de pages ?), le chemin de destination, puis sauver le fichier. }//GEN-LAST:event_jMenuItemExtractActionPerformed private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveActionPerformed if (openDocument != null && openDocumentPath != null) { try { openDocument.save(openDocumentPath); } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_jMenuItemSaveActionPerformed private void jButtonPreviousPageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPreviousPageActionPerformed if (images != null && !images.isEmpty() && currentPage > 0) { currentPage--; jScrollPaneImageContainer.getVerticalScrollBar().setValue(offset.get(currentPage)); } }//GEN-LAST:event_jButtonPreviousPageActionPerformed private void jButtonNextPageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNextPageActionPerformed if (images != null && !images.isEmpty() && images.size() > currentPage + 1) { currentPage++; jScrollPaneImageContainer.getVerticalScrollBar().setValue(offset.get(currentPage)); } }//GEN-LAST:event_jButtonNextPageActionPerformed private void ScrollChanged(java.awt.event.AdjustmentEvent evt) { //Get actual offset int currentOffset = evt.getAdjustable().getValue(); int distance = Math.abs(currentOffset - offset.get(0)); int index = 0; for (int compteur = 1; compteur < offset.size(); compteur++) { int cdistance = Math.abs(currentOffset - offset.get(compteur)); if (cdistance < distance) { index = compteur; distance = cdistance; } else { break; } } this.currentPage = index; jSpinner1.setValue(index + 1); } private void jMenuItemSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveAsActionPerformed // TODO add your handling code here: if (openDocument != null) { try { // todo : prompt path => openDocumentPath openDocument.save(openDocumentPath); } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_jMenuItemSaveAsActionPerformed private void jMenuItemCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCloseActionPerformed if (openDocument != null) { try { openDocument.close(); } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } } openDocumentPath = null; images = null; // ne plus afficher d'image dans le panel }//GEN-LAST:event_jMenuItemCloseActionPerformed private void buttonAddJoinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddJoinActionPerformed // TODO add your handling code here: Edit.joinDoc = importFile(evt); fileUrlJoin.setText(openDocumentPath); }//GEN-LAST:event_buttonAddJoinActionPerformed private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jRadioButton1ActionPerformed private void fileUrlJoinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileUrlJoinActionPerformed // TODO add your handling code here: }//GEN-LAST:event_fileUrlJoinActionPerformed private void addJoinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addJoinActionPerformed Edit.JoinSelectedOption = buttonGroup1.getSelection().getMnemonic(); if (Edit.JoinSelectedOption == 3) { Edit.joinStart = (int) joinStartInput.getValue(); } PDDocument joined = Edit.join(); try { saveFile(joined); } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_addJoinActionPerformed private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jRadioButton2ActionPerformed private void cancelJoinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJoinActionPerformed }//GEN-LAST:event_cancelJoinActionPerformed private void jButtonJoinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonJoinActionPerformed // TODO add your handling code here: FrameJoin.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("favicon.png"))); FrameJoin.setSize(new Dimension(400, 250)); FrameJoin.setLocation(this.getWidth() / 2 - FrameJoin.getWidth() / 2, this.getHeight() / 2 - FrameJoin.getHeight() / 2); FrameJoin.setVisible(true); }//GEN-LAST:event_jButtonJoinActionPerformed private void jButtonExtractActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExtractActionPerformed // TODO add your handling code here: int from = (int) fromPage.getValue(); int to = (int) toPage.getValue(); PDDocument extract = Edit.split(actualFile, from, to); try { saveFile(extract); } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } try { extract.close(); } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } FrameJoin.setVisible(false); }//GEN-LAST:event_jButtonExtractActionPerformed private void OuvrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OuvrirActionPerformed // TODO add your handling code here: jMenuItemOpenActionPerformed(evt); }//GEN-LAST:event_OuvrirActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed TextAdd textArea = new TextAdd(); textArea.setOpaque(false); textArea.setBackground(new Color(0, 0, 0, 0)); textArea.setBorder(BorderFactory.createLineBorder(Color.black)); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fonts = ge.getAvailableFontFamilyNames(); Font font = new Font("Times New Roman", Font.PLAIN, 12); textArea.setFont(font); textArea.setSize(20, 100); SpringLayout layout = (SpringLayout) panels.get(currentPage).getLayout(); layout.putConstraint(SpringLayout.WEST, textArea, 0, SpringLayout.WEST, panels.get(currentPage)); layout.putConstraint(SpringLayout.NORTH, textArea, 0, SpringLayout.NORTH, panels.get(currentPage)); /*textArea.addMouseMotionListener(new MouseMotionListener(){ @Override public void mouseDragged(MouseEvent e) { System.out.println(e); } @Override public void mouseMoved(MouseEvent e) { System.out.println(e); } });*/ textArea.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { textArea.prevPosX = e.getX(); textArea.prevPosY = e.getY(); } @Override public void mouseReleased(MouseEvent e) { textArea.newPosX = e.getX(); textArea.newPosY = e.getY(); if (textArea.newPosX - textArea.prevPosX != 0 && textArea.newPosY - textArea.prevPosY != 0) { textArea.setLocation(textArea.getLocation().x + textArea.newPosX - textArea.prevPosX, textArea.getLocation().y + textArea.newPosY - textArea.prevPosY); textArea.newPosX = textArea.getLocation().x; textArea.newPosY = textArea.getLocation().y; } } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } }); panels.get(currentPage).add(textArea); panels.get(currentPage).revalidate(); }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: buildPDF(); }//GEN-LAST:event_jButton2ActionPerformed private void buildPDF() { for (int index = 0; index < panels.size(); index++) { Component[] addingComponent = panels.get(index).getComponents(); for (Component cmp : addingComponent) { if ("pdf.TextAdd".equals(cmp.getClass().getName())) { TextAdd cmpToAdd = (TextAdd) cmp; String text = cmpToAdd.getText(); try { Edit.addText(actualFile, index, cmpToAdd.getLocation().x, cmpToAdd.getLocation().y + cmpToAdd.getHeight() / 2, text); } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } } } } try { saveFile(actualFile); } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } } private void setIcon() { setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("favicon.png"))); } /** * @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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(PDFWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(() -> { new PDFWindow().setVisible(true); }); } private PDDocument openDocument; private ArrayList<BufferedImage> images; boolean hasChanged = false; String openDocumentPath; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JFrame FrameJoin; private javax.swing.JButton Ouvrir; private javax.swing.JButton addJoin; private javax.swing.JButton buttonAddJoin; private javax.swing.ButtonGroup buttonGroup1; private javax.swing.JButton cancelJoin; private javax.swing.JTextField fileUrlJoin; private javax.swing.JSpinner fromPage; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButtonExtract; private javax.swing.JButton jButtonJoin; private javax.swing.JButton jButtonNextPage; private javax.swing.JButton jButtonPreviousPage; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenu jMenuEdit; private javax.swing.JMenu jMenuFile; private javax.swing.JMenuItem jMenuItemClose; private javax.swing.JMenuItem jMenuItemExtract; private javax.swing.JMenuItem jMenuItemJoin; private javax.swing.JMenuItem jMenuItemOpen; private javax.swing.JMenuItem jMenuItemQuit; private javax.swing.JMenuItem jMenuItemSave; private javax.swing.JMenuItem jMenuItemSaveAs; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanelBody; private javax.swing.JPopupMenu jPopupMenu1; private javax.swing.JPopupMenu jPopupMenu2; private javax.swing.JRadioButton jRadioButton1; private javax.swing.JRadioButton jRadioButton2; private javax.swing.JRadioButton jRadioButton3; private javax.swing.JScrollPane jScrollPaneImageContainer; private javax.swing.JSlider jSlider1; private javax.swing.JSpinner jSpinner1; private javax.swing.JTabbedPane jTabbedPane2; private javax.swing.JToolBar jToolBar1; private javax.swing.JSpinner joinStartInput; private javax.swing.JSpinner toPage; private org.jdesktop.beansbinding.BindingGroup bindingGroup; // End of variables declaration//GEN-END:variables private void loadDisplay(int totalPages, int actualPage) { float coef = 100 / totalPages; int percent = Math.round((actualPage + 1) * coef); System.out.println(percent); } private void saveFile(PDDocument saving) throws IOException { String filename = ""; String dir = ""; JFileChooser c = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF Documents", "pdf"); c.setFileFilter(filter); // Demonstrate "Save" dialog: int rVal = c.showSaveDialog(null); if (rVal == JFileChooser.APPROVE_OPTION) { filename = c.getSelectedFile().getName(); dir = c.getCurrentDirectory().toString(); saving.save(dir + "/" + filename + ".pdf"); } } PDDocument importFile(ActionEvent e) { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF Documents", "pdf"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { File chosen = chooser.getSelectedFile(); PDDocument document; try { document = PDDocument.load(chosen); openDocumentPath = chosen.getPath(); return document; } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } } return null; } private void getImages(PDDocument doc) { PDFRenderer pdfRenderer = new PDFRenderer(doc); images = new ArrayList(); int numberOfPages = doc.getNumberOfPages(); Thread thread = new Thread(new Runnable() { public void run() { for (int page = 0; page < numberOfPages; ++page) { BufferedImage bim; i = page; try { bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB); images.add(bim); } catch (IOException ex) { Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex); } displayPage(); try { java.lang.Thread.sleep(100); } catch (Exception e) { } } } }); thread.start(); } }