建立空白 XPS OM

本主題描述如何建立空白 XPS OM。 它提供程式碼範例,說明如何使用 XPS OM 來建置具有一個空白頁面之 XPS 檔的檔結構。

若要儲存為 XPS 檔,XPS OM 至少需要下列元件:

使用這些介面時,XPS OM 將包含一個空白頁面的檔。 若要在 XPS OM 中建立這份檔,程式必須先建立個別元件,然後再將它們連結在一起。

使用下列程式碼範例之前,請先閱讀 Common XPS 檔程式設計工作 中的 免責聲明。

程式碼範例

下列程式碼範例假設初始化 XPS OM 中所述 的初始化已成功。

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

最佳做法

使用 IOpcPartUri 介面來建立元件之後(例如,在程式碼範例中呼叫 CreateDocument 方法之後),請釋放該介面的指標,除非您需要它進行另一個呼叫。

後續步驟

流覽 XPS OM

將文字寫入 XPS OM

在 XPS OM 中繪製圖形

將影像放在 XPS OM 中

用於此頁面

IOpcPartUri

IXpsOMObjectFactory

IXpsOMPackage

IXpsOMDocumentSequence

IXpsOMDocumentCollection

IXpsOMDocument

IXpsOMPage

IXpsOMPageReference

IXpsOMPageReferenceCollection

XPS_SIZE

詳細資訊

初始化 XPS OM

封裝 API

XPS 檔 API 參考

XML 紙張規格