C# 带WebBrowser控件的双面打印仅打印一面

C# 带WebBrowser控件的双面打印仅打印一面,c#,printing,browser,duplex,C#,Printing,Browser,Duplex,我的.NET应用程序只打印HTML文件的第二页,而完全忽略了第一页(没有打印其他页面,并且后面是空白的),我遇到了一个问题 当我打开打印机的队列窗口时,它确实显示它从“假脱机”到“打印”,并列出了这两个页面,因此我不知道它为什么不打印第一个页面 (我的打印机设置为双面打印,如果我只是从浏览器中打印HTML文档,它将按预期工作) 以下是我正在做的: private void Form1_Load(object sender, EventArgs e) { // Creat

我的.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添加到代码中,但它没有任何改变


我非常感谢你们在这方面提供的任何帮助!谢谢大家!

Wow,我不认为CSS会影响它,但不管出于什么原因,我使用的CSS(包括z-index)在WebBrowser控件下打印预览时,使第一页不显示,但在实际的IE8中工作得很好。。在稍微更改CSS后,它现在可以正常工作。

这是
PrintDocument
PrintCOAPage
的问题吗?我不确定,我没有收到任何错误,只是打印了一面(第二页)。这可能是WebBrowser打印的问题吗?在多台打印机上得到相同的结果吗?打印机是什么牌子/型号的?Xerox Workcenter 5655 PS,我试过我们这里的另一台Brother打印机,但遇到了同样的问题:(你能分享一下吗?我也有同样的问题。谢谢:)