Date Expiration on Existing Document

Hi !

I was trying to implement a Javascript to get "date expiration" into an existing PDF document.

It seems the PDFDocument() class is different of PDF.Parsing.PdfLoadedDocument() because the first accepts the "Action" function and the second does not.

Since the loaded document may contain signatures and even password/permissions, how could I set also an expiration there?

In another approach, could I open a new document to set the Javascript code and copy all pages from the original loaded document to there, without lossing its permissions and data?

I appreciate any help on it.

13 Replies 1 reply marked as answer

SL Sowmiya Loganathan Syncfusion Team June 22, 2020 01:16 PM UTC

Hi David,  
 
Thank you for contacting Syncfusion support.  
 
We have analyzed your requirement. At present we do not have support to add actions in an existing PDF document. Currently we are validating on this and will update the further details on June 24, 2020.  
 
Regards, 
Sowmiya Loganathan      



DA DavidBS June 22, 2020 04:21 PM UTC

Hi Sowmiya,

Thank you for attention and answer.
I'll be looking forward for this.

Kindest regards and hugs!





SL Sowmiya Loganathan Syncfusion Team June 24, 2020 02:04 PM UTC

Hi David,  
 
As we said earlier, at present we do not have support for “Adding actions to an existing PDF document” and already we have logged a feature request for this. And we do not have any immediate plan to implement this feature. We will implement this feature in any of our upcoming releases. We usually have an interval of at least three months between releases, at the planning stage of every release cycle, will review all the open features and we will notify you once this support is included. You can track the status of the implementation in the below feedback.  
 
However we can do this with a workaround of importing the pages from the loaded document and adding JavaScript action to the pages and saving the document.  
please try the below code and let us know whether it is fulfil your need.  
  
// Loads PDF document  
PdfLoadedDocument lDoc = new PdfLoadedDocument("file.pdf");  
  
//Creates a new document  
PdfDocument document = new PdfDocument();  
  
//Imports the pages from the lDoc  
document.ImportPageRange(lDoc, 0, lDoc.Pages.Count - 1);  
PdfJavaScriptAction javaAction = new PdfJavaScriptAction("app.alert(\"Hello World !!! \")");  
document.Actions.AfterOpen = javaAction;  
  
//Saves the document  
document.Save("sample.pdf");  
  
//Closes the documents  
document.Close(true);  
lDoc.Close(true);  
  
Note: We can’t able to achieve this requirement without losing its permissions and password using this workaround. 
 
Regards, 
Sowmiya Loganathan 


Marked as answer

DA DavidBS June 24, 2020 03:59 PM UTC

Hi Sowmiya!

Thank you very much for the entire explanation and I'll try what you're recommending as a "tip".
I'll be back soon with my result.

Kindest regards and hugs!!


DA DavidBS June 25, 2020 03:09 AM UTC

Hmmm.... you code functioned like a charm.

However, using a code a little bit more complex than a single "app.alert" seems to be inefective.

The code is:

dim MyCode as string = "Function CheckExpiration(LastYear, LastMonth, LastDate, LastHour, LastMin, LastSec, LastMS) " &
"{ " &
"var myDate = new Date(Number(LastYear), Number(LastMonth - 1), Number(LastDate), Number(LastHour), Number(LastMin), Number(LastSec), Number(LastMS)).valueOf();" & vbCrLf &
"var Today = New Date().valueOf(); " & vbCrLf &
"return (myDate < Today); " &
"} " &

"if (CheckExpiration(2020, 6, 19, 0, 0 )) Then" &
"{" &
"app.alert(" & Chr(34) & "This files has expired at 19/06/2020" & Chr(34) & ", 1, 0, " & Chr(34) & "Expired" & Chr(34) & ");" &
"this.closeDoc(1);" &
"}" 


No error at all, but the document opens without any warning...
:(




SL Sowmiya Loganathan Syncfusion Team June 25, 2020 03:03 PM UTC

Hi David,    
   
We have analyzed your requirement. Could you please provide us the below details, it will helpful for us to provide the precise solution on this,    
   
  • Provide the content which needs to shows in App.Alert
  • Expected output or image
  • More details about your requirement  
   
Please let us know if you have any concerns about this.    
 
Regards, 
Sowmiya Loganathan 



DA DavidBS June 25, 2020 05:15 PM UTC

Hi Sowmiya,

I appreciate you efforts and, in reponse to you question, follow the details:

1- I want to put a "date expiration" into PDF.

2- It can be done easily following that sample code:

Function CheckExpiration(LastYear, LastMonth, LastDate, LastHour, LastMin, LastSec, LastMS) " &
     {
var myDate = new Date(Number(LastYear), Number(LastMonth - 1), Number(LastDate), Number(LastHour), Number(LastMin), Number(LastSec), Number(LastMS)).valueOf();
var Today = New Date().valueOf();
return (myDate < Today); 
    }


if (CheckExpiration(2020, 6, 19, 0, 0 )) 

     {
app.alert(" & Chr(34) & "This files has expired at 19/06/2020" & Chr(34) & ", 1, 0, " & Chr(34) & "Expired" & Chr(34) & ");" &
this.closeDoc(1);
    }

3- So, the "Open" event must call the code above, which call the function CheckExpiration...

The result is a MessageBox (or like this) before close de doc.

Thank you again for any help





AP Anand Panchamoorthi Syncfusion Team June 26, 2020 12:12 PM UTC

Hi David, 

Thanks for your appreciation and the detailed explanation of your requirement. 

We can add JavaScript action to the PDF document to validate date as you expected by using the below code snippet. 
//Javascript action to validate date and throw alert message. 
PdfJavaScriptAction javaAction = new PdfJavaScriptAction("var myDate = new Date(2020, 05, 19); var today = Date.now(); if (myDate < today) { app.alert(\"This files has expired at 19/06/2020.\"); }"); 
 
//Add action to execute the script while opening the PDF document 
document.Actions.AfterOpen = javaAction; 

Please try the above sample code at your end and let us know if you need any further assistance. 

With Regards, 
Anand Panchamoorthi 



DA DavidBS June 26, 2020 02:52 PM UTC

Thank you very much Anand !
It worked.

Have a good weekend all of you.



SS Shane Scholl August 3, 2021 11:12 PM UTC

In regards to the PdfJavaScriptAction, is there a way to have the expiration date (expireDate), dynamic, say 30 days from the day the pdf was downloaded? 


PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("function Expire()" +

                                                                       "{var currentDate = new Date();" +

                                                                       "var expireDate = new Date(2018, 9, 30);" +    

                                                                       "if (currentDate > expireDate)" +

                                                                       "{app.alert(\"This Document has Expired.\");" +

                                                                       "this.closeDoc();}" +

                                                                       "} " +

                                                                       " Expire(); ");



GK Gowthamraj Kumar Syncfusion Team August 4, 2021 02:33 PM UTC

Hi Shane, 
 
Thank you for your update. 
 
We can set the expiry date as limited days by using System DateTime AddDays method. Using this, we can add the 15 days from the current date. Please refer the below code snippet to set the expiry date to PDF document. 
 
DateTime startDate = DateTime.Now.Date; 
DateTime expiryDate1 = startDate.AddDays(15); 
 
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("function Expire() 
{ 
   var currentDate = new Date(); 
   var expireDate = new Date("+ expiryDate1.Year.ToString() + "," + expiryDate1.Month.ToString() + "," + expiryDate1.Day.ToString() + "); 
   if (currentDate > expireDate) 
      { 
        app.alert(\"This Document has Expired.\"); 
        this.closeDoc(); 
      } 
} 
Expire();  
"); 
 
 
Please let us know if you need any further assistance with this. 
 
Regards, 
Gowthamraj K 



DA DavidBS replied to Gowthamraj Kumar August 5, 2021 10:57 AM UTC

It's great Gowthamraj!

Is there any way to avoid bypass this method by disabling Javascript?

Or, in other hand, to overwrite the file to avoid any other bypass?

Thanks a lot.



GK Gowthamraj Kumar Syncfusion Team August 6, 2021 01:04 PM UTC

Hi DavidBS, 
 
Thank you for your update.

 
No. It is not possible to avoid bypass this method by disabling JavaScript. Please let us know if you need any further assistance with this. 
 
Regards, 
Gowthamraj K 


Loader.
Up arrow icon