Sunday, March 9, 2014

Print Document Class

Print Document Class

.NET supports to print any type of documents through the printer.
Library: System.Drawing.Printing.PrintDocument
This class object represents a document (any type of file), that is ready to print.
Note:  By default, System.Drawing assembly will not be linked with the Console applications. 
That‘s why you need to add a reference to that as follows:

  • Click on ―> Project menu  ―> Add Reference.
  • Select ―> System.Drawing assembly.
  • Click  on  OK.  Then  the  System.Drawing  assembly  will  be  linked  with  the  current project and it will be displayed in References folder in the Solution Explorer.
Implementation:
  • Import the namespace:          using System.Drawing.Printing;
  • Construct the object:
  •         PrintDocument obj = new PrintDocument();
  • Assign the document name, that is to be printed:
  •          obj.DocumentName = file path to be printed;
  • Start Printing:
  •          obj.Print();
PROGRAM FOR PRINT CLASS
                                                            
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
namespace PrintingDemo
{
 class Program
 {
  static void Main(string[] args)
  {
   PrintDocument doc = new PrintDocument();
   doc.DocumentName = "c:\\SampleDocument.doc";
   doc.Print();
   Console.WriteLine("Printing started...");
   Console.Read(); 
  }
 }
}