从 webbrowser 控件打印的 API 在技术上可以支持这一点,如果你能弄清楚 C# 的语法来做到这一点。Microsoft 有一篇知识库文章“如何在 Internet Explorer 中为 WebBrowser 控件打印自定义页眉和页脚”
在那篇文章中,阐明了可以将 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);