共用方式為


流覽 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 標記中的 Glyphs 元素
IXpsOMGlyphs
檔階層的結尾。
字型部分
IXpsOMFontResource
檔階層的結尾。
影像元件
IXpsOMImageResource
檔階層的結尾。

在程式中使用下列程式碼範例之前,請先閱讀 Common 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 紙張規格