4

I'm using the WebBrowser class to open document, change values, save and print. The problem is, that it prints the document including the header("Page 1 of 1") and footer(root of the document + date)

I looked at the documentation and didn't find a way to remove them. Is it even possible using WebBrowser or should I look for alternatives?

CatWithGlasses
  • 1,493
  • 2
  • 18
  • 21
  • It's definitely not possible in just .NET. Theoretically this can be done by accessing the underlying native component using the WebBrowser's ActiveXInstance property but I seem to be having the damnedest time getting it to work right now. – j__m Mar 25 '13 at 10:36
  • Possible duplicate of http://stackoverflow.com/q/7460761/161052 – JYelton May 15 '14 at 17:28

3 Answers3

3

There is a solution, probably not as cleaner as it could have been. Since WebBrowser inhertis it's settings from Internet Explorer it is possible to change the values in the registry. Luckily the values are under HKCU so no administration permissions are needed.

Take a look at https://stackoverflow.com/a/1321314/1630928

Community
  • 1
  • 1
coolmine
  • 4,427
  • 2
  • 33
  • 45
1

The trick to doing this is to pass a Variant containing a ByRef SafeArray of Variants to the WebBrowser control. I haven't figured out how to do it from C#. Here's someone else who was working on the same problem who resorted to using managed C++

http://www.limilabs.com/blog/printing-in-webbrowser-control-custom-header-and-footer

j__m
  • 9,392
  • 1
  • 32
  • 56
  • http://support.microsoft.com/kb/267240 explains similarly what goes in the variant safearray, but would need to be translated from C++ to C# syntax – Jessica Brown Aug 07 '13 at 23:21
0

Not a C#, but here's C++ code that I came up with based on a now defunct KB267240. It will remove the header and the footer while printing:

BOOL bRes = FALSE;

//Get IWebBrowser2 from your IE control
CComPtr<IWebBrowser2> pWebBrowser = this->GetIWebBrowser2();
if(pWebBrowser)
{
    HRESULT hr;
    COleVariant varNull;

    SAFEARRAYBOUND psabBounds[1];
    SAFEARRAY *psaHeadFoot;
    hr = S_OK;

    VARIANT vArg;
    BOOL bGot_vArg = FALSE;

    VARIANT vHeadStr, vFootStr;
    long rgIndices;
    VariantInit(&vHeadStr);
    VariantInit(&vFootStr);

    // Initialize header and footer parameters to send to ExecWB().
    psabBounds[0].lLbound = 0;
    psabBounds[0].cElements = 3;
    psaHeadFoot = SafeArrayCreate(VT_VARIANT, 1, psabBounds);
    if(psaHeadFoot)
    {
        // Argument 1: Header
        vHeadStr.vt = VT_BSTR;
        vHeadStr.bstrVal = SysAllocString(L" ");    //Must be at least one space
        if (vHeadStr.bstrVal)
        {
            // Argument 2: Footer
            vFootStr.vt = VT_BSTR;
            vFootStr.bstrVal = SysAllocString(L" ");    //Must be at least one space
            if(vFootStr.bstrVal)
            {
                rgIndices = 0;
                SafeArrayPutElement(psaHeadFoot, &rgIndices, static_cast<void *>(&vHeadStr));
                rgIndices = 1;
                SafeArrayPutElement(psaHeadFoot, &rgIndices, static_cast<void *>(&vFootStr));
                rgIndices = 2;
                SafeArrayPutElement(psaHeadFoot, &rgIndices, static_cast<void *>(&varNull));    //Set stream to NULL as we don't need it

                //NOTE: Currently, the SAFEARRAY variant must be passed by using
                // the VT_BYREF vartype when you call the ExecWeb method.
                VariantInit(&vArg);
                vArg.vt = VT_ARRAY | VT_BYREF;
                vArg.parray = psaHeadFoot;

                //Got it
                bGot_vArg = TRUE;
            }
        }
    }


    //Did we get all the vars?
    if(bGot_vArg)
    {
        if(SUCCEEDED(hr = pWebBrowser->ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, &vArg, NULL)))
        {
            //All good
            bRes = TRUE;
        }
    }
    else
    {
        //Use fallback (that will keep the footer & header)
        if(SUCCEEDED(hr = pWebBrowser->ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_PROMPTUSER, varNull, varNull)))
        {
            //Printed via fallback
            bRes = TRUE;
        }
    }

    //Clean up
    VariantClear(&vHeadStr);
    VariantClear(&vFootStr);
    if(psaHeadFoot)
    {
        SafeArrayDestroy(psaHeadFoot);
        psaHeadFoot = NULL;
    }
}
c00000fd
  • 20,994
  • 29
  • 177
  • 400