2013-02-28 125 views
2

我遇到了一個問題,我的.NET應用程序只打印了我的HTML文件的第二頁,並完全忽略了第一頁(沒有打印其他頁面,它是空白的)。使用WebBrowser控件的雙面打印只能打印1面

當我拉起打印機的隊列窗口時,它確實顯示它從「假脫機」轉到「打印」並列出了這兩個頁面,所以我不知道它爲什麼不打印第一頁。

(我的打印機設置爲雙面打印,如果我真的只是從我的瀏覽器打印HTML文檔,它按預期工作)

下面是我在做什麼:

private void Form1_Load(object sender, EventArgs e) 
    { 
     // Create a FileSystemWatcher to monitor all files on drive C. 
     FileSystemWatcher fsw = new FileSystemWatcher("C:\\COAForms"); 

     // Watch for changes in LastAccess and LastWrite times, and 
     // the renaming of files or directories. 
     fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
      | NotifyFilters.FileName | NotifyFilters.DirectoryName; 

     // Register a handler that gets called when a 
     // file is created, changed, or deleted. 
     //fsw.Changed += new FileSystemEventHandler(OnChanged); 

     fsw.Created += new FileSystemEventHandler(OnChanged); 
     fsw.Error += new ErrorEventHandler(fsw_Error); 

     //fsw.Deleted += new FileSystemEventHandler(OnChanged); 
     fsw.EnableRaisingEvents = true; 
     fsw.SynchronizingObject = this; 
     PrinterSettings settings = new PrinterSettings(); 
     label2.Text = settings.PrinterName; 

     Thread.CurrentThread.SetApartmentState(ApartmentState.STA); 
    } 

    void fsw_Error(object sender, ErrorEventArgs e) 
    { 
     MessageBox.Show(e.ToString()); 
    } 

    private void OnChanged(object source, FileSystemEventArgs e) 
    { 
     notifyIcon1.BalloonTipText = "Printing document " + e.Name + "..."; 
     notifyIcon1.BalloonTipTitle = "Printing Application"; 
     notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; 
     notifyIcon1.ShowBalloonTip(500); 

     PrintCOAPage(e.Name); 
    } 

    private void PrintCOAPage(string name) 
    { 
     try 
     { 
      // 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(@"C:\COAForms\" + name); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 

    private void PrintDocument(object sender, 
     WebBrowserDocumentCompletedEventArgs e) 
    { 
     try 
     { 
      PrinterSettings ps = new PrinterSettings(); 
      ps.Duplex = Duplex.Vertical; 

      // Print the document now that it is fully loaded. 
      ((WebBrowser)sender).Print(); 

      // Dispose the WebBrowser now that the task is complete. 
      ((WebBrowser)sender).Dispose(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 

    } 

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) 
    { 
     this.Show(); 
     this.Activate(); 
     if (this.WindowState == FormWindowState.Minimized) 
     { 
      this.WindowState = FormWindowState.Normal; 
     } 
    } 

    private void Form1_Resize(object sender, EventArgs e) 
    { 
     if (FormWindowState.Minimized == WindowState) 
     { 
      Hide(); 
     } 
    } 

我只是最近纔將PrinterSettings添加到代碼中,並沒有改變任何內容。

我將不勝感激任何幫助,你們可以提供這個!謝謝!

+0

這是'PrintDocument'還是'PrintCOAPage'的問題? – 2013-02-28 16:21:11

+0

我不確定,我沒有收到任何錯誤,只是打印一面(第二頁)。 – kogh 2013-02-28 16:23:11

+0

這可能是從WebBrowser打印的問題嗎? – kogh 2013-02-28 16:24:28

回答

0

哇,沒想到CSS會影響它,但無論出於何種原因,我使用的CSS(涉及z-index)使得第一頁不顯示,當我打印預覽WebBrowser控件下,但工作得很好在實際的IE8中..在改變了CSS後,它現在按預期工作。

+0

你能分享這個嗎?我有同樣的問題。謝謝:) – 2014-07-17 11:39:53