how can i print the windows form?

Posted by Sudheep.grandhe under C# on 11/15/2011 | Points: 10 | Views : 4314 | Status : [Member] | Replies : 2
Hi,

presently i am developing windows applications.Here is my query is i have form and in the form i place the button as print.when i click the print button it should be print the form.how can i execute this one.can you provide the code for this and explain step by step.please do the needful.

Best,
Sudheep.



Responses

Posted by: RohiK on: 11/15/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

check this

http://www.codeproject.com/KB/printing/printform.aspx

Happy Programming!!
Rohi

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 11/16/2011 [Member] Starter | Points: 25

Up
0
Down
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

namespace PrintingDemo
{
public class TextPrintDocument : PrintDocument
{
private Font printFont;
private TextReader printStream;
private string fileToPrint;
private Bitmap imgWatermark;

public bool Watermark = false;

public TextPrintDocument()
{
imgWatermark = new Bitmap(GetType(), "Watermark.gif");
}

public TextPrintDocument(string fileName) : this()
{
this.FileToPrint = fileName;
}

public string FileToPrint
{
get
{
return fileToPrint;
}
set
{
if (File.Exists(value))
{
fileToPrint = value;
this.DocumentName = value;
}
else
throw(new Exception("File not found."));
}
}

public Font Font
{
get { return printFont; }
set { printFont = value; }
}

protected override void OnBeginPrint(PrintEventArgs e)
{
base.OnBeginPrint(e);
printFont = new Font("Verdana", 10);
printStream = new StreamReader(fileToPrint);
}

protected override void OnEndPrint(PrintEventArgs e)
{
base.OnEndPrint(e);
printFont.Dispose();
printStream.Close();
}

protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);

// Slow down printing for demo.
System.Threading.Thread.Sleep(200);

Graphics gdiPage = e.Graphics;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
float lineHeight = printFont.GetHeight(gdiPage);
float linesPerPage = e.MarginBounds.Height / lineHeight;
int lineCount = 0;
string lineText = null;

// Watermark?
if (this.Watermark)
{
int top = Math.Max(0,
(e.PageBounds.Height - imgWatermark.Height) / 2);
int left = Math.Max(0,
(e.PageBounds.Width - imgWatermark.Width) / 2);
gdiPage.DrawImage(imgWatermark, left, top,
imgWatermark.Width, imgWatermark.Height);
}

// Print each line of the file.
while (lineCount < linesPerPage &&
((lineText = printStream.ReadLine()) != null))
{
gdiPage.DrawString(lineText, printFont, Brushes.Black,
leftMargin, (topMargin + (lineCount++ * lineHeight)));
}

// If more lines exist, print another page.
if(lineText != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
}

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response