วิธีจัดรูปแบบข้อความใน 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");หนึ่ง 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 ใหม่ลงในคอลเลกชัน 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() ตั้งค่าครอบครัวฟอนต์ Latin. หากไม่ได้ตั้งค่า จะใช้ฟอนต์ของธีมการนำเสนอ. ฟอนต์ที่กำหนดเองต้องฝังไว้หรือมีอยู่ในเครื่องที่ดู.
คำถามที่พบบ่อย
ฉันจะเปลี่ยนตระกูลฟอนต์อย่างไร?
ตั้งค่า 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