Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用列表时如何循环HTML打印?_C#_Html_Printing - Fatal编程技术网

C# 使用列表时如何循环HTML打印?

C# 使用列表时如何循环HTML打印?,c#,html,printing,C#,Html,Printing,我有一个列表,其中包含指向我电脑上html文件的路径。我想循环浏览这个列表,并按列表中的顺序将它们全部打印出来 我尝试循环在msdn.microsoft.com上找到的用于打印HTML文件的代码 List<string> AllHTMLsToPrint = new List<string>(); //things added to AllHTMLsToPrint list foreach (string strHTMLToPrint in AllHTMLsToPrin

我有一个列表,其中包含指向我电脑上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();
}
List AllHTMLsToPrint=new List();
//添加到AllHTMLsToPrint列表的内容
foreach(字符串strHTMLToPrint在AllHTMLsToPrint中)
{
打印帮助页(STRHTMLOTPRINT);
}
私有void打印帮助页(字符串strHTMLToPrint)
{
//创建WebBrowser实例。
WebBrowser webBrowserForPrinting=新WebBrowser();
//添加一个事件处理程序,在加载文档后打印文档。
webBrowserForPrinting.DocumentCompleted+=
新的WebBrowserDocumentCompletedEventHandler(PrintDocument);
//设置Url属性以加载文档。
webBrowserForPrinting.Url=新Uri(strHTMLToPrint);
睡眠(100);
}
私有void打印文档(对象发送者,WebBrowserDocumentCompletedEventArgs e)
{
//现在文档已完全加载,请打印文档。
((WebBrowser)发送方).Print();
//任务完成后,立即处置WebBrowser。
((WebBrowser)发送器).Dispose();
}

这里有一个设计问题。您可以遍历要打印的html页面列表。然后在浏览器中打开页面。加载页面后,您可以打印它

但是

加载页面可能需要超过100ms的时间。这是浏览器加载下一页的时间。您应该更改代码,以便在打印当前页面后加载下一页。在这种情况下,您可能不希望使用循环,而是希望在打印后增加索引

应与此类似(未测试):

List AllHTMLsToPrint=new List();
私有整数指数=0;
打印帮助页(所有HTMLSTOPRINT[索引]);
私有void打印帮助页(字符串strHTMLToPrint)
{
//创建WebBrowser实例。
WebBrowser webBrowserForPrinting=新WebBrowser();
//添加一个事件处理程序,在加载文档后打印文档。
webBrowserForPrinting.DocumentCompleted+=
新的WebBrowserDocumentCompletedEventHandler(PrintDocument);
//设置Url属性以加载文档。
webBrowserForPrinting.Url=新Uri(strHTMLToPrint);
}
私有void打印文档(对象发送者,WebBrowserDocumentCompletedEventArgs e)
{
//现在文档已完全加载,请打印文档。
((WebBrowser)发送方).Print();
if(索引
您已经声明您有一堆本地html文件。 设置URI可能无法加载本地html文件。 您可以尝试设置DocumentStream。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);
    }

}
公共部分类表单1:表单
{
内部列表allhtmlstoprit=新列表();
公共表格1()
{
初始化组件();
}
公共无效开始打印()
{
//添加到AllHtmlStorPrint列表的内容,请注意,如果是本地文件,可能需要将file:///添加到URI列表,除非它是compact framework
//开始打印第一项
BackgroundWorker bgw=新的BackgroundWorker();
bgw.DoWork+=bgw_DoWork;
bgw.RunWorkerAsync();
/*foreach(字符串strHTMLToPrint在AllHTMLsToPrint中)
{
打印帮助页(STRHTMLOTPRINT);
}*/
}
无效bgw_DoWork(对象发送方,DoWorkEventArgs e)
{
PrintHelpPage(AllHtmlStorPrint[0],(BackgroundWorker)发件人);
}
私有void打印帮助页(字符串strHTMLToPrint,BackgroundWorker bgw)
{
//创建WebBrowser实例。
WebBrowser webBrowserForPrinting=新WebBrowser();
//添加一个事件处理程序,在加载文档后打印文档。
webBrowserForPrinting.DocumentCompleted+=(s,ev)=>{
webBrowserForPrinting.Print();
webBrowserForPrinting.Dispose();
//您可以在此处添加进度报告
//删除第一个元素,然后看看是否需要再次执行所有操作
所有HTMLSTOPRINT.RemoveAt(0);
如果(所有HTMLSTOPRINT.Count>0)
打印帮助页(所有HTMLSTOPRINT[0],bgw);
};
//设置Url属性以加载文档。
webBrowserForPrinting.Url=新Uri(strHTMLToPrint);
}
}

“我在打印HTML文件时遇到问题。”您忘了告诉我们是什么问题。
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);
    }

}