【问题标题】:How to print in C# html document in ONE page如何在一页中打印 C# html 文档
【发布时间】:2015-07-03 10:52:11
【问题描述】:

如何在 windows 打印机的一页中打印 C# html 文档? 我需要打印不同长度的账单,并且必须在一页上。 但在我的例子中,账单中有很多字符串,它是分开的几页。

我正在使用这个代码示例来打印 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();
}

我知道如何从 WebBrowser 控件中读取页面的高度 wb.Document.Body.ScrollRectangle.Height

并尝试通过几种不同的方式更改页面尺寸:

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;

但在打印时它仍然分离为几页。 谢谢

【问题讨论】:

  • 我在卷筒纸上打印,所以页面长度是可变的。

标签: c# html printing


【解决方案1】:

我在这里找到了解决方案https://gist.github.com/huanlin/5671168 使用 P/Invoke 更改打印机设置的帮助类。

【讨论】:

    猜你喜欢
    • 2015-08-07
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    相关资源
    最近更新 更多