0

I have written a c sharp console application that writes a file, now I want to print it. I saw many examples while searching, but many of them start with a form and buttons, I don't want the user to press any button since I already know the file, the font and the size, I just want to print the document. Also every application I found is a Windows Form application, can you tell me how to call it from a console application?

Sorry if the question is a little basic, I'm just beginning to learn.

John
  • 101
  • 1
  • 2
  • I think you can check https://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.print.aspx and https://www.daniweb.com/software-development/csharp/threads/101927/printing-from-a-console-application – Akhil May 25 '15 at 08:02
  • Once check this link.. http://www.dreamincode.net/forums/topic/44330-printing-in-c%23/#/ – iTechOwl May 25 '15 at 08:09
  • possible duplicate of [Send document to printer with C#](http://stackoverflow.com/questions/218556/send-document-to-printer-with-c-sharp) – Eugene Podskal May 25 '15 at 17:00

3 Answers3

1

You can use PrintDocument object

here is a sample code 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();
     }
 }
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • I saw that example, but when I compile it nothing happens, can you tell me how to use it? – John May 25 '15 at 08:06
  • did you set a default printer? because this code prints with the default printer – Buda Gavril May 25 '15 at 08:12
  • did you add the arguments? – TaW May 25 '15 at 08:13
  • I have a PDF printer set as default, is that fine? No I didn't add the arguments because it says that the file name should be provided by console, but the console isn't starting – John May 25 '15 at 08:24
  • so in your console app in the main method try to print. First, you could hardcode a file path.Second, try with a real printer(not file printer) to see if it works. Third, put a break-point in the PrintPageEventHandler to see if it tries to print – Buda Gavril May 25 '15 at 08:39
  • how do I call it from the console app? – John May 25 '15 at 08:56
0

Make sure to add System.Drawing reference (System.Drawing.dll) to your project.

using System.Drawing;
using System.Drawing.Printing;

namespace PrintingFromConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintDocument doc = new PrintDocument();
            doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
            doc.Print();
        }

        private static void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawString("text", SystemFonts.DefaultFont, Brushes.Black, new PointF(100f, 100f));
            e.HasMorePages = false;
        }
    }
}

I have tested this code with Microsoft XPS Document Writer and Bullzip PDF Printer - both works as expected.

Anton Kedrov
  • 1,767
  • 2
  • 13
  • 20
0
  string fl = Application.StartupPath + "\\BarCode.txt";
  string StrFileName = "Barcode.txt";
  int intFN;
  intFN = FileSystem.FreeFile();
  FileSystem.FileOpen(intFN, fl, OpenMode.Output, OpenAccess.Default,OpenShare.Default, 1024);
  // Write Text In File FileSystem.PrintLine(intFN, "TEST");
  FileSystem.FileClose(intFN);
  Microsoft.VisualBasic.Interaction.Shell(Application.StartupPath + "\\PRINT.BAT " + pStrFileName, AppWinStyle.Hide, false, -1);

Now create new Batch File i.e.PRINT.BAT and paste Below Command:-

Type %1>\\ "PRINTER PATH WITHOUT QUOTES"

Write this command in notepad and Save as PRINT.BAT file Run The Application