如何将 GeckoWebBrowser 打印到默认打印机? [英] How to print GeckoWebBrowser to default printer?

查看:83
本文介绍了如何将 GeckoWebBrowser 打印到默认打印机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 GeckoWebBrowser 中打印文档,但文档有限,对我来说,它一点也不清晰.

I'm trying to print the document in a GeckoWebBrowser, but documentation is limited and to me, it's not at all clear.

我在互联网上找到了一些至少与打印机通信的代码(它开始发出哔哔声),但我认为打印机要求使用 Letter 尺寸的纸张,但它要求设置来自 print.GetGlobalPrintSettingsAttribute(),如果我尝试自己的设置,它会给我一个 NotImplementedException.

I found some code on the internet that at least communicates with the printer (it starts beeping) but I think the printer is asking for a Letter size paper, but it requires the settings to be from print.GetGlobalPrintSettingsAttribute(), if I try my own settings, it gives me a NotImplementedException.

我怀疑这是在我的 Gecko.PrinterSettings 上引发的异常,因为当我在 print.Print(ps, null); 中交换 ps使用全局设置,不会引发此异常.

I suspect this is exception is raised on my Gecko.PrinterSettings, because when I swap ps in the print.Print(ps, null); with the global settings, this exception isn't raised.

代码如下:

        var domWindow = browser.Window.DomWindow;
        var print = Gecko.Xpcom.QueryInterface<Gecko.nsIWebBrowserPrint>(domWindow);

        Gecko.PrintSettings ps = new Gecko.PrintSettings();
        ps.SetPrintSilentAttribute(false);
        ps.SetPrintToFileAttribute(false);
        ps.SetShowPrintProgressAttribute(false);
        ps.SetOutputFormatAttribute(1); //2 == PDF, so I assume 1 is actual printer
        ps.SetPrintBGImagesAttribute(true);
        ps.SetStartPageRangeAttribute(1);
        ps.SetEndPageRangeAttribute(100);
        ps.SetPrintOptions(2, true); // evenPages
        ps.SetPrintOptions(1, true); // oddpages
        ps.SetEffectivePageSize(768 * 20f, 1024 * 20f);
        ps.SetShrinkToFitAttribute(true);
        ps.SetScalingAttribute(1.0);
        ps.SetPrintBGImagesAttribute(true);

        print.Print(ps, null);

推荐答案

设法提出了解决方案.

引发异常的是

public void SetPersistMarginBoxSettingsAttribute(bool aPersistMarginBoxSettings)
{
    throw new NotImplementedException();
}

以上是在 PrinterSettings.cs 中,因此它是硬编码的以在许多关闭属性上抛出 NotImplementedException(上面的属性不是唯一一个硬编码的抛出异常),因为它还没有完成(?),所以我不能使用它.

The above is in PrinterSettings.cs, so it is hard-coded coded to throw a NotImplementedException on a number off attributes (the attribute above isn't the only one hard-coded to throw the exception) as it is not finished(?), so I cannot use it.

但是,我可以使用 GetGlobalSettingsAttribute(),因为它使用与 PrinterSettings (nsiPrintSettings) 相同的接口,因此它将为我填充相同的属性.

However, I can use the GetGlobalSettingsAttribute() as it uses the same interface as PrinterSettings (nsiPrintSettings), so therefore it will have the same attributes all populated for me.

那么我能做的是:

我只是将 GetGlobalPrintSettingsAttribute() 复制到我自己的打印机设置中,并根据需要进行调整.

I simply copy the GetGlobalPrintSettingsAttribute() into my own printer settings, and adjust them as necessary.

var mySettings = print.GetGlobalPrintSettingsAttribute();
mySettings.SetPrintSilentAttribute(true);
mySettings.SetPrintToFileAttribute(true);
mySettings.SetShowPrintProgressAttribute(false);
mySettings.SetOutputFormatAttribute(2); //2 == PDF
mySettings.SetToFileNameAttribute(@"c:\temp\temp.pdf");
mySettings.SetPrintBGImagesAttribute(true);
mySettings.SetStartPageRangeAttribute(1);
mySettings.SetEndPageRangeAttribute(100);
mySettings.SetPrintOptions(2, true); // evenPages
mySettings.SetPrintOptions(1, true); // oddpages
mySettings.SetShrinkToFitAttribute(true);
mySettings.SetScalingAttribute(1.0);
mySettings.SetPrintBGImagesAttribute(true);

print.Print(mySettings, new Gecko.WebProgressListener());

  • 请注意,我现在在 SetOutputFormatAttribute(2); 中恢复为 PDF.//2 == PDF

    还将 print.Print(ps, null); 更改为 print.Print(mySettings, new Gecko.WebProgressListener()); 但我认为nullGecko.WebProgressListener() 不会有什么不同.

    Also changed the print.Print(ps, null); to print.Print(mySettings, new Gecko.WebProgressListener()); but I think having null or Gecko.WebProgressListener() won't make a difference.

    等等!- 现在,进入下一步,即打印到打印机,而不是 PDF 文件.

    这篇关于如何将 GeckoWebBrowser 打印到默认打印机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆