Skip to content

Commit 90e331c

Browse files
authored
Merge pull request #333 from aspose-pdf/PDFNET_56529_Set_expiry_date
Set expiry date using Javascript (PDFNET-56529)
2 parents 4c82163 + cea6fa5 commit 90e331c

File tree

2 files changed

+71
-2
lines changed
  • net
    • advanced-operations/working-with-javascript
    • converting/convert-pdf-to-other-files

2 files changed

+71
-2
lines changed

net/advanced-operations/working-with-javascript/_index.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ The example below applies the OpenAction to a specific document.
9696

9797
{{< gist "aspose-pdf" "7e1330795d76012fcb04248bb81d45b3" "Examples-CSharp-AsposePDF-Working-Document-AddJavaScriptToPage-AddJavaScriptToPage.cs" >}}
9898

99-
### **Adding/Removing JavaScript to Document Level**
99+
### Adding/Removing JavaScript to Document Level
100100

101101
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:
102102

@@ -106,6 +106,75 @@ A new property named JavaScript is added in Document class which has JavaScript
106106

107107
{{< gist "aspose-pdf" "7e1330795d76012fcb04248bb81d45b3" "Examples-CSharp-AsposePDF-Working-Document-AddRemoveJavascriptToDoc-AddRemoveJavascriptToDoc.cs" >}}
108108

109+
### 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+
private static void CreateDocumentWithExpiryDate()
131+
{
132+
// Initialize a new PDF document
133+
using (var document = new Aspose.Pdf.Document())
134+
{
135+
document.Pages.Add();
136+
137+
// Define the expiry date and time (e.g., April 1, 2024, 12:00:00 PM)
138+
DateTime expiryDateTime = new DateTime(2024, 4, 1, 12, 0, 0);
139+
140+
// Create JavaScript code to enforce the expiry date and time
141+
string jsCode =
142+
// Get the current date and time
143+
"var rightNow = new Date();\n" +
144+
// Set the expiry date and time
145+
"var endDate = new Date(" +
146+
$"{expiryDateTime.Year}," +
147+
$"{expiryDateTime.Month - 1}," + // Months are zero-based in JavaScript
148+
$"{expiryDateTime.Day}," +
149+
$"{expiryDateTime.Hour}," +
150+
$"{expiryDateTime.Minute}," +
151+
$"{expiryDateTime.Second}" +
152+
");\n" +
153+
"if(rightNow > endDate)\n" +
154+
"{\n" +
155+
" app.alert(\"This Document has Expired as of \" + endDate.toLocaleString() + \".\");\n" +
156+
" this.closeDoc();\n" +
157+
"}";
158+
159+
// Create a JavascriptAction with the defined JavaScript code
160+
var javaScript = new Aspose.Pdf.Annotations.JavascriptAction(jsCode);
161+
162+
// Set the JavaScript action to execute when the document is opened
163+
document.OpenAction = javaScript;
164+
165+
// Save the updated PDF document
166+
string outputPath = "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.
177+
109178
<script type="application/ld+json">
110179
{
111180
"@context": "http://schema.org",

net/converting/convert-pdf-to-other-files/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ Since the 24.2 release, Aspose.PDF has implemented converting Searchable PDF to
219219
The following code snippet shows the process of converting PDF file into XPS format.
220220

221221
```csharp
222-
using (var document = new Document("input.pdf"))
222+
using (var document = new Aspose.Pdf.Document("input.pdf"))
223223
{
224224
var xpsOptions = new XpsSaveOptions
225225
{

0 commit comments

Comments
 (0)