PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

Create an Auto-Print PDF

Learn to create a PDF that will display the print dialog when the document is opened.
By V. Subhash

Recently, we had a customer who wanted to create a PDF that would automatically prompt the user to print the document. Our initial suggestion involved adding a "JavaScript PDF action" to the "open" event of the document.

using System;
using System.Collections.Generic;
using System.Text;
using Gnostice.PDFOne;

namespace Create_Auto_Print_PDF {
  class Program {
    static void Main(string[] args) {
      PDFDocument doc = new PDFDocument();
      // Use this statement to make the viewer application display
      // the Print dialog and allow the user to specify the print settings
      doc.AddOpenActionJavaScript("this.print();");

      // Use the following statement (instead of the previous one)
      // to make the viewer application attempt to silently print the 
      // document (to the default printer using default settings). 
      // Note: There still will be a confirmation dialog prompt to the user
      doc.AddOpenActionJavaScript("this.print(false);");

      doc.WriteText("This PDF will attempt to print itself when you open it.");
      doc.Save("Auto_Print_PDF1.pdf");
      doc.Close();
    }
  }
}

Our customer came back saying that the document worked fine in AdobeĀ® Reader but not in another PDF viewer. Even in Adobe Reader, the document may not have worked as intended if JavaScript scripting was turned off.

Our alternative solution involves the use of "print named action" in place of the JavaScript action.

using System;
using System.Collections.Generic;
using System.Text;
using Gnostice.PDFOne;

namespace Create_Auto_Print_PDF {
  class Program {
    static void Main(string[] args) {
      PDFDocument doc = new PDFDocument();
      doc.AddOpenActionNamed(PDFActionName.Print);
      doc.WriteText("This PDF will attempt to print itself when you open it.");
      doc.Save("Auto_Print_PDF2.pdf");
      doc.Close();
    }
  }
}

When we checked, the second PDF document worked as intended in both viewers. However, we would like to introduce a caveat here that the "print" and several other named actions available in PDFOne .NET are undocumented features* that are supported by Adobe Reader.

---o0O0o---

* - As of PDF Reference Version 1.7.

---o0O0o---

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.

PDFOne .NET

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).