Create custom print port in .NET
I am trying to figure out if it is possible to开发者_如何转开发 create a custom print port in .NET. Functionality I am trying to achieve is to intercept data generated by printer driver and send it to a remote server instead of device.
Yes, it is. I've done it in C#, writing a PDF printer controlled by software.
Steps to achive your goal are:
- Create a new port using RedMon
- Set port for sending output to your application
- Write an application that gets incoming stream and send it wherever you want
Here is a bunch of code for your custom application:
string fname = Environment.GetEnvironmentVariable("TEMP") + @"\";
fname += DateTime.Now.ToString("yyyyMMdd-hhmmss-fffff") + ".ps";
FileStream fs = new FileStream(fname, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
StreamReader sr = new StreamReader(Console.OpenStandardInput());
sw.Write(sr.ReadToEnd());
sw.Flush();
sw.Close();
sr.Close();
I edit my post to answer your comment:
- you can easily use Redmon on Windows7. I do with my printer without any problem and I think you can do it in Windows 2008 too. If you want to discover how you can do install redmon port without installer take a look at PDFCreator setup source. If you want I can you explain how to do that and could post a C# implementation, but I don't think this is what you need now.
- You can use your single Redmon port with any number of printers, not only one; when your software receive spooled job from printer, using environment vars Redmon set up for you, you can read the name of the printer sending the job... so you can decide what to do according to that.
Not really. Either creating a virtual port driver or a network redirector requires native code. However...
You could capture the data by installing a network print port and implementing the server side in .NET. For example, LPR would require you to create a TCP socket server, and that's completely feasible to do in C#.
I don't know of any existing C# implementation, but you could learn a lot from the source code of p910nd.
You can also make use of the multi file port by installing the mfilemon which is a free utility. With the help of this port you can direct the output of a printer to a folder of your wish and then u can start your application too.
精彩评论