We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Expired PDF can be opened in Browsers

Hi Team,

we are trying to set expiration to PDF File and do not want the file contents to be read by the users using any Other Application.

Below is the code used for setting the expiration. File gets closed in Adobe Reader but can be accessed normally via Chrome/Edge etc.

Is there a possible way to avoid users from reading the file contents.(Making PDF Blank, close file etc) using any application.

Code:

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”);

Thanks
Karthik

@karthik13

A similar issue has already been logged in our issue tracking system under the ticket ID PDFJAVA-40539. We have linked it with this forum thread so that you will be able to receive a notification as soon as it is resolved. Please be patient and spare us some time.

We are sorry for the inconvenience.

@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"); 

Hi Asad,

This Solution is not feasible as we want the users to not see the contents of document after specific period of time. if we add password, it would ask the password each time user opens the document. Please suggest.

Thanks,
Karthik

@karthik13

We are afraid that we cannot offer much in this case. As we already have shared all limitations with you, we regret that no solution would be possible for your case. In case you need more information or face any issues related to other functionalities of the API, please feel free to let us know.