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