如何在使用列表时循环 HTML 打印
本文关键字:循环 HTML 打印 列表 | 更新日期: 2023-09-27 18:31:31
>我有一个列表,其中包含我电脑上html文件的路径。我想循环浏览此列表并按照它们在列表中的相同顺序打印它们。
我试图循环我在 msdn.microsoft.com 上找到的代码来打印HTML文件。
List<string> AllHTMLsToPrint = new List<string>();
//things added to AllHTMLsToPrint list
foreach (string strHTMLToPrint in AllHTMLsToPrint)
{
PrintHelpPage(strHTMLToPrint);
}
private void PrintHelpPage(string strHTMLToPrint)
{
// 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(strHTMLToPrint);
Thread.Sleep(100);
}
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();
}
你这里有一个设计问题。您浏览要打印的 html 页面列表。然后在浏览器中打开该页面。加载页面后,您可以打印它。
但。。。
加载页面可能需要超过 100 毫秒的时间。这是浏览器加载下一页的时间。您应该更改代码,以便在打印当前页面后加载下一页。在这种情况下,您可能不想使用循环,而是要在打印后增加索引。
应该看起来与此类似(未测试):
List<string> AllHTMLsToPrint = new List<string>();
private int index = 0;
PrintHelpPage(AllHTMLsToPrint[index]);
private void PrintHelpPage(string strHTMLToPrint)
{
// 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(strHTMLToPrint);
}
private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Print the document now that it is fully loaded.
((WebBrowser)sender).Print();
if (index < AllHTMLsToPrint.Count -1)
PrintHelpPage(AllHTMLsToPrint[++index]);
}
你已经说过你有一堆本地html文件。通过设置 URI,加载本地 html 文件可能无法正常工作。您可以尝试设置文档流。strHTMLToPrint 必须包含本地 html 文件的完整/相对路径。
webBrowserForPrinting.DocumentStream = File.OpenRead(strHTMLToPrint);
不确定确切的问题是什么,但我会把它放到后台工作线程中,这样你就不会阻止主线程。 我还会将循环移动到文档加载系统中,这样一旦它加载并打印出来,它就会移动到下一个循环。
也就是说,你还没有说你的代码没有做什么。
public partial class Form1 : Form
{
internal List<string> AllHTMLsToPrint = new List<string>();
public Form1()
{
InitializeComponent();
}
public void StartPrinting()
{
//things added to AllHTMLsToPrint list, please note you may need to add file:/// to the URI list if it is a local file, unless it is compact framework
// start printing the first item
BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += bgw_DoWork;
bgw.RunWorkerAsync();
/*foreach (string strHTMLToPrint in AllHTMLsToPrint)
{
PrintHelpPage(strHTMLToPrint);
}*/
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
PrintHelpPage(AllHTMLsToPrint[0], (BackgroundWorker)sender);
}
private void PrintHelpPage(string strHTMLToPrint, BackgroundWorker bgw)
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();
// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted += (s, ev) => {
webBrowserForPrinting.Print();
webBrowserForPrinting.Dispose();
// you can add progress reporting here
// remove the first element and see if we have to do it all again
AllHTMLsToPrint.RemoveAt(0);
if (AllHTMLsToPrint.Count > 0)
PrintHelpPage(AllHTMLsToPrint[0], bgw);
};
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(strHTMLToPrint);
}
}