Developer Tools
|
Office Productivity Applications
|
Enterprise Solutions
|
|||||||||||||||||||||||







When you print a webpage, the browser usually adds the current date and time to one of the corners of sheet. Why not do the same for PDF documents?
For this, you need to add a JavaScript "action" that will add the current date and time to the document just before a viewer application (such as Adobe Reader) prints it. One of the simplest way to add isolated text content is via form fields. To make it more authentic, this form field can be hidden when the document is displayed on the screen but made available when it is printed.
Here is how you do this with PDFOne (for Java).
import java.io.IOException;
import com.gnostice.pdfone.PdfAction;
import com.gnostice.pdfone.PdfAction.PdfEvent;
import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfFormField;
import com.gnostice.pdfone.PdfFormTextField;
import com.gnostice.pdfone.PdfRect;
public class Print_With_Date {
public static void main(String[] args) throws IOException, PdfException {
// Load an existing document
PdfDocument doc = new PdfDocument();
doc.load("sample_doc.pdf");
// Create a form field that is hidden on screen but available in print
PdfFormTextField tf = new PdfFormTextField(new PdfRect(10, 5, 400, 50), "tf_date1");
tf.setFlags(PdfFormField.FLAG_PRINT | PdfFormField.FLAG_NO_VIEW);
// Add the form field to document
doc.addFormField((PdfFormField)tf, 1);
// Build the JavaScript script that will update
// the form field with the current date and time
String sNL = "\r\n";
String js = "var dt = new Date(); " + sNL +
"var fld = this.getField('tf_date1');" + sNL +
"fld.value = 'Printed on ' + " +
"dt.getFullYear() + '-' + dt.getMonth() + '-' + " +
"dt.getDate() + ' at ' + dt.getHours() + ':' + " +
"dt.getMinutes() + ':' + dt.getSeconds();" + sNL;
/* For this JavaScript code
var dt = new Date();
var fld = this.getField("tf_date1");
fld.value = "Printed on " +
"dt.getFullYear() + "-" + dt.getMonth() + "-" +
"dt.getDate() + " at " + dt.getHours() + ":" +
"dt.getMinutes() + ":" + dt.getSeconds();
*/
// Add the script as a JavaScript action to be executed
// before the document is printed
doc.addAction(PdfEvent.ON_BEFORE_DOCUMENT_PRINT, PdfAction.JAVASCRIPT, js);
// Save the modified document to file
doc.save("sample_doc2.pdf");
// Clean up
doc.close();
}
}
Now, here is the output document when it is viewed in Adobe Reader.

When you print this document, the value in the form field will be printed. In the screenshot below, I have printed the document to an XPS document.

---o0O0o---
| Our Developer Tools | |
|---|---|
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
XtremePDFConverter VCLA Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents. |
|
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
XtremeDocumentStudio .NETMulti-format document-processing component suite for .NET developers |
PDFOne (for Java™)A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications. |
XtremeFontEngine (for Java)Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts |
| Our Office Productivity Applications |
|---|
Free PDF ReaderA free, fast, and portable application for viewing, printing and converting PDF documents. |
| Privacy | Legal | Feedback | Newsletter | Resellers | © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved. |
This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).