Printing documents to Microsoft XPS Document Writer without user interaction

Now that the XPS storm has been started, people are generating XPS documents from all kinds of sources. Among different ways of XPS generation, the easily way is still through printing to the Microsoft XPS Document Writer (the MXDW printer driver).

If you're printing from your own applications, it's easy to specify MXDW as the printer driver and provide a file name for the XPS document to be saved to. But if you're using certain applications to print a documents, a print dialog box and a file save dialog box could pop up on screen, blocking attempts for fully automatic document conversion. 

For different document types, you can check Windows registry to find the command to print it. For example, the command to print an HTML page is:

rundll32.exe mshtml.dll,PrintHTML "<document>" "<driver>" "<device>" "<port>"

So you should be able to print any webpage to any printing device, to any printer/file, fully automatically. But this creates a security risk, so it has since been blocked. User confirmation is needed to print an HTML page through MSHTML.DLL.

But if you're willing to write some code, automatic document printing is still possible. Here is one solution:

#include <windows.h>

#include <strsafe.h>

const wchar_t * Dialog_Class = L"#32770";

HRESULT PrintHTML(const wchar_t * pUri, const wchar_t * pFileName)

{

    HRESULT hr;

    STARTUPINFO si = { sizeof(si) };

    PROCESS_INFORMATION pi;

    wchar_t command[MAX_PATH];

    hr = StringCchPrintf(command, _countof(command),

             L"rundll32.exe mshtml.dll,PrintHTML \"%s\"", pUri);

    if (FAILED(hr))

    {

        return hr;

    }

    if (CreateProcess(0, command, 0, 0, FALSE, 0, 0, 0, &si, &pi))

   {

        printf("\r\nStarting '%S'\r\n", command);

        CloseHandle(pi.hProcess);

        CloseHandle(pi.hThread);

    }

    DWORD size = _countof(command);

    GetDefaultPrinter(command, & size);

    printf(" Default printer: '%S'\r\n", command);

    printf("\r\nWaiting for Print dialog box\r\n");

    Sleep(5 * 1000);

    // Find Print Dialog box

    HWND hWnd = FindWindow(Dialog_Class, L"Print");

    // Print button

    HWND hChild = FindWindowEx(hWnd, NULL, NULL, L"&Print");

    GetWindowText(hChild, command, _countof(command));

    printf(" hwnd = %p %p('%S')\r\n", hWnd, hChild, command);

    // Press Print button

    SendMessage (hChild, WM_IME_KEYDOWN, 'P', 0);

   

    printf("\r\nWaiting for File Save dialog box\r\n");

    Sleep(5 * 1000);

    // Find Save File Dialog Box

    hWnd = FindWindow(Dialog_Class, L"Save the file as");

   

    hChild = FindWindowEx(hWnd, NULL, L"ComboBoxEx32", NULL);

    hChild = FindWindowEx(hChild, NULL, L"ComboBox", NULL);

    hChild = FindWindowEx(hChild, NULL, L"Edit", NULL); // File name edit control

    printf(" hwnd = %p %p\r\n", hWnd, hChild);

   

    // Enter file name

    SendMessage(hChild, WM_SETTEXT, NULL, (LPARAM) pFileName);

    Sleep(1000);

    // Find Save button

    hChild = FindWindowEx(hWnd, NULL, NULL, L"&Save");

    GetWindowText(hChild, command, _countof(command));

    printf(" hwnd = %p %p('%S')\r\n", hWnd, hChild, command);

    // Press Save button

    SendMessage (hChild, WM_IME_KEYDOWN, 'S', 0);

    printf("\r\nSaving to '%S'\r\n", pFileName);

    return hr;

}

 

The code uses CreateProcess to call rundll32 with the right parameters to start the printing process. It then waits for a while for the Print dialog box to pop up and fakes a click on the Print button. After that, it waits for the File Save dialog box to enter a destination file name and click on the Save button.

 

Here is how this routine can be called:

 

      PrintHTML(L"https://blogs.msdn.com/fyuan", L"c:\\fyuan.xps"); prefix="o" ?>

 

The code will generate enough logging information for you to see if things are working or not:

 

        

Starting 'rundll32.exe mshtml.dll,PrintHTML "http://blogs.msdn.com/fyuan"'   

      Default printer: 'Microsoft XPS Document Writer'

Waiting for Print dialog box

  hwnd = 0021075C 002B0754('&Print')

Waiting for File Save dialog box

  hwnd = 001E04A2 00260B72

  hwnd = 001E04A2 002D0BA6('&Save')

 

Saving to 'c:\fyuan.xps'

Note:

 

1) The code assumes MXDW is already the default printer driver. If this is not the case, you may want to add code to make it so before the Print dialog box pops up; or add code to mimic selection of MXDW as the printer driver.

 

2) The code uses fixed length wait for the Print dialog box and File SaveAs dialog box to pop up. This is not a very stable solution or it could take too much time. You may want to add better robust and efficient solutions.

 

3) The code is clealy very sensitive to OS version, application version, and UI language. You may need to find a more generic solution and a more robust solution.