2

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!

kogh
  • 995
  • 4
  • 17
  • 30
  • Is this issue with `PrintDocument` or `PrintCOAPage`? – Ryan Gates Feb 28 '13 at 16:21
  • I'm not sure, I'm not receiving any error, it's simply only printing the one side (2nd page). – kogh Feb 28 '13 at 16:23
  • Could it be an issue with printing from WebBrowser? – kogh Feb 28 '13 at 16:24
  • Do you get the same result on multiple printers? What make/model is the printer? – Ryan Gates Feb 28 '13 at 16:54
  • Xerox WorkCentre 5655 PS, I have tried on another Brother printer we have here and got the same issue :( – kogh Feb 28 '13 at 16:57
  • What settings do you have if you use [`WebBrowser.ShowPrintDialog`](http://msdn.microsoft.com/en-us/library/0xbfs8f9.aspx) instead of print? – Ryan Gates Feb 28 '13 at 17:07
  • Everything is set to default, and when I do the ShowPrintPreviewDialog it shows exactly what's being printed, and when I open up IE and go to print preview, it shows what I DO want printed out.. and the settings are exactly the same. – kogh Feb 28 '13 at 18:17

1 Answers1

0

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.

kogh
  • 995
  • 4
  • 17
  • 30