大网数据广告活动
打印与设备

Navigate the XPS OM

· 52CE 我爱测

This topic describes how to navigate an XPS OM and to access different parts of the in-memory document.

This topic describes how to navigate an XPS OM and to access different parts of the in-memory document.

The organization of the XPS Document API mirrors the organization of an XPS document. The following table illustrates how the interfaces of the XPS Document API relate to the components of an XPS document.

XPS document component XPS Document API interface Method to call for the next level down in the hierarchy
XPS document file (contains OPC package) IXpsOMPackage GetDocumentSequence
FixedDocumentSequence part IXpsOMDocumentSequence GetDocuments
FixedDocument part IXpsOMDocument GetPageReferences
PageContent element in the FixedDocument markup IXpsOMPageReference GetPage CollectPartResources
FixedPage part IXpsOMPage GetVisuals
Canvas element in the FixedPage markup IXpsOMCanvas GetVisuals
Path element in the FixedPage markup IXpsOMPath End of document hierarchy.
Glyphs element in the FixedPage markup IXpsOMGlyphs End of document hierarchy.
Font part IXpsOMFontResource End of document hierarchy.
Image part IXpsOMImageResource End of document hierarchy.

Before using the following code examples in your program, read the disclaimer in Common XPS Document Programming Tasks.

Code Example

The following code example assumes the existence of an initialized XPS OM, and package points to an IXpsOMPackage interface that represents that XPS OM. For information about creating an XPS OM, see Read an XPS Document into an XPS OM or Create a Blank 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();

Related topics

Next Steps

Write Text to an XPS OM

Draw Graphics in an XPS OM

Place Images in an XPS OM

Used in This Page

IXpsOMDocument

IXpsOMDocumentCollection

IXpsOMDocumentSequence

IXpsOMFontResource

IXpsOMFontResourceCollection

IXpsOMImageResource

IXpsOMImageResourceCollection

IXpsOMPackage

IXpsOMPage

IXpsOMPageReference

IXpsOMPageReferenceCollection

IXpsOMVisual

IXpsOMVisualCollection

For More Information

Initialize an XPS OM

Read an XPS Document into an XPS OM

Create a Blank XPS OM

Packaging API

XPS Document API Reference

XML Paper Specification

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。