使用XPS打印机不弹保存文件名打印XPS文档
来源:互联网 发布:stm32串口收发数据 编辑:程序博客网 时间:2024/04/29 22:10
Printing documents to Microsoft XPS Document Writer without user interaction
- 19
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.
- 使用XPS打印机不弹保存文件名打印XPS文档
- 在Windows 7上使用Microsoft XPS Document Writer无法打印出XPS文档的问题
- xps
- wpf中xps打印
- XPS文档阅读器
- 无法找到打印机microsoft xps document writer
- 将Xaml文档转成XPS文档
- 将Xaml文档转成XPS文档
- 将Xaml文档转成XPS文档
- 将Xaml文档转成XPS文档
- 以编程方式打印 XPS 文件
- c# 调用Microsoft XPS Document Writer打印机,将Pdf文件转换成Xps文件
- XPS入门
- xps说明
- XPS阅读器
- powerpoint2007 无法找到打印机“Microsoft XPS Document Writer"
- 使用Jacob批量转换word为txt、pdf、xps、html、xml等文档
- 使用Jacob批量转换word为txt、pdf、xps、html、xml等文档
- 实时监听输入框值变化的完美方案:oninput & onpropertychange
- PyQt4 精彩实例分析* 实例1 Hello Kitty!
- 关于Menu的根菜单项背景图片
- win32 对栈进行安全的多线程操作
- (C++实现)——代理模式(Proxy Pattern)
- 使用XPS打印机不弹保存文件名打印XPS文档
- NUGI poplist接收消息
- oracle workflow 详解
- GCC gtest编译
- (C++实现)——责任链模式(Chain Of Responsibility Pattern)
- powerpc e500系列,linux初始化的tlb汇编fsl_booke_entry_mapping.S,添加人肉代码注释
- hdu 4009(最小有向生成树)
- (C++实现)——命令模式(Command Pattern)
- jquery UI dialog 内iframe 如何调整大小