双面打印与WebBrowser控制只打印一面

本文关键字:打印 控制 WebBrowser | 更新日期: 2023-09-27 18:01:30

我有一个问题,我的。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添加到代码中,它没有改变什么。

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

双面打印与WebBrowser控制只打印一面

哇,没想到CSS会影响它,但无论出于什么原因,我使用的CSS(包括z-index)使第一页不显示当我在WebBrowser控制下打印预览,但在实际的IE8中工作得很好。在改变了一些CSS之后,它现在可以正常工作了。