PDF.PDFRemover.java Source code

Java tutorial

Introduction

Here is the source code for PDF.PDFRemover.java

Source

/*
 * 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.Dimension;
import java.io.IOException;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import Utilities.GlobalVar;

/**
 *
 * @author bob
 */
public class PDFRemover {
    /**
     * @param args the command line arguments
     */
    private JRadioButton voidButton;
    private JRadioButton selectButton;
    private JRadioButton skipButton;
    private ButtonGroup statusButtonGroup;
    private JLabel seqText;
    private Boolean[][] statusArray;

    private JButton submitPDFButton;

    public PDFRemover(String pdfFileName) {

        initComponent(pdfFileName);

        SwingSimpleController controller = new SwingSimpleController(statusButtonGroup, seqText, statusArray);
        controller.openDocument(pdfFileName); // show the component 
        int pageNum = controller.getDocument().getNumberOfPages();

        System.out.println(controller.getDocument().getNumberOfPages());

        SwingViewSimpleBuilder factory = new SwingViewSimpleBuilder(controller, statusButtonGroup, seqText,
                submitPDFButton, statusArray);

        JPanel viewerComponentPanel = factory.buildViewerPanel();
        // add interactive mouse link annotation support via callback 
        //        controller.getDocumentViewController().setAnnotationCallback(
        //                new org.icepdf.ri.common.MyAnnotationCallback(controller.getDocumentViewController())); 

        JFrame applicationFrame = new JFrame();
        applicationFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        applicationFrame.getContentPane().add(viewerComponentPanel); // Now that the GUI is all in place, we can try opening a PDF 
        //        controller.openDocument(filePath); // show the component 
        //        System.out.println(controller.getDocument().getNumberOfPages());
        applicationFrame.pack();
        applicationFrame.setVisible(true);
    }

    private void initComponent(final String pdfFileName) {
        voidButton = new JRadioButton();
        selectButton = new JRadioButton();
        skipButton = new JRadioButton();

        voidButton.setText("Delete");
        skipButton.setText("Skip");
        skipButton.setEnabled(false);

        selectButton.setText("Select");
        selectButton.setEnabled(false);

        statusButtonGroup = new ButtonGroup();
        statusButtonGroup.add(voidButton);
        statusButtonGroup.add(skipButton);
        statusButtonGroup.add(selectButton);

        seqText = new JLabel();
        seqText.setText("----");
        seqText.setSize(new Dimension(500, 20));
        //Icon ic = new ImageIcon("heart.gif");
        // seqText.setIcon(new ImageIcon("heart.gif"));
        seqText.setIcon(new ImageIcon(getClass().getResource("heart.gif")));
        statusArray = new Boolean[GlobalVar.NUM_BUTTON][GlobalVar.MAX_NUM_PAGES];

        for (int i = 0; i < GlobalVar.NUM_BUTTON; i++) {
            for (int j = 0; j < GlobalVar.MAX_NUM_PAGES; j++) {
                statusArray[i][j] = false;
            }
        }

        for (int j = 0; j < GlobalVar.MAX_NUM_PAGES; j++) {
            statusArray[GlobalVar.SELECT_BUTTON_INDEX][j] = true;
        }

        submitPDFButton = new JButton();
        submitPDFButton.setText("Generate PDF");
        //Icon ic4 = new ImageIcon("signup.gif");
        // this.setIconImage(new ImageIcon(getClass().getResource(GlobalVar.ICON_NAME)).getImage());
        //submitPDFButton.setIcon(new ImageIcon("signup.gif"));   
        submitPDFButton.setIcon(new ImageIcon(getClass().getResource("signup.gif")));
        submitPDFButton.setSize(500, 200);
        //submitPDFButton.setBorder(BorderFactory.createLineBorder(Color.black, 5, true));
        submitPDFButton.setBorder(BorderFactory.createRaisedBevelBorder());
        submitPDFButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {

                // System.out.println("PDF is pressed");
                //String cycle = JOptionPane.showInputDialog(null, "Please enter a cycle number");
                // System.out.println("The cycle number is " + cycle);
                try {
                    generatePDFFile(pdfFileName, statusArray);
                    JOptionPane.showMessageDialog(null, "Truncated pdf file is created successfully.");
                } catch (IOException ex) {
                    Logger.getLogger(PDFRemover.class.getName()).log(Level.SEVERE, null, ex);
                } catch (COSVisitorException ex) {
                    Logger.getLogger(PDFRemover.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // given the original pdf file, date stamp every page, sequence selected pages and
    // output two pdf files, one for audit, the other for reject 
    public void generatePDFFile(String pdfFileName, Boolean[][] statusArray)
            throws IOException, COSVisitorException {
        PDDocument pdf = PDDocument.load(pdfFileName);

        String truncatedPdfFileName = pdfFileName.replace(".pdf", "_Truncated.pdf");
        extractGoodPdf(pdf, truncatedPdfFileName, statusArray);

        pdf.close();
    }

    //given the date stamped, sequenced pdf file, audit pdf filename, reject pdf file name, and status array,
    // output two pdf files: one for audit, the other for reject
    private void extractGoodPdf(PDDocument pdf, String auditPdfFileName, Boolean[][] statusArray)
            throws COSVisitorException, IOException {

        PDDocument auditPdf = new PDDocument();
        int pageNum = pdf.getNumberOfPages();

        // add reject page into rejectPdf
        for (int i = 0; i < pageNum; i++) {
            PDPage page = (PDPage) pdf.getDocumentCatalog().getAllPages().get(i);
            if (!statusArray[GlobalVar.VOID_BUTTON_INDEX][i]) {

                auditPdf.addPage(page);
            }
        }

        auditPdf.save(auditPdfFileName);
        auditPdf.close();
    }
}