2017-10-19 199 views
8

我試圖從一個winform的web瀏覽器控制application.The無論是它設置默認紙張大小,但我需要A4打印設置紙張尺寸和無邊距打印。另外它會自動設置一些頁邊距錯誤,我可以將它們設置爲手動更正設置,但我想以編程方式進行設置。如何從Web瀏覽器控制

這怎麼可能?

這是我的打印代碼。

private void metroButton1_Click(object sender, EventArgs e) 
    { 
     loadprintData(); 
     // Create a WebBrowser instance. 
     WebBrowser webBrowserForPrinting = new WebBrowser(); 

     // Add an event handler that prints the document after it loads. 
     wa.DocumentCompleted += 
      new WebBrowserDocumentCompletedEventHandler(ShowPrintDocument); 
     wa.ShowPrintPreviewDialog(); 
     reloadpage(); 

    } 
    private void ShowPrintDocument(object sender,WebBrowserDocumentCompletedEventArgs e) 
    { 
     // Print the document now that it is fully loaded. 
     ((WebBrowser)sender).ShowPrintPreviewDialog(); 

     // Dispose the WebBrowser now that the task is complete. 
     // ((WebBrowser)sender).Dispose(); 
     reloadpage(); 
    } 
    private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     // Print the document now that it is fully loaded. 
     ((WebBrowser)sender).Print(); 

     // Dispose the WebBrowser now that the task is complete. 
     // ((WebBrowser)sender).Dispose(); 
    } 
+0

所有的答案在這裏:https://stackoverflow.com/questions/19098571/webbrowser-print-settings –

回答

0

嗯,我試了很多但最終我發現無法輕鬆地從代碼中編寫打印機設置。但是我可以通過@jeremy的回答來做邊界。 我發現,從WebBrowser控件打印它使用Internet Explorer所有我們知道,但在一開始它使用的瀏覽器7,我不得不改變它作爲默認的瀏覽器11。 然後我看到它的資源管理器沒有他自己的打印設置。它使用默認的打印機設置。 因此,您必須更改默認打印機預覽。您將看到預覽將以此方式顯示。

8

要更改打印前編輯(HKCU)註冊表中的保證金大小:

string pageSetupKey = "Software\\Microsoft\\Internet Explorer\\PageSetup"; 
bool isWritable = true; 

RegistryKey rKey = Registry.CurrentUser.OpenSubKey(pageSetupKey, isWritable); 

if (stringToPrint.Contains("something")) 
{ 
    rKey.SetValue("margin_bottom", 0.10); 
    rKey.SetValue("margin_top", 0.25); 
} 
else 
{ 
    //Reset old value 
    rKey.SetValue("margin_bottom", 0.75); 
    rKey.SetValue("margin_top", 0.75); 
} 

不要忘記把它設回默認值。

Ref Microsoft KB Article


更改紙張大小,你打印前編輯在另一個地方(HKCU)註冊表:

string pageSetupKey2 = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"; 
isWritable = true; 

rKey = Registry.CurrentUser.OpenSubKey(pageSetupKey2, isWritable); 

// Use 1 for Portrait and 2 for Landccape 
rKey.SetValue("PageOrientation", 2, RegistryValueKind.DWord); 
// Specifies paper size. Valid settings are 1=letter, 5=Legal, 9=A4, 13=B5.Default setting is 1. 
rKey.SetValue("PaperSize", 9, RegistryValueKind.DWord); 
// Specifies print quality 
rKey.SetValue("PrintQuality ", 1, RegistryValueKind.DWord); 

Ref MSDN

+0

非常感謝您的答覆。我認爲解決方案正在使用您的解決方案。但我想我錯過了一些東西。當我添加這些行,然後我添加我的wa.ShowPrintPreviewDialog();有一個打印空白打印預覽對話框出現爲空,之後出現預定義打印預覽並顯示上一個問題。我錯過了什麼? –

+0

你能檢查可能的原因嗎https://support.microsoft.com/en-au/help/973479/unable-to-print-or-view-the-print-preview-of-a-webpage-in- internet-exp –

+0

實際上,利潤率正如你所想的那樣工作得很好。但方向不工作和頁面大小。我檢查了我的IE設置,並做了他們所說的。 –