尝试打开pdf时如何弹出

时间:2018-07-11 14:00:05

标签: java itext7

如果PDF的创建日期早于固定的天数,我会弹出一个pdf过期的弹出窗口。我正在弹出窗口,但是在打开pdf之后。 我希望与pdf在实际打开pdf之前要求输入密码时一样。

public class ValidateExpirationPDF {
public static final String DEST = "./resources/GlobalSRG_Homepage_deycpted.pdf";
public static final String SRC = "./resources/GlobalSRG_Homepage.pdf";

public static void main(String[] args) throws Exception {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new ValidateExpirationPDF().validatePdf(DEST);
}

protected void validatePdf(String dest) throws Exception {
    PdfReader reader = new PdfReader(SRC);
    PdfWriter writer = new PdfWriter(DEST, new WriterProperties());
    PdfDocument pdfDoc = new PdfDocument(reader, writer);

    String fileCreationDate = "05-18-2018 17:10:10"; //dummy date
    PdfPage page = pdfDoc.getFirstPage();


if(!validatePdfExpiration(fileCreationDate)) {          
    page.setAdditionalAction(PdfName.O,PdfAction.createJavaScript("var nButton = app.alert({cMsg: 'This PDF Document has been Expired.',cTitle: 'GXG Alert',nIcon: 1, nType: 0 });"
        + "var flag=nButton;"
        + "if (flag == 1) app.execMenuItem('Close');"
        + ""
        + ""));
}else{
    //System.out.println("OUTside :"+validateExpiryDate(fileCreationDate)); 
}

pdfDoc.close();
}


public boolean validatePdfExpiration(String fileCreationDate) throws Exception {

    Boolean validFlag=false;

    Calendar fileCreationCalendar = Calendar.getInstance();
    fileCreationCalendar.set(2018, 4, 14, 19, 10,10);  //testing purpose set datecalendar.set(year, month, date, hourOfDay, minute, second);
    fileCreationCalendar.add(Calendar.DAY_OF_YEAR,50);
    Date expiryDate = fileCreationCalendar.getTime();

    Calendar currentCalendar = Calendar.getInstance();
    Date currentDate = currentCalendar.getTime();

     if(currentDate.before(expiryDate) ||currentDate.equals(expiryDate)) {
         System.out.println("This PDF Document has not been Expired. \n TODAY DATE\t:"+currentDate+"\n"+"EXPIRY DATE\t:"+expiryDate);
         validFlag=true;
        }else{
            System.out.println("This PDF Document has been Expired. \n TODAY DATE\t:"+currentDate+"\n"+"EXPIRY DATE\t:"+expiryDate);
             validFlag=false;
        }

     return validFlag;
}
} 

0 个答案:

没有答案