จัดการรายการหัวข้อย่อยและลำดับเลขในงานนำเสนอด้วย C++

ภาพรวม

Aspose.Slides for C++ ช่วยให้คุณสร้างและจัดรูปแบบรายการที่มีหัวข้อย่อยและรายการลำดับเลขในงานนำเสนอ PowerPoint และ OpenDocument รายการหนึ่งเป็นย่อหน้าที่การตั้งค่าหัวข้อย่อยควบคุมผ่านรูปแบบย่อหน้า

ใช้เมธอด IParagraph::get_ParagraphFormat เพื่อเข้าถึงการตั้งค่ารายการระดับย่อหน้า จุดเริ่มต้นหลักคือ IParagraphFormat::get_Bullet, ซึ่งจะคืนค่าอ็อบเจ็กต์ IBulletFormat ด้วยอ็อบเจ็กต์นี้ คุณสามารถตั้งค่าประเภทหัวข้อย่อย สัญลักษณ์ รูปภาพ สี ขนาด สไตล์การลำดับเลข และเลขเริ่มต้นได้

บทความนี้แสดงวิธีการ:

  • สร้างรายการที่มีหัวข้อย่อยด้วยสัญลักษณ์กำหนดเอง
  • สรึงหัวข้อย่อยแบบรูปภาพ
  • สร้างรายการหลายระดับโดยตั้งค่าความลึกของย่อหน้า
  • สร้างรายการลำดับเลข
  • ตรวจสอบและเปลี่ยนรูปแบบรายการในงานนำเสนอที่มีอยู่
  • อ่านเครื่องหมายและหมายเลขรายการจริงพร้อมข้อความย่อหน้า

สร้างรายการที่มีหัวข้อย่อย

เพื่อสร้างรายการที่มีหัวข้อย่อย ให้เพิ่มอ็อบเจ็กต์ Paragraph ไปยัง ITextFrame และตั้งค่า IBulletFormat::set_Type เป็น BulletType::Symbol. จากนั้นคุณสามารถตั้งค่า IBulletFormat::set_Char, IBulletFormat::get_Color, และ IBulletFormat::set_Height เพื่อควบคุมลักษณะของหัวข้อย่อยได้

โค้ด C++ ด้านล่างแสดงวิธีการสร้างรายการที่มีหัวข้อย่อยในสไลด์:

#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBulletFormat.h>
#include <DOM/IColorFormat.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/NullableBool.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;

auto createParagraph = [](System::String text)
{
    auto paragraph = System::MakeObject<Paragraph>();
    auto paragraphFormat = paragraph->get_ParagraphFormat();
    auto bulletFormat = paragraphFormat->get_Bullet();

    bulletFormat->set_Type(BulletType::Symbol);
    bulletFormat->set_Char(u'*');
    paragraphFormat->set_Indent(15);
    bulletFormat->set_IsBulletHardColor(NullableBool::True);
    bulletFormat->get_Color()->set_Color(System::Drawing::Color::get_IndianRed());
    bulletFormat->set_Height(100);
    paragraph->set_Text(text);

    return paragraph;
};

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 20, 200, 50);

auto textFrame = autoShape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();

auto paragraph1 = createParagraph(u"The first paragraph");
textFrame->get_Paragraphs()->Add(paragraph1);

auto paragraph2 = createParagraph(u"The second paragraph");
textFrame->get_Paragraphs()->Add(paragraph2);

presentation->Save(u"symbol_bullets.pptx", SaveFormat::Pptx);
presentation->Dispose();

ผลลัพธ์:

สัญลักษณ์หัวข้อย่อย

สร้างรายการลำดับเลข

ใช้รายการลำดับเลขเมื่อลำดับของรายการมีความสำคัญ ตั้งค่า IBulletFormat::set_Type เป็น BulletType::Numbered. คุณยังสามารถเลือกรูปแบบการลำดับเลขด้วย IBulletFormat::set_NumberedBulletStyle หรือกำหนดค่าเริ่มต้นด้วย IBulletFormat::set_NumberedBulletStartWith เมื่อต้องการให้รายการเริ่มจากค่าที่ไม่ใช่ 1

โค้ด C++ ด้านล่างแสดงวิธีการสร้างรายการลำดับเลขในสไลด์:

#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBulletFormat.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 20, 90, 80);

auto textFrame = autoShape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();

auto paragraph1 = System::MakeObject<Paragraph>();
paragraph1->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Numbered);
paragraph1->set_Text(u"Apple");
textFrame->get_Paragraphs()->Add(paragraph1);

auto paragraph2 = System::MakeObject<Paragraph>();
paragraph2->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Numbered);
paragraph2->set_Text(u"Orange");
textFrame->get_Paragraphs()->Add(paragraph2);

auto paragraph3 = System::MakeObject<Paragraph>();
paragraph3->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Numbered);
paragraph3->set_Text(u"Banana");
textFrame->get_Paragraphs()->Add(paragraph3);

presentation->Save(u"numbered_bullets.pptx", SaveFormat::Pptx);
presentation->Dispose();

ผลลัพธ์:

หัวข้อย่อยลำดับเลข

สร้างหัวข้อย่อยแบบรูปภาพ

Aspose.Slides อนุญาตให้คุณแทนที่สัญลักษณ์หัวข้อย่อยปกติด้วยรูปภาพ หัวข้อย่อยแบบรูปภาพทำงานได้ดีที่สุดกับภาพที่ง่ายและยังอ่านได้เมื่อขนาดเล็ก เช่น ไอคอนหรือไฟล์ PNG โปร่งใสขนาดเล็ก

เพื่อสร้างหัวข้อย่อยแบบรูปภาพ ให้เพิ่มรูปภาพลงใน IPresentation::get_Images และกำหนดอ็อบเจ็กต์ IPPImage ที่คืนค่าให้กับ IBulletFormat::get_Picture. ตั้งค่า IBulletFormat::set_Type เป็น BulletType::Picture ก่อนกำหนดรูปภาพ

สมมุติว่าเรามีไฟล์ “image.png”:

รูปภาพสำหรับหัวข้อย่อย

โค้ด C++ ด้านล่างแสดงวิธีการสร้างหัวข้อย่อยแบบรูปภาพในสไลด์:

#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBulletFormat.h>
#include <DOM/IImageCollection.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/ITextFrame.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <IImage.h>
#include <Util/Images.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;

auto createParagraph = [](System::String text, System::SharedPtr<IPPImage> image)
{
    auto paragraph = System::MakeObject<Paragraph>();
    auto paragraphFormat = paragraph->get_ParagraphFormat();
    auto bulletFormat = paragraphFormat->get_Bullet();

    bulletFormat->set_Type(BulletType::Picture);
    bulletFormat->get_Picture()->set_Image(image);
    paragraphFormat->set_Indent(15);
    bulletFormat->set_Height(100);
    paragraph->set_Text(text);

    return paragraph;
};

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 20, 200, 50);

auto textFrame = autoShape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();

auto sourceImage = Images::FromFile(u"image.png");
auto bulletImage = presentation->get_Images()->AddImage(sourceImage);
sourceImage->Dispose();

auto paragraph1 = createParagraph(u"The first paragraph", bulletImage);
textFrame->get_Paragraphs()->Add(paragraph1);

auto paragraph2 = createParagraph(u"The second paragraph", bulletImage);
textFrame->get_Paragraphs()->Add(paragraph2);

presentation->Save(u"picture_bullets.pptx", SaveFormat::Pptx);
presentation->Dispose();

ผลลัพธ์:

หัวข้อย่อยแบบรูปภาพ

สร้างรายการหลายระดับ

ใช้ IParagraphFormat::set_Depth เพื่อวางรายการในระดับที่ต่างกัน ระดับ 0 คือระดับบนสุด ระดับ 1 อยู่ด้านในระดับนั้น และต่อไป

โค้ด C++ ด้านล่างแสดงวิธีการสร้างรายการหลายระดับที่มีหัวข้อย่อย:

#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;

auto presentation = System::MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);

auto autoShape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 20, 20, 260, 110);

auto textFrame = autoShape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();

auto paragraph1 = System::MakeObject<Paragraph>();
paragraph1->get_ParagraphFormat()->set_Depth(0);
paragraph1->set_Text(u"My text - Depth 0");
textFrame->get_Paragraphs()->Add(paragraph1);

auto paragraph2 = System::MakeObject<Paragraph>();
paragraph2->get_ParagraphFormat()->set_Depth(1);
paragraph2->set_Text(u"My text - Depth 1");
textFrame->get_Paragraphs()->Add(paragraph2);

auto paragraph3 = System::MakeObject<Paragraph>();
paragraph3->get_ParagraphFormat()->set_Depth(2);
paragraph3->set_Text(u"My text - Depth 2");
textFrame->get_Paragraphs()->Add(paragraph3);

auto paragraph4 = System::MakeObject<Paragraph>();
paragraph4->get_ParagraphFormat()->set_Depth(3);
paragraph4->set_Text(u"My text - Depth 3");
textFrame->get_Paragraphs()->Add(paragraph4);

presentation->Save(u"multilevel_bullets.pptx", SaveFormat::Pptx);
presentation->Dispose();

ผลลัพธ์:

รายการหลายระดับ

เปลี่ยนรายการที่มีอยู่

เพื่อเปลี่ยนรูปแบบรายการในงานนำเสนอที่มีอยู่ ให้เข้าถึงย่อหน้าที่ต้องการและอัปเดตการตั้งค่า IParagraphFormat::get_Bullet ของมัน คุณสมบัติเหล่านั้นที่ใช้ในการสร้างรายการสามารถใช้เพื่อตรวจสอบหรือแก้ไขรายการที่โหลดจากไฟล์ PPT, PPTX หรือ ODP ได้เช่นกัน

โค้ด C++ ด้านล่างเปลี่ยนย่อหน้าตัวแรกในกรอบข้อความให้ใช้สไตล์รายการลำดับเลข:

#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBulletFormat.h>
#include <DOM/IParagraph.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/NumberedBulletStyle.h>
#include <DOM/Presentation.h>
#include <Export/SaveFormat.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;

auto presentation = System::MakeObject<Presentation>(u"input.pptx");
auto slide = presentation->get_Slide(0);
auto autoShape = System::ExplicitCast<IAutoShape>(slide->get_Shape(0));

auto paragraph = autoShape->get_TextFrame()->get_Paragraph(0);
auto paragraphFormat = paragraph->get_ParagraphFormat();
auto bulletFormat = paragraphFormat->get_Bullet();

bulletFormat->set_Type(BulletType::Numbered);
bulletFormat->set_NumberedBulletStyle(NumberedBulletStyle::BulletRomanUCPeriod);
bulletFormat->set_NumberedBulletStartWith(1);
paragraphFormat->set_MarginLeft(30);
paragraphFormat->set_Indent(-20);

presentation->Save(u"updated_list.pptx", SaveFormat::Pptx);
presentation->Dispose();

อ่านค่าหัวข้อย่อยจริง

เมื่อสกัดเนื้อหางานนำเสนอ ให้รักษาเครื่องหมายรายการไว้คู่กับข้อความย่อหน้า เรียก IParagraphFormat::GetEffective และเข้าถึง IParagraphFormatEffectiveData::get_Bullet เพื่อรับรูปแบบหัวข้อย่อยที่มีผลตรวจสอบ get_Type ก่อนอ่าน get_ActualBulletValue

สำหรับรายการลำดับเลข ให้แยกแยะค่าสามค่าเหล่านี้:

  • get_NumberedBulletStyle กำหนดรูปแบบ เช่น ตัวเลขอาหรับตามด้วยจุด หรืออักษรเล็กตามด้วยจุด
  • get_NumberedBulletStartWith ระบุเลขเริ่มต้นสำหรับกลุ่มย่อหน้าที่เป็นลำดับเลข
  • get_ActualBulletValue คืนค่าเครื่องหมายที่จัดรูปแบบสำหรับย่อหน้าที่กำหนด ตัวอย่าง รายการที่สองในรายการที่เริ่มที่ 5 จะคืนค่า 6.

เก็บรักษา IParagraphFormat::get_Depth กับแต่ละย่อหน้าเพื่อคงระดับการซ้อนกันของมัน เครื่องหมายจริงไม่ได้เข้ารหัสลำดับชั้นทั้งหมด: รายการที่ซ้อนอาจคืนค่า c. แทนการรวมเป็นป้ายเช่น 6.c.

ตัวอย่างที่ทำงานอิสระต่อไปนี้สร้างรายการ บันทึกลงงานนำเสนอ และโหลดงานนำเสนอใหม่เพื่อรายงานข้อความย่อหน้า ความลึก ประเภทหัวข้อย่อยและค่าจริงของแต่ละรายการ สัญลักษณ์รูปภาพสีน้ำเงินขนาดเล็กสร้างจาก SVG ภายใน

#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IBulletFormat.h>
#include <DOM/IBulletFormatEffectiveData.h>
#include <DOM/IColorFormat.h>
#include <DOM/IFillFormat.h>
#include <DOM/IImageCollection.h>
#include <DOM/ILineFormat.h>
#include <DOM/IParagraph.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IParagraphFormat.h>
#include <DOM/IParagraphFormatEffectiveData.h>
#include <DOM/IPortionFormat.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ISlideSize.h>
#include <DOM/ISlidesPicture.h>
#include <DOM/ITextFrame.h>
#include <DOM/FillType.h>
#include <DOM/NullableBool.h>
#include <DOM/NumberedBulletStyle.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/SlideSizeScaleType.h>
#include <DOM/SvgImage.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/console.h>

using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System::Drawing;

{
    auto presentation = System::MakeObject<Presentation>();
    presentation->get_SlideSize()->SetSize(720, 440, SlideSizeScaleType::DoNotScale);
    auto slide = presentation->get_Slide(0);

    auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 30, 20, 660, 400);
    shape->get_FillFormat()->set_FillType(FillType::NoFill);
    shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);

    auto textFrame = shape->get_TextFrame();
    textFrame->get_Paragraphs()->Clear();

    // สร้างเครื่องหมายรูปภาพโดยไม่ใช้ไฟล์ภาพภายนอก.
    auto svg = System::MakeObject<SvgImage>(u"<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16'><rect width='16' height='16' rx='4' fill='#0078D4'/></svg>");
    auto bulletImage = presentation->get_Images()->AddImage(svg);

    auto addParagraph = [&](System::String text, int16_t depth, BulletType type, int16_t start = 1, NumberedBulletStyle style = NumberedBulletStyle::BulletArabicPeriod)
    {
        auto paragraph = System::MakeObject<Paragraph>();
        paragraph->set_Text(text);

        auto format = paragraph->get_ParagraphFormat();
        format->set_Depth(depth);
        format->set_MarginLeft(30 + depth * 40);
        format->set_Indent(-28);
        format->set_SpaceAfter(8);
        format->get_DefaultPortionFormat()->set_FontHeight(22);
        format->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
        format->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
        auto bullet = format->get_Bullet();
        bullet->set_Type(type);
        bullet->set_IsBulletHardColor(NullableBool::True);
        bullet->get_Color()->set_Color(Color::get_Black());
        if (type == BulletType::Numbered)
        {
            bullet->set_NumberedBulletStyle(style);
            bullet->set_NumberedBulletStartWith(start);
        }
        else if (type == BulletType::Symbol)
        {
            bullet->set_Char(u'\u2022');
        }
        else if (type == BulletType::Picture)
        {
            bullet->get_Picture()->set_Image(bulletImage);
        }

        textFrame->get_Paragraphs()->Add(paragraph);
    };

    addParagraph(u"First item", 0, BulletType::Numbered, 5);
    addParagraph(u"Second item", 0, BulletType::Numbered, 5);
    addParagraph(u"Nested item", 1, BulletType::Numbered, 3, NumberedBulletStyle::BulletAlphaLCPeriod);
    addParagraph(u"Next nested item", 1, BulletType::Numbered, 3, NumberedBulletStyle::BulletAlphaLCPeriod);
    addParagraph(u"Deep item", 2, BulletType::Numbered, 4, NumberedBulletStyle::BulletRomanLCPeriod);
    addParagraph(u"Back to top level", 0, BulletType::Numbered, 5);
    addParagraph(u"Symbol marker", 0, BulletType::Symbol);
    addParagraph(u"No bullet", 0, BulletType::None);
    addParagraph(u"Picture marker", 0, BulletType::Picture);
    presentation->Save(u"actual_bullet_values.pptx", SaveFormat::Pptx);
    presentation->Dispose();
}

auto loadedPresentation = System::MakeObject<Presentation>(u"actual_bullet_values.pptx");
auto loadedShape = System::ExplicitCast<IAutoShape>(loadedPresentation->get_Slide(0)->get_Shape(0));
System::Console::WriteLine(u"Text | Depth | Type | Actual value");
for (auto paragraph : loadedShape->get_TextFrame()->get_Paragraphs())
{
    auto bullet = paragraph->get_ParagraphFormat()->GetEffective()->get_Bullet();
    auto actualValue = bullet->get_ActualBulletValue();
    System::String marker;
    switch (bullet->get_Type())
    {
        case BulletType::Numbered:
        case BulletType::Symbol:
            marker = actualValue;
            break;
        case BulletType::None:
            marker = (actualValue.IsNull() ? System::String(u"null") : actualValue) + u" (no bullet)";
            break;
        case BulletType::Picture:
            marker = (actualValue.IsNull() ? System::String(u"null") : actualValue) + u" (image required)";
            break;
        default:
            marker = u"unsupported bullet type";
            break;
    }
    System::Console::WriteLine(u"{0} | {1} | {2} | {3}", paragraph->get_Text(), paragraph->get_ParagraphFormat()->get_Depth(), bullet->get_Type(), marker);
}
loadedPresentation->Dispose();

รายการต้นฉบับแสดงด้านล่าง.

รายการเริ่มที่ 5 รายการย่อยตัวอักษรและโรมัน สัญลักษณ์สัญลักษณ์ ย่อหน้าที่ไม่มีหัวข้อย่อย และสัญลักษณ์รูปภาพสีฟ้า

รายงานตรงกับเครื่องหมายที่แสดงใน PowerPoint:

Text | Depth | Type | Actual value
First item | 0 | Numbered | 5.
Second item | 0 | Numbered | 6.
Nested item | 1 | Numbered | c.
Next nested item | 1 | Numbered | d.
Deep item | 2 | Numbered | iv.
Back to top level | 0 | Numbered | 7.
Symbol marker | 0 | Symbol | •
No bullet | 0 | None | null (no bullet)
Picture marker | 0 | Picture | null (image required)

สำหรับ BulletType::None and BulletType::Picture , get_ActualBulletValue คืนค่า System::String เป็น null ไม่ใช่สตริงว่าง ใช้ IsNull เพื่อตรวจสอบค่าดังกล่าว จัดการประเภทเหล่านี้แยกกัน: ตัวแรกไม่มีเครื่องหมาย ในขณะที่ตัวหลังต้องดึงรูปภาพหัวข้อย่อยออกมาแยกต่างหาก หัวข้อย่อยแบบรูปภาพไม่สามารถทำซ้ำได้โดยการสกัดเครื่องหมายข้อความอย่างเดียว

คำถามที่พบบ่อย

Can bulleted and numbered lists be exported to PDF or images?

Yes. Aspose.Slides preserves list formatting when the target format supports the corresponding text layout and bullet features.

Can I edit lists in existing presentations?

Yes. Load the presentation, access the target paragraph, inspect or update its IParagraphFormat::get_Bullet settings, and save the presentation.

Can lists contain non-Latin text?

Yes. List item text can contain Unicode characters, so you can create lists in multilingual presentations. Make sure the fonts used in the presentation support the characters you need.