Skip to main content
 首页 » 编程设计

c#之使用 WebBrowser 控件的双面打印仅打印 1 面

2024年01月01日49kenshinobiy

我的 .NET 应用程序出现问题,它只打印我的 HTML 文件的第二页,而完全忽略了第一页(没有打印其他页面,而且它的背面是空白的)。

当我打开打印机的队列窗口时,它确实显示它从“假脱机”转到“正在打印”并列出了两页,所以我不知道为什么它不打印第一页。

(我的打印机设置为双面打印,如果我真的只是从浏览器打印 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 添加到代码中,但它没有任何改变。

如果你们能提供任何帮助,我将不胜感激!谢谢!

请您参考如下方法:

哇,没想到 CSS 会影响它,但无论出于何种原因,我使用的 CSS(涉及 z-index)在 WebBrowser 控件下打印预览时使第一页不显示,但在实际的 IE8.. 在稍微改变了 CSS 之后,它现在可以按预期工作了。