Écrire du texte dans un modèle d’objet XPS

Cette rubrique explique comment écrire du texte dans un modèle d’objet XPS.

Le texte est placé dans un modèle OM XPS en créant et en formatant une interface IXpsOMGlyphs , puis en ajoutant l’interface IXpsOMGlyphs à la liste des objets visuels de la page ou de la zone de dessin. Chaque interface IXpsOMGlyphs représente une exécution de glyphe, qui est une exécution continue de caractères qui partagent un format commun. Lorsqu’un élément de format caractère (tel que le type ou la taille de police) change ou qu’un saut de ligne est nécessaire, une nouvelle interface IXpsOMGlyphs doit être créée et ajoutée à la liste des objets visuels.

Certaines propriétés d’une exécution de glyphes peuvent être définies à l’aide des méthodes de l’interface IXpsOMGlyphs . Toutefois, certaines propriétés interagissent avec d’autres et doivent être définies à l’aide d’une interface IXpsOMGlyphsEditor .

Avant d’utiliser les exemples de code suivants dans votre programme, lisez l’exclusion de responsabilité dans les tâches de programmation de document XPS courantes.

Exemple de code

Créer une exécution de glyphe à partir d’une chaîne

Une exécution de glyphe est couramment créée en plusieurs étapes, notamment le chargement des ressources de police utilisées par l’exécution du glyphe, la définition d’un pinceau de remplissage, la spécification de la taille de la police et de l’emplacement de départ, ainsi que la définition de la chaîne Unicode.

La section suivante de l’exemple de code contient une routine qui accepte certaines variables, y compris la taille de la police, la couleur et l’emplacement, ainsi que les caractères à écrire. Le code crée ensuite une exécution de glyphe, puis l’ajoute à une page. L’exemple de code suppose que l’initialisation décrite dans Initialize an XPS OM s’est produite et que le document comporte au moins une page. Pour plus d’informations sur la création d’un modèle d’objet XPS vide, consultez créer un modèle d’objet XPS vide.

// 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;
}

Charger et créer des ressources

La création d’une interface IXpsOMGlyphs nécessite une ressource de police. Dans de nombreux cas, un bloc de texte utilise la même police et la même couleur. Par conséquent, cette section de l’exemple de code créera les interfaces de ressource de police qui seront utilisées dans les appels qui placent le texte sur la page.

    // 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.

Dessiner du texte dans une page

La dernière section de l’exemple de code crée les exécutions de glyphes pour chaque exécution du texte mis en forme de la même façon. Pour exécuter le code dans cette section finale, l’interface xpsFactory , ainsi que la ressource de police et un pinceau de couleur de texte sont nécessaires et doivent avoir été instanciés et initialisés. Dans cet exemple, la fonction décrite dans la première section est utilisée pour créer les exécutions de glyphe et les ajouter à la page.

    // 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++;
    }

Next Steps

Naviguer dans le modèle d’objet XPS

Dessiner des graphiques dans un modèle d’objet XPS

Placer des images dans un modèle d’objet XPS

Écrire un modèle OM XPS dans un document XPS

Imprimer un modèle OM XPS

Utilisé dans cette section

IOpcPartUri

IXpsOMFontResource

IXpsOMGlyphs

IXpsOMGlyphsEditor

IXpsOMObjectFactory

IXpsOMPage

IXpsOMSolidColorBrush

IXpsOMVisual

IXpsOMVisualCollection

Pour plus d’informations

Initialiser un modèle d’objet XPS

Informations de référence sur l’API de document XPS

XML Paper Specification