Hi Team,
I wanted to show the current date time on the Aspose PDF without any token embedded on word template. Is their anyway to display the current date time whenever Aspose convert the word document into pdf ? Please note, i don’t have any custom token to use in the word template . I want to use some inbuild word/aspose functionality to show the current datetime.
@arindamk
To display the current date and time in an Aspose PDF document without using any custom tokens in your Word template, you can utilize the TextStamp
feature in Aspose.PDF. Below is a code example in C# that demonstrates how to add a date and time stamp to a PDF document after converting it from a Word document.
C# Code Example
using System;
using Aspose.Pdf;
using Aspose.Pdf.Text;
namespace Aspose.Pdf.Examples
{
public class AddDateTimeStamp
{
public static void Run()
{
// Load the PDF document
Document pdfDocument = new Document("path/to/your/document.pdf");
// Get the current date and time
string currentDateTime = DateTime.Now.ToString("MM/dd/yy hh:mm:ss tt");
// Create a text stamp with the current date and time
TextStamp textStamp = new TextStamp(currentDateTime)
{
BottomMargin = 10,
RightMargin = 20,
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Bottom
};
// Add the stamp to the first page of the PDF
pdfDocument.Pages[1].AddStamp(textStamp);
// Save the updated PDF document
pdfDocument.Save("path/to/your/output/document_with_timestamp.pdf");
Console.WriteLine("Date time stamp added successfully.");
}
}
}
Explanation
- Load the PDF Document: Replace
"path/to/your/document.pdf"
with the path to your PDF file.
- Get Current Date and Time: The current date and time are formatted as a string.
- Create a Text Stamp: A
TextStamp
object is created with the current date and time, and its position is set to the bottom right of the page.
- Add the Stamp: The stamp is added to the first page of the PDF document.
- Save the Document: Finally, the updated document is saved to a specified path.
This approach allows you to dynamically display the current date and time in your PDF without needing any custom tokens in your Word template.
Feel free to adjust the formatting of the date and time as needed.
If you have any further questions or need additional assistance, let me know!
Sources:
[1]: AddDateTimeStamp.cs
Hi @Professionalize.Discourse ,
In the above example, I need to make the code changes on c# end and re-deploy . We have a production incident where customer document is not showing current date. So, we are planning to do some quick fix (As i mentioned in the original post without making any deployment) on the template itself and upload the latest template … In the upcoming release, we will make these permanent fix from code behind. But as of now we need something quick fix . Is their any way to show the date ?
@arindamk You can use DATE
field in your template. It’s value will be update to the current date.
Will it work Linq reporting engine template ?
@arindamk This is a standard MS Word field, so it should work properly. If you are using LONQ Reporting Engine, you can simply use the following tag
<<[DateTime.Now]>>
But in this case it is required to register DateTime
as know type and will require changes in your code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("<<[DateTime.Now]>>");
ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(DateTime));
engine.BuildReport(doc, new object());
doc.Save(@"C:\Temp\out.docx");
Ok. Since we are using LINQ REPORTING engine, we must have to make the code behind changes in c#? I’s looking for something without code behind changes …
@arindamk Please try using use standard MS Word DATE
field in your template.