Search code examples
c#printingbrowserduplex

Duplex printing with WebBrowser control only printing 1 side


I'm having an issue with my .NET application only printing the second page of my HTML file, and completely ignoring the first page (no other page is printed, and the back of it is blank).

When I pull up my printer's queue window, it does show it go from "Spooling" to "Printing" and lists both pages, so I'm at a loss as to why it's not printing the first page.

(My printer IS set to duplex printing, and if I literally just print the HTML document from my browser, it works as expected)

Here's what I'm doing:

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();
        }  
    }

I only just recently added the PrinterSettings to the code and it changed nothing.

I would greatly appreciate any help you guys can provide on this! Thank you!


Solution

  • Wow, didn't think the CSS would impact it, but for whatever reason the CSS I was using (involving z-index) made the first page NOT display when I print previewed under the WebBrowser control, but worked just fine in actual IE8.. after changing the CSS around a bit it now works as intended.