วิธีจัดรูปแบบข้อความใน 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() คืนค่าตัวชี้ไปยังของ shape’s 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 เทียบกับ 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