C++でテキストをフォーマットする方法

C++でテキストをフォーマットする方法

Aspose.Slides FOSS for C++ は、PortionFormat クラスを通じて細かいテキスト書式設定を提供します。Portion はテキストの最小の独立単位であり、段落内の単一の書式ランに対応します。このガイドでは、プレゼンテーション内のテキストに太字、斜体、フォントサイズ、カラーの書式設定を適用する方法を示します。

ステップバイステップガイド

ステップ 1: ライブラリのビルドとリンク

git clone https://github.com/aspose-slides-foss/Aspose.Slides-FOSS-for-Cpp.git
cd Aspose.Slides-FOSS-for-Cpp && mkdir build && cd build
cmake . && cmake --build .

ステップ 2: テキスト フレーム付きのシェイプを追加

テキストをフォーマットする前に、シェイプを追加し、shape.text_frame()->set_text() を介してテキスト内容を設定してください。

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
    shape.text_frame()->set_text("Default text: will be formatted");
    prs.save("output.pptx", asf::SaveFormat::PPTX);
    return 0;
}

ステップ 3: TextFrame にアクセス

shape.text_frame() はシェイプの TextFrame へのポインタを返します。-> を使用して、その上でメソッドを呼び出します。

auto* tf = shape.text_frame();          // pointer to the shape's text frame
tf->set_text("your text here");

TextFrameParagraph 個のオブジェクト(tf->paragraphs())を含みます。
ParagraphPortion 個のオブジェクト(paragraph.portions())を含みます。


ステップ 4: 太字と斜体の書式を適用

portion_format().set_font_bold()portion_format().set_font_italic() を使用してください。これらのメソッドは NullableBool::TRUENullableBool::FALSE、または NullableBool::NOT_DEFINED(マスターから継承)を受け入れます。

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
    shape.text_frame()->set_text("Bold and italic text");
    auto* tf = shape.text_frame();

    auto& fmt = tf->paragraphs()[0].portions()[0].portion_format();
    fmt.set_font_bold(asf::NullableBool::TRUE);
    fmt.set_font_italic(asf::NullableBool::TRUE);

    prs.save("bold-italic.pptx", asf::SaveFormat::PPTX);
    return 0;
}

ステップ5: フォントサイズと色を設定

サイズ(ポイント)には portion_format().set_font_height() を設定し、色には fill_format() を使用してください。

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 500, 150);
    shape.text_frame()->set_text("Large corporate-blue heading");
    auto* tf = shape.text_frame();

    auto& fmt = tf->paragraphs()[0].portions()[0].portion_format();
    fmt.set_font_height(32);                               // 32pt font
    fmt.set_font_bold(asf::NullableBool::TRUE);
    fmt.fill_format().set_fill_type(asf::FillType::SOLID);
    fmt.fill_format().solid_fill_color().set_color(
        asf::Color::from_argb(255, 0, 70, 127));

    prs.save("colored-text.pptx", asf::SaveFormat::PPTX);
    return 0;
}

Color::from_argb(alpha, red, green, blue) は各チャンネルに対して 0〜255 の値を受け入れます。


ステップ 6: 1つの段落に複数の部分

単一の段落には、異なる書式設定を持つ複数の部分を含めることができます。段落の portions() コレクションに新しい Portion を追加します:

#include <Aspose/Slides/Foss/presentation.h>

int main() {
    namespace asf = Aspose::Slides::Foss;

    asf::Presentation prs;
    auto& slide = prs.slides()[0];
    auto& shape = slide.shapes().add_auto_shape(
        asf::ShapeType::RECTANGLE, 50, 50, 600, 100);
    shape.text_frame()->set_text(""); // start with empty text
    auto* tf = shape.text_frame();

    auto& paragraph = tf->paragraphs()[0];

    // First portion: normal text
    auto& portion1 = paragraph.portions()[0];
    portion1.set_text("Normal text followed by ");
    portion1.portion_format().set_font_height(20);

    // Second portion: bold red text
    asf::Portion portion2;
    portion2.set_text("bold red text");
    portion2.portion_format().set_font_height(20);
    portion2.portion_format().set_font_bold(asf::NullableBool::TRUE);
    portion2.portion_format().fill_format().set_fill_type(asf::FillType::SOLID);
    portion2.portion_format().fill_format().solid_fill_color().set_color(
        asf::Color::from_argb(255, 200, 0, 0));
    paragraph.portions().add(portion2);

    prs.save("mixed-format.pptx", asf::SaveFormat::PPTX);
    return 0;
}

一般的な問題と対策

色を設定してもテキストが黒のままです

色を割り当てる前に fill_format().set_fill_type(FillType::SOLID) が設定されていることを確認してください。塗りつぶしタイプを設定しないと、色の変更が効果を持たない可能性があります。

NullableBool::TRUEtrue

portion_format().set_font_bold()NullableBool::TRUE を期待しており、C++ の true ではありません。true を直接渡すと、オーバーロード解決に応じてコンパイルエラーになるか、未定義動作になります。

保存したファイルにフォントが表示されません

set_latin_font() メソッドはラテンフォントファミリーを設定します。設定されていない場合、プレゼンテーションテーマのフォントが使用されます。カスタムフォントは埋め込むか、閲覧マシンにインストールされている必要があります。


よくある質問

フォント ファミリーを変更するにはどうすればよいですか?

設定 portion_format().set_latin_font():

fmt.set_latin_font(asf::FontData("Arial"));

FontData はフォントファミリ名を文字列として受け取ります。

段落の配置を設定するにはどうすればよいですか?

paragraph_format().set_alignment()を使用してください:

tf.paragraphs()[0].paragraph_format().set_alignment(asf::TextAlignment::CENTER);

サポートされている値: LEFT, CENTER, RIGHT, JUSTIFY

行間を設定するにはどうすればよいですか?

paragraph_format().set_space_before()(段落の前にポイント)またはparagraph_format().set_space_after()(段落の後にポイント)を使用してください:

tf.paragraphs()[0].paragraph_format().set_space_before(12); // 12pt before
tf.paragraphs()[0].paragraph_format().set_space_after(6);   // 6pt after

参照

 日本語