使用WebBrowser控制的双面打印仅打印1面

时间:2013-02-28 16:03:48

标签: c# printing browser duplex

我的.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,但没有改变任何内容。

我非常感谢你们能提供的任何帮助!谢谢!

1 个答案:

答案 0 :(得分:0)

哇,不认为CSS会影响它,但无论出于什么原因,我使用的CSS(涉及z-index)使得我在WebBrowser控件下预览时打印的第一页不显示,但工作得很好实际的IE8 ..在稍微改变CSS之后它现在按预期工作。