C++ 打印XPS文档

2019/07/27 17:56
阅读数 59
 1 CoInitializeEx(0, COINIT_MULTITHREADED);
 2 
 3 IXpsOMObjectFactory *xpsFactory;
 4 
 5 HRESULT hr = CoCreateInstance(__uuidof(XpsOMObjectFactory), NULL, CLSCTX_INPROC_SERVER,__uuidof(IXpsOMObjectFactory),reinterpret_cast<LPVOID*>(&xpsFactory));
 6 
 7 IXpsOMPackage *package = NULL;
 8 
 9 std::tstring tstrFilename = _T("C:\\MyXPSFile.xps");
10 
11 hr = xpsFactory->CreatePackageFromFile((LPCTSTR)tstrFilename.c_str(),FALSE,&package);
12 
13 HANDLE completionEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
14 
15 IXpsPrintJob *job = NULL;
16 IXpsPrintJobStream *jobStream = NULL;
17 
18 StartXpsPrintJob(_T("MyPrinter"),_T("Print Job 1"), NULL, NULL, completionEvent, NULL, 0, &job, &jobStream, NULL);
19 
20 hr = package->WriteToStream (jobStream, FALSE);
21 
22 hr = jobStream->Close();
23 
24 if (completionEvent != NULL)
25 {
26    if (WaitForSingleObject(completionEvent, INFINITE) == WAIT_OBJECT_0)
27    {
28       XPS_JOB_STATUS jobStatus;
29       hr = job->GetJobStatus(&jobStatus);
30    }
31 
32    CloseHandle(completionEvent);
33    completionEvent = NULL;
34 }
35 
36 jobStream->Release();
37 jobStream = NULL;
38 
39 job->Release();
40 job = NULL;
41 
42 package->Release();
43 package = NULL;

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部