建立空白 XPS OM
本主題描述如何建立空白 XPS OM。 它提供程式碼範例,說明如何使用 XPS OM 來建置具有一個空白頁面之 XPS 檔的檔結構。
若要儲存為 XPS 檔,XPS OM 至少需要下列元件:
- 描述 XPS 檔套件的 IXpsOMPackage
- 包含套件內容的架構的 IXpsOMDocumentSequence
- 包含套件內檔架構的 IXpsOMDocument
- 包含檔中頁面集合的 IXpsOMPageReference
- 包含空白頁面的 IXpsOMPage
使用這些介面時,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 方法之後),請釋放該介面的指標,除非您需要它進行另一個呼叫。
相關主題
-
後續步驟
-
用於此頁面
-
詳細資訊
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應