------------------------------------------------------------------------------------------------------------------------
I'm trying to direct print a string to a network printer using this code.
using (PrintDocument pd = new PrintDocument())
{
pd.PrinterSettings.PrinterName = @"\\192.168.0.186\HP LaserJet 1018";
streamToPrint = new StringReader("test string content");
printFont = new Font("Arial", 10);
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
When I try to run my website in iis.It gives me the error
The RPC server is unavailable in the line pd.Print()
------------------------------------------------------------------------------------------------------------------------
System.ComponentModel.Win32Exception (0x80004005): The RPC server is unavailable at System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument
document, PrintEventArgs e) at System.Windows.Forms.PrintControllerWithStatusDialog.OnStartPrint(PrintDocument document, PrintEventArgs e) at
System.Drawing.Printing.PrintController.Print(PrintDocument document) at System.Drawing.Printing.PrintDocument.Print() at _Default.Page_Load(Object sender, EventArgs
e) in c:\inetpub\wwwroot\local\Default.aspx.cs:line 46
------------------------------------------------------------------------------------------------------------------------
If I run my website using visual studio development server everithing works fine.
Any Ideas? Please help me
My code gven below
------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Printing;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using System.Security.Principal;
public partial class _Default : System.Web.UI.Page
{
private System.ComponentModel.Container components;
private Font printFont;
private StringReader streamToPrint;
protected void Page_Load(object sender, EventArgs e)
{
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
Response.Write(printer + "<br>");
}
try
{
using (PrintDocument pd = new PrintDocument())
{
pd.PrinterSettings.PrinterName = @"\\192.168.0.186\HP LaserJet 1018";
streamToPrint = new StringReader("test string content");
printFont = new Font("Arial", 10);
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
}
------------------------------------------------------------------------------------------------------------------------