printing - 如何将 GeckoWebBrowser 打印到默认打印机?

标签 printing geckofx

我正在尝试在 GeckoWebBrowser 中打印文档,但文档有限,对我来说,它根本不清楚。

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

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

代码如下:

        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 (上面的属性并不是唯一一个硬编码抛出异常),因为它还没有完成(?),所以我不能使用它。

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

那么我能做的是:

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

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() 不会产生任何影响。

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

关于printing - 如何将 GeckoWebBrowser 打印到默认打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45433937/

相关文章:

java - Java中逐行写入Json对象数组

html - 内部分页 :avoid equivalent for Firefox and/or IE

matlab - 如何打印多个开放图形,如何简化将数据导入 MATLAB

c# - Geckofx C# 阻止特定域

java - 为 DOM 元素 HTML 更新添加监听器。壁虎、C#.Net、Java

C# Geckofx 浏览器图像下载

css - 为什么在打印模式下我会在 SVG 图标附近看到额外的线条?

java - 对齐二维数组打印输出 [Java]

C# GeckoFX 浏览器 - 如何单击/将值设置为 XPath?

javascript - 使用 javascript 在 vb.net 中嵌入带有 geckofx 的 youtube 视频