Reputation: 11430
I want to read a folder for all the files and programmatically print them as though I right-click > print.
I am aware printing this way is default-application specific. So I think this is a two step procedure: How do I check whether a file with its default application supports print; and how do I actually issue the command to print the file?
Is printing this way called "shell command printing" or something like that? Will need the correct terms for googling for information.
Any other better ways you would suggest for this task?
EDIT: The files types can be anything other than simple .txt files e.g. PDF, DWG, JPEG etc.
Upvotes: 1
Views: 2104
Reputation: 4866
As you said: You want to read files in a folder and automatically print them
So, one option could be to read the file > open the FileStream and send the stream to print. Here is a sample which prints a stream -
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.print.aspx
Notice the Printing()
function in the sample. [I have not tried, but looks achievable]
// 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);
}
}
Here is one more option - http://channel9.msdn.com/forums/TechOff/151242-How-to-send-a-PDF-to-a-printer/ --- See the last post.
Upvotes: -1
Reputation: 57922
You can enumerate the files in the folder using Directory.GetFiles and then use the ShellExecute mode of Process.Start
to execute the "print" verb (command) on each of the files in turn.
See Process.Start here, and you will need to pass in a ProcessStartInfo with the UseShellExecute
and Verb
set appropriately.
By asking the operating system to work out how to print them, you don't have to worry about the intricacies of how to print different types of data, etc.
Upvotes: 1
Reputation: 340
I believe this is what you're looking for:
http://support.microsoft.com/kb/314499
If that doesn't work then there are plenty of others method to do it using macros or by writing a really simple visual basic program to do it for you.
Comment back if that doesn't work and I'll edit my post.
Regards~
Upvotes: 1