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

A PDF Printer Demo For PDFOne .NET

Learn how to print a PDF document in .NET.
By V. Subhash

Printing a PDF document using PDFOne is very simple - create a PDFPrinter instance, load the PDF document, and call the "Print" method.

The PDF printer component exposes a System.Drawing.Printing.PrinterSettings property for full .NET printing support. This allows your .NET application to enumerate available printers, paper sizes, media trays and other settings and choose appropriate ones for PDF print jobs. Apart from this, the PDF printer component has numerous printing settings of its own, such as the ability to:

The following code snippet illustrates how to a PDF document in C# using PDFOne .NET.

using System;
using System.Collections.Generic;
using System.Text;
using Gnostice.PDFOne;
using Gnostice.PDFOne.PDFPrinter;
using System.Drawing.Printing;

class PDF_Printer_Demo {

  static void Main(string[] args) {
    PDFPrinter PDFPrinter1
        = new PDFPrinter("your license key");

    PDFPrinter1.PasswordRequired
        += new PasswordRequiredEventHandler(PDFPrinter1_PasswordRequired);
    PDFPrinter1.PrintError
        += new PrintErrorDelegate(PDFPrinter1_PrintError);
    PDFPrinter1.RenderError
        += new RenderErrorHandler(PDFPrinter1_RenderError);
    PDFPrinter1.AfterPrintPage
        +=new PrintPageDelegate(PDFPrinter1_AfterPrintPage);

    try {
      Console.Write("Opening document ... ");
      // Load document for printing
      PDFPrinter1.LoadDocument("sample_doc.pdf");
      Console.WriteLine("Done");

      // Specify print settings
      PDFPrinter1.AutoCenter = true;
      PDFPrinter1.AutoRotate = true;
      PDFPrinter1.PageScaleType = PrintScaleType.None;
      PDFPrinter1.PrintOptions.PrintRange = PrintRange.Selection;
      PDFPrinter1.SelectedPages = "2,4";

      Console.Write("Selecting printer ... ");
      // Select first one among installed printers
      PDFPrinter1.PrintOptions.PrinterName
          = (String) PrinterSettings.InstalledPrinters[0];
      Console.WriteLine("Done");

      Console.WriteLine("Sending print output to selected printer [" +
                    PDFPrinter1.PrintOptions.PrinterName + "] ... ");
      // Start printing
      PDFPrinter1.Print();
      Console.Write("\r                                ");
      Console.WriteLine("\rDone.");

    } catch (Exception Exception1) {
      Console.WriteLine("\nError: " + Exception1.Message);
    } finally {
      try {
        Console.Write("Closing document ... ");
        // Close printed document
        PDFPrinter1.CloseDocument();
        Console.WriteLine("Done.");
      } catch (Exception Exception2) {
         Console.WriteLine("\nError: " + Exception2.Message);
      } finally {
        // Clean up
        PDFPrinter1.Dispose();

        Console.WriteLine("Press Enter to exit.");
        Console.In.ReadLine();
      }
    }
  }

  // Event handler for PDFPrinter.PrintError
  private static void PDFPrinter1_PrintError(
      object sender,
      int pageNumber,
      Exception e) {
    Console.WriteLine("\nError: While creating print output on page #" +
                      pageNumber.ToString() +
                      "\nOriginal Error Message: " +
                      e.Message);
  }


  // Event handler for PDFPrinter.RenderError
  private static void PDFPrinter1_RenderError(
      object sender,
      int pageNum,
      Exception renderError,
      ref bool continueRendering)  {
    Console.WriteLine("\n\nError: While rendering page #" +
                      pageNum.ToString() + " on printer." +
                      "\nOriginal Error Message: " +
                      renderError.Message);
  }


  // Event handler for PDFPrinter.AfterPrintPage
  private static void PDFPrinter1_AfterPrintPage(
      object sender,
      int pageNumber,
      PrintPageEventArgs e) {
    // Clear the current line
    Console.Write("\r                                ");
    // Write number of last page that was printed
    Console.Write("\rPrinting page #" + pageNumber.ToString());
  }


  // Event handler for PDFPrinter.PasswordRequired
  private static void PDFPrinter1_PasswordRequired(
      object sender,
      ref string password,
      ref bool continueLoading) {
    // Prompt for password
    Console.WriteLine("\n\nEnter a password to open this document: ");
    // Obtain password entered by user
    String sPassword = Console.In.ReadLine();

    // Open document with obtained password
    password = sPassword;
    continueLoading = true;
  }

}

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