簡體   English   中英

如何從 Web 瀏覽器控件設置紙張尺寸和邊距打印

[英]How to set Paper Size and Margins printing from a web browser control

我正在嘗試從 winform 應用程序中的 Web 瀏覽器控件進行打印。問題是它將letter設置為默認紙張大小,但我需要A4 它還會自動設置一些邊距錯誤,我可以手動將它們設置為更正設置,但我想以編程方式進行。

這怎么可能?

這是我要打印的代碼。

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();
    }

要更改邊距大小,您必須在打印前編輯 (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);
}

不要忘記將其設置回默認值。

參考 Microsoft 知識庫文章


要更改紙張尺寸,您必須在打印前在其他地方編輯 (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);

參考 MSDN

好吧,我已經嘗試了很多東西,但最后我發現無法輕松地從代碼中對打印機設置進行編程。 但我可以通過@jeremy 的回答來做保證金。 我發現對於從 WebBrowser 控件進行打印,它使用了我們所知道的 Internet Explorer,但一開始它使用的是資源管理器 7,我不得不將其更改為默認的資源管理器 11。 然后我看到資源管理器沒有自己的打印設置。 它使用默認打印機設置。 因此,您必須更改默認打印機預覽。您將看到預覽會以這種方式顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM