Pages

Friday, November 4, 2011

Print images directly from AX without user interaction

I have written one other post on doing this by calling WinApi::ShellExecute directly, but here is a more elaborate sample using .NET.

The sample prints an image (in this case a TIFF image) to your default printer without prompting first. You need to add a reference to System.Printing in AX to use the sample.

static void PrintImage(Args _args)
{
    System.Printing.PrintServer         localPrintServer    = new System.Printing.LocalPrintServer();
    System.Printing.PrintQueue          printQueue          = System.Printing.LocalPrintServer::GetDefaultPrintQueue();
    System.Diagnostics.Process          printProcess        = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo processStartInfo;
    str                                 arguments;
    Filename                            fileName            = @'C:\TESTFILE.TIF';
    ;

    processStartInfo = printProcess.get_StartInfo();

    processStartInfo.set_Verb("Print");
    processStartInfo.set_FileName(@"C:\WINDOWS\System32\rundll32.exe");

    arguments  = @'C:\WINDOWS\System32\shimgvw.dll,ImageView_PrintTo /pt ';
    arguments += @'"';
    arguments += filename;
    arguments += @'" ';
    arguments += @'"';
    arguments += ClrInterop::getAnyTypeForObject(localPrintServer.get_Name()) + @'\' + ClrInterop::getAnyTypeForObject(printQueue.get_Name());
    arguments += @'" ';
    arguments += @'"%3" "%4"';

    processStartInfo.set_Arguments(arguments);

    processStartInfo.set_UseShellExecute(false);

    printProcess.Start();
    printProcess.Close();
}

No comments: