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
|
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();
");
|
Gowthamraj,
Thank you for your response, this worked wonderfully!
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
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); |
Regards,
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
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"?
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")");
|
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.
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
Thank you for keeping me updated. I look forward to your reply.
Regards,
Shane
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.
|
//Action in a pdf document.
loadedDocument.Actions.AfterOpen= new PdfJavaScriptAction("Script")"); |
Gowthamraj,
Do you suggest, placing this inside the pdf itself? If not, this will never be called when the pdf is opened.
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.
|
|
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
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); |
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
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.
Sent: Friday, September 10, 2021 4:23 AM
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
|
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.
Please refer the below link for more information,
UG: https://help.syncfusion.com/file-formats/pdf/working-with-layers KB: https://www.syncfusion.com/kb/9486/how-to-add-layers-in-a-pdf-using-c-and-vb-net |
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
|
//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()); |
To: Syncfusion Support <[email protected]>
Subject: RE: Syncfusion Forum [167829] has a new reply - PdfJavaScriptAction
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); |
- 29 Replies
- 5 Participants
- Marked answer
-
SS Shane Scholl
- Aug 4, 2021 12:12 AM UTC
- Sep 14, 2021 02:55 PM UTC