使用XPS打印机不弹保存文件名打印XPS文档

Printing documents to Microsoft XPS Document Writer without user interaction

24 Feb 2007 6:03 PM 

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"http://blogs.msdn.com/fyuan", L"c:\\fyuan.xps");

 

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.

WPF流文档、固定文档XPS文档都是WPF中用于显示文档的三种不同方式。下面是它们的具体结构和使用方法: 1. WPF流文档 WPF流文档是一种基于XML的文档格式,可以在WPF应用程序中嵌入和显示。它的结构类似于HTML文档,包含一系列元素和属性,可以用来定义文本、图像、链接等内容。WPF流文档可以通过FlowDocument类来创建和编辑,可以使用XAML或代码来定义其内容和样式。WPF流文档适用于需要动态生成和展示文本内容的应用程序。 2. 固定文档 WPF固定文档是一种基于XPS文档格式的文档类型,它是一种固定页面大小和布局的文档格式,可以包含文本、图像、矢量图形等内容。WPF固定文档可以使用FixedDocument类来创建和编辑,可以使用XAML或代码来定义其内容和样式。WPF固定文档适用于需要生成和显示静态文档的应用程序。 3. XPS文档 WPF XPS文档是一种基于XML的文档格式,可以在WPF应用程序中嵌入和显示。它是一种可扩展的页面描述语言,可以描述文本、图像、矢量图形等内容,并且支持多页文档XPS文档可以使用XpsDocument类来创建和编辑,可以使用XAML或代码来定义其内容和样式。WPF XPS文档适用于需要生成和展示复杂文档,例如报告、手册等。 总的来说,WPF流文档适用于动态生成和展示文本内容,WPF固定文档适用于生成和展示静态文档,而XPS文档适用于生成和展示复杂文档。在开发WPF应用程序时,可以根据具体需要选择合适的文档类型来实现所需的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值