C++에서 텍스트를 서식 지정하는 방법
Aspose.Slides FOSS for C++ provides fine-grained text formatting through the PortionFormat class. 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을 추가하고 해당 텍스트 내용을 통해 설정합니다. 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() shape의 포인터를 반환합니다. 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: 하나의 단락에 여러 Portion 사용
단일 단락은 서로 다른 서식을 가진 여러 부분을 포함할 수 있습니다. 새 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::TRUE vs true
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