//PrintPage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Printing;
using System.IO;
using System.Drawing;
namespace PrintManagement
{
public partial class PrintPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics g = e.Graphics;
SolidBrush Brush = new SolidBrush(Color.Blue);
string textToPrint = TextBox2.Text;
g.DrawString(textToPrint, new Font("verdana", 14), Brush, 10, 10);
}
protected void PrintButton_Click(object sender, EventArgs e)
{
try
{
string currentTime = DateTime.Now.ToString("yyyyMMddHHmm");
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrintToFile = true;
// ps.PrintFileName = "D:\\PRINT\\Print_"+Time+".oxps"; /* you can save file here */
System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
printDoc.PrintPage += new PrintPageEventHandler(printDocument2_PrintPage);
System.Drawing.Printing.StandardPrintController standardPrintControl = new System.Drawing.Printing.StandardPrintController();
printDoc.PrintController = standardPrintControl;
printDoc.DefaultPageSettings.Landscape = true;
printDoc.PrinterSettings = printerSettings;
printDoc.Print();
TextBox2.Text = "";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Printed Successfully.Check: Drive D')", true);
}
catch (Exception ex)
{
}
}
protected void RefreshButton_Click(object sender, EventArgs e)
{
Response.Redirect("PrintPage.aspx");
}
}
}
//PrintPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PrintPage.aspx.cs" Inherits="PrintManagement.PrintPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox2" runat="server" Width="240px" Height="120px"
TextMode="MultiLine"></asp:TextBox>
<br />
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Empty message cannot be printed!"
ValidationGroup="vgp1"></asp:RequiredFieldValidator>
<br />
<br />
<asp:Button ID="PrintButton" runat="server" Text="Print" onclick="PrintButton_Click"
ValidationGroup="vgp1" />
<asp:Button ID="RefreshButton" runat="server" onclick="RefreshButton_Click" Text="Refresh"
ValidationGroup="vgp2" />
</form>
</body>
</html>