0

I want to print a document from C# but don't want any UI for it. The document should be printed silently using C#.

I have tried ProcessStartInfo with Verb = "Print" but is shows UI to print the document.

Any kind of help is appreciated.

Sandip D
  • 103
  • 1
  • 14
  • Just to clarify - u really want to print a document on a physical printer? – Tobias Theel Aug 28 '17 at 15:24
  • Possible duplicate of [How can I send a file document to the printer and have it print?](https://stackoverflow.com/questions/6103705/how-can-i-send-a-file-document-to-the-printer-and-have-it-print) – Tobias Theel Aug 28 '17 at 15:27
  • 1
    What are you targetting: Winforms, WPF, ASP..? __Always__ TAG your question correctly! - Also: What kind of document are we talking about? Word, pdf, text??? – TaW Aug 28 '17 at 15:39
  • @TobiasTheel I want a document to be printed using Printer. As per the link provided by you, If I use `ProcessStartInfo` to print a document it will show an UI which I do not want to show. – Sandip D Aug 29 '17 at 06:13
  • @Taw I want generic solution for this. For documents like word, pdf, text, images, xml, ppt, xls etc. – Sandip D Aug 29 '17 at 06:14
  • Is the UI to _save printing output as_ is displayed because I am not connecting to any printer or default printer is not selected? Anybody know about this? – Sandip D Aug 29 '17 at 06:54

1 Answers1

0

Perhaps the PrintDocument.Print Method might help :)

Note that I don't think this will work with .net Core as the System.Drawing namespace has not been fully ported over. (Source - GitHub corefx Issue #20325

Nor will this work in a UWP app, because they use a different API for printing things.

I don't have a printer with me to test this, but here's the code example from msdn

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

public class PrintingExample 
{
   private Font printFont;
   private StreamReader streamToPrint;
   static string filePath;


   public PrintingExample() 
   {
       Printing();
   }

   // The PrintPage event is raised for each page to be printed.
   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) ;

       // Iterate over the file, printing each line.
       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;
   }

   // Print the file.
   public void Printing()
   {
       try 
       {
          streamToPrint = new StreamReader (filePath);
          try 
          {
             printFont = new Font("Arial", 10);
             PrintDocument pd = new PrintDocument(); 
             pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
             // Print the document.
             pd.Print();
          } 
          finally 
          {
             streamToPrint.Close() ;
          }
      } 
      catch(Exception ex) 
      { 
          MessageBox.Show(ex.Message);
      }
   }

   // This is the main entry point for the application.
   public static void Main(string[] args) 
   {
      string sampleName = Environment.GetCommandLineArgs()[0];
      if(args.Length != 1)
      {
         Console.WriteLine("Usage: " + sampleName +" <file path>");
         return;
      }
      filePath = args[0];
      new PrintingExample();
   }
}