Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

c# - How to set Paper Size and Margins printing from a web browser control

I am trying to print from a web browser control in a winform application.The matter is it sets letter as default paper size but I need A4. Also it automatically sets some margins wrong, I can set them to correct settings manually but I want to do it programmatically.

How is it possible?

Here is my code to print.

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();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To change the Margin size you have to edit the (HKCU) registry before printing:

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

Dont forget to set it back to the default.

Ref Microsoft KB Article


To change the Paper size you have to edit the (HKCU) registry in another place before printing:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...