PDFOne (for Java™)
Create, edit, view, print & enhance PDF documents and forms on Java™ platform
J2SE J2EE Windows Linux Mac (OS X)

Creating Print-Only PDF Content

Learn to create PDF content that will be available only when the document is printed.
By V. Subhash and L. Santhanam

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();
    // 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
    // Clean up

Now, here is the output document when it is viewed in Adobe Reader.

This PDF document contains a hidden form field

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.

Contents of the hidden form field is rendered when the PDF is printed (to XPS in this case)


Our Free Online Document-Processing Tools
Gnostice Apps

Gnostice Apps provides several PDF document-processing services over the Web. It enables you to merge, split, convert, secure and redact PDF documents - all for FREE. All you need to access these services is a HTML5-capable browser such as the latest version of Firefox/Seamonkey or IE 11.

Our Developer Tools
eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.


A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

XtremeDocumentStudio .NET

Multi-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 Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2014 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).