次の方法で共有


XPS OM にテキストを書き込む

このトピックでは、XPS OM にテキストを書き込む方法について説明します。

テキストは、 IXpsOMGlyphs インターフェイスを作成して書式設定し、ページまたはキャンバスのビジュアル オブジェクトの一覧 に IXpsOMGlyphs インターフェイスを 追加することによって XPS OM に配置されます。 各 IXpsOMGlyphs インターフェイスは、共通の形式を共有する文字の連続実行であるグリフ実行を表します。 文字書式要素 (フォントの種類やサイズなど) が変更されたときや改行が発生した場合は、新しい IXpsOMGlyphs インターフェイスを作成してビジュアル オブジェクトの一覧に追加する必要があります。

グリフ実行のプロパティの一部は、 IXpsOMGlyphs インターフェイスのメソッドを使用して設定できます。 ただし、一部のプロパティは他のプロパティと対話し、 IXpsOMGlyphsEditor インターフェイスを使用して設定する必要があります。

プログラムで次のコード例を使用する前に、「一般的な XPS ドキュメント プログラミング タスク 免責事項」を参照してください。

コード例

文字列からグリフ実行を作成する

グリフ実行は、グリフ実行で使用されるフォント リソースの読み込み、塗りつぶしブラシの設定、フォント サイズと開始位置の指定、Unicode 文字列の設定など、いくつかの手順で一般的に作成されます。

コード例の次のセクションには、フォント サイズ、色、場所、書き込む文字など、いくつかの変数を受け入れるルーチンが含まれています。 次に、グリフ実行を作成し、それをページに追加します。 このコード例では、「 XPS OM の初期化 」で説明されている初期化が行われ、ドキュメントに少なくとも 1 つのページがあることを前提としています。 空の XPS OM の作成の詳細については、「空の XPS OM を作成する」を参照してください。

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

リソースの読み込みと作成

IXpsOMGlyphs インターフェイスを作成するには、フォント リソースが必要です。 多くの場合、テキストブロックは同じフォントと色を使用します。 そのため、コード例のこのセクションでは、ページにテキストを配置する呼び出しで使用されるフォント リソース インターフェイスを作成します。

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

ページにテキストを描画する

コード例の最後のセクションでは、同様に書式設定されたテキストの実行ごとにグリフの実行を作成します。 この最後のセクションでコードを実行するには、 xpsFactory インターフェイスとフォント リソースとテキスト カラー ブラシが必要であり、インスタンス化および初期化されている必要があります。 この例では、最初のセクションで説明した関数を使用して、グリフの実行を作成し、ページに追加します。

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

次の手順

XPS OM をナビゲートする

XPS OM でグラフィックスを描画する

XPS OM にイメージを配置する

XPS OM を XPS ドキュメント に書き込む

XPS OM を印刷する

このセクションで使用

IOpcPartUri

IXpsOMFontResource

IXpsOMGlyphs

IXpsOMGlyphsEditor

IXpsOMObjectFactory

IXpsOMPage

IXpsOMSolidColorBrush

IXpsOMVisual

IXpsOMVisualCollection

詳細

XPS OM を初期化する

XPS ドキュメント API リファレンス

XML 紙の仕様