Posted by on Apr 24, 2012 in Blog, c#, Printing, VS2010 | 3 comments

In developer’s life, it is so common we used to encounter lot of issues. I am working in a small proof concept. Automatically printing documents to file or printer in C# (.Net Framework 4.0).

  1.         private void Print()
  2.         {
  3.             if (m_streams == null || m_streams.Count == 0)
  4.                 throw new Exception(“Error: no stream to print.”);
  5.             PrintDocument printDoc = new PrintDocument();
  6.             if (!printDoc.PrinterSettings.IsValid)
  7.             {
  8.                 throw new Exception(“Error: cannot find the default printer.”);
  9.             }
  10.             else
  11.             {
  12.                 printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
  13.                 m_currentPageIndex = 0;
  14.                 if (printDoc.PrinterSettings.PrinterName == “Microsoft XPS Document Writer”)
  15.                 {
  16.                     printDoc.PrinterSettings.PrintToFile = true;
  17.                     if (filename == “”)
  18.                         printDoc.PrinterSettings.PrintFileName = DateTime.Now.Ticks.ToString() + “.xps”;
  19.                     else
  20.                         printDoc.PrinterSettings.PrintFileName = filename;
  21.                 }
  22.                 printDoc.Print();
  23.             }
  24.         }

    But while printing the documents to file, I was annoyed by a pop up which showed me Printing page 1 of 5. I wanted to get rid of that.  
    After a bit google and MSDN, I have found that Print controller PrintController class Controls how a document is printed, when printing from a Windows Forms application.
    If you look at the PrintDocument class Meta data view, you will notice the following statement.
  1.         // Summary:
  2.         //     Gets or sets the print controller that guides the printing process.
  3.         //
  4.         // Returns:
  5.         //     The System.Drawing.Printing.PrintController that guides the printing process.
  6.         //     The default is a new instance of the System.Windows.Forms.PrintControllerWithStatusDialog
  7.         //     class.
  8.         [SRDescription(“PDOCprintControllerDescr”)]
  9.         [Browsable(false)]
  10.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  11.         public PrintController PrintController { get; set; }

    MSDN states the following:

    The .NET Framework includes three print controllers that are derived from PrintController that help accomplish common tasks. The StandardPrintController prints a document to a printer. The PreviewPrintController generates a preview of what the document will look like when printed and is used by the PrintPreviewControl and PrintPreviewDialog classes. The PrintControllerWithStatusDialog provides a printing status dialog during the printing process.

    So I have just introduced a new statement before
    PrintDocument.Print()
      printDoc.PrintController = new StandardPrintController();
    Voila it started working like a magic.  Also I liked to call it as “Silent Printing”. Hope this helps some one in need.