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