我在这里粘贴了代码,用于在wpf C#中创建html文件。所以请帮我打印那个文件。在下面的代码中,我创建了一个html字符串,并将该html文件保存到代码中给出的路径中。现在我想直接将这个文件转到打印对话框,这样我就可以打印html文件了。
private void Button_Click_1(object sender, RoutedEventArgs e) {
string path = @"C:\Users\EACD-017\Documents\Host.html";
string HTML = "<html>" +
"<head>" +
"<title>"+
" HOST INFORMATION " +
"</title>" +
//" <style type=\"text/css\">" +
//" .title {" +
//" color: blue;" +
//" text-decoration: bold;" +
//" text-size: 1em;" +
//" }" +
//" .author {" +
//" background-color:#FFECF5FD;" +
//" }" +
//" .table {" +
//" width:100%;" +
//" height:600px;" +
//" border:1px;" +
//" align:center;" +
//" }" +
//" </style>" +
"</head>" +
"<body>" +
"<table>" +
"<tr>" +
"<td>" +
"<img src=\"images.jpg\" style=\"width:29%;\" />" +
"<span style=\"padding-left: 18%;font-size: medium;\">HOST INFORMATION</span>" +
"</td>" +
//"<td style=\"width:18%;\">" +
//"</td>" +
//"<td style=\"font-size: 182%; text-align: center;\">" +
// " <span>CONTRACT INFORMATION</span>" +
//"</td>" +
"</tr>" +
"<table>" +
"<table style=\"margin: 0px auto; padding-top: 2%;\">" +
"<tr>" +
"<td style=\"border: 1px solid black;\">" +
" <span>Name</span>" +
"</td>" +
"<td style=\"border: 1px solid black; width: 84%;font-size: 110%;\">" +
txtName.Text +
"</td>" +
"<tr>" +
"<tr>" +
"<td style=\"border: 1px solid black;\">" +
" <span>Address</span>" +
"</td>" +
"<td style=\"border: 1px solid black; width: 84%;font-size: 110%;\">" +
txtAddress.Text +
"</td>" +
"<tr>" +
"<tr>" +
"<td style=\"border: 1px solid black;\">" +
" <span>City</span>" +
"</td>" +
"<td style=\"border: 1px solid black; width: 84%;font-size: 110%;\">" +
txtCity.Text +
"</td>" +
"<tr>" +
"<tr>" +
"<td style=\"border: 1px solid black;\">" +
" <span>State</span>" +
"</td>" +
"<td style=\"border: 1px solid black; width: 84%;font-size: 110%;\">" +
txtState.Text +
"</td>" +
"<tr>" +
"<tr>" +
"<td style=\"border: 1px solid black;\">" +
" <span>Zip</span>" +
"</td>" +
"<td style=\"border: 1px solid black; width: 84%;font-size: 110%;\">" +
txtZip.Text +
"</td>" +
"<tr>" +
"<tr>" +
"<td style=\"border: 1px solid black;\">" +
" <span>Phone No.</span>" +
"</td>" +
"<td style=\"border: 1px solid black; width: 84%;font-size: 110%;\">" +
txtPhone.Text +
"</td>" +
"<tr>" +
"</table>" +
//" <p>" +
//" <span class=\"title\">{0}</span>" +
//" <span class=\"author\">{1}</span>" +
//" </p>" +
"</body>" +
"</html>" +
Environment.NewLine;
System.IO.File.AppendAllText(path,HTML);
MessageBox.Show("Document created successfully !");
}
答案 0 :(得分:1)
这应该这样做。
private void PrintDocument(Uri uri)
{
var wb = new System.Windows.Forms.WebBrowser();
wb.DocumentCompleted += PageLoaded;
wb.Url = uri;
}
private void PageLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
((System.Windows.Forms.WebBrowser)sender).Print();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var url = new Uri(@"C:\Users\EACD-017\Documents\Host.html");
PrintDocument(url);
}
见这里:https://msdn.microsoft.com/en-us/library/b0wes9a3(v=vs.110).aspx
答案 1 :(得分:0)
以下代码应以给定文件作为参数打开IEXPLORE.exe。
Process process = new Process();
process.StartInfo.FileName = "iexplore.exe";
process.StartInfo.Verb = "runas";
process.StartInfo.Arguments = "PATH/TO/YOUR/FILE.HTML";
process.Start();
但首先,在HTML内容中,您应该添加以下代码以在启动时打印:
<script type="text/javascript">
window.onload = function () {
window.print();
}
</script>