Windows Printing: The Document to Print |
|
Introduction
In order to print, the Print dialog box must be given a document to print. This means that you must first prepare a document prior to printing. To support this, the .NET Framework provides the PrintDocument class that is defined in the System.Drawing.Printing namespace. This class is represented in the Toolbox by the PrintDocument button . Based on this, to prepare a document for printing, you can either add a PrintDocument object to your project or declare a variable of type PrintDocument. Here is an example:
using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Printing; public class Exercise : Form { Button btnPrint; PrintDialog dlgPrint; PrintDocument docPrint; public Exercise() { InitializeComponent(); } void InitializeComponent() { btnPrint = new Button(); btnPrint.Location = new Point(12, 12); btnPrint.Text = "&Print..."; btnPrint.Click += new EventHandler(btnPrintDocument); dlgPrint = new PrintDialog(); docPrint = new PrintDocument(); Controls.Add(btnPrint); Text = "Printing"; } void btnPrintDocument(object sender, EventArgs e) { dlgPrint.ShowDialog(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Exercise()); } }
After creating the document to print through a PrintDocument object, you can associate it with a PrintDialog. To support this, the PrintDialog class is equipped with a property named Document. To specify the object that would carry the printing, you can assign the PrintDocument object to the PrintDialog.Document property. Here is an example:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class Exercise : Form
{
Button btnPrint;
PrintDialog dlgPrint;
PrintDocument docPrint;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnPrint = new Button();
btnPrint.Location = new Point(12, 12);
btnPrint.Text = "&Print...";
btnPrint.Click += new EventHandler(btnPrintDocument);
dlgPrint = new PrintDialog();
docPrint = new PrintDocument();
dlgPrint.Document = docPrint;
Controls.Add(btnPrint);
Text = "Printing";
}
void btnPrintDocument(object sender, EventArgs e)
{
dlgPrint.ShowDialog();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Exercise());
}
}
A document to print can be made of only one or many pages. Each page has a number of characteristics. The characteristics of a page are controlled by the PrintDocument.PageSettings property which itself is based on the PageSettings class. The PageSettings class is defined in the System.Drawing.Printing namespace. This class holds the dimensions of the page, the values of the margins applied on the page, the tray that would supply the paper (since some printers have many trays), the printer resolution, whether the page would be printed in color or black and white, whether the page would be printed in Portrait or Landscape orientation, etc. If you don't want to specify these characteristics, you can set the PrintDocument.PageSettings property to DefaultPageSettings.
If you know the name of the document to be printed, you can assign it to the PrintDocument.DocumentName property. Here is an example:
void btnPrintDocument(object sender, EventArgs e)
{
PrintDialog dlgPrint = new PrintDialog();
PrintDocument docPrint = new PrintDocument();
docPrint.DocumentName = "Exercise";
dlgPrint.Document = docPrint;
dlgPrint.ShowDialog();
}
To actually print the document, you can call the PrintDocument.Print() method. Its syntax is:
public void Print();
Events Related to Printing
When the PrintDocument.Print() method is called, the printing process would start by firing the BeginPrint event but this event occurs before the first page is printed. The BeginPrint event is of type PrintEventArgs which does not hold any particular information, especially for the BeginPrint event. This event allows you to take some early actions, if necessary, before the printer receives the job.
Once the printer is ready, the application would then need to know what needs to be printed on the paper. At this time, the PrintPage event is fired. The PrintPage event is of type PrintPageEventArgs. The PrintPageEventArgs class allows you to fully customize the page of the document to be printed. For example, it is equipped with a Graphics property that allows you to "draw" anything you want on the paper. Here is an example:
using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Printing; public class Exercise : Form { Button btnPrint; PrintDialog dlgPrint; PrintDocument docPrint; public Exercise() { InitializeComponent(); } void InitializeComponent() { btnPrint = new Button(); btnPrint.Location = new Point(12, 12); btnPrint.Text = "&Print..."; btnPrint.Click += new EventHandler(btnPrintDocument); dlgPrint = new PrintDialog(); docPrint = new PrintDocument(); dlgPrint.Document = docPrint; docPrint.PrintPage += new PrintPageEventHandler(docPrintPrintPage); Controls.Add(btnPrint); Text = "Printing"; } private void docPrintPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.DrawString("Picture Preview", new Font("Times New Roman", 28, FontStyle.Bold), Brushes.Red, 60F, 80F); e.Graphics.DrawLine(new Pen(Color.Black, 2), 60, 120, 700, 120); e.Graphics.DrawImage(Image.FromFile(@"C:\Exercise1\fall1.jpg"), 60F, 200F); } void btnPrintDocument(object sender, EventArgs e) { if( dlgPrint.ShowDialog() == System.Windows.Forms.DialogResult.OK ) docPrint.Print(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Exercise()); } }
The PrintPageEventArgs class also allows you to get the location and dimensions of the area to be printed. This is represented by the PageBounds property, which produces a Rectangle object.
Once the PrintDocument object is ready, you must pass it to the Print dialog box. To do that, assign the name of the PrintDocument variable to the PrintDialog.Document property.
|
||
Home | Copyright © 2014-2020, FunctionX | |
|