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

PDFOne .NET

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.