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 Cloud-based document-processing
Gnostice StarDocs

Gnostice StarDocs is a scalable, fault-tolerant document-processing server solution, designed to be the central go-to place for all kinds of electronic document-processing work. StarDocs includes a web application (as seen at stardocs.gnostice.com) that frees your end-users from the need to have format-specific software to merge, split, redact, convert, edit, and sign documents - all they need is a network connection and a HTML5 browser. StarDocs provides a REST-based API that your developers can use to add document-processing functions to a variety of software applications. As the REST API is platform agnostic, your developers can write software on a wide variety of platforms - from desktop to mobile devices to Web services in any development language. To get them started quickly, StarDocs is supported by language-specific native SDK. StarDocs document server has load-balancing built-in and is designed to be scalable across enterprise Intranets and the Internet. StarDocs is available as packaged software for on-premise hosting and also as a subscription-based cloud service.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

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.

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.


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

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java™)

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

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-2015 Gnostice Information Technologies Private Limited. All rights reserved.