2016-01-22 38 views
0

我有一個列表,其中包含我的電腦上html文件的路徑。我想循環瀏覽這個列表,並按照列表中的順序將它們全部打印出來。如何在使用列表時循環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(); 
} 
+3

「我在打印HTML文件時遇到了問題。」你忘了告訴我們這是什麼問題。 – spender

回答

0

這裏有一個設計問題。你走你的html頁面列表打印。然後你在瀏覽器中打開該頁面。當頁面加載時,您可以打印它。

但是...

加載頁面可能需要更長的時間超過100ms。這是瀏覽器加載下一頁後的時間。您應該更改您的代碼,以便在打印完當前頁面後加載下一頁。在這種情況下,您可能不想使用循環,但打印後可能需要增加一個索引。

應該類似於此(未測試):

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

你說,你有一堆的本地HTML文件。 通過設置URI,加載本地html文件可能不起作用。 您可以嘗試設置DocumentStream。然後,strHTMLToPrint必須包含本地html文件的完整/相對路徑。

webBrowserForPrinting.DocumentStream = File.OpenRead(strHTMLToPrint); 
0

不知道確切的問題是什麼,但我會把它放到後臺工作,所以你不佔用主線程。我也會將循環移動到文檔加載系統中,這種方式一旦加載並打印出來就會轉移到下一個。

這就是說你還沒有說你的代碼沒有做什麼。

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

}