You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### **Adding/Removing JavaScript to Document Level**
99
+
### Adding/Removing JavaScript to Document Level
100
100
101
101
A new property named JavaScript is added in Document class which has JavaScript collection type and provides access to JavaScript scenarios by its key. This property is used to add Document level JavaScript. The JavaScript collection has the following properties and methods:
102
102
@@ -106,6 +106,75 @@ A new property named JavaScript is added in Document class which has JavaScript
### Setting Expiry Date of a PDF Document Using JavaScript Actions
110
+
111
+
Aspose.PDF allows you to set an expiry date for a PDF document by embedding JavaScript Actions. This functionality ensures the PDF becomes inaccessible after a specified date and time, enhancing document security and control. By leveraging JavaScript Actions, you can define precise expiration conditions down to the second, ensuring the document's accessibility is tightly regulated.
112
+
113
+
**You can achieve this by following these steps**
114
+
115
+
1.**Initialize Document:** Create a new PDF document and add a blank page or open an existing PDF document.
116
+
2.**Define Expiry Date and Time:** Set the date and time after which the document will expire.
117
+
3.**Prepare JavaScript Code:**
118
+
- Retrieve the current date and time.
119
+
- Define the exact expiry date and time, considering that months are zero-based in JavaScript.
120
+
- Compare the current date and time with the expiry date and time.
121
+
- If the current date and time exceed the expiry date and time, display an alert and close the document.
122
+
4.**Set Open Action:** Associate the JavaScript action with the document's open action.
123
+
5.**Save Document:** Save the PDF with the embedded JavaScript that enforces the expiry condition.
124
+
125
+
Below are code snippets demonstrating this functionality in both C# (.NET) and Java.
126
+
127
+
The following C# code snippet demonstrates how to set an expiry date and time for a PDF document using JavaScript Actions with Aspose.PDF:
128
+
129
+
```csharp
130
+
privatestaticvoidCreateDocumentWithExpiryDate()
131
+
{
132
+
// Initialize a new PDF document
133
+
using (vardocument=newAspose.Pdf.Document())
134
+
{
135
+
document.Pages.Add();
136
+
137
+
// Define the expiry date and time (e.g., April 1, 2024, 12:00:00 PM)
// Set the JavaScript action to execute when the document is opened
163
+
document.OpenAction=javaScript;
164
+
165
+
// Save the updated PDF document
166
+
stringoutputPath="C:\\PDFExpiry.pdf";
167
+
document.Save(outputPath);
168
+
}
169
+
}
170
+
```
171
+
172
+
-**JavaScript Date Object:** In JavaScript, the month index starts at `0` for January and ends at `11` for December. Ensure that the month value is adjusted accordingly when setting the expiry date and time.
173
+
174
+
-**Security Considerations:** While JavaScript actions can control the behavior of a PDF document, they rely on the PDF viewer's support for JavaScript. Not all PDF viewers may honor these scripts, and users might have JavaScript execution disabled for security reasons.
175
+
176
+
-**Customization:** Modify the JavaScript code to perform additional actions upon expiry, such as disabling certain features, redirecting to a specific page, or logging the event. Additionally, if necessary, you can check only the date of expiry without specifying the time.
0 commit comments