Tuesday, September 13, 2011

Querying printer queues from X++

For a client we need to print a page of an X++ report and then some attached files, like PDF or Word documents.

We need all the connected documents to be printed in one batch and we must not print the next X++ page before the attached documents are printed.

So we need to figure out if the shell and the printer are finished printing the attached documents before sending the next document from AX.

Luckily we can use the .NET library System.Printing to query the queue  and in particular we can call the NumberOfJobs to figure out if the attached documents have been printed.

Here's a few bits of code for querying the printer queues:
    System.Printing.PrintServer           printServer = new System.Printing.LocalPrintServer();
    System.Printing.PrintQueueCollection  printQueueCollection = printServer.GetPrintQueues();
    System.Printing.PrintQueue            printQueue;
    System.Collections.IEnumerator        enumerator;
    // Print the local server name
    print printServer.get_Name();
    // Enumerate and print names of all the queues on the server
    enumerator = printQueueCollection.GetEnumerator();
    while (enumerator.MoveNext())
        printQueue = enumerator.get_Current();
        print printQueue.get_FullName();
    // Get information for a particular print queue
    printQueue = new System.Printing.PrintQueue(printServer, "HP Color LaserJet 5550 PS");
    print printQueue.get_Description();
    print printQueue.get_NumberOfJobs();
    print ClrInterop::getAnyTypeForObject(printQueue.get_IsBusy());
    print ClrInterop::getAnyTypeForObject(printQueue.get_IsPrinting());


In AX 2012 I would probably have placed the supporting code in a Visual Studio project rather than calling these CLR objects from X++.

1 comment:

John Doe said...

Thanks a lot. When somebody told me I had to add System.Printing under References, this was exactly what I needed.

Tusind tak