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

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

Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat クラス。A 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");

A TextFrame 含む Paragraph オブジェクト(tf->paragraphs())。各 Paragraph 含む Portion オブジェクト (paragraph.portions()).


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

使用 portion_format().set_font_bold() および portion_format().set_font_italic(). これらのメソッドは受け付けます NullableBool::TRUE, NullableBool::FALSE,、または NullableBool::NOT_DEFINED (master から継承)。.

#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 つの段落に複数のポーションを配置する

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

#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

参照

 日本語