1

I am attempting to print reports from our webserver but I ran into a problem where if the network printer is not mapped locally to the server then there is a considerable delay (20+ seconds) before the printer actually starts printing. If the printer is mapped then the problem goes away and the print job goes through almost immediately. Unfortunately due to the size of our company mapping all the possible printers to our webserver is infeasible.

   streamToPrint = new StreamReader
          ("C:\\test.txt");
        try
        {
            printFont = new Font("Arial", 10);
            var pd = new PrintDocument();
            pd.PrintPage += pd_PrintPage;
            pd.PrintController = new StandardPrintController();
            pd.PrinterSettings.PrinterName = "networkprinternamehere";
            pd.Print();
        }
        finally
        {
            streamToPrint.Close();
        }

    private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        string line = null;

        // Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height /
           printFont.GetHeight(ev.Graphics);

        // Print each line of the file. 
        while (count < linesPerPage &&
           ((line = streamToPrint.ReadLine()) != null))
        {
            yPos = topMargin + (count *
               printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, Brushes.Black,
               leftMargin, yPos, new StringFormat());
            count++;
        }

        // If more lines exist, print another page. 
        if (line != null)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
    }

Example code here pulled pretty much straight from Microsoft's documentation. All the solutions I've seen have just recommended mapping the printers. Is there a way I can better performance without mapping? It seems like this question may be related by they did not get any responses.

After running a profile on the code (per suggestion the comments) 87% the time is spent in the PrintDocument.Print() method. The pd_PrintPage method takes almost no time so the problem is associated with whatever Print does under the hood.

Community
  • 1
  • 1
mirv120
  • 111
  • 4
  • I'd imagine that this is a problem with the printer subsystem, and has nothing to do with your code. Do you get the same behavior if you just print a document from the server using Word or Notepad? – Jason Jul 02 '13 at 20:20
  • If I try printing via cmd: print /d:"networkprinternamehere" test.txt then it prints quickly either way. – mirv120 Jul 02 '13 at 20:30
  • Have you profiled the code to see if there is a different in execution time depending on the printer selected? – Jason Jul 02 '13 at 20:32
  • can you post the code for pd_PrintPage()? – Jason Jul 02 '13 at 20:37
  • Added pd_PrintePage(). I pulled that straight from the MS documentation. – mirv120 Jul 02 '13 at 20:59
  • You need to profile your code and figure out exactly what parts are slow. I suspect that when you use a non-mapped printer it is having to do an expensive query every time it wants to find out printer properties, but I don't know exactly which lines would cause that. – Jason Jul 02 '13 at 21:08

0 Answers0