Crear un OM XPS en blanco

En este tema se describe cómo crear un OM XPS en blanco. Presenta los ejemplos de código que muestran cómo usar un OM XPS para compilar la estructura de documentos de un documento XPS que tiene una página en blanco.

Para guardarse como documento XPS, el OM XPS requiere al menos los siguientes componentes:

Cuando se usan estas interfaces, el OM XPS contendrá un documento que tenga una página en blanco. Para crear este documento en un OM XPS, el programa debe crear primero los componentes individuales y, a continuación, vincularlos juntos.

Antes de usar los ejemplos de código siguientes, lea la declinación de responsabilidades en Tareas comunes de programación de documentos XPS.

Ejemplos de código

En el ejemplo de código siguiente se da por supuesto que la inicialización, descrita en Inicializar un OM XPS, se ha realizado correctamente.

    // Declare the variables used in this section.
    HRESULT                       hr = S_OK;
    IOpcPartUri                   *opcPartUri = NULL;
    IXpsOMPackage                 *xpsPackage = NULL;
    IXpsOMDocumentSequence        *xpsFDS = NULL;
    IXpsOMDocumentCollection      *fixedDocuments = NULL;
    IXpsOMDocument                *xpsFD = NULL;
    IXpsOMPage                    *xpsPage = NULL;
    IXpsOMPageReferenceCollection *pageRefs = NULL;
    IXpsOMPageReference           *xpsPageRef = NULL;
    // These values are set outside of this code example.
    XPS_SIZE pageSize = {width, height}; 
    
    // Create the package.
    hr = xpsFactory->CreatePackage( &xpsPackage );

    // Create the URI for the fixed document sequence part and then  
    //  create the fixed document sequence
    hr = xpsFactory->CreatePartUri( 
        L"/FixedDocumentSequence.fdseq", &opcPartUri );
    hr = xpsFactory->CreateDocumentSequence( opcPartUri, &xpsFDS );
    // Release this URI to reuse the interface pointer.
    if (NULL != opcPartUri) {opcPartUri->Release(); opcPartUri = NULL;}

    // Create the URI for the document part and then create the document.
    hr = xpsFactory->CreatePartUri( 
        L"/Documents/1/FixedDocument.fdoc", &opcPartUri );
    hr = xpsFactory->CreateDocument( opcPartUri, &xpsFD );
    // Release this URI to reuse the interface pointer.
    if (NULL != opcPartUri) {opcPartUri->Release(); opcPartUri = NULL;}

    // Create a blank page.
    hr = xpsFactory->CreatePartUri( 
        L"/Documents/1/Pages/1.fpage", &opcPartUri );
    hr = xpsFactory->CreatePage(
        &pageSize,                  // Page size
        L"en-US",                   // Page language
        opcPartUri,                 // Page part name
        &xpsPage);                
    // Release this URI to reuse the interface pointer.
    if (NULL != opcPartUri) {opcPartUri->Release(); opcPartUri = NULL;}

    // Create a page reference for the page.
    hr = xpsFactory->CreatePageReference( &pageSize, &xpsPageRef );

    // Add the fixed document sequence to the package.
    hr = xpsPackage->SetDocumentSequence( xpsFDS );

    // Get the document collection of the fixed document sequence
    //  and then add the document to the collection.
    hr = xpsFDS->GetDocuments( &fixedDocuments );
    hr = fixedDocuments->Append( xpsFD );

    // Get the page reference collection from the document
    //  and add the page reference and blank page.
    hr = xpsFD->GetPageReferences( &pageRefs );
    hr = pageRefs->Append( xpsPageRef );
    hr = xpsPageRef->SetPage( xpsPage );

    // Release interface pointer
    if (NULL != xpsPage) xpsPage->Release();
    if (NULL != pageRefs) pageRefs->Release();
    if (NULL != fixedDocuments) fixedDocuments->Release();
    if (NULL != xpsPageRef) xpsPageRef->Release();
    if (NULL != xpsFD) xpsFD->Release();
    if (NULL != xpsFDS) xpsFDS->Release();
    if (NULL != xpsPackage) xpsPackage->Release();

Procedimientos recomendados

Después de haber usado una interfaz IOpcPartUri para crear un componente (como después de llamar al método CreateDocument en el ejemplo de código),libere el puntero a esa interfaz—a menos que lo necesite para otra llamada.

Pasos siguientes

Navegar por el OM XPS

Escribir texto en un OM XPS

Dibujar gráficos en un OM XPS

Colocar imágenes en un OM XPS

Se usa en esta página

IOpcPartUri

IXpsOMObjectFactory

IXpsOMPackage

IXpsOMDocumentSequence

IXpsOMDocumentCollection

IXpsOMDocument

IXpsOMPage

IXpsOMPageReference

IXpsOMPageReferenceCollection

XPS_SIZE

Para obtener más información

Inicializar un OM XPS

API de empaquetado

Referencia de la API de documentos XPS

XML Paper Specification