如何将 GeckoWebBrowser 打印到默认打印机?
How to print GeckoWebBrowser to default printer?
我正在尝试在 GeckoWebBrowser 中打印文档,但文档有限,对我来说,一点也不清楚。
我在 Internet 上找到了一些至少可以与打印机通信的代码(它开始发出哔哔声),但我认为打印机需要 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) 相同的接口,因此它会为我填充相同的属性。
所以我能做的是:
我只是将 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); //2 == PDF
中暂时恢复为 PDF
也将 print.Print(ps, null);
更改为 print.Print(mySettings, new Gecko.WebProgressListener());
但我认为 null
或 Gecko.WebProgressListener()
不会有什么不同。
瞧瞧! - 现在,进入下一步,即打印到打印机,而不是 PDF 文件。
我正在尝试在 GeckoWebBrowser 中打印文档,但文档有限,对我来说,一点也不清楚。
我在 Internet 上找到了一些至少可以与打印机通信的代码(它开始发出哔哔声),但我认为打印机需要 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) 相同的接口,因此它会为我填充相同的属性。
所以我能做的是:
我只是将 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); //2 == PDF
中暂时恢复为 PDF
也将
print.Print(ps, null);
更改为print.Print(mySettings, new Gecko.WebProgressListener());
但我认为null
或Gecko.WebProgressListener()
不会有什么不同。
瞧瞧! - 现在,进入下一步,即打印到打印机,而不是 PDF 文件。