Click here to Skip to main content
11,706,159 members (54,733 online)
Click here to Skip to main content

Simple Printing in .NET

, 26 Apr 2008 CPOL 23.9K 1.1K 21
Rate this:
Please Sign up or sign in to vote.
Simple printing sample without Crystal report and non-databinding in design time using .NET

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

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

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

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

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)

Share

About the Author

Senthil Sambandam
Technical Lead
India India
No Biography provided

You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 26 Apr 2008
Article Copyright 2008 by Senthil Sambandam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid