Add Print Date in PDF using Print Action

Hi,

I am using Aspose PDF for JAVA to add text stamps in my pdf. I want to add a printed date stamp whenever pdf is printed.

The method that I thought to use was to create a text stamp that has a Javascript variable with no value while generating the pdf

Code for this:

private TextStamp generatePrintedDateStamp(float fontSize, double bottomMargin, Color color) {
TextState textState = new TextState();
textState.setForegroundColor(color);
textState.setFontSize(fontSize);
TextStamp footerStamp = new TextStamp(${VAR}, textState);
footerStamp.setVerticalAlignment(com.aspose.words.VerticalAlignment.BOTTOM);
footerStamp.setHorizontalAlignment(com.aspose.words.HorizontalAlignment.RIGHT);
footerStamp.setBottomMargin(bottomMargin);
return footerStamp;
}

Now this text stamp will have a variable VAR. I am unable to see any method in which I can pass a JS variable in textstamp
After this I wanted to embed JS code wich will populate the variable with current date and trigger on print action

public Document addPrintDateCodeInPDF(Document doc) {
    String code = "VAR=currentDate()";
    JavascriptAction javaScript = new JavascriptAction(code);
    doc.getActions().setBeforePrinting(javaScript);
    return doc;
}

My question here is des text stamp support a method which will embed a JS variable in the PDF if not is there another way to add printed date in the pdf?

@momin.siddique

No, the TextStamp does not support any method to embed JS. However, you can add a text box filed in the PDF at desired location with ReadOnly mode and set its value using JS Action. In case you face any issues, please let us know.

@asad.ali
Thanks for the help, this approach worked.