Expired PDF can be opened

Hi Team,

we are trying to set expiry to pdf file. Once we open expired pdf file, we are receiving pop up that pdf is expired but data of the file can be still seen.

//Expiration Logic
Document doc = new Document(“D:\PDF1.pdf”);
LocalDate date1=LocalDate.now().plusDays(1);
int year = date1.getYear();
int mon=date1.getMonthValue()-1;
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”);

Please suggest.

Thank You

@karthik13

Please use the below code snippet in order to set expiry date and close the PDF after the PopUp is shown:

Document doc = new Document();
doc.getPages().add();
java.time.LocalDate date1=java.time.LocalDate.now().minusDays(10);
int year = date1.getYear();
int mon=date1.getMonthValue();
int dated=date1.getDayOfMonth();


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" +
                        "}");

doc.setOpenAction(javaScript);

doc.save(dataDir + "PDFExpiry.pdf");

Hi Team,

Thanks for the update.
By incorporating the above change, PDF gets closed in Adobe Reader but it can be still opened using Chrome/Edge.

Please suggest.

Thank You
Karthik

@karthik13

This cannot be considered as a Bug in API as Aspose.PDF follows the standard of Adobe Acrobat Reader. The Chrome or Edge Viewer has its own settings to display the PDFs and process the JavaScript in these documents. However, we will try to investigate the scenario as well and let you know about our feedback. Please share the sample PDF generated at your end so that we can further proceed accordingly.

ExpirationOutput1.pdf (181.7 KB)

Hi Team,

Please find the sample file.

Thank You

@karthik13

We have logged an investigation ticket as PDFJAVA-40539 in our issue tracking system. We will look into its details and let you know as soon as it is resolved. Please be patient and spare us some time.

Hi Team,

Could you please let us know if there is any ETA for the Ticket Resolution.
Also do let us know if there is any alternate way to make the PDF non accessible.

Thank You

@karthik13

We are afraid that we cannot share any ETA as the ticket is logged under free support model and will be resolved on a first come first serve basis. Furthermore, the API mimics behavior of Adobe Reader and PDF is working fine in it. We need to perform additional investigation about why Chrome is unable to execute the JavaScript. As soon as the ticket is investigated, we will share our feedback with you in this forum thread. Please spare us some time.

We are sorry for the inconvenience.

Hi Team,

Could you please let us know if there is any update.

Thanks
Karthik

@karthik13

We are afraid that earlier logged ticket has not been yet investigated due to other issues logged prior to it. We will surely inform you as soon as we have some definite update regarding its resolution.

We apologize for your inconvenience.

Hi Team,

Please let us know if there is any update. This Issue is very critical for us and could be blocker for our Go Live.

Thank You
Karthik

@karthik13

The ticket is currently under the phase of the investigation. We have recorded your concerns and will surely consider them during anlaysis. You will be informed as soon as we have certain news about ticket resolution. We highly appreciate your patience in this matter.

@karthik13

We have investigated the earlier logged ticket. There is a reason why .closeDoc(); or window.close(); do not work - these methods are designed to close the standalone version of Adobe Reader/Acrobat.

In a browser you use Adobe Reader as a plug-in, Chrome’s PDF viewer, pdf.js in Firefox, or any other PDF viewer.

And you need to close the browser window from a PDF document. PDFs don’t have the power to control your browser. That could be a serious security issue.

closeDoc method does not work in web browsers due to security limitations. It is not feasible to control privileges using JavaScript.

The browser does not allow this behavior. Javascript can only close a tab that it opened. It is guaranteed that the closing of tabs will not be tolerated in any future browsers.

But you can protect his documents using password:

Document doc = new Document();
doc.getPages().add();
PdfFileSecurity security = new PdfFileSecurity(doc);
security.encryptFile("pass1", "pass2", DocumentPrivilege.getAllowAll(), KeySize.x40);
security.save(dataDir + "PDFExpiry_21_8__Encripted.pdf");