Printing to a network (shared) printer - Word Interop

I need to print a server side document on a network printer. My web page sends the location of the document and file to the server to open and replace the merge items, and then print the document to the preferred (not the default) network printer. The name of the preferred printer changes when you select a document when you change the web page.

I am using the Word 14.0 object library, Asp.Net MVC 4.0, .Net Framework 4.0, IIS 7 on a Windows 2008 R2 server. In IIS, I created an application pool that runs in a specific Identity account (accountName @DomainName). Download the user profile to true to load network printer connections into the registry. I allowed the account to have permissions to start COM interoperability services. I managed to open the document and replace the merge fields and save it in pdf format to send the file as an attachment to an email.

The word application has a default printer in the ActivePrinter property ActivePrinter that I can print to the default printer. But my ultimate goal is to print this word on my preferred network printer before I close the word application and active document.

The following two methods throw exceptions if I try to change the ActivePrinter property.

 Word.Application wordApp = new Word.Application(); 

First method:

 wordApp.ActivePrinter = "preferredPrinterName"; 

Second method;

 object[] oWordDialogParams = { "\\<serverName>\<PrinterName>", true }; string[] argNames = { "Printer", "DoNotSetAsSysDefault" }; object wordBasic = wordApp.WordBasic; wordBasic.GetType().InvokeMember("FilePrintSetup" , System.Reflection.BindingFlags.InvokeMethod , null , wordBasic , oWordDialogParams , null , null , argNames); 

I found that the Word application object does not load all printers installed in the user account. It loads only the default printer. I assume this was the reason for the exception when the above two methods tried to change or add the preferred printer for the application object, because the printer I was trying to install was never found in the list of active printers.

How to get all installed shared printers under a user profile loaded in a Word application object?

+4
source share
1 answer

Using Office internetworking in server scripts (for example, ASP.NET, Windows service, etc.) is NOT supported by MS - see http://support.microsoft.com/default.aspx?scid=kb;EN-US; q257757 # kb2

In addition, there have been several security-related changes with Windows Vista that basically make it very difficult to do something “desktop” in the Windows service (IIS / ASP.NET is just a special case of the Windows service in this regard).

Another point is that “printing” from a server script is likely to cause problems, since IIS is a (special) Windows service ... Windows Service usually does not have a “full / real” desktop, which, in turn, is necessary for reliable printing ...

I don’t think there is a simple solution for your scenario ...

I would break it into different components:

  • Word document processing (e.g. Aspose.Words )
  • Create a PDF file from the resulting Word file (e.g. Aspose.Words )
  • Implement HotFolder on the target network printer
  • Copy PDF to this HotFolder to print

This will be a reliable and supported option for your scenario ...

+1
source

Source: https://habr.com/ru/post/1495999/


All Articles