Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Simple Printing in .NET

2.46/5 (15 votes)
26 Apr 2008CPOL1 min read 1   1.3K  
Simple printing sample without Crystal report and non-databinding in design time using .NET
Image 1

Introduction

This is a simple printing sample without Crystal report and non-databinding in design time using .NET.

Background

I intend to re-introduce optimized coding in a simple style. The first step is simple printing in .NET.

Using the Code

In .NET, printing is very simple. Create Component (in code only) and make that as instance,
i.e.:

C#
PrintPreviewDialog objPrev = new PrintPreviewDialog();
PrintDocument objDoc = new PrintDocument();   

Where component:

  • PrintPreviewDialog - shows the preview of the printable object which will show in non-editable format. It has zooming option, navigating option and printing option.
  • PrintDocument - a kind of document which has the printable objects that we have given.

This instance is enabled by event(PrintPage) handler(PrintPageEventHandler), i.e.:

C#
objDoc.PrintPage += new PrintPageEventHandler(funSendDocPrint);

where funSendDocPrint is a function with PrintPageEventArgs as argument. This will assign the printing objects with the document through PrintPageEventArgs(e).

Here in this article, I used to create the printable objects for String, Lines, and Images with their requirements. All these printable objects are assigned into the document in the means of drawing work. So we intended to create pens, brushes and their color, i.e.:

C#
//Horizontal lines
eve.Graphics.DrawLine(pen, new Point(eve.MarginBounds.Left, eve.MarginBounds.Top),
new Point(eve.MarginBounds.Right, eve.MarginBounds.Top));
    
eve.Graphics.DrawLine(pen, new Point(eve.MarginBounds.Left, eve.MarginBounds.Top + 200),
new Point(eve.MarginBounds.Right, eve.MarginBounds.Top + 200));
    
//Vertical lines
eve.Graphics.DrawLine(pen, new Point(eve.MarginBounds.Left, eve.MarginBounds.Left),
new Point(eve.MarginBounds.Left, eve.MarginBounds.Top + 200));
    
eve.Graphics.DrawLine(pen, new Point(eve.MarginBounds.Right, eve.MarginBounds.Top),
new Point(eve.MarginBounds.Right, eve.MarginBounds.Top + 200));
    
//Drawing strings
eve.Graphics.DrawString("Amount : ", new Font("Ariel", 13, FontStyle.Bold, 
GraphicsUnit.World), brsh, eve.MarginBounds.Left + 50, eve.MarginBounds.Top + 50);
eve.Graphics.DrawString(strNumber, new Font("Arial", 13, FontStyle.Regular, 
GraphicsUnit.World), brsh, eve.MarginBounds.Left + 130, eve.MarginBounds.Top + 50);
eve.Graphics.DrawString("In Rupees : ", new Font("Arial", 13, FontStyle.Bold, 
GraphicsUnit.World), brsh, eve.MarginBounds.Left + 50, eve.MarginBounds.Top + 75);
eve.Graphics.DrawString(strRupees, new Font("Arial", 13, FontStyle.Regular, 
GraphicsUnit.World), brsh, eve.MarginBounds.Left + 130, eve.MarginBounds.Top + 75);
eve.Graphics.DrawString("In Dollars : ", new Font("Arial", 13, FontStyle.Bold, 
GraphicsUnit.World), brsh, eve.MarginBounds.Left + 50, eve.MarginBounds.Top + 100);
eve.Graphics.DrawString(strDollers, new Font("Arial", 13, FontStyle.Regular, 
GraphicsUnit.World), brsh, eve.MarginBounds.Left + 130, eve.MarginBounds.Top + 100);
    
//Inserting Image
float x = eve.MarginBounds.Left + 450;
float y = eve.MarginBounds.Top + 25;
Bitmap bmp = new Bitmap(Application.StartupPath + "\\member_unknown.gif");
Image img = bmp;
eve.Graphics.DrawImage(img, x, y); 

This values are assigned to printing document and re-allocated with preview dialog component for preview, i.e.:

C#
objPrev.Document = objDoc;
objPrev.ShowDialog();

Which will give:

SImage.JPG

This is a simple way of printing the object in .NET without Crystal Report and without databinding with Component in design time.

Points of Interest

Simple coding, component creation-used in code and amount to word convention's sample.

History

  • 26th April, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)