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.io.IOException; import java.util.GregorianCalendar; import java.util.Iterator; import java.util.List; import javax.swing.JOptionPane; 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 PDFDateStamp { private String DATE = null; public PDFDateStamp(String fileName, String ADSNname) throws IOException, COSVisitorException { DATE = JOptionPane.showInputDialog(null, "Please enter a date stamp date if other than today."); System.out.println("pdfDateStamp.java: Date given is " + DATE); if (DATE == null || DATE.equalsIgnoreCase("")) { GregorianCalendar today = new GregorianCalendar(); DATE = today.getTime().toString(); } else { DATE = "Received on " + DATE + " " + ADSNname + " DMPO"; } generatePDFFile(fileName); } public void generatePDFFile(String pdfFileName) throws IOException, COSVisitorException { PDDocument pdf = PDDocument.load(pdfFileName); List pages = pdf.getDocumentCatalog().getAllPages(); Iterator<PDPage> iter = pages.iterator(); while (iter.hasNext()) { PDPage page = iter.next(); PDPageContentStream stream = new PDPageContentStream(pdf, page, true, false); // == date stamp stream.beginText(); stream.setFont(PDType1Font.HELVETICA, GlobalVar.DATE_STAMP_FONT_SIZE); stream.moveTextPositionByAmount(GlobalVar.DATE_STAMP_X_POSITION, GlobalVar.DATE_STAMP_Y_POSITION); stream.drawString(DATE); //date stamp stream.endText(); // end of date stamp stream.close(); } // out put two pdf files: one for audit, the other for reject pdfFileName = pdfFileName.replace(".pdf", "_DateStamped.pdf"); pdf.save(pdfFileName); pdf.close(); } }