Silverlight:以编程方式打印XPS

时间:2010-04-30 16:47:25

标签: silverlight printing xps

是否有任何方法(库或本机)使用打印API来打印XPS文档?我尝试使用Document Toolkit API,但是当我尝试设置PrintPageEventArgs.PageVisual时,我得到了COM异常。

1 个答案:

答案 0 :(得分:1)

这应该在本机代码中执行,不要忘记先启用COM:

    SmartHandle event = ::CreateEvent(NULL, TRUE, FALSE, NULL);
    if (event.get() == 0)
    {
        ::AtlThrow(::GetLastError());
    }

    CComPtr<IXpsOMObjectFactory> pXpsFactory;
    HRESULT hr = pXpsFactory.CoCreateInstance(CLSID_XpsOMObjectFactory);
    if (FAILED(hr))
    {
        ::AtlThrow(hr);
    }

    CComPtr<IXpsPrintJobStream> pDocumentStream;
    hr = ::StartXpsPrintJob(printer, file, NULL, NULL, event.get(), NULL, NULL, NULL, &pDocumentStream, NULL);
    if (FAILED(hr))
    {
        ::AtlThrow(hr);
    }

    CComPtr<IXpsOMPackage> pXpsOMPackage;

    hr = pXpsFactory->CreatePackageFromFile(argv[2], false, &pXpsOMPackage);
    if (FAILED(hr))
    {
        ::AtlThrow(hr);
    }

    hr = pXpsOMPackage->WriteToStream(pDocumentStream, FALSE);
    if (FAILED(hr))
    {
        ::AtlThrow(hr);
    }

    pDocumentStream->Close();

    if (::WaitForSingleObject(event.get(), INFINITE) != WAIT_OBJECT_0)
    {
        ::AtlThrow(hr);
    }