PDF Expiry Not working if Date is in Current Month

Hi Team,

we are trying to set the Expiration to PDF File. Criteria for expiration is 10 Days.
we want the PDF to be expired after 10 days of creation. we are using the below script to test the behavior by setting expiry date before 10 days but functionality is not working as expected.

//Expiration Logic
Document doc = new Document("D:\\PDF1.pdf");
LocalDate date1=LocalDate.now().minusDays(10);
int year = date1.getYear();
int mon=date1.getMonthValue();
int dated=date1.getDayOfMonth();


JavascriptAction javaScript = new JavascriptAction(

"var year="+year+";"

+"var month="+mon+";"

+ "var date="+dated+";"

+ "today = new Date(); today = new Date(today.getFullYear(), today.getMonth(),today.getDate());"

+ "expiry = new Date(year,month,date);"

+ "if (today.getTime() > expiry.getTime())"

+ "app.alert('RELEASE:This electronic copy has expired.');");

doc.setOpenAction(javaScript);

doc.save("D:\\PDFExpiry.pdf");

Thank You
Karthik

@karthik13

Please try using the below script in JavaScriptAction and let us know in case you still face any issue:

JavascriptAction javaScript = new JavascriptAction(

                "// Get the current date and time\n" +
                        "var rightNow = new Date();\n" +
                        "// Set End Date to 11/28/16 for instance\n" +
                        "var endDate = new Date(2016, 11, 28);\n" +
                        "if(rightNow.getTime() > endDate)\n" +
                        "{\n" +
                        "app.alert(\"This Document has Expired.\");\n" +
                        "this.closeDoc();\n" +
                        "}");

Hi Asad,

Thanks for your reply. I have tried the above code and it works.
Once I change the date to current month, expiration is not working. Please suggest.
“var endDate = new Date(2021, 04, 01);\n”

Thank You
Karthik

@karthik13

It seems like the Adobe Reader is incrementing in the month value while executing the JavaScript. It is interpreting 04 as of May instead of April. So, the following script gave the correct output and you could use it at the moment.

JavascriptAction javaScript = new JavascriptAction(

                "// Get the current date and time\n" +
                        "var rightNow = new Date();\n" +
                        "// Set End Date to 2021/04/01 for instance\n" +
                        "var endDate = new Date(2021, 03, 01);\n" +
                        "if(rightNow > endDate)\n" +
                        "{\n" +
                        "app.alert(\"This Document has Expired.\");\n" +
                        "this.closeDoc();\n" +
                        "}");

You can further test using below script to see the behavior yourself:

JavascriptAction javaScript = new JavascriptAction(

                "// Get the current date and time\n" +
                        "var rightNow = new Date();\n" +
                        "// Set End Date to 11/28/16 for instance\n" +
                        "var endDate = new Date(2021, 04, 01);\n" +
                        "app.alert(endDate);\n" +
                        "app.alert(rightNow);\n" +
                        "if(rightNow > endDate)\n" +
                        "{\n" +
                        "app.alert(\"This Document has Expired.\");\n" +
                        "this.closeDoc();\n" +
                        "}");

We have logged an investigation ticket as PDFJAVA-40390 as well in our issue tracking system to further investigate the issue and let you know once it is resolved.

We are sorry for the inconvenience.

@karthik13

We would like to share with you that we have investigated the earlier logged ticket and found that in Javascript constructor of the Date class with parameter, month is 0-based. e.g., 0 for January. Also, the date getMonth() method returns the month in the specified date according to local time. The value returned by getMonth() is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

So, according to Javascript engine: 04 is not an April but May. Also, notice that if you use another Date constructor with the text value of date - the months can be parsed from 1 to 12:

var endDate = new Date('2021-04-01'); instead of

var endDate = new Date(2021, 04, 01);

Also, please take into account, that even different Java classes have a different base number for months:

java.time.LocalDate#getMonthValue Returns the month-of-year, from 1 to 12 but, another JVM class java.util.GregorianCalendar has a month parameter, where the Month value is 0-based. e.g., 0 for January.