导航 XPS OM

本主题介绍如何导航 XPS OM 以及访问内存中文档的不同部分。

XPS 文档 API 的组织反映了 XPS 文档的组织。 下表说明了 XPS 文档 API 的接口与 XPS 文档组件的关系。

XPS 文档组件 XPS 文档 API 接口 为层次结构中的下一个级别调用的方法
XPS 文档文件 (包含 OPC 包)
IXpsOMPackage
GetDocumentSequence
FixedDocumentSequence 部件
IXpsOMDocumentSequence
GetDocuments
FixedDocument 部件
IXpsOMDocument
GetPageReferences
FixedDocument 标记中的 PageContent 元素
IXpsOMPageReference
GetPage
CollectPartResources
FixedPage 部件
IXpsOMPage
GetVisuals
FixedPage 标记中的 Canvas 元素
IXpsOMCanvas
GetVisuals
FixedPage 标记中的 Path 元素
IXpsOMPath
文档层次结构的结尾。
FixedPage 标记中的字形元素
IXpsOMGlyphs
文档层次结构的结尾。
字体部件
IXpsOMFontResource
文档层次结构的结尾。
图像部件
IXpsOMImageResource
文档层次结构的结尾。

在程序中使用以下代码示例之前,请阅读 常见 XPS 文档编程任务中的免责声明。

代码示例

下面的代码示例假定存在初始化的 XPS OM,并且 指向表示该 XPS OM 的 IXpsOMPackage 接口。 有关创建 XPS OM 的信息,请参阅 将 XPS 文档读入 XPS OM创建空白 XPS OM

    HRESULT                        hr = S_OK;

    IXpsOMDocumentSequence         *docSeq = NULL;
    IXpsOMDocumentCollection       *docs = NULL;
    IXpsOMDocument                 *doc = NULL;
    IXpsOMPageReferenceCollection  *pages = NULL;
    IXpsOMPageReference            *pageRef = NULL;
    IXpsOMPage                     *page = NULL;
    IXpsOMCanvas                   *canvas = NULL;
    IXpsOMPath                     *path = NULL;
    IXpsOMPartResources            *pageParts = NULL;
    IXpsOMGlyphs                   *glyphs = NULL;
    IXpsOMFontResourceCollection   *fonts = NULL; 
    IXpsOMFontResource             *font = NULL;
    IXpsOMImageResourceCollection  *images = NULL;
    IXpsOMImageResource            *image = NULL;
    IXpsOMVisualCollection         *visuals = NULL;
    IXpsOMVisual                   *visual = NULL;

    UINT32  numDocs = 0;
    UINT32  thisDoc = 0;

    UINT32  numPageRefs = 0;
    UINT32  thisPageRef = 0;

    UINT32  numVisuals = 0;
    UINT32  thisVisual = 0;

    UINT32  numFonts = 0;
    UINT32  thisFont = 0;

    UINT32  numImages = 0;
    UINT32  thisImage = 0;

    XPS_OBJECT_TYPE  visualType;
 
    // package points to the IXpsOMPackage interface to walk.

    // Get the fixed document sequence of the package.
    hr = package->GetDocumentSequence(&docSeq);

    // Get the fixed documents in the fixed document sequence.
    hr = docSeq->GetDocuments(&docs);

    // Walk the collection of documents.
    hr = docs->GetCount(&numDocs);
    thisDoc = 0;
    while (thisDoc < numDocs) {
        hr = docs->GetAt(thisDoc, &doc);
        
        // 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);

                // Get the visual tree of this page.
                hr = page->GetVisuals (&visuals);
                
                // Walk the visuals.
                hr = visuals->GetCount (&numVisuals);
                thisVisual = 0;
                while (thisVisual < numVisuals) {
                    hr = visuals->GetAt (thisVisual, &visual);
                    // Get visual type.
                    hr = visual->GetType( &visualType );
                    
                    // Do other stuff with the visual.

                    // Release the visual.
                    if (NULL != visual) {visual->Release(); visual = NULL;}
                    thisVisual++; // Get next visual in collection.
                }
                if (NULL != visuals) {visuals->Release(); visuals = NULL;}
                if (NULL != page) {page->Release(); page = NULL;}
            }
            // Get the part resources used by this page.
            hr = pageRef->CollectPartResources (&pageParts);

            // Get the font resources.
            {
                hr = pageParts->GetFontResources (&fonts);
                
                // Walk the fonts.
                hr = fonts->GetCount (&numFonts);
                thisFont = 0;
                while (thisFont < numFonts) {
                    hr = fonts->GetAt(thisFont, &font);
                    if (NULL != font) {font->Release(); font = NULL;}

                    thisFont++; // Get the next font in collection.
                }
                if (NULL != fonts) {fonts->Release(); fonts = NULL;}
            }

            // Get the image resources.
            {
                hr = pageParts->GetImageResources (&images);

                // walk the images
                hr = images->GetCount (&numImages);
                thisImage = 0;
                while (thisImage < numImages) {
                    hr = images->GetAt(thisImage, &image);
                    thisImage++;
                    if (NULL != image) {image->Release(); image = NULL;}
                }
                if (NULL != images) {images->Release(); images = NULL;}
            }
            if (NULL != pageRef) {pageRef->Release(); pageRef = NULL;}
            thisPageRef++; // Get next page in doc.
        }
        if (NULL != pages) {pages->Release(); pages = NULL;}
        if (NULL != doc) {doc->Release(); doc = NULL;}
        thisDoc++; // Get next doc in collection.
    }

    // Release interface pointers.
    if (NULL != docs) docs->Release();
    if (NULL != docSeq) docSeq->Release();

后续步骤

将文本写入 XPS OM

在 XPS OM 中绘制图形

将图像放置在 XPS OM 中

在此页面中使用

IXpsOMDocument

IXpsOMDocumentCollection

IXpsOMDocumentSequence

IXpsOMFontResource

IXpsOMFontResourceCollection

IXpsOMImageResource

IXpsOMImageResourceCollection

IXpsOMPackage

IXpsOMPage

IXpsOMPageReference

IXpsOMPageReferenceCollection

IXpsOMVisual

IXpsOMVisualCollection

详细信息

初始化 XPS OM

将 XPS 文档读入 XPS OM

创建空白 XPS OM

打包 API

XPS 文档 API 参考

XML 纸张规范