Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, April 9, 2008 4:07 AM
hi guys,
What is the command for printing in console application?
by the way how can we use "\f"(form feed) for printing new page with printing command in console application.
Thanks
All replies (3)
Wednesday, April 9, 2008 4:17 AM âś…Answered | 1 vote
Dear simosi,
Please find the code for printing through console application below:
Code Snippet
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Management;
/// <summary>
/// Add the printer connection for specified pName.
/// </summary>
/// <param name="pName"></param>
/// <returns></returns>
[DllImport("winspool.drv")]
public static extern bool AddPrinterConnection(string pName);
/// <summary>
/// Set the added printer as default printer.
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Name);
#region Print File
/// <summary>
/// Sends the file to the printer choosed.
/// </summary>
/// <param name="fileName">Name & path of the file to be printed</param>
/// <param name="printerPath">The path of printer</param>
/// <param name="numCopies">The number of copies send to printer</param>
public bool PrintFile(string fileName, string printerPath, int numCopies)
{
if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(printerPath))
{
return false;
}
//Instantiate the object of ProcessStartInfo
Process objProcess = new Process();
try
{
AddPrinterConnection(printerPath);
SetDefaultPrinter(printerPath);
//Print the file with number of copies sent.
for (int intCount = 0; intCount < numCopies; intCount++)
{
objProcess.StartInfo.FileName = fileName;
objProcess.StartInfo.Verb = "Print";
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
objProcess.StartInfo.UseShellExecute = true;
objProcess.Start();
}
return true;
}
catch (Exception ex)
{
// Log the exception here...
return false;
}
finally
{
objProcess.Close();
}
}
#endregion
Also if you want to see the thread discussion apart from the above code look here:
http://www.daniweb.com/forums/thread101927.html
Hope this helps you !
Friday, May 7, 2010 3:11 AM | 1 vote
The trick should be used in client applications (console, WinForms, WPF) only. Please avoid it in ASP.NET applications, as it can lead to serious problems.Lex Li Support Engineer at Microsoft This posting is provided "AS IS" with no warranties, and confers no rights.
Monday, April 8, 2013 10:55 AM
I used the way Sandeep has mentioned. though it prints the PDF files i'm sending to the printer queue, It opens up and closes the PDF Viewer window for a second.
How can I make PDF Viewer hidden.