gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:58:31 24 4
gpt4 key购买 nike

背景信息: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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com