想在Wpf c#中打印html文件

本文关键字:html 文件 打印 Wpf 想在 | 更新日期: 2023-09-27 18:17:50

我已经在这里粘贴了代码,我已经用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 !");
    }

想在Wpf c#中打印html文件

应该可以了。

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

下面的代码应该以给定的文件作为参数打开iexplorer .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>