使用 IXpsOMDocument 接口

本主题介绍提供对 XPS OM 的文档级组件的访问的接口。

接口名称 逻辑子接口 说明
IXpsOMDocument
IXpsOMPageReference
表示单个 FixedDocument 部件并绑定页引用的集合。
IXpsOMPageReferenceCollection 是用于循环访问文档中 的 IXpsOMPageReference 接口的集合接口。
IXpsOMDocumentStructureResource

表示 DocumentStructure 部件。

代码示例

本节中的代码示例演示了如何在程序中使用某些文档接口。

获取文档的页面引用

下面的代码示例获取指向 IXpsOMPageReferenceCollection 的指针,该指针包含 doc 参数引用的文档的 IXpsOMPageReference 接口列表。

    HRESULT                               hr = S_OK;


    IXpsOMPageReferenceCollection         *pages;
    IXpsOMPageReference                   *pageRef;
    IXpsOMPage                            *page;

    UINT32  numPageRefs = 0;
    UINT32  thisPageRef = 0;

    // get the doc contents
    hr = doc->GetPageReferences(&pages);
        
    // walk the collection of page references
    hr = pages->GetCount(&numPageRefs);
    thisPageRef = 0;
    while (thisPageRef < numPageRefs) {
        // get this page reference
        hr = pages->GetAt(thisPageRef, &pageRef);

        // get the page content of this page reference
        hr = pageRef->GetPage (&page);

        // use the page

        // free this page & page reference and go to next
        page->Release();
        pageRef->Release();
        thisPageRef++;
    }

    pages->Release();

获取文档的文档结构

下面的代码示例获取包含文档结构的资源。

    HRESULT                             hr = S_OK;
    
    IXpsOMDocumentStructureResource     *docStruct;
    IStream                             *docStructResStream;

    // doc is passed in as an argument
    // get the doc contents
    hr = doc->GetDocumentStructureResource(&docStruct);
   
    hr = docStruct->GetStream ( &docStructResStream );

    // access the document structure resource 
    //  contents by reading from the stream

    docStructResStream->Release();
    docStruct->Release();