Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i have to print to html output . i have tried but i cant able to print html output but its printing html code

protected void Button1_Click(object sender, EventArgs e)
{
   try
   {
     try
     {
       // print the document using PrintDocument
       PrintDocument doctoprint = new PrintDocument();
       doctoprint.PrintPage += new  PrintPageEventHandler(document_PrintPage);
       doctoprint.Print();               
     }
     finally
     {
       stream.Close();
     }
   }
   catch (Exception ex)
   {
     Response.Write(ex.Message);

   }      
}

//Event handle  to print html 
private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   float yPos = 0;
   float leftMargin = e.MarginBounds.Left;
   float topMargin = e.MarginBounds.Top;
   string line = null;
   Font printFont = new Font("Arial", 10);
   Byte[] buffer = null;
 
   //buffer = StreamFile(@"C:\\Documents and Settings\\sweta.rani\\Desktop\\2008-03-12_osam_pr.pdf");
   buffer = StreamFile(@"C:\\Documents and Settings\\sweta.rani\\Desktop\\A.html");
   if (buffer != null)
   {
     Response.ContentType = "text/html";
     Response.AddHeader("content-length", buffer.Length.ToString());
     Response.BinaryWrite(buffer);
            
     line = Encoding.ASCII.GetString(buffer);
     e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());         
     Response.End();
   }     
}

but i can't able to print html output. only html code


ya i know but how i will use this in web . By adding any refence of System.Window.Form.WebBrowser. i am not getting what is the dll name

Can u pls help me
Posted
Updated 24-Mar-10 3:37am
v3

I don't know what you are actually trying to do?

If you want to display the html code as text in a browser, you need to encode it using one of the HtmlEncode( ... ) methods.

Nick
 
Share this answer
 
If I understand you correctly, you want to print an html page on a printer attached to your server? This sounds odd, especially for a web app.

Anyway, if that is the case, you need an html rendering engine. The easiest one to use is the System.Windows.Forms.WebBrowser control.

Create an instance of one and set the DocumentText property to your html string. Handle the DocumentCompleted event which fires when the html is loaded. Then set up your print job and call WebBrowser.Print().

I can't imagine why you would want to do this, though :confused:

Nick
 
Share this answer
 
Exactly i want to print the html output means whatever displayed on browser .
Step i have done
1) read the A.html file
2) write it to the browser
3) print the html output through server side. not client side code To print the html output i have used server side System.Drawing.PtintDocument Class but i cant able to print html output but its printing html Code like ......
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900