Erstellen eines leeren XPS-OM

In diesem Thema wird beschrieben, wie Sie ein leeres XPS-OM erstellen. Es stellt die Codebeispiele dar, die veranschaulichen, wie sie ein XPS-OM verwenden, um die Dokumentstruktur eines XPS-Dokuments zu erstellen, das eine leere Seite enthält.

Um als XPS-Dokument gespeichert zu werden, benötigt das XPS-OM mindestens die folgenden Komponenten:

Wenn diese Schnittstellen verwendet werden, enthält das XPS-OM ein Dokument mit einer leeren Seite. Um dieses Dokument in einem XPS-OM zu erstellen, muss das Programm zuerst die einzelnen Komponenten erstellen und dann miteinander verknüpfen.

Lesen Sie vor der Verwendung der folgenden Codebeispiele den Haftungsausschluss in Allgemeine XPS-Dokumentprogrammierungsaufgaben.

Codebeispiele

Im folgenden Codebeispiel wird davon ausgegangen, dass die in Initialisieren eines XPS-OM beschriebene Initialisierung erfolgreich war.

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

Bewährte Methoden

Nachdem Sie eine IOpcPartUri-Schnittstelle zum Erstellen einer Komponente (z. B. nach dem Aufrufen der CreateDocument-Methode im Codebeispiel) verwendet haben, geben Sie den Zeiger auf diese Schnittstelle frei, es sei denn, Sie benötigen sie für einen anderen Aufruf.

Nächste Schritte

Navigieren im XPS-OM

Schreiben von Text in ein XPS-OM

Zeichnen von Grafiken in einem XPS-OM

Platzieren von Bildern in einem XPS-OM

Wird auf dieser Seite verwendet

IOpcPartUri

IXpsOMObjectFactory

IXpsOMPackage

IXpsOMDocumentSequence

IXpsOMDocumentCollection

IXpsOMDocument

IXpsOMPage

IXpsOMPageReference

IXpsOMPageReferenceCollection

XPS_SIZE

Weitere Informationen

Initialisieren eines XPS-OM

Verpacken von APIs

XPS-Dokument-API-Referenz

XML Paper Specification