본문 바로가기
C# /WindowsForm

[C#] WebBrowser Control 로 인쇄 시에 Footer ,Header 제거 테스트

by Hwoarang757 2022. 4. 13.

출처 : Need advice on removing header/footer during HTML printing | Visual Studio.Net Help Community (dotnet.community)

 

Need advice on removing header/footer during HTML printing

I have the IE web control in my app - and Im using the EXECWB command to print the current page. However, Im trying to remove the header / footer that is automatically placed on the page. The only real way Ive found to do this so far is by modifying the re

dotnet.community

 

// Print 전에 Internet Explorer Registry 설정 진행 하였습니다. ( HKCU )

RegistryKey IERegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup",true);

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SHDocVw;
using Microsoft.Win32;

namespace WebBrowserPrintNET
{
    public partial class FrmWebBrowser : Form
    {
        public FrmWebBrowser()
        {
            InitializeComponent();
        }

        private void FrmWebBrowser_Load(object sender, EventArgs e)
        {

        }

        private void FrmWebBrowser_Shown(object sender, EventArgs e)
        {
            try {
                // 스크립트 에러
                webBrowser.ScriptErrorsSuppressed = true;
                // Navigate 한다.
                webBrowser.Navigate(Environment.GetCommandLineArgs()[1]);
                
            } catch {
                Environment.Exit(Environment.ExitCode);
            }
        }

        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                // Print 전에 Internet Explorer Registry 설정 진행 
                RegistryKey IERegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup",true);

                IERegKey.SetValue("footer", "");
                IERegKey.SetValue("header", "");


                var iwb2 = (IWebBrowser2)webBrowser.ActiveXInstance;
                var events = (DWebBrowserEvents2_Event)webBrowser.ActiveXInstance;
                events.PrintTemplateTeardown += new DWebBrowserEvents2_PrintTemplateTeardownEventHandler(events_PrintTemplateTeardown);
                var missing = Type.Missing;

                iwb2.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);

            }
            catch
            {
                Environment.Exit(Environment.ExitCode);
            }
        }

        void events_PrintTemplateTeardown(object pDisp)
        {
            try
            {
                // Print 전에 Internet Explorer Registry 설정 진행 
                RegistryKey IERegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", true);

                IERegKey.DeleteValue("footer");
                IERegKey.DeleteValue("header");
                
            }
            catch
            {

            }
            finally
            {
                Environment.Exit(Environment.ExitCode);
            }
        }


    }
}