我試圖在GeckoWebBrowser中打印文檔,但文檔是有限的,對我來說,它並不是一目瞭然。如何將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);
你有沒有設法直接打印到打印機? – Dohab
是的,我只是將SetOutPutFormatAttribute交換爲適當的格式。不需要使用SetToFileNameAttribute。它可以即時打印,無需任何其他用戶輸入,這正是我所需要的。 –