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::TRUE, NullableBool::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: 하나의 문단에 여러 부분

단일 단락은 서로 다른 서식을 가진 여러 부분을 포함할 수 있습니다. 새 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

또한 보기

 한국어