Set an Expiration Date for PDF Document in Java

This article demonstrates how to set an expiration date for a PDF document using Spire.PDF for Java.

import com.spire.pdf.actions.PdfJavaScriptAction;

public class ExpiryDate {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Set expiration date and warning information,and close the document through JavaScript
        String javaScript = "var rightNow = new Date();"
                + "var endDate = new Date('June 20, 2020 23:59:59');"
                + "if(rightNow.getTime() > endDate)"
                + "app.alert('This document is no longer valid, please contact us for a updated one.',1);"
                + "this.closeDoc();";

        //Create a PdfJavaScriptAction object based on the javascript
        PdfJavaScriptAction js = new PdfJavaScriptAction(javaScript);

        //Set PdfJavaScriptAction as the AfterOpenAction
        doc.setAfterOpenAction(js);

        //Save to file
        doc.saveToFile("ExpirationDate.pdf", FileFormat.PDF);
    }
}

Set an Expiration Date for PDF Document in Java