Click here to Skip to main content
Licence CPOL
First Posted 26 Apr 2008
Views 16,755
Downloads 497
Bookmarked 20 times

Simple Printing in .NET

By | 26 Apr 2008 | Article
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)

About the Author

Senthil S

Software Developer

India India

Member

Hello,
This is Senthil.S. I am a Software Engineer at TCS. I am Currently Standing on .Net and Flex Platform.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 26 Apr 2008
Article Copyright 2008 by Senthil S
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid