如何从Web浏览器控制

如何从Web浏览器控制

问题描述:

我试图从一个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 –

嗯,我试了很多但最终我发现无法轻松地从代码中编写打印机设置。但是我可以通过@jeremy的回答来做边界。 我发现,从WebBrowser控件打印它使用Internet Explorer所有我们知道,但在一开始它使用的浏览器7,我不得不改变它作为默认的浏览器11。 然后我看到它的资源管理器没有他自己的打印设置。它使用默认的打印机设置。 因此,您必须更改默认打印机预览。您将看到预览将以此方式显示。

要更改打印前编辑(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设置,并做了他们所说的。 –