2017-08-01 77 views
2

我試圖在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); 

回答

0

託管拿出的溶液。

什麼是拋出一個異常,是

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()); 
  • 請注意我恢復到PDF現在,在SetOutputFormatAttribute(2); //2 == PDF

  • 也改變了print.Print(ps, null);print.Print(mySettings, new Gecko.WebProgressListener());,但我認爲有nullGecko.WebProgressListener()不會有所作爲。

的Et瞧! - 現在進入下一步,即打印到打印機,而不是PDF文件。

+0

你有沒有設法直接打印到打印機? – Dohab

+0

是的,我只是將SetOutPutFormatAttribute交換爲適當的格式。不需要使用SetToFileNameAttribute。它可以即時打印,無需任何其他用戶輸入,這正是我所需要的。 –

相關問題