How to Add an Auto-Updating Date-Time Field to a PDF using C#

This example demonstrates how to insert a date-time field into a PDF that automatically updates to the current date and time whenever the PDF page is opened. This is useful for documents requiring real-time timestamps, such as reports or legal forms. The solution uses Aspose.PDF for .NET to programmatically add the field and embed JavaScript for dynamic updates.

Steps to Add an Auto-Updating Date-Time Field to a PDF using C#

  1. Load the existing PDF document using the Document class with a using statement for proper disposal.
  2. Define a rectangle area on the first page where the date field will be placed using Aspose.Pdf.Rectangle.
  3. Create a DateField instance at the specified rectangle and set its display format to ‘dd/MM/yyyy HH:mm:ss’.
  4. Initialize the field’s Value property with the current date and time using DateTime.Now.
  5. Create a JavascriptAction containing PDF JavaScript to update the field value on page open.
  6. Assign the JavaScript action to the DateField’s Actions.OnOpenPage event to trigger auto-updates.
  7. Add the DateField to the document’s form collection using doc.Form.Add(dateField).
  8. Save the modified PDF document to disk using doc.Save(outputPath).

The steps above walk through the process of embedding a dynamic, auto-updating date-time field into a PDF. By leveraging Aspose.PDF’s form and JavaScript capabilities, the field updates each time the document is opened, ensuring the timestamp remains current.

Code to Add an Auto-Updating Date-Time Field to a PDF using C#

This example demonstrates how to programmatically insert a date-time field into a PDF that updates automatically upon opening the document—ideal for time-sensitive documents requiring real-time metadata.

Similar Topics