John Conor
John Conor

Reputation: 71

How to print in C# html document in ONE page

How to print in C# html document in ONE page on windows printer? I need to print bill with different length and it must be at one page. But in my example with many amount of strings in bill it is separate to few pages.

I was use this example of code for printing html:

private void PrintHelpPage()
{
    // Create a WebBrowser instance. 
    WebBrowser webBrowserForPrinting = new WebBrowser();

    // Add an event handler that prints the document after it loads.
    webBrowserForPrinting.DocumentCompleted +=
        new WebBrowserDocumentCompletedEventHandler(PrintDocument);

    // Set the Url property to load the document.
    webBrowserForPrinting.Url = new Uri(@"\\myshare\bill.html");
}

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

I know how to read hight of my page from WebBrowser control wb.Document.Body.ScrollRectangle.Height

And try to change dimensions of page by few different ways:

PrintDocument pd = new PrintDocument();
PaperSize pkCustomSize1 = new PaperSize("First custom size", 310, 250);

pd.DefaultPageSettings.PaperSize = pkCustomSize1;
System.Drawing.Printing.PaperSize("Custom Paper Size", 250, 550);

pd.SDefaultPageSettings.PaperSize.Kind = PaperKind.Custom;
pd.DefaultPageSettings.PaperSize.Height = 1633;

But it still separate to few pages while printing. Thanks

Upvotes: 1

Views: 1204

Answers (1)

John Conor
John Conor

Reputation: 71

I was found solution here https://gist.github.com/huanlin/5671168 Helper classes for change printer settings with P/Invoke.

Upvotes: 1

Related Questions