PdfJavaScriptAction

Im using the "Expire" feature of the PdfJavaScriptAction and looking for a way to make the expiration date, dynamic. Currently, the PDF will be downloaded by a user (when the user clicks a download button) and i would like to set an expiration date of say, 15 days from the date of download rather than hard coding it.


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


Thank you for your help,

Shane


29 Replies 1 reply marked as answer

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 


Marked as answer

SS Shane Scholl replied to Gowthamraj Kumar August 5, 2021 12:30 AM UTC

Gowthamraj,

Thank you for your response, this worked wonderfully! 



AA Anitha Azhagesan Syncfusion Team August 16, 2021 04:18 AM UTC

Sent: Sunday, August 15, 2021 2:24 PM
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
 

Gothamraj, 
For some reason, the expire function isn’t working, here is my code inside my button click event. Can you look it over and see if im doing something wrong?  
 
//Load the PDF document 
            PdfLoadedDocument document = new PdfLoadedDocument(dlFile); 
 
            //Insert a new page into the document 
            document.Pages.Insert(0, PdfPageSize.Letter); 
 
            //int docPages = document.Pages.Count; 
 
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 30); 
            DateTime startDate = DateTime.Now.Date; 
            //Change the number in ".AddDays" for however many days the document should stay active 
            DateTime expireDate1 = startDate.AddDays(1); 
 
            //Watermarks the pages 
            foreach (var section in document.Pages) 
            { 
                //Insert Graphic on the new page 
                PdfPageBase loadedPage = section as PdfPageBase; 
                PdfGraphics WMgraphics = loadedPage.Graphics; 
 
                // watermark text. 
                PdfGraphicsState state = WMgraphics.Save(); 
                WMgraphics.SetTransparency(0.25f); 
                WMgraphics.RotateTransform(-55); 
                WMgraphics.DrawString( 
                    "Kokusai Document - Valid " + DateTime.Now.ToShortDateString() + " to " + 
                    expireDate1.AddDays(1).ToShortDateString(), font, PdfPens.Red, PdfBrushes.Red, 
                    new PointF(-500, 460)); 
            } 
 
            //PDF document security  
            PdfSecurity security = document.Security; 
 
            //Specifies encryption key size, algorithm and permission.  
            security.KeySize = PdfEncryptionKeySize.Key256Bit; 
            security.Algorithm = PdfEncryptionAlgorithm.AES; 
 
            //Provide owner and user password. 
            security.OwnerPassword = "test"; 
            //security.UserPassword = "test"; 
 
 
            //Expire function add via Javascript to the document 
            PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("function Expire()" + 
                                                                       "{var currentDate = new Date();var expireDate = new Date(2021,8,14);" + 
                                                                       "if (currentDate > expireDate){app.alert(\"This Document has Expired.\");this.closeDoc();}}            Expire(); "); 
 
 
 
 
            //Add the JavaScript action 
            document.Actions.AfterOpen = scriptAction; 
 
            //End Expire function 
 
            //Save the document. 
            document.Save("Protected - " + myFile, HttpContext.Current.Response, HttpReadType.Save); 
 
            //Close the document. 
            document.Close(true); 
 
Regards,  
Shane Scholl 



GK Gowthamraj Kumar Syncfusion Team August 17, 2021 01:15 PM UTC

Hi Shane, 
 
Thank you for your update.

We have checked the reported issue with provided issue on our end, the existing script does not work properly. We have modified the expire script code based on the current time, now its working fine. We have attached the JavaScript code in text file below,
 
 
string text = File.ReadAllText("../../JS.txt"); 
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction(text); 
 
Note: Read all text the from text file and assign it to JavaScript Action. If we directly place this code on the Visual Studio, it may occurs some issues on the input string. 
 
Please let us know if you need any further assistance with this. 

Regards,
 
Gowthamraj K 



AA Anitha Azhagesan Syncfusion Team August 18, 2021 05:07 AM UTC

Sent: Tuesday, August 17, 2021 11:41 PM
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
 

Gowthamraj, 
I get an error when trying to read the file. Is there something I can do to modify the inline script? Can I call a separate js file with the PdfJavaScriptAction? 
 
Regards,  
Shane Scholl 



SS Shane Scholl replied to Anitha Azhagesan August 21, 2021 10:32 PM UTC

Gowthamraj,

I think i know the issue. Adding javascript to "Document-Action" requires an action to perform inorder for the script to start (i.e Print or Save) and since i disable the ability to Print or Save, the script is never running. If there is a way to add the script to the "Document-Level" the script will run when it opens. 


Is there a way, using Syncfusion, to add javascript to the "Document Level"?





GK Gowthamraj Kumar Syncfusion Team August 23, 2021 12:30 PM UTC

Hi Shane, 
 
Thank you for your update.

No. It is not possible to call the separate js file for PdfJavaScriptAction on our end. We can only add the JavaScript code using PdfJavaScriptAction class to a PDF document action property.
 
 
//Action in a pdf document. 
loadedDocument.Actions.AfterOpen= new PdfJavaScriptAction("Script")"); 
 
 
Regards, 
Gowthamraj K 



SS Shane Scholl replied to Gowthamraj Kumar August 23, 2021 02:28 PM UTC

Gowthamraj,

What about accessing the "Document-Level" rather than "Document-Action"? Document-Level runs a script as soon as the document is open where as Document-Action, requires an action to take place to start the script. 





GK Gowthamraj Kumar Syncfusion Team August 24, 2021 12:29 PM UTC

Hi Shane, 
 
Thank you for your update. 
 
We do not have support for adding the JavaScript expire function directly in document-level using PDF library. As of now, we can only add the JavaScript code using PdfJavaScriptAction class to a PDF document action property. We request you to share the details about which tool are you used for document level script to start in your previous screenshot. 
 
Regards, 
Gowthamraj K 



AA Anitha Azhagesan Syncfusion Team August 25, 2021 04:40 AM UTC

Sent: Tuesday, August 24, 2021 10:31 AM
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
 

Gowthamraj, 
When I open the PDF manually and add javascript to the file, it places the JS in the “Document-Level” automatically for me.  
 
 
 
The issue with the code I have, is the “document.Action.AfterOpen=scriptAction;” only runs on the webserver, not on the PDF itself whenever its open.  
 
 
 
Regards,  
Shane Scholl 



GK Gowthamraj Kumar Syncfusion Team August 26, 2021 03:10 PM UTC

Hi Shane, 
 
Thank you for sharing the details.

 
Currently, we are analysing the requirement on our end and we will update the further details on August 27th 2021.

 
Regards, 
Gowthamraj K 



SS Shane Scholl August 26, 2021 03:11 PM UTC

Thank you for keeping me updated. I look forward to your reply. 


Regards,

Shane



GK Gowthamraj Kumar Syncfusion Team August 27, 2021 01:47 PM UTC

Hi Shane, 

Thank you for your patience. 

On our further analysis , currently we do not have support to add a document level JavaScript in the pdf document. We have logged  this requirement as feature request. And we do not have any immediate plans to implement these features and we will implement this support in any of our upcoming releases. We usually have an interval of at least three months between releases, at the planning stage for every release cycle, we review all open features. We will let you know when this feature is implemented. 

The status of the feature can be tracked through our Feature Management System,    

Regards, 
Gowthamraj K 



SS Shane Scholl August 27, 2021 02:26 PM UTC

Gowthamraj,

Is there another method I can use to make the JS in the PDF, run? Currently, there is no action to cause the JS to run. 



GK Gowthamraj Kumar Syncfusion Team August 30, 2021 12:58 PM UTC

Hi Shane, 
 
As we said earlier in the forum, we can run the javascript action once the document is opened. 
 
//Action in a pdf document.  
loadedDocument.Actions.AfterOpen= new PdfJavaScriptAction("Script")"); 
 
You can also set the java script action the button field in the PDF document. 
 
We do not have any method to run the javascript in the PDF document. 
 
Regards, 
Gowthamraj K 



SS Shane Scholl August 30, 2021 02:27 PM UTC

Gowthamraj,

Do you suggest, placing this inside the pdf itself? If not, this will never be called when the pdf is opened. 


//Action in a pdf document.  
loadedDocument.Actions.AfterOpen= new PdfJavaScriptAction("Script")"); 


AP Anand Panchamoorthi Syncfusion Team August 31, 2021 10:16 AM UTC

 Hi Shane, 

Yes. We have to specify that when this particular action gets triggered. Also we have below options for PDF document actions. 

AfterOpen - PdfAction execute when the document is opened. 
AfterPrint - PdfAction to be performed after the document is printed. 
AfterSave - The java script action to be performed after the document is saved. 
BeforeClose - The java script action to be performed before the document is closed. 
BeforePrint - The PdfAction to be performed before the document is printed. 
BeforeSave - The java script action to be performed before the document is saved. 
  
With Regards, 
Anand Panchamoorthi


SS Shane Scholl September 1, 2021 04:21 PM UTC

Anand,

Im asking the questions because right now, when the user downloads the pdf, the C# code can do the check, but AFTER the pdf document is downloaded on the users computer, the pdf doesn't seem to be checking, the "afterOpen" doesnt seem to trigger the javascript in the Document.Actions; the pdf has a "Document.Load" section that can look at the javascript as soon as the pdf is opened. 



GK Gowthamraj Kumar Syncfusion Team September 2, 2021 02:11 PM UTC

Hi Shane, 
 
Thank you for your update.

 
When we downloaded the PDF document with JavaScript action from web application, PdfAction execute when the document is opened. We have ensured with simple script action, it is working properly. Please find the below sample and screenshot. 
 
Please find the below screenshot, 
 
 
Please let us know , whether your requirement is want to perform the action when document is opened?  
 
As we said earlier, we do not have support to add a document level JavaScript in the pdf document. And we do not have any immediate plans to implement these features and we will implement this support in any of our upcoming releases. We will let you know when this feature is implemented.  
 
The status of the feature can be tracked through our Feature Management System,     
 
Note: We do not have any alternative method to run the JavaScript in the PDF document. 
 
Regards, 
Gowthamraj K 



SS Shane Scholl September 8, 2021 05:17 AM UTC

Gowthamraj,

I still continue to have issues with the pdf's running the action script after the script has been added to the pdf. I can test the script and confirm it to run, immediately after download (by changing the dates) but if i download my test file and try to open it a few days after download, the Javascript doesnt seem to run. Do you have any suggestions for this? Is there an issue when trying to compare 

"new Date()" with 

"expireDate = new Date(" + expireDate1.Year.ToString() + ", " + expireDate1.Month.ToString() + ", " + expireDate1.Day.ToString()"


Do i need to compare the value of the date's rather? 

Does "Date()" return time as well as the mm/dd/yyyy? If so, do i need to add or reset the time in "expireDate"?


Thank you for all of your help.

Shane



GK Gowthamraj Kumar Syncfusion Team September 8, 2021 01:56 PM UTC

Hi Shane, 
 
Thank you for your update.

We have requested you to try the below
modified expire script code based on the current time, instead of checking with current date. We have attached the JavaScript code in text file below,   
  
string text = File.ReadAllText("../../JS.txt");  
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction(text);  
  
Note: Read all text the from text file and assign it to JavaScript Action.  
  
 
Please try the above suggestion and let us know if you need any further assistance with this.

 
Regards,  
Gowthamraj K 



AA Anitha Azhagesan Syncfusion Team September 9, 2021 04:53 AM UTC

Sent: Wednesday, September 8, 2021 12:13 PM
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
 

Gowthamraj, 
I have tried this method but was unsuccessful, nothing was added to the pdf file. 
 
Regards,  
Shane Scholl 



GK Gowthamraj Kumar Syncfusion Team September 9, 2021 02:23 PM UTC

Hi Shane, 

Thank you for your update.

Could you please share the generated PDF document with simple sample to replicate the issue on our end. This will be helpful to investigate further and provide the solution at the earliest.    

Regards, 
Gowthamraj K 



RP Ranjani Prabakaran Syncfusion Team September 10, 2021 04:16 AM UTC

From: Scholl, Shane <[email protected]>
Sent: Friday, September 10, 2021 4:23 AM
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
 

Gowthamraj, 
I think I have figured out my issue. Im passing in a date of, say, September 10th, 2021 (9/1/2021) and asking JS to compare and throw an alert if expire date is greater than current date, but since JS dates are 0-11 (o=Jan – 11=Dec) I have to wait 30+/- days before it will deem the expire date past the current date. To test this theory I’ve used the following: 
                                DateTime expireDate1 = startDate.AddDays(expireDays); 
              DateTime expireDate2 = expireDate1.AddMonths(-1); 
 
And so far, it seems to work properly. Do you have any suggestion as to how I can get JS dates to run 1-12 (1=Jan – 12=Dec)? 
 
I do have another question for you: I want to add layers to my document and have JS turn the layers off. So far, I’ve been able to add a layer to one page of the document, but cannot seem to add the layer to  ALL pages of the document, here is what I have so far: 
                                 
                                //Load the PDF document 
       PdfLoadedDocument document = new PdfLoadedDocument(newFile); 
 
//Testing a layer 
              PdfLoadedPage layerPage = document.Pages[0] as PdfLoadedPage; 
              PdfPageLayer layer = layerPage.Layers.Add("Layer1",true); 
              PdfGraphics layerGraphics = layer.Graphics; 
              layerGraphics.TranslateTransform(100,60); 
 
              PdfPen pen = new PdfPen(System.Drawing.Color.Gray, 1000); 
              RectangleF bounds = new RectangleF(-50, 0, 550, 700); 
              layerGraphics.DrawRectangle(pen,bounds); 
 
 
Regards,  
Shane Scholl 



GK Gowthamraj Kumar Syncfusion Team September 10, 2021 12:03 PM UTC

Hi Shane, 
 
Thank you for your update. We are glad to know that your problem has been solved. 
 
And so far, it seems to work properly. Do you have any suggestion as to how I can get JS dates to run 1-12 (1=Jan – 12=Dec)?  
Currently, we are checking on this requirement on our end and we will update the further details September 14th 2021. 
I do have another question for you: I want to add layers to my document and have JS turn the layers off. So far, I’ve been able to add a layer to one page of the document, but cannot seem to add the layer to  ALL pages of the document, here is what I have so far: 
We can add layers to all the pages from the existing PDF document. We have to iterate all the page from the document and we can add the layers. We have attached the modified code snippet and output document for your reference. Please try the below code on your end and let us know the result. 
 
//Load the PDF document  
PdfLoadedDocument document = new PdfLoadedDocument(finalDocumentPath); 
 
for (int i = 0;i < document.Pages.Count; i++) 
{ 
    //Testing a layer  
    PdfLoadedPage layerPage = document.Pages[i] as PdfLoadedPage; 
    PdfPageLayer layer = layerPage.Layers.Add("Layer1", true); 
    PdfGraphics layerGraphics = layer.Graphics; 
    layerGraphics.TranslateTransform(100, 60); 
 
    PdfPen pen = new PdfPen(System.Drawing.Color.Gray, 1000); 
    RectangleF bounds = new RectangleF(-50, 0, 550, 700); 
    layerGraphics.DrawRectangle(pen, bounds); 
} 
MemoryStream ms = new MemoryStream(); 
 
document.Save(ms); 
 
 
 
 
Regards, 
Gowthamraj K 



AA Anitha Azhagesan Syncfusion Team September 13, 2021 05:04 AM UTC

Sent: Friday, September 10, 2021 7:42 PM
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
 

Gowthamraj, 
Thank you again for the great support!  
 
When adding layers, I noticed I cannot use the layer.Locked=true with a PdfPageLayer, is there another way to lock a layer with PdfPageLayer or do I need to convert everything to PdfLayer? Also, whats the difference with the two (PdfPageLayer and PdfLayer)? 
 
If I have to convert to PdfLayer, do I need to change anything else with my below code? 
 
for (int i = 1; i < document.Pages.Count; i++) 
        { 
            //Testing a layer 
            PdfLoadedPage layerPage = document.Pages[i] as PdfLoadedPage; 
            PdfPageLayer layer = layerPage.Layers.Add("Layer1", true); 
            PdfPageLayer layerText = layerPage.Layers.Add("Layer2", true); 
             //layer.Locked = true; 
            PdfGraphics layerGraphics = layer.Graphics; 
            layerGraphics.TranslateTransform(100, 60); 
            PdfPen pen = new PdfPen(System.Drawing.Color.WhiteSmoke, 1000); 
            RectangleF bounds = new RectangleF(-50, 0, 550, 700); 
              //layerGraphics.DrawEllipse(pen, bounds); 
            layerGraphics.DrawRectangle(pen, bounds); 
 
            PdfGraphics layerTextGraphics = layerText.Graphics; 
            PdfFont layerFont = new PdfStandardFont(PdfFontFamily.Courier, 10, PdfFontStyle.Italic); 
            layerTextGraphics.DrawString( 
                "Please enable Javascript or use a device that", layerFont, PdfPens.Red, 
                new PointF(100, 30)); 
            layerTextGraphics.DrawString( 
                "has Javascript enabled for pdf viewing", layerFont, PdfPens.Red, 
                new PointF(100, 45)); 
        } 
 
Regards,  
Shane Scholl 
 



GK Gowthamraj Kumar Syncfusion Team September 13, 2021 12:29 PM UTC

Hi Shane, 

Thank you for your update.

 
Both the PdfLayers and PdfPageLayers are similar, its refers to sections of content in a PDF document that can be selectively viewed or hidden by document authors or consumers. We can add the layer and lock the layers by using PdfLayer class. Please refer the below code snippet,

 
//Load the PDF document   
            PdfLoadedDocument document = new PdfLoadedDocument("E://TableFeatures.pdf"); 
 
            for (int i = 0; i < document.Pages.Count; i++) 
            { 
                //Testing a layer  
                PdfLoadedPage layerPage = document.Pages[i] as PdfLoadedPage; 
                PdfLayer layer = document.Layers.Add("Layer1"); 
                //Create graphics for layer 
                PdfGraphics layerGraphics = layer.CreateGraphics(layerPage); 
                layer.Locked = true; 
            
                //Add the layer 
                PdfLayer layer1 = document.Layers.Add("Layer2"); 
                //Create graphics for layer 
                PdfGraphics layerTextGraphics = layer1.CreateGraphics(layerPage); 
                //Set a lock state   
                layer1.Locked = true; 
                
                layerGraphics.TranslateTransform(100, 60); 
                PdfPen pen = new PdfPen(System.Drawing.Color.WhiteSmoke, 1000); 
                RectangleF bounds = new RectangleF(-50, 0, 550, 700); 
                //layerGraphics.DrawEllipse(pen, bounds);  
                layerGraphics.DrawRectangle(pen, bounds); 
 
             
                PdfFont layerFont = new PdfStandardFont(PdfFontFamily.Courier, 10, PdfFontStyle.Italic); 
                layerTextGraphics.DrawString( 
                    "Please enable Javascript or use a device that", layerFont, PdfPens.Red, 
                    new PointF(100, 30)); 
                layerTextGraphics.DrawString( 
                    "has Javascript enabled for pdf viewing", layerFont, PdfPens.Red, 
                    new PointF(100, 45)); 
            } 
            MemoryStream ms = new MemoryStream(); 
 
            document.Save(ms); 
            File.WriteAllBytes("E://Layers.pdf", ms.ToArray()); 

Note: By using PdfLayer, we can link/add annotation to layers in a PDF document. This is the only difference. 

Regards, 
Gowthamraj K 



AA Anitha Azhagesan Syncfusion Team September 14, 2021 05:05 AM UTC

Sent: Tuesday, September 14, 2021 12:27 AM
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
 

Gowthamraj, 
Thank you for all of your help. 



GK Gowthamraj Kumar Syncfusion Team September 14, 2021 02:55 PM UTC

Hi Shane, 
 
Thank you for your update.

On our further analysis, we get the month format (as January is 1) by using below JS code snippet. Please try the below code snippet on your end and let us know the result.
 
 
var t = new Date(); 
var date = ('0' + t.getDate()).slice(-2); 
var month = ('0' + (t.getMonth() + 1)).slice(-2); 
var year = t.getFullYear(); 
 
var time = `${date}/${month}/${year}`; 
console.log(time); 
 
 
Regards, 
Gowthamraj K 


Loader.
Up arrow icon