Click here to Skip to main content
15,917,321 members
Articles / Desktop Programming / Windows Forms

Simple Printing in .NET

Rate me:
Please Sign up or sign in to vote.
2.46/5 (15 votes)
26 Apr 2008CPOL1 min read 37.2K   1.3K   21   3
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)


Written By
Technical Lead
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 97945766-Jun-14 5:57
Member 97945766-Jun-14 5:57 
QuestionUnhandled Exception in while Executing Pin
Itz.Irshad17-Sep-12 18:50
Itz.Irshad17-Sep-12 18:50 
Questionprinter button does not cause report to print Pin
kentuckyjoe19-Aug-12 14:38
kentuckyjoe19-Aug-12 14:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.