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 .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-2025 Gnostice Information Technologies Private Limited. All rights reserved.