c# - 在不触及注册表的情况下将页眉和页脚信息添加到 WebBrowser 的打印输出

标签 c# html css windows browser

背景信息:C#、Visual Studio 2010、目标:Windows XP 及更高版本

我需要将分类信息添加到从 System.Windows.Forms.WebBrowser 打印的每个页面的顶部和底部目的。目前我们有一个加载到 WebBrowser 中的 HTML 文档。 , 然后用 WebBrowser.ShowPrintDialog() 打印功能。我需要以某种方式在分类类型居中打印的每个页面的顶部和底部添加横幅。

我看到有人提到了一种涉及修改注册表设置的方法,但在我的情况下这不是一个选项。我也尝试过使用以下 CSS 代码,但似乎 WebBrowser不适用于 position: fixed .

CSS:

@media screen
{
    div#ClassificationTop
    {
        display: none;
    }
    div#ClassificationBottom
    {
        display: none;
    }
}
@media print
{
    div#ClassificationTop
    {
        display: block;
        position: fixed;
        top: 0;
    }
    div#ClassificationBottom
    {
        display: block;
        position: fixed;
        bottom: 0;
    }
}

并且在 <body> :

<div id="ClassificationTop">UNCLASSIFIED
</div>
<div id="ClassificationBottom">UNCLASSIFIED
</div>

因此,由于这两种方法都不起作用(注册表解决方法或 CSS position: fixed ),有人知道我可以尝试的其他方法吗?

如果问题不清楚或需要更多信息,请告诉我。

最佳答案

从技术上讲,从 webbrowser 控件打印的 API 可以支持这一点,如果你能弄清楚 C# 的语法来做到这一点。 Microsoft 有一篇知识库文章“How to print custom headers and footers for a WebBrowser control in Internet Explorer

在那篇文章中,阐明了可以指定 ExecWB 方法的第三个参数以包含包含自定义页眉和页脚的 SAFEARRAY。

这里是从知识库文章中选择的代码示例部分,已转换为 C# 语法。它不是完全可用的代码,但它应该可以帮助您指明正确的方向:

    SAFEARRAYBOUND[] psabBounds = Arrays.InitializeWithDefaultInstances<SAFEARRAYBOUND>(1);
    SAFEARRAY psaHeadFoot;

    // Initialize header and footer parameters to send to ExecWB().
    psabBounds[0].lLbound = 0;
    psabBounds[0].cElements = 3;
    psaHeadFoot = SafeArrayCreate(VT_VARIANT, 1, psabBounds);

    VARIANT vHeadStr = new VARIANT();
    VARIANT vFootStr = new VARIANT();
    VARIANT vHeadTxtStream = new VARIANT();
    int rgIndices;

    VariantInit(vHeadStr);
    VariantInit(vFootStr);
    VariantInit(vHeadTxtStream);

    // Argument 1: Header
    vHeadStr.vt = VT_BSTR;
    vHeadStr.bstrVal = SysAllocString("This is my header string.");

    // Argument 2: Footer
    vFootStr.vt = VT_BSTR;
    vFootStr.bstrVal = SysAllocString("This is my footer string.");


    // Argument 3: IStream containing header text. Outlook and Outlook 
    // Express use this to print out the mail header.   
    if ((sMem = (string)CoTaskMemAlloc(512)) == null)
    {
        goto cleanup;
    }
    // We must pass in a full HTML file here, otherwise this 
         // becomes corrupted when we print.
    sMem = "<html><body><strong>Printed By:</strong> Custom WebBrowser Host 1.0<p></body></html>\0";

    rgIndices = 0;
    SafeArrayPutElement(psaHeadFoot, rgIndices, (object)(vHeadStr));
    rgIndices = 1;
    SafeArrayPutElement(psaHeadFoot, rgIndices, (object)(vFootStr));
    rgIndices = 2;
    SafeArrayPutElement(psaHeadFoot, rgIndices, (object)(vHeadTxtStream));

    //NOTE: Currently, the SAFEARRAY variant must be passed by using
            // the VT_BYREF vartype when you call the ExecWeb method.
    VARIANT vArg = new VARIANT();
    VariantInit(vArg);
    vArg.vt = VT_ARRAY | VT_BYREF;
    vArg.parray = psaHeadFoot;
    hr = webOC.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, vArg, null);

关于c# - 在不触及注册表的情况下将页眉和页脚信息添加到 WebBrowser 的打印输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10592027/

相关文章:

javascript - 链接单击更改按钮

javascript - 根据当前状态删除按钮

javascript - Jquery 窗帘下降效果

Javascript ES6 HTML5 Canvas 图像未绘制到背景 Canvas 或背景 Canvas 未显示

CSS 将登录按钮粘贴到顶部

c# - 我可以在 RESTful 服务中使用 TCP 吗?

c# - 如何为一种产品设置多个价格(nopcommerce)

javascript - 如何使用 HTML 输入来提供 JavaScript 类

c# - 使用表达式动态评估属性字符串

c# - 如何从 Fluent API 为外键设置 onUpdate?