c# print html,Print html file in C#, window

I have Window Client , and server

Client call server to get html (by string).

Client edit html file, and want to PRINT IT. Real print- paper, offline.

=> almost everyone said, for html printing, use WebBrowser.

At first, I want to print html string directly, by using WebBrowser.DocumentText.

It didn't work.

=>

WebBrowser webBrowserForPrinting = new WebBrowser();

WebBrowser.DocumentText = (my html strings. like "contents";)

WebBrowser.Print();

so I'm trying to write html string to text and create aaa.html

I made it Successfully, but then..

I tried to use

private void PrintHelpPage()

{

// Create a WebBrowser instance.

WebBrowser webBrowserForPrinting = new WebBrowser();

// Add an event handler that prints the document after it loads.

webBrowserForPrinting.DocumentCompleted +=

new WebBrowserDocumentCompletedEventHandler(PrintDocument);

// Set the Url property to load the document.

webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html");

}

private void PrintDocument(object sender,

WebBrowserDocumentCompletedEventArgs e)

{

// Print the document now that it is fully loaded.

((WebBrowser)sender).Print();

// Dispose the WebBrowser now that the task is complete.

((WebBrowser)sender).Dispose();

}

I don't know how I get Uri(@"\myshare\help.html" );

my file's uri...

somebody helps... i searched and searched, but I can't find anything.

I tried to take it on my desktop and do for test

Uri(@"file:\\C:\Users\Mine\Desktop\aaa1.html" ); and it's not working...

I did one more try

using System.Net;

using System.IO;

using System.Drawing.Printing;

private Font printFont;

private StreamReader reader;

string url = "http://www.google.com";

WebResponse response = null;

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 = reader.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;

}

private void printButton_Click(object sender, EventArgs e)

{

try

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.Method = "GET";

response = request.GetResponse();

reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

try

{

printFont = new Font("Arial", 10);

PrintDocument pd = new PrintDocument();

pd.PrintPage += new PrintPageEventHandler

(this.pd_PrintPage);

pd.Print();

}

finally

{

reader.Close();

}

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

and on paper it shows html all lines.

like this- < html > blah blah < / html >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值