Criar um OM do XPS em branco
Este tópico descreve como criar um OM do XPS em branco. Ele apresenta os exemplos de código que ilustram como usar um OM do XPS para criar a estrutura do documento de um documento XPS que tem uma página em branco.
Para ser salvo como um documento XPS, o OM do XPS requer pelo menos os seguintes componentes:
- Um IXpsOMPackage que descreve o pacote de documentos XPS
- Um IXpsOMDocumentSequence que contém a estrutura do conteúdo do pacote
- Um IXpsOMDocument que contém a estrutura de um documento dentro do pacote
- Um IXpsOMPageReference que contém a coleção de páginas no documento
- Um IXpsOMPage que contém uma página em branco
Quando essas interfaces são usadas, o OM do XPS conterá um documento que tem uma página em branco. Para criar este documento em um OM do XPS, o programa deve primeiro criar os componentes individuais e, em seguida, vinculá-los juntos.
Antes de usar os exemplos de código a seguir, leia o aviso de isenção de responsabilidade nas Tarefas comuns de programação de documentos XPS.
Exemplos de código
O exemplo de código a seguir pressupõe que a inicialização, descrita em Inicializar um OM do XPS, foi bem-sucedida.
// 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();
Práticas Recomendadas
Depois de usar uma interface IOpcPartUri para criar um componente (como depois de chamar o método CreateDocument no exemplo de código), libere o ponteiro para essa interface, a menos que você precise dele para outra chamada.
Tópicos relacionados
-
Próximas Etapas
-
Usado nesta página
-
Para obter mais informações