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.Dimension; import java.io.IOException; 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 PDFNumberingPartial { private JRadioButton voidButton; private JRadioButton selectButton; private JRadioButton skipButton; private ButtonGroup statusButtonGroup; private JLabel seqText; private Boolean[][] statusArray; private JButton submitPDFButton; private List<Boolean> SELECTED; public PDFNumberingPartial(String pdfFileName, String cycle, List<Boolean> selected) { SELECTED = selected; initComponent(pdfFileName, cycle); 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); } public PDFNumberingPartial(String pdfFileName, String cycle) { this(pdfFileName, cycle, null); } private void initComponent(final String pdfFileName, final String cycle) { voidButton = new JRadioButton(); selectButton = new JRadioButton(); skipButton = new JRadioButton(); voidButton.setText("Delete"); voidButton.setEnabled(false); skipButton.setText("Skip"); 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; } } if (SELECTED == null) { for (int j = 0; j < GlobalVar.MAX_NUM_PAGES; j++) { statusArray[GlobalVar.SELECT_BUTTON_INDEX][j] = true; } } else { for (int j = 0; j < GlobalVar.MAX_NUM_PAGES; j++) { if (j < SELECTED.size()) { //System.out.println(SELECTED.get(j)); statusArray[GlobalVar.SELECT_BUTTON_INDEX][j] = SELECTED.get(j); } else { 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) { try { // System.out.println("PDF is pressed"); //String cycle = JOptionPane.showInputDialog(null, "Please enter a cycle number"); // System.out.println("The cycle number is " + cycle); generatePDFFile(pdfFileName, statusArray, cycle); } catch (IOException ex) { Logger.getLogger(PDFNumberingPartial.class.getName()).log(Level.SEVERE, null, ex); } catch (COSVisitorException ex) { Logger.getLogger(PDFNumberingPartial.class.getName()).log(Level.SEVERE, null, ex); } JOptionPane.showMessageDialog(null, "pdf file is successfully numbered on some pages."); } }); } // given the original pdf file, date stamp every page, sequence selected pages and // output two pdf files, one for audit, the other for reject private void generatePDFFile(String pdfFileName, Boolean[][] statusArray, String cycle) throws IOException, COSVisitorException { PDDocument pdf = PDDocument.load(pdfFileName); List pages = pdf.getDocumentCatalog().getAllPages(); Iterator<PDPage> iter = pages.iterator(); PDDocument pdfBlank = new PDDocument(); int pageNum = 0; // 0 based int sequenceNum = 1; // start from 0001 while (iter.hasNext()) { PDPage page = iter.next(); PDPage pageBlank = new PDPage(); PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false); PDPageContentStream streamBlank = new PDPageContentStream(pdfBlank, pageBlank, true, false); if (statusArray[GlobalVar.SELECT_BUTTON_INDEX][pageNum]) { pageWrite(stream, sequenceNum, cycle); pageWrite(streamBlank, sequenceNum, cycle); sequenceNum++; } pdfBlank.addPage(pageBlank); stream.close(); streamBlank.close(); pageNum++; } // out put two pdf files: one is template for printer print hardcopies, the other is digital copy String suffix = "_" + cycle + "_P numbered.pdf"; pdfFileName = pdfFileName.replace(".pdf", suffix); String blankPdfFileName = pdfFileName.replace(".pdf", "BLANK.pdf"); pdf.save(pdfFileName); pdfBlank.save(blankPdfFileName); pdf.close(); pdfBlank.close(); } // write a sequence number on a specific page private void pageWrite(PDPageContentStream stream, int sequenceNum, String cycle) throws IOException { stream.beginText(); stream.setFont(PDType1Font.HELVETICA, GlobalVar.SEQ_NUM_FONT_SIZE); stream.moveTextPositionByAmount(GlobalVar.SEQ_NUM_TEXT_X_POSITION, GlobalVar.SEQ_NUM_TEXT_Y_POSITION); stream.setTextRotation(3.14 / 2, GlobalVar.SEQ_NUM_TEXT_X_POSITION, GlobalVar.SEQ_NUM_TEXT_Y_POSITION); // rotate text 90 degree at x = 600, y = 400 stream.drawString(cycle + "/" + GlobalVar.globalCountGenerator5Digit(sequenceNum)); //sequenceNum++; stream.endText(); } }