2

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

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

4

1 回答 1

0

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

于 2013-02-28T21:50:28.370 回答