Compartilhar via


Gravar texto em um OM XPS

Este tópico descreve como gravar texto em um OM XPS.

O texto é colocado em um OM XPS criando e formatando uma interface IXpsOMGlyphs e, em seguida, adicionando a interface IXpsOMGlyphs à lista de objetos visuais da página ou da tela. Cada interface IXpsOMGlyphs representa uma execução de glifo, que é uma execução contínua de caracteres que compartilham um formato comum. Quando um elemento de formato de caractere (como tipo ou tamanho de fonte) é alterado ou quando uma linha quebra, uma nova interface IXpsOMGlyphs deve ser criada e adicionada à lista de objetos visuais.

Algumas das propriedades de uma execução de glifo podem ser definidas usando os métodos da interface IXpsOMGlyphs Algumas propriedades, no entanto, interagem com outras e devem ser definidas usando uma interface IXpsOMGlyphsEditor

Antes de usar os exemplos de código a seguir no seu programa, leia o aviso de isenção de responsabilidade em Tarefas Comuns de Programação do Documento XPS.

Exemplo de código

Criar uma execução de glifo a partir de uma cadeia de caracteres

Uma execução de glifo é comumente criada em várias etapas que incluem carregar os recursos de fonte que são usados pela execução de glifo, definir um pincel de preenchimento, especificar o tamanho da fonte e o local de início e definir a cadeia de caracteres Unicode.

A seção a seguir do exemplo de código contém uma rotina que aceita algumas variáveis, incluindo o tamanho da fonte, cor e local e os caracteres a serem gravados. Em seguida, o código cria uma execução de glifo e, em seguida, adiciona-o a uma página. O exemplo de código pressupõe que a inicialização descrita em Inicializar um OM XPS ocorreu e que o documento tem pelo menos uma página. Para mais informações sobre como criar um OM XPS em branco, consulte Criar um WM XPS em branco.

// Write Text to an XPS OM
HRESULT
WriteText_AddTextToPage(
            // A pre-created object factory
    __in    IXpsOMObjectFactory   *xpsFactory,
            // The font resource to use for this run
    __in    IXpsOMFontResource    *xpsFont,
            // The font size
    __in    float                 fontEmSize,
            // The solid color brush to use for the font
    __in    IXpsOMSolidColorBrush *xpsBrush,
            // The starting location of this glyph run
    __in    XPS_POINT             *origin,
            // The text to use for this run
    __in    LPCWSTR               unicodeString,
            // The page on which to write this glyph run
    __inout IXpsOMPage            *xpsPage        
)
{
    // The data type definitions are included in this function
    // for the convenience of this code example. In an actual
    // implementation they would probably belong in a header file.
    HRESULT                       hr = S_OK;
    XPS_POINT                     glyphsOrigin = {origin->x, origin->y};
    IXpsOMGlyphsEditor            *glyphsEditor = NULL;
    IXpsOMGlyphs                  *xpsGlyphs = NULL;
    IXpsOMVisualCollection        *pageVisuals = NULL;

    // Create a new Glyphs object and set its properties.
    hr = xpsFactory->CreateGlyphs(xpsFont, &xpsGlyphs);
    hr = xpsGlyphs->SetOrigin(&glyphsOrigin);
    hr = xpsGlyphs->SetFontRenderingEmSize(fontEmSize);
    hr = xpsGlyphs->SetFillBrushLocal(xpsBrush);

    // Some properties are inter-dependent so they
    //    must be changed by using a GlyphsEditor.
    hr = xpsGlyphs->GetGlyphsEditor(&glyphsEditor);
    hr = glyphsEditor->SetUnicodeString(unicodeString);
    hr = glyphsEditor->ApplyEdits();

    // Add the new Glyphs object to the page
    hr = xpsPage->GetVisuals(&pageVisuals);
    hr = pageVisuals->Append(xpsGlyphs);

    // Release interface pointers.
    if (NULL != xpsGlyphs) xpsGlyphs->Release();
    if (NULL != glyphsEditor) glyphsEditor->Release();
    if (NULL != pageVisuals) pageVisuals->Release();

    return hr;
}

Carregar e criar recursos

A criação de uma interface IXpsOMGlyphs requer um recurso de fonte. Em muitos casos, um bloco de texto usa a mesma fonte e cor. Portanto, esta seção do exemplo de código criará as interfaces de recurso de fonte que serão usadas nas chamadas que colocam o texto na página.

    // fontFileName is the name of the font file and it 
    //  is defined outside of this example.

    HRESULT                       hr = S_OK;

    GUID                          fontNameGuid;
    WCHAR                         guidString[128] = {0};
    WCHAR                         uriString[256] = {0};

    IStream                       *fontStream  = NULL;
    IOpcPartUri                   *fontUri = NULL;
    IXpsOMFontResource            *fontResource = NULL;
    IXpsOMVisualCollection        *pageVisuals = NULL;
    IXpsOMPage                    *page = NULL;
    IXpsOMVisual                  *canvasVisual = NULL;
    IXpsOMSolidColorBrush         *xpsTextColor = NULL;
    XPS_COLOR                     xpsColorBodyText;
 
    // Create font stream.
    hr = xpsFactory->CreateReadOnlyStreamOnFile ( 
        fontFileName, &fontStream );
    
    // Create new obfuscated part name for this resource using a GUID.
    hr = CoCreateGuid( &fontNameGuid );
    hr = StringFromGUID2( 
            fontNameGuid, 
            guidString, 
            ARRAYSIZE(guidString));

    // Create a URI string for this font resource that will place 
    //  the font part in the /Resources/Fonts folder of the package.
    wcscpy_s(uriString, ARRAYSIZE(uriString), L"/Resources/Fonts/");

    // Create the part name using the GUID string as the name and 
    //  ".odttf" as the extension GUID string start and ends with 
    //  curly braces so they are removed.
    wcsncat_s(uriString, ARRAYSIZE(uriString), 
        guidString + 1, wcslen(guidString) - 2); 
    wcscat_s(uriString, ARRAYSIZE(uriString), L".odttf");

    // Create the font URI interface.
    hr = xpsFactory->CreatePartUri(
        uriString,
        &fontUri);
    // Create the font resource.
    hr = xpsFactory->CreateFontResource(
        fontStream,
        XPS_FONT_EMBEDDING_OBFUSCATED,
        fontUri,
        FALSE,     // isObfSourceStream
        &fontResource);
    if (NULL != fontUri) fontUri->Release();

    // Create the brush to use for the font.
    xpsColorBodyText.colorType = XPS_COLOR_TYPE_SRGB;
    xpsColorBodyText.value.sRGB.alpha = 0xFF;
    xpsColorBodyText.value.sRGB.red = 0x00;
    xpsColorBodyText.value.sRGB.green = 0x00;
    xpsColorBodyText.value.sRGB.blue = 0x00;

    hr = xpsFactory->CreateSolidColorBrush( 
        &xpsColorBodyText,
        NULL, // This color type does not use a color profile resource.             
        &xpsTextColor);

    // xpsTextColor is released after it has been used.

Desenhar texto em uma página

A seção final do exemplo de código cria o glifo executado para cada execução de texto formatado de forma semelhante. Para executar o código nesta seção final, a interface xpsFactory bem como o recurso de fonte e um pincel de cor de texto são necessários e devem ter sido instanciados e inicializados. Neste exemplo, a função descrita na primeira seção é usada para criar as execuções de glifo e adicioná-las à página.

    // The following interfaces are created outside of this example.

    // The page on which to place the text.
    IXpsOMPage                *page = NULL;

    // The object factory used by this program.
    IXpsOMObjectFactory       *xpsFactory = NULL;

    // The font resource created in the previous snippet.
    IXpsOMFontResource        *fontResource = NULL;

    // The color brush created in the previous snippet.
    IXpsOMSolidColorBrush     *xpsTextColor = NULL;

    // The following variables are defined outside of this example.

    // An array of pointers to the Unicode strings to write.
    LPCWSTR                   *textRuns = NULL;

    // An array of start points that correspond to the 
    //    strings in textRuns.
    XPS_POINT                 *startPoints = NULL;

    // The number of text runs to add to the page.
    UINT32                    numRuns = 0;            

    HRESULT                   hr = S_OK;

    FLOAT                     fontSize = 7.56f;
    UINT32                    thisRun;

    // Add all the text runs to the page.
    thisRun = 0;
    while (thisRun < numRuns) {  
        hr = WriteText_AddTextToPage(
            xpsFactory, 
            fontResource,
            fontSize,
            xpsTextColor,
            &startPoints[thisRun],
            textRuns[thisRun],
            page);
        thisRun++;
    }

Próximas Etapas 

Navegar pelo OM XPS

Desenhar gráficos em um OM XPS

Inserir imagens em um OM XPS

Escrever um OM XPS em um documento XPS

Imprimir um OM XPS

Usado nesta seção

IOpcPartUri

IXpsOMFontResource

IXpsOMGlyphs

IXpsOMGlyphsEditor

IXpsOMObjectFactory

IXpsOMPage

IXpsOMSolidColorBrush

IXpsOMVisual

IXpsOMVisualCollection

Para obter mais informações

Inicializar um OM XPS

Referência da API de documento XPS

XML Paper Specification