GeckoWebBrowser 实现网页打印 html打印 去掉页眉页脚 页头页尾

管理GeckoFx

PM> Install-Package Geckofx45 -Version 45.0.34

 

GeckoWebBrowser geckoWebBrowser;  
  
public Form1()  
{  
    InitializeComponent();  
  
    Xpcom.Initialize("Firefox");  
    geckoWebBrowser = new GeckoWebBrowser { Dock = DockStyle.Fill };  
    this.Controls.Add(geckoWebBrowser);  
    geckoWebBrowser.Navigate("www.baidu.com");  
}  
nsIWebBrowserPrint print = Xpcom.QueryInterface<nsIWebBrowserPrint>(geckoWebBrowser.Window.DomWindow);  
            var service = Xpcom.GetService<nsIPrintSettingsService>("@mozilla.org/gfx/printsettings-service;1");  
            var ps = service.GetNewPrintSettingsAttribute();  
  
  
            ps.SetTitleAttribute(SystemSetting.GetInstance().SystemName);  
  
            ps.SetEdgeBottomAttribute(0);  
            ps.SetEdgeTopAttribute(0);  
            ps.SetEdgeLeftAttribute(0);  
            ps.SetEdgeRightAttribute(0);  
  
            ps.SetMarginBottomAttribute(0);  
            ps.SetMarginTopAttribute(0);  
            ps.SetMarginLeftAttribute(0);  
            ps.SetMarginRightAttribute(0);  
  
  
            ps.SetShrinkToFitAttribute(true);  
            ps.SetPrintSilentAttribute(true);//是否发声  
            ps.SetPrintReversedAttribute(true);  
//去掉页眉页脚
                    ps.SetFooterStrCenterAttribute("");
                    ps.SetFooterStrLeftAttribute("");
                    ps.SetFooterStrRightAttribute("");
                    ps.SetHeaderStrCenterAttribute("");
                    ps.SetHeaderStrLeftAttribute("");
                    ps.SetHeaderStrRightAttribute("");
  
            ps.SetOrientationAttribute(1);//横向打印  
  
            try  
            {  
                print.Print(ps, new WebProgressListener());  
            }  
            catch (Exception ex)  
            {  
                MessageBox.Show("打印失败"+ex.Message);  
            }  

 

posted @ 2019-09-07 14:04  ToLing·  阅读(881)  评论(0编辑  收藏  举报