Using .NET or COM API to Automate Printing

Automate printing in your own custom applications or scripts using the PEERNET PNSrv12 COM Object. Included with TIFF Image PrinterRaster Image Printer and PDF Image Printer, no additional files need to be included with your application. The end-user only needs the appropriate printer installed and activated.

The PNSrv12 COM Object can be used in Visual Basic 6.0, Visual Basic .NET, Visual C# .NET, Visual C++, VBScript and PowerShell, and anywhere else you can use COM objects.

When using PNSrv12 COM, you can:

  • set file naming and conversion preferences on a per file basis
  • wait until the file has been created to continue your workflow
  • use a pool of printers to increase conversion capacity
  • track the files printed and the output files associated with each file
  • retrieve information about the output files created
  • interact with the printer in a thread-safe manner

Using the PNSrv12 COM Object

To reference the PNSrv12Lib in your application, from the list of available COM objects, you would select “PNSrv12 1.0 Type Library“.


Creating The Conversion Profiles

Conversion profiles are used to set the conversion settings when initializing the PNSrv12Lib COM. They are created and exported using the Profile Manager. These files are referenced by file path when initializing the COM object and can be included with your application’s program files. A conversion profile can contain all of your settings if you are always converting documents with the same options, or just the common settings and the COM methods can be used to change settings dynamically as you print. You can also have multiple conversion profiles and switch between them as needed.

Initializing PNSession

The PNSession object is used to initialize and connect with the Image Printer you are using, and establishes the number of printers in the printer pool and the conversion settings. A prefix can be added to the name of each printer in the printer pool for easy recognition. The process ID of your application is passed in to ensure all COM objects and printers are properly disposed of upon exit.

using PNSrv12Lib;

PNSession session = new PNSession();
int pID = Process.GetCurrentProcess().Id;

session.SetSessionPrinter(pID, "TIFF Image Printer 12", 2, "Converter-", "C:\Profiles\FaxTIFF.pntifprofile");

Dynamically Set the Output Location and Filename

The filename formatting options in the conversion profile provide many options for custom naming including path building, unique IDs, JobIDs, custom prefix and suffix and page numbers. See the Filename Creator topic for all the options to build your filename. When you need greater control over the filename, the method SetSaveOptionsOutputLocationAndOutputFileName allows you to override the output folder, the output file name or both before you print each file, while the OnGetNextFileName event can be used to set the output folder and filename just a s the output file is created on disk.

Printing to a Printer from the Pool

The PNSrv12Lib COM object is created with a chosen number of printers in it’s printer pool. This can be as little as one printer, or you can have as many printers in the pool as needed, up to the limits of your system resources. Having more than one printer in the pool of printers allows more than one file to be submitted at a time, or for the printer pool to be shared in a multi-threaded application.

Before each document is printed, a printer from the printer pool needs to be requested using the NewPrintSession method, and the document is then printed to that printer. The returned IPNPrintSession object contains the name of the printer to use when printing the document, and is used to test that the document was successfully sent, or spooled to the printer, as well as monitoring document conversion to know when the output file has been created on disk.

Waiting for the Output File

After printing the document, your workflow may need to know when the document is created, or done, in order to move on to the next file or to take the created file and perform the next task in your process. The IPPrintSession‘s WaitForJobsCompleted method is a timed method that is used to wait for the document to be created on disk and ready for further processing.

When you need more information about the files created, such as a list of the names of all the files created, the OnEndJob event on the IPNSession object should be used. This event is fired when the document has completed conversion and all files are on disk and ready to be processed. An IPNJob object is passed to the event that contains information about the job, such as the name of the source file, and a list of the file or files created, allowing you to get the exact name of the file created, even when serialized or other custom file naming properties have been set.

Dive in With Sample Programs 

Several C# samples are included that demonstrate how to automate printing with Microsoft Word, Adobe Reader and PNSrv12Lib COM. The samples cover such topics as initializing the COM object for the printer, creating printer pools, passing in a conversion profile containing the settings to for the output files, overriding the file name as you print or querying for unique file names just as the output file is being created, how to safely use the COM object in multiple threads using a Parallel.For loop, and methods to determine when the file has been converted.

For an overview of each sample and where to find this, see Getting Started With PNSrv12Lib.

Additional Information

For more detailed information on using PNSrv12 COM Object, see :

Using the PNSrv12 COM Object in TIFF User Guide
Using the PNSrv12 COM Object in Raster User Guide
Using the PNSrv12 COM Object in PDF User Guide