PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
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();
    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.

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)

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

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

PDFOne .NET

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

Our Delphi/C++Builder developer tools
Gnostice Document Studio 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.

Our Java developer tools
Gnostice Document Studio 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 Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.