WebBrowser打印输出中缺少字符

时间:2015-12-17 10:26:09

标签: c# html wpf printing

我有一个WPF应用程序,它使用WebBrowser控件来打印html个文档。
我的问题是,从时间生成的文档中缺少一些字符 我尝试更换打印机和电缆,并且还验证了html标记here 这就是我生成打印的方式:

public static class HtmlPrint
{
    public static void Print(string html, string documentTitle)
    {
        var wb = new System.Windows.Forms.WebBrowser { DocumentText = html };
        wb.DocumentCompleted += wb_DocumentCompleted;
    }

    private static void wb_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
    {
        var wb = ((System.Windows.Forms.WebBrowser)sender);

        wb.Print();
        wb.Dispose();
    }
}

HTML示例:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Items report</title>
<style type='text/css'>
    html, body {
        margin: 0;
        padding: 0;
        border: 0;
        width: 100%;
    }

    table.gridtable {
        font-family: verdana,arial,sans-serif;
        font-size: 11px;
        color: #333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
        margin-top: 10px;
    }

        table.gridtable th {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #dedede;
        }

        table.gridtable td {
            border-width: 1px;
            padding: 8px;
            border-style: solid;
            border-color: #666666;
            background-color: #ffffff;
            /*white-space:nowrap*/
        }
</style>
</head>
<body dir="rtl">
<h3>15/12/2015 09:22:17</h3>
<h3>Items for station: 1</h3>
<br />
<table class="gridtable">
    <tr>
        <th>Location</th>
        <th>Item</th>
        <th>Description</th>
        <th>Color</th>
        <th>Size</th>
        <th>Qty</th>
        <th>Remarks</th>
    </tr>
    <tr>
        <td>AD393</td>
        <td>06957TO_GRE_S</td>
        <td>Green shirt</td>
        <td>GREEN-GRAY</td>
        <td>S(36)</td>
        <td>0</td>
        <td>AD-37-5<br />AD-38-3<br />AD-38-5<br />AD-39-0<br />AD-39-3<br />AD-39-5<br />BC-38-1</td>
    </tr>

    <tr>
        <td>CA183</td>
        <td>A100000000464</td>
        <td>White golf shirt</td>
        <td>WHITE</td>
        <td>XS/S (34-36)</td>
        <td>0</td>
        <td>AI-25-1<br />AK-25-1<br />AK-36-6<br />AK-37-6<br />AL-24-1<br />AL-25-1<br />CA-18-3</td>
    </tr>
</table>

关于如何解决它的任何想法?
更新
我尝试使用System.Windows.Controls.WebBrowser,如下所示,但它会打印空白页:

public static void Print(string html)
    {
        /*
        html is the actual HTML data e.g. 
        <!DOCTYPE html>
            <html lang="he-IL" xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta charset="utf-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=9">
            ...
        */
        try
        {
            var wb = new System.Windows.Controls.WebBrowser();
            wb.LoadCompleted += Wb_LoadCompleted; //never called
            wb.Loaded += Wb_Loaded; //never called

            wb.NavigateToString(html);
            mshtml.IHTMLDocument2 doc = wb.Document as mshtml.IHTMLDocument2;
            doc.execCommand("Print", true, false);
        }
        catch (Exception ex)
        {
            //no exception raised
            throw;
        }
    }

0 个答案:

没有答案