Writing a Virtual Printer in .NET

54,458

Solution 1

Visit http://www.printerplusplus.com. It is open source .NET virtual printer. It gives you an installer and a .NET class for writing code to process your "printer data".

Solution 2

Did exactly what you are asking using the Github project: Microsoft/Windows-driver-samples/print/XPSDrvSmpl

https://github.com/Microsoft/Windows-driver-samples/tree/master/print/XPSDrvSmpl

Installer: http://wixtoolset.org/

Application: Listen to internal port

Flow: Install printer and application from a single installer. User prints something with your driver while the application listens to the internal port. When data is sent the application picks it up. This is for XPS, can be converted to PDF, but the flow is similar no matter what you are printing. If you need anything else check out Microsoft/Windows-driver-samples/print/ on GitHub or other sources specific to your needs.

Update:

A lot of questions about how to get the driver working so here is a quick example:

Start by downloading Windows Driver Kit (WDK) if you do not have it installed already. When installing choose to add the extension for Visual Studio as well in the final step. In your Visual Studio 2017 Install you need to have Desktop development with C++ to have the right SDKs. If you don't have it see the anser below on how to add it.

How do I add features to Visual Studio 2017?

https://developer.microsoft.com/en-us/windows/hardware/windows-driver-kit

Then download the .zip file for Windows-driver-samples master:

https://github.com/Microsoft/Windows-driver-samples/archive/master.zip

Navigate to the folder <UnzipFolder>\print\XPSDrvSmpl and open XPSDrvSmpl.sln in Visual Studio.

Then follow Microsofts own guide on GitHub. As you can see it is for Visual Studio 2015 but I think it is the same for 2017 (not tested yet):

To build a driver solution using Windows Driver Kit (WDK) 10 and Visual Studio 2015, perform the following steps.

  1. Open the solution file in Visual Studio 2015.
  2. Add all non-binary files (usually located in the \install directory of the sample) to the Package project: a. In the Solution Explorer, right click Driver Files b. Select Add, then click Existing Item c. Navigate to the location to which you downloaded the sample, and select all the files in the install directory, or the equivalent set of non-binary files such as INFs, INIs, GPD, PPD files, etc. d. Click Add
  3. Configure these files to be added into the driver package: a. In the Solution Explorer, right click on the solution and choose Add > New Project. Choose Driver Install Package under Visual C++/Windows Driver/Package. b. In the Solution Explorer, right click the Package project and select Properties. c. In the left pane, click Configuration Properties > Driver Install > Package Files. d. In the right pane, use the ellipsis button (...) to browse to the set of files that needs to be added to the driver package. All the data files that you added in Step 2-c, except the INF file, should be added. This configuration is per-architecture, so this configuration must be repeated for each architecture that will be built. e. Click OK.
  4. Open the INF file and edit it to match the built output. a. Open the INF file. b. In the Version section, add a reference to a catalog file like this: CatalogFile=XpsDrvSmpl.cat. c. In the SourceDisksFiles section, change the location of the DLL files you are building, to =1. This indicates that there is no architecture specific directory in this driver. If you ship multiple architectures simultaneously, you will need to collate the driver INF manually.

At this point, Visual Studio 2015 will be able to build a driver package and output the files to disk. In order to configure driver signing and deployment, see Developing, Testing, and Deploying Drivers.

https://github.com/Microsoft/Windows-driver-samples/tree/master/print/XPSDrvSmpl#build-the-sample

Solution 3

I think you will have to do a lot of WinAPI wrapping. Start researching on Windows Driver Development Kit to find the things you have to do.

I also found this commercial Printer Driver Resource Toolkit for .NET...

Solution 4

You could simply have your app expose itself like an LPD type printer or monitor port 9100. You could then install any print driver you like, and point it a your app.

Solution 5

Was looking for an answer for similar question, and found this link through Wikipedia - http://www.colorpilot.com/emfprinterpilot.html (allows to create Virtual Printers in different languages)

Share:
54,458
ctrlalt313373
Author by

ctrlalt313373

Software Developer in .NET

Updated on May 11, 2020

Comments

  • ctrlalt313373
    ctrlalt313373 about 4 years

    I'm looking to create a virtual printer that passes data to my .NET application. I want to then create an installer that installs both the printer and the .NET application. It would we really nice to be able to write it all in C#, but I have a feeling that this will require a printer driver to be written is unmanaged code. Does anyone know of a fairly clean tutorial or example of how to do this?