Printing from IIS
- From: Brian Taylor
- Date: Thu, 17 Sep 2009 17:28:16 -0700
Hi Prabakaran,
The printer needed to be local on the IIS server even if the port is an IP address and is referred to by name (string). Below is the printing code for your information - not all is applicable.
Regards,
Brian
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;
using System.Configuration;
using System.Collections;
using System.Security.Principal;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BatchPrintUtility;
//[assembly: System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.RequestMinimum, Unrestricted=true)]
//[AspNetHostingPermission(System.Security.Permissions.SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Unrestricted)]
public partial class Execute : System.Web.UI.Page
{
Thread printReports = null;
protected void Page_Load(object sender, EventArgs e)
{
// check for valid page parameters
bool ok = Request.QueryString["BatchId"] != null && Request.QueryString["printer"] != null;
pnlForm.Visible = ok;
pnlError.Visible = !ok;
cvBatchId.IsValid = ok;
if (!ok)
return;
// print the batch in a new background thread so that we can monitor the status
if (!IsPostBack)
{
Session["Printing"] = "Start"; // initialise the session on the main thread so that it can be accessed in the printing thread
lblResults.Text = "Printing Status" + " - " + (Session["Printing"] == null ? "null" : Session["Printing"]) + ", " + (printReports == null ? "null" : printReports.IsAlive.ToString());
Session["Results"] = null;
tmrResults.Enabled = true;
printReports = new Thread(new ParameterizedThreadStart(PrintReportBatch));
Session["Printing"] = "Starting";
printReports.Start(Request.QueryString["printer"]);
Session["Printing"] = "Started";
//PrintReportBatch(Request.QueryString["printer"]);
}
}
/// <summary>
/// Print the report batch by:
/// 1. Open the specified batch in the database
/// 2. Get the next report from the batch
/// 3. Stream the report from the server and print the report
/// 4. Return the status on the print operation (in colour to the web page using AJAX)
/// 5. Log the print operation and result (including any error message) to the log file
/// 6. Repeat until all report have been printed
/// </summary>
private void PrintReportBatch(object printer)
{
// changes to get this thread working in the background: changes aspnet.config rather than the Impersonate option
// changes to aspnet.config: see http://www.leastprivilege.com/CommentView.aspx?guid=4b87938a-ee1b-4bf8-994c-befbfeac9b97
//Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); // Session["User1"] = this.Context.User.Identity.Name;
//((WindowsIdentity)this.Context.User.Identity).Impersonate();
//((WindowsIdentity)Thread.CurrentPrincipal.Identity).Impersonate();
//this.Request.LogonUserIdentity.Impersonate();
//string applicationName = "BatchPrint";
//IUserManager userManager = GetUserManager();
//bool authenticated;
//authenticated = userManager.Authenticate(applicationName, userName, password);
//if (authenticated)
//{
// IIdentity identity = new GenericIdentity(userName);
// CustomPrincipal.Attach(identity, applicationName, userManager, CacheRoles);
//}
// initialise variables
bool status = true;
string report = null;
int copies;
string parameters;
PrintReport printReport = new PrintReport((string) printer, this.Context.User.Identity.Name, this.Context.Request.RawUrl); // @"\\biarritz\L1 Xerox C4300"
// open the specified batch in the database
//sdsExecute.SelectParameters["BatchId"].DefaultValue = "1";
//sdsExecute.SelectParameters["ReportCondition"].DefaultValue = "Week";
SqlDataReader reports = (SqlDataReader) sdsExecute.Select(new DataSourceSelectArguments());
// check for no reports
if (reports == null)
{
Session["Printing"] = false;
return;
}
// create the results table
Table tblResults = new Table();
TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell thc;
TableRow tr;
TableCell tc;
//CellPadding="1" CellSpacing="1">
tblResults.BorderColor = Color.Black;
tblResults.BorderStyle= BorderStyle.Solid;
tblResults.BorderWidth = 1;
// create the header row for the table
tblResults.Rows.Add(thr);
thc = new TableHeaderCell(); thr.Cells.Add(thc); thc.Text = "#";
thc = new TableHeaderCell(); thr.Cells.Add(thc); thc.Text = "Report";
thc = new TableHeaderCell(); thr.Cells.Add(thc); thc.Text = "Copies";
thc = new TableHeaderCell(); thr.Cells.Add(thc); thc.Text = "Result";
thc = new TableHeaderCell(); thr.Cells.Add(thc); thc.Text = "Message";
// loop through the reports
for (int count=1; reports.Read(); count++)
{
// print the report
report = reports.GetString(0);
if (reports.IsDBNull(1))
parameters = null;
else
parameters = reports.GetString(1);
if (reports.IsDBNull(2))
copies = 1;
else
copies = reports.GetInt32(2);
if (copies > 0)
status = printReport.RenderPrint(report, parameters, copies);
else
{
status = true;
printReport.ErrorMessage = ""; // clear the last error message
}
// status = true;
//System.Threading.Thread.Sleep(2500);
// display the results
tr = new TableRow();
tr.BackColor = status ? Color.Lime : Color.Red;
tblResults.Rows.Add(tr);
tc = new TableCell(); tr.Cells.Add(tc); tc.Text = count.ToString();
tc = new TableCell(); tr.Cells.Add(tc); tc.Text = report;
tc = new TableCell(); tr.Cells.Add(tc); tc.Text = copies.ToString();
tc = new TableCell(); tr.Cells.Add(tc); tc.Text = status ? "Printed" : "Error";
tc = new TableCell(); tr.Cells.Add(tc); tc.Text = status ? "at: " + DateTime.Now.ToString() + @", to: " + (string) printer : printReport.ErrorMessage;
Session["Results"] = tblResults;
}
// finish up
reports.Close();
Session["Printing"] = "End";
}
/// <summary>
/// Return to the batch screen
/// </summary>
protected void btnReturn_Click(object sender, EventArgs e)
{
Response.Redirect("Batch.aspx");
}
/// <summary>
/// Collect and display the latest results
/// </summary>
protected void tmrResults_Tick(object sender, EventArgs e)
{
lblResults.Text = "Print status as at: " + DateTime.Now.ToLongTimeString(); // +" - " + (Session["Printing"] == null ? "null" : Session["Printing"]) + ", " + (printReports == null ? "null" : printReports.IsAlive.ToString());
Table tblResults = (Table) Session["Results"];
if (tblResults != null)
udpResults.ContentTemplateContainer.Controls.Add(tblResults);
if (Session["Printing"] == null)
{
lblResults.Text = "A fatal error occured with the background printing process. Please report this error on the helpdesk system.";
tmrResults.Enabled = false;
return;
}
if ((string) Session["Printing"] == "End")
{
lblResults.Text = "Printing Complete"; // +" - " + (Session["Printing"] == null ? "null" : Session["Printing"]) + ", " + (printReports == null ? "null" : printReports.IsAlive.ToString());
tmrResults.Enabled = false;
}
}
}
Posted as a reply to:
Printing from IIS
Hi Brain,
I am trying to print a document from asp.net which is hosted in IIS. When i print a document from asp.net application, i can able to print to the network printer. But, if i try to print the same from IIS, It hits the printer but it doesn't print
Our network printer is Password protected. I believe when i call the printer from IIS, some security options is added and i doesn't print.
I need a some sample to print document from IIS. Actually i'm very new to this.
Thanks for yor help.
Regards,
Prabakaran
EggHeadCafe - Software Developer Portal of Choice
WCF Workflow Services Using External Data Exchange
http://www.eggheadcafe.com/tutorials/aspnet/3d49fa0d-a120-4977-842a-6dafb17b6d74/wcf-workflow-services-usi.aspx
.
- Prev by Date: RE: Cookies in IE8
- Next by Date: RE: Cookies in IE8
- Previous by thread: Encrypting .config files
- Next by thread: Role Manager Cookies
- Index(es):
Relevant Pages
|