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