Compartilhar via


Navegar pelo OM do XPS

Este tópico descreve como navegar em um OM XPS e acessar diferentes partes do documento na memória.

A organização da API de Documento XPS espelha a organização de um documento XPS. A tabela a seguir ilustra como as interfaces da API de Documento XPS se relacionam com os componentes de um documento XPS.

Componente do documento XPS Interface da API de Documento XPS Método para chamar para o próximo nível abaixo na hierarquia
Arquivo de documento XPS (contém pacote OPC)
IXpsOMPackage
GetDocumentSequence
Parte FixedDocumentSequence
IXpsOMDocumentSequence
GetDocuments
Parte FixedDocument
IXpsOMDocument
GetPageReferences
Elemento PageContent na marcação FixedDocument
IXpsOMPageReference
GetPage
CollectPartResources
Parte FixedPage
IXpsOMPage
GetVisuals
Elemento Canvas na marcação FixedPage
IXpsOMCanvas
GetVisuals
Elemento Path na marcação FixedPage
IXpsOMPath
Fim da hierarquia de documentos.
Elemento Glifos na marcação FixedPage
IXpsOMGlyphs
Fim da hierarquia de documentos.
Parte da fonte
IXpsOMFontResource
Fim da hierarquia de documentos.
Parte da imagem
IXpsOMImageResource
Fim da hierarquia de documentos.

Antes de usar os exemplos de código a seguir em seu programa, leia o aviso de isenção de responsabilidade em Tarefas comuns de programação de documentos XPS.

Exemplo de código

O exemplo de código a seguir pressupõe a existência de um OM XPS inicializado e o pacote aponta para uma interface IXpsOMPackage que representa esse OM XPS. Para obter informações sobre como criar um OM XPS, consulte Ler um documento XPS em um OM XPS ou Criar um OM XPS em branco.

    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();

Próximas etapas

Gravar texto em um OM XPS

Desenhar gráficos em um OM XPS

Colocar imagens em um OM XPS

Usado nesta página

IXpsOMDocument

IXpsOMDocumentCollection

IXpsOMDocumentSequence

IXpsOMFontResource

IXpsOMFontResourceCollection

IXpsOMImageResource

IXpsOMImageResourceCollection

IXpsOMPackage

IXpsOMPage

IXpsOMPageReference

IXpsOMPageReferenceCollection

IXpsOMVisual

IXpsOMVisualCollection

Para obter mais informações

Inicializar um OM XPS

Ler um documento XPS em um OM XPS

Criar um OM XPS em branco

API de empacotamento

Referência da API de Documento XPS

Especificação de Papel XML