إدارة فقرات نص PowerPoint في C++
نظرة عامة
يمثل Aspose.Slides للـ C++ النص كهرمية من إطارات النص والفقرات والأجزاء:
- ITextFrame يمثل حاوية النص داخل الشكل ويوفر الوصول إلى مجموعة الفقرات الخاصة به.
- IParagraph يمثل فقرة واحدة في إطار النص ويوفر الوصول إلى أجزائه وتنسيق مستوى الفقرة.
- IPortion يمثل مقطع نص داخل الفقرة. يمكن لكل جزء أن يكون له نصه الخاص وتنسيق مستوى الحرف.
وبالتالي يمكن للفقرة أن تحتوي على نص بخطوط وألوان وأحجام وتنسيقات مختلفة باستخدام أجزاء متعددة.
إنشاء وتنسيق الفقرات
أنشئ فقرات في إطار النص الخاص بالشكل واستخدم الأجزاء لتنسيق مقاطع النص الفردية. يوضح هذا القسم كيفية دمج أجزاء متعددة داخل كل فقرة وتطبيق ألوان مختلفة، وأحجام خطوط، وأنماط.
إنشاء فقرات مع أجزاء متعددة
المثال التالي بلغة C++ ينشئ ثلاث فقرات، كل منها يحتوي على ثلاث مقاطع نص في مجموعة IParagraph::get_Portions. يعيد استعمال الفقرة الافتراضية والجزء الفارغ، ثم يضيف الفقرات والأجزاء المتبقية. يتم تطبيق التنسيق عبر IPortion::get_PortionFormat: الجزء الأول في كل فقرة أحمر، عريض، بحجم 15 نقطة؛ الثاني أزرق، مائل، بحجم 18 نقطة. الجزء الثالث يحتفظ بالتنسيق الافتراضي:
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IPortionCollection.h>
#include <DOM/NullableBool.h>
#include <DOM/Paragraph.h>
#include <DOM/Portion.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/string.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 150, 300, 150);
auto textFrame = shape->get_TextFrame();
auto firstParagraph = textFrame->get_Paragraph(0);
firstParagraph->get_Portions()->Add(MakeObject<Portion>());
firstParagraph->get_Portions()->Add(MakeObject<Portion>());
auto secondParagraph = MakeObject<Paragraph>();
secondParagraph->get_Portions()->Add(MakeObject<Portion>());
secondParagraph->get_Portions()->Add(MakeObject<Portion>());
secondParagraph->get_Portions()->Add(MakeObject<Portion>());
textFrame->get_Paragraphs()->Add(secondParagraph);
auto thirdParagraph = MakeObject<Paragraph>();
thirdParagraph->get_Portions()->Add(MakeObject<Portion>());
thirdParagraph->get_Portions()->Add(MakeObject<Portion>());
thirdParagraph->get_Portions()->Add(MakeObject<Portion>());
textFrame->get_Paragraphs()->Add(thirdParagraph);
auto paragraphCount = textFrame->get_Paragraphs()->get_Count();
for (auto paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++)
{
auto paragraph = textFrame->get_Paragraph(paragraphIndex);
auto portionCount = paragraph->get_Portions()->get_Count();
for (auto portionIndex = 0; portionIndex < portionCount; portionIndex++)
{
auto portion = paragraph->get_Portion(portionIndex);
portion->set_Text(String::Format(u"Portion {0}.{1}", paragraphIndex + 1, portionIndex + 1));
auto portionFormat = portion->get_PortionFormat();
if (portionIndex == 0)
{
portionFormat->get_FillFormat()->set_FillType(FillType::Solid);
portionFormat->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red());
portionFormat->set_FontBold(NullableBool::True);
portionFormat->set_FontHeight(15);
}
else if (portionIndex == 1)
{
portionFormat->get_FillFormat()->set_FillType(FillType::Solid);
portionFormat->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Blue());
portionFormat->set_FontItalic(NullableBool::True);
portionFormat->set_FontHeight(18);
}
}
}
presentation->Save(u"paragraphs_with_portions.pptx", SaveFormat::Pptx);
presentation->Dispose();
إنشاء قوائم نقطية ومرقمة
نظم نص الفقرات في قوائم برموز أو أرقام أو صور مخصصة. تغطي الأمثلة في هذا القسم تنسيق النقاط، القوائم المتعددة المستويات، وتحديد أرقام البداية المخصصة.
إنشاء قائمة نقطية أو مرقمة
تسهل النقاط والترقيم قراءة العناصر المرتبطة. في Aspose.Slides يتم تعريف إعدادات القوائم عبر IBulletFormat.
يضيف هذا المثال بلغة C++ نقطة برمز وعدًداً إلى نفس إطار النص. يحدد IBulletFormat::set_Type نوع النقطة، بينما يزود IBulletFormat::set_Char الرمز وIBulletFormat::set_NumberedBulletStyle نمط الترقيم. يستخدم العنصران نقاطًا سوداء بارتفاع 100 ٪ وإزاحة فقرة قدرها 25 نقطة:
#include <DOM/BulletType.h>
#include <DOM/ColorType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/NullableBool.h>
#include <DOM/NumberedBulletStyle.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/convert.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
auto textFrame = shape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();
auto symbolParagraph = MakeObject<Paragraph>();
symbolParagraph->set_Text(u"Welcome to Aspose.Slides");
symbolParagraph->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Symbol);
symbolParagraph->get_ParagraphFormat()->get_Bullet()->set_Char(Convert::ToChar(0x2022));
symbolParagraph->get_ParagraphFormat()->set_Indent(25);
symbolParagraph->get_ParagraphFormat()->get_Bullet()->get_Color()->set_ColorType(ColorType::RGB);
symbolParagraph->get_ParagraphFormat()->get_Bullet()->get_Color()->set_Color(Color::get_Black());
symbolParagraph->get_ParagraphFormat()->get_Bullet()->set_IsBulletHardColor(NullableBool::True);
symbolParagraph->get_ParagraphFormat()->get_Bullet()->set_Height(100);
textFrame->get_Paragraphs()->Add(symbolParagraph);
auto numberedParagraph = MakeObject<Paragraph>();
numberedParagraph->set_Text(u"This is a numbered item");
numberedParagraph->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Numbered);
numberedParagraph->get_ParagraphFormat()->get_Bullet()->set_NumberedBulletStyle(NumberedBulletStyle::BulletCircleNumWDBlackPlain);
numberedParagraph->get_ParagraphFormat()->set_Indent(25);
numberedParagraph->get_ParagraphFormat()->get_Bullet()->get_Color()->set_ColorType(ColorType::RGB);
numberedParagraph->get_ParagraphFormat()->get_Bullet()->get_Color()->set_Color(Color::get_Black());
numberedParagraph->get_ParagraphFormat()->get_Bullet()->set_IsBulletHardColor(NullableBool::True);
numberedParagraph->get_ParagraphFormat()->get_Bullet()->set_Height(100);
textFrame->get_Paragraphs()->Add(numberedParagraph);
presentation->Save(u"bulleted_and_numbered_list.pptx", SaveFormat::Pptx);
presentation->Dispose();
استخدام نقاط صورة
تتيح نقاط الصورة استخدام صورة مخصصة بدلاً من رمز أو رقم.
يتطلب المثال وجود bullets.png في دليل العمل. يضيف هذا المثال بلغة C++ صورة إلى مجموعة صور العرض كـ IPPImage ويستخدمها كنقطة فقرة. يضبط IBulletFormat::set_Type لاستخدام نقطة صورة ويعيّن الصورة عبر IBulletFormat::get_Picture. يُضبط IBulletFormat::set_Height على 100 ٪ لتحديد حجم النقطة نسبة إلى نص الفقرة:
#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IImageCollection.h>
#include <DOM/IParagraphCollection.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;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto bulletImage = Images::FromFile(u"bullets.png");
auto presentationImage = presentation->get_Images()->AddImage(bulletImage);
bulletImage->Dispose();
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
auto textFrame = shape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();
auto paragraph = MakeObject<Paragraph>();
paragraph->set_Text(u"Welcome to Aspose.Slides");
paragraph->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Picture);
paragraph->get_ParagraphFormat()->get_Bullet()->get_Picture()->set_Image(presentationImage);
paragraph->get_ParagraphFormat()->get_Bullet()->set_Height(100);
textFrame->get_Paragraphs()->Add(paragraph);
presentation->Save(u"picture_bullet.pptx", SaveFormat::Pptx);
presentation->Save(u"picture_bullet.ppt", SaveFormat::Ppt);
presentation->Dispose();
إنشاء قائمة متعددة المستويات
حدد IParagraphFormat::set_Depth لوضع الفقرات في مستويات مختلفة من القائمة. المستوى الأعلى له عمق 0.
يُعيّن هذا المثال بلغة C++ قيم IParagraphFormat::set_Depth من 0 إلى 3 لأربع فقرات، مُنشئًا هرمًا داخل إطار نص واحد. تستخدم كل فقرة نقطة رمز، مع تبديل IBulletFormat::set_Char بين نقطة دائرية وشرطة لتمييز المستويات المتجاورة:
#include <DOM/BulletType.h>
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
#include <system/convert.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
auto textFrame = shape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();
auto firstParagraph = MakeObject<Paragraph>();
firstParagraph->set_Text(u"Content");
firstParagraph->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Symbol);
firstParagraph->get_ParagraphFormat()->get_Bullet()->set_Char(Convert::ToChar(0x2022));
firstParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
firstParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
firstParagraph->get_ParagraphFormat()->set_Depth(0);
auto secondParagraph = MakeObject<Paragraph>();
secondParagraph->set_Text(u"Second level");
secondParagraph->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Symbol);
secondParagraph->get_ParagraphFormat()->get_Bullet()->set_Char(u'-');
secondParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
secondParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
secondParagraph->get_ParagraphFormat()->set_Depth(1);
auto thirdParagraph = MakeObject<Paragraph>();
thirdParagraph->set_Text(u"Third level");
thirdParagraph->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Symbol);
thirdParagraph->get_ParagraphFormat()->get_Bullet()->set_Char(Convert::ToChar(0x2022));
thirdParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
thirdParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
thirdParagraph->get_ParagraphFormat()->set_Depth(2);
auto fourthParagraph = MakeObject<Paragraph>();
fourthParagraph->set_Text(u"Fourth level");
fourthParagraph->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Symbol);
fourthParagraph->get_ParagraphFormat()->get_Bullet()->set_Char(u'-');
fourthParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
fourthParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
fourthParagraph->get_ParagraphFormat()->set_Depth(3);
textFrame->get_Paragraphs()->Add(firstParagraph);
textFrame->get_Paragraphs()->Add(secondParagraph);
textFrame->get_Paragraphs()->Add(thirdParagraph);
textFrame->get_Paragraphs()->Add(fourthParagraph);
presentation->Save(u"multilevel_list.pptx", SaveFormat::Pptx);
presentation->Dispose();
بدء عناصر القائمة المرقمة بقيم مخصصة
استخدم IBulletFormat::set_NumberedBulletStartWith لتحديد الرقم الأول الذي يُعرض للفقرة المرقمة.
يحدد هذا المثال بلغة C++ IBulletFormat::set_Type لاستخدام الترقيم لثلاث فقرات ويُعيّن قيم IBulletFormat::set_NumberedBulletStartWith 2 و3 و7. يتم تكوين قيمة البدء بشكل منفصل لكل فقرة، مما يسمح بتخطي الأرقام:
#include <DOM/BulletType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.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;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
auto textFrame = shape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();
auto firstParagraph = MakeObject<Paragraph>();
firstParagraph->set_Text(u"Start at 2");
firstParagraph->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Numbered);
firstParagraph->get_ParagraphFormat()->get_Bullet()->set_NumberedBulletStartWith(2);
textFrame->get_Paragraphs()->Add(firstParagraph);
auto secondParagraph = MakeObject<Paragraph>();
secondParagraph->set_Text(u"Start at 3");
secondParagraph->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Numbered);
secondParagraph->get_ParagraphFormat()->get_Bullet()->set_NumberedBulletStartWith(3);
textFrame->get_Paragraphs()->Add(secondParagraph);
auto thirdParagraph = MakeObject<Paragraph>();
thirdParagraph->set_Text(u"Start at 7");
thirdParagraph->get_ParagraphFormat()->get_Bullet()->set_Type(BulletType::Numbered);
thirdParagraph->get_ParagraphFormat()->get_Bullet()->set_NumberedBulletStartWith(7);
textFrame->get_Paragraphs()->Add(thirdParagraph);
presentation->Save(u"custom_numbered_list.pptx", SaveFormat::Pptx);
presentation->Dispose();
التحكم في تخطيط الفقرة وخصائص النهاية
عدّل هوامش الفقرة وإزاحات السطر للتحكم في موضع السطر الأول والسطروف المتعدد. يمكنك أيضًا تنسيق علامة النهاية للفقرة بشكل منفصل عن أجزاء النص المرئية.
تحديد إزاحة السطر الأول
استخدم طريقة IParagraphFormat::set_Indent للتحكم في إزاحة السطر الأول للفقرة. تتحرك هذه الطريقة السطر الأول فقط نسبة إلى الهامش الأيسر للفقرة. القيمة الموجبة تُحرك السطر الأول إلى اليمين، بينما تُبقى الأسطر المتبقية محاذاة مع جسم الفقرة.
استخدم IParagraphFormat::set_MarginLeft عندما تحتاج إلى تحريك الفقرة بالكامل. استخدم IParagraphFormat::set_Indent عندما تحتاج إلى تحريك السطر الأول فقط.
يعطي المثال التالي ثلاث فقرات نفس IParagraphFormat::set_MarginLeft البالغ 20 نقطة وقيم IParagraphFormat::set_Indent 0 و20 و40 نقطة. تبدأ الأسطر الأولى بإزاحات متزايدة، بينما تحتفظ الأسطر المتداخلة بنفس الهامش الأيسر:
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/TextAutofitType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 420, 220);
shape->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Gray());
auto textFrame = shape->get_TextFrame();
textFrame->get_TextFrameFormat()->set_AutofitType(TextAutofitType::Shape);
textFrame->get_Paragraphs()->Clear();
auto firstParagraph = MakeObject<Paragraph>();
firstParagraph->set_Text(u"No first-line indent. Wrapped lines start at the same position as the first line.");
firstParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
firstParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
firstParagraph->get_ParagraphFormat()->set_MarginLeft(20);
firstParagraph->get_ParagraphFormat()->set_Indent(0);
auto secondParagraph = MakeObject<Paragraph>();
secondParagraph->set_Text(u"First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body.");
secondParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
secondParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
secondParagraph->get_ParagraphFormat()->set_MarginLeft(20);
secondParagraph->get_ParagraphFormat()->set_Indent(20);
auto thirdParagraph = MakeObject<Paragraph>();
thirdParagraph->set_Text(u"First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see.");
thirdParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
thirdParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
thirdParagraph->get_ParagraphFormat()->set_MarginLeft(20);
thirdParagraph->get_ParagraphFormat()->set_Indent(40);
textFrame->get_Paragraphs()->Add(firstParagraph);
textFrame->get_Paragraphs()->Add(secondParagraph);
textFrame->get_Paragraphs()->Add(thirdParagraph);
presentation->Save(u"paragraph_indent.pptx", SaveFormat::Pptx);
presentation->Dispose();
النتيجة:

تحديد إزاحة معلقة
الإزاحة المعلقة هي تخطيط فقرة يبدأ فيه السطر الأول إلى اليسار عن باقي الأسطر. في Aspose.Slides، تُنشئ هذا التأثير باستخدام طريقة IParagraphFormat::set_Indent. مرّر قيمة سالبة لتحريك السطر الأول إلى اليسار نسبة إلى جسم الفقرة.
في الواقع، يحدد IParagraphFormat::set_MarginLeft موضع الجسم الأيسر للفقرة، ويحدد IParagraphFormat::set_Indent موضع السطر الأول نسبة إلى ذلك الهامش. لإنشاء إزاحة معلقة، عيّن قيمة MarginLeft موجبة وقيمة Indent سالبة.
يُستخدم هذا التنسيق في الببليوغرافيات، المراجع، المدخلات القاموسية، وغيرها من الفقرات التي يجب أن تكون الأسطر المتداخلة محاذية تحت جسم الفقرة وليس تحت الحرف الأول للسطر الأول.
يقوم المثال التالي بلغة C++ بزوج قيم IParagraphFormat::set_MarginLeft 40 و60 نقطة مع قيم IParagraphFormat::set_Indent -20 و-30 نقطة. في كل فقرة، يبدأ السطر الأول إلى اليسار عن الأسطر المتداخلة؛ الفقرة الثانية لها إزاحة معلقة أعمق:
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/TextAutofitType.h>
#include <Export/SaveFormat.h>
#include <drawing/color.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::Drawing;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 420, 220);
shape->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid);
shape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Gray());
auto textFrame = shape->get_TextFrame();
textFrame->get_TextFrameFormat()->set_AutofitType(TextAutofitType::Shape);
textFrame->get_Paragraphs()->Clear();
auto firstParagraph = MakeObject<Paragraph>();
firstParagraph->set_Text(u"A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body.");
firstParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
firstParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
firstParagraph->get_ParagraphFormat()->set_MarginLeft(40);
firstParagraph->get_ParagraphFormat()->set_Indent(-20);
auto secondParagraph = MakeObject<Paragraph>();
secondParagraph->set_Text(u"This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare.");
secondParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
secondParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Black());
secondParagraph->get_ParagraphFormat()->set_MarginLeft(60);
secondParagraph->get_ParagraphFormat()->set_Indent(-30);
textFrame->get_Paragraphs()->Add(firstParagraph);
textFrame->get_Paragraphs()->Add(secondParagraph);
presentation->Save(u"hanging_indent.pptx", SaveFormat::Pptx);
presentation->Dispose();
النتيجة:

تحديد خصائص تشغيل نهاية الفقرة
تتحكم الطريقة IParagraph::set_EndParagraphPortionFormat بتنسيق علامة النهاية للفقرة. يحمل المثال ملف Test.pptx الذي يجب أن يحتوي على شريحة واحدة على الأقل. ينشئ المثال التالي PortionFormat مع تعيين IBasePortionFormat::set_FontHeight إلى 48 نقطة وIBasePortionFormat::set_LatinFont إلى Times New Roman. يعيّن هذا التنسيق إلى علامة النهاية للفقرة الثانية، مع ترك أجزاء النص المرئية دون تغيير:
#include <DOM/Fonts/FontData.h>
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IPortionCollection.h>
#include <DOM/Paragraph.h>
#include <DOM/Portion.h>
#include <DOM/PortionFormat.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>(u"Test.pptx");
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 10, 10, 200, 250);
auto textFrame = shape->get_TextFrame();
textFrame->get_Paragraphs()->Clear();
auto firstParagraph = MakeObject<Paragraph>();
firstParagraph->get_Portions()->Add(MakeObject<Portion>(u"Sample text"));
auto secondParagraph = MakeObject<Paragraph>();
secondParagraph->get_Portions()->Add(MakeObject<Portion>(u"Sample text 2"));
auto endParagraphFormat = MakeObject<PortionFormat>();
endParagraphFormat->set_FontHeight(48);
endParagraphFormat->set_LatinFont(MakeObject<FontData>(u"Times New Roman"));
secondParagraph->set_EndParagraphPortionFormat(endParagraphFormat);
textFrame->get_Paragraphs()->Add(firstParagraph);
textFrame->get_Paragraphs()->Add(secondParagraph);
presentation->Save(u"end_paragraph_format.pptx", SaveFormat::Pptx);
presentation->Dispose();
عدد الأسطر المرسومة
للقواعد التي تؤثر على التفاف النص الآلي وعلامات الترقيم في نهاية السطر، راجع Control Line Breaking وControl Hanging Punctuation.
استخدم IParagraph::GetLinesCount لحساب عدد الأسطر التي تحتلها فقرة بعد تخطيط النص، بما في ذلك التفاف النص الآلي. هذا مفيد عند فحص طول النص وتخطيط القالب العرضي.
الفقرة هي عنصر في ITextFrame::get_Paragraphs، ويمكن أن تحتل عدة أسطر مرسومة. كسر السطر الصريح داخل الفقرة يُنشئ سطرًا جديدًا دون إنشاء فقرة أخرى. التفاف النص الآلي يُنشئ أسطرًا بناءً على العرض المتاح دون إدخال كسر سطر صريح في النص. لذا فإن عد الفقرات أو أحرف كسر السطر لا يعطي عدد الأسطر المرسومة.
ينشئ المثال التالي شكل نص، يحسب أسطره، يضيق الشكل، ثم يستبدل النص بسلسلة أقصر. يفعّل المثال ITextFrameFormat::set_WrapText ويعطّل ITextFrameFormat::set_AutofitType بحيث يتحكم عرض الشكل في التفاف النص دون تصغير النص أو تعديل حجم الشكل تلقائيًا. كل قياس يستخدم IParagraph::GetLinesCount. أبعاد الشكل بالوحدات النقطية. أخيرًا، يضيف المثال فقرة أخرى ويجمع عدد الأسطر عبر إطار النص.
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/NullableBool.h>
#include <DOM/Paragraph.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <DOM/TextAutofitType.h>
#include <system/console.h>
using namespace Aspose::Slides;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 50, 50, 400, 200);
auto textFrame = shape->get_TextFrame();
textFrame->get_TextFrameFormat()->set_WrapText(NullableBool::True);
textFrame->get_TextFrameFormat()->set_AutofitType(TextAutofitType::None);
auto paragraph = textFrame->get_Paragraph(0);
paragraph->get_ParagraphFormat()->get_DefaultPortionFormat()->set_FontHeight(20);
paragraph->set_Text(u"This text demonstrates how automatic wrapping changes the number of rendered lines.");
Console::WriteLine(u"Original width: {0}", paragraph->GetLinesCount());
shape->set_Width(150);
Console::WriteLine(u"Narrower shape: {0}", paragraph->GetLinesCount());
paragraph->set_Text(u"Short text.");
Console::WriteLine(u"Shorter text: {0}", paragraph->GetLinesCount());
auto secondParagraph = MakeObject<Paragraph>();
secondParagraph->set_Text(u"Another paragraph.");
secondParagraph->get_ParagraphFormat()->get_DefaultPortionFormat()->set_FontHeight(20);
textFrame->get_Paragraphs()->Add(secondParagraph);
auto totalLineCount = 0;
for (auto currentParagraph : textFrame->get_Paragraphs())
{
totalLineCount += currentParagraph->GetLinesCount();
}
Console::WriteLine(u"Total lines in the text frame: {0}", totalLineCount);
presentation->Dispose();
مع هذا النص وهذه الأبعاد، يزيد تضييق الشكل عدد الأسطر، بينما يقلل استبدال النص بالسلسلة القصيرة عدد الأسطر. قد تختلف الأعداد الفعلية حسب توفر الخطوط والاستبدال، حجم الخط، الهوامش، الإزاحات، التفاف النص، وإعدادات الملاءمة التلقائية. استخدم الخطوط وإعدادات التخطيط المقصودة للبيئة المستهدفة عند فحص القالب.
عدد الأسطر بمفرده لا يحدد ما إذا كان النص يفيض عن حاويته. الارتفاع المتاح، ارتفاع الأسطر، تباعد الفقرات والأسطر، وسلوك الملاءمة التلقائية كلها عوامل مهمة؛ حتى سطر واحد يمكن أن يتجاوز العرض المتاح عندما يُعطل التفاف النص.
استيراد وتصدير محتوى الفقرة
استخدم HTML لجلب النص إلى عرض تقديمي أو لتصدير الفقرات لاستخدامها خارج العرض. يظهر هذا القسم أيضًا كيفية تحويل الفقرات الفردية إلى صور، بما في ذلك الفقرات داخل خلايا الجداول.
استيراد نص HTML إلى الفقرات
استخدم ParagraphCollection::AddFromHtml لتحويل ترميز HTML إلى فقرات وأجزاء داخل إطار النص.
يقوم المثال التالي بلغة C++ بقراءة HTML من ملف وتمرير الترميز إلى ParagraphCollection::AddFromHtml. يفرغ الفقرة الافتراضية لإطار النص بحيث يحتوي الإطار فقط على المحتوى المستورد. يترك الشكل المستطيل هامشًا قدره 10 نقاط على كل جانب من الشريحة:
#include <DOM/FillType.h>
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/ISlideSize.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/SaveFormat.h>
#include <system/io/stream_reader.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto slideSize = presentation->get_SlideSize()->get_Size();
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 10, 10, slideSize.get_Width() - 20, slideSize.get_Height() - 20);
shape->get_FillFormat()->set_FillType(FillType::NoFill);
shape->get_TextFrame()->get_Paragraphs()->Clear();
auto reader = MakeObject<StreamReader>(u"file.html");
auto html = reader->ReadToEnd();
reader->Close();
shape->get_TextFrame()->get_Paragraphs()->AddFromHtml(html);
presentation->Save(u"html_text.pptx", SaveFormat::Pptx);
presentation->Dispose();
تصدير نص الفقرة إلى HTML
استخدم ParagraphCollection::ExportToHtml لتصدير نطاق محدد من الفقرات كـ HTML. عند إعادة استخدام نص العرض على موقع ويب، قد ترغب في الحفاظ على التأكيد المحلي دون نقل خط العرض أو الحجم أو أنماط صندوق النص. عيّن TextToHtmlConversionOptions::set_TextInheritanceLimit للتحكم في مدى اتباع المُصدّر لوراثة التنسيق.
حمِّل العرض التوضيحي وضعه في دليل عمل التطبيق. يحتوي شريحةه الأولى على شكل نص واحد بالنص “Inherited text; explicit red 18pt”. يستخدم الإدخال عدة مستويات تنسيق عمدًا:
- نمط النص الافتراضي للعرض يوفّر خط Calibri بحجم 28 pt.
- نمط النص من المستوى الأول لإطار النص يوفّر نصًا أزرق ومائل.
- الفقرة تحدد نصًا عريضًا صراحة.
- الجزء الثاني، “explicit red 18pt”، يحدد نصًا أحمر بحجم 18 pt صراحة.
يصدّر المثال التالي الفقرات نفسها ثلاث مرات. يمرّر TextToHtmlConversionOptions إلى طريقة التصدير ويكتب كل نتيجة كملف HTML UTF-8 منفصل:
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/Presentation.h>
#include <Export/TextInheritanceLimit.h>
#include <Export/TextToHtmlConversionOptions.h>
#include <system/array.h>
#include <system/console.h>
#include <system/io/file.h>
#include <system/object_ext.h>
#include <system/text/encoding.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
using namespace System::IO;
using namespace System::Text;
auto presentation = MakeObject<Presentation>(u"text-inheritance.pptx");
auto shape = presentation->get_Slide(0)->get_Shape(0);
auto textShape = AsCast<IAutoShape>(shape);
if (textShape != nullptr && textShape->get_TextFrame() != nullptr)
{
auto paragraphs = textShape->get_TextFrame()->get_Paragraphs();
auto modes = MakeArray<TextInheritanceLimit>({TextInheritanceLimit::All, TextInheritanceLimit::TextBox, TextInheritanceLimit::ParagraphOnly});
auto fileNames = MakeArray<String>({u"All.html", u"TextBox.html", u"ParagraphOnly.html"});
for (auto i = 0; i < modes->get_Length(); i++)
{
auto options = MakeObject<TextToHtmlConversionOptions>();
options->set_TextInheritanceLimit(modes[i]);
auto html = paragraphs->ExportToHtml(0, paragraphs->get_Count(), options);
File::WriteAllText(fileNames[i], html, Encoding::get_UTF8());
}
}
else
{
Console::WriteLine(u"The first shape is not a text shape.");
}
presentation->Dispose();
يحتوي HTML المستخرج على CSS التالي للجزء الأول، “Inherited text; “. تُظهر الجدول البيانات ذات الصلة؛ تُحذف الخصائص الأخرى لتوضيح الفهم.
| الناتج | CSS ذو الصلة للجزء الأول | المظهر |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
نص أزرق، عريض، مائل بحجم 28 pt بخط Calibri إذا كان الخط متاحًا. |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
نص أزرق، عريض، مائل؛ تُوفر الصفحة أو المتصفح عائلة الخط والحجم. |
ParagraphOnly.html |
font-weight:bold |
نص عريض؛ تُوفر الصفحة أو المتصفح عائلة الخط، الحجم، اللون، والنمط المائل. |
يحافظ الجزء الثاني على color:#ff0000 وfont-size:18pt وfont-weight:bold في جميع المخرجات الثلاث. كما يبقى مائلًا في All وTextBox، لكنه غير مائل في ParagraphOnly. لذا فإن تقييد الوراثة لا يُزيل التنسيق المعيّن صراحةً للفقرة أو الجزء.
افتح الملفات في نفس المتصفح للمقارنة. في الوضعين المُقيدين، تُستكمل الخصائص CSS الغائبة بأنماط الصفحة الهدف أو الإعدادات الافتراضية للمتصفح، لذا يعتمد المظهر النهائي على مكان استخدام HTML.
إنشاء مقطع HTML للوحة الحافظة
عيّن TextToHtmlConversionOptions::set_AddClipboardFragmentHeader إلى false للحصول على HTML عادي أو إلى true لتضمين رأس لوحة الحافظة. يوفّر الرأس إزاحات البايت UTF-8 للـ HTML المحيط (StartHTML وEndHTML) والجزء الذي يُلصق (StartFragment وEndFragment).
يصدّر هذا المثال فقرتين منسقتين كـ HTML عادي (html) وكجزء لوحة حافظة HTML (clipboardHtml). كتابة الجزء إلى لوحة الحافظة النظامية هي خطوة تطبيق منفصلة. تتطلب واجهة برمجة تطبيقات الحافظة التي تقبل بايتات HTML صافية UTF-8 بدون BOM.
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/IShapeCollection.h>
#include <DOM/ISlide.h>
#include <DOM/ITextFrame.h>
#include <DOM/Presentation.h>
#include <DOM/ShapeType.h>
#include <Export/TextToHtmlConversionOptions.h>
using namespace Aspose::Slides;
using namespace Aspose::Slides::Export;
using namespace System;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto shape = slide->get_Shapes()->AddAutoShape(ShapeType::Rectangle, 0, 0, 400, 100);
auto paragraphs = shape->get_TextFrame()->get_Paragraphs();
paragraphs->Clear();
paragraphs->AddFromHtml(u"<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
auto options = MakeObject<TextToHtmlConversionOptions>();
options->set_AddClipboardFragmentHeader(false);
auto html = paragraphs->ExportToHtml(0, paragraphs->get_Count(), options);
options->set_AddClipboardFragmentHeader(true);
auto clipboardHtml = paragraphs->ExportToHtml(0, paragraphs->get_Count(), options);
presentation->Dispose();
تحويل فقرة إلى صورة
يُعيد IParagraph::GetImage تحويل فقرة فردية مباشرةً ويُعيد كائنًا من نوع IImage. احفظ النتيجة إلى ملف أو تدفق باستخدام IImage::Save. لا تحتاج إلى تحويل الشكل الحاوي أو قص صورة يدوية.
قد تُعيد IParagraph::GetImage nullptr إذا لم تُعثر الفقرة في مجموعة الوالد، أو لا توجد حدود رسم صالحة، أو لا يمكن رسمها. تحقّق من النتيجة قبل حفظها وتأكد من تحرير الصورة المسترجعة بعد الاستخدام.
تحويل فقرة بالمقياس الافتراضي
يستخدم المثال sample.pptx، عرضًا به شريحة واحدة وشكل نص يحتوي على ثلاث فقرات.

يختار المثال الفقرة الثانية عند الفهرس 1 ويستدعي IParagraph::GetImage دون معاملات مقياس. يتحقق من وجود الفقرة المطلوبة داخل الشكل، ومن أن عملية الرسم تُعيد صورة قبل استدعاء IImage::Save لحفظها بصيغة PNG. يُفرغ الصورة بعد حفظها.
#include <DOM/IAutoShape.h>
#include <DOM/IParagraph.h>
#include <DOM/Presentation.h>
#include <IImage.h>
#include <ImageFormat.h>
#include <system/console.h>
#include <system/object_ext.h>
using namespace Aspose::Slides;
using namespace System;
auto presentation = MakeObject<Presentation>(u"sample.pptx");
auto shape = presentation->get_Slide(0)->get_Shape(0);
auto textShape = AsCast<IAutoShape>(shape);
if (textShape != nullptr && textShape->get_TextFrame() != nullptr && textShape->get_TextFrame()->get_Paragraphs()->get_Count() > 1)
{
auto paragraph = textShape->get_TextFrame()->get_Paragraph(1);
auto paragraphImage = paragraph->GetImage();
if (paragraphImage != nullptr)
{
paragraphImage->Save(u"paragraph.png", ImageFormat::Png);
paragraphImage->Dispose();
}
else
{
Console::WriteLine(u"The paragraph could not be rendered.");
}
}
else
{
Console::WriteLine(u"The expected text shape or paragraph was not found.");
}
presentation->Dispose();
النتيجة:

تحويل فقرة داخل خلية جدول مع مقياس
استخدم overload لـ IParagraph::GetImage الذي يقبل معلمات float scaleX و float scaleY لتحديد عوامل المقياس الأفقي والعمودي. يُنشئ المثال جدولًا، يحوّل الفقرة في خلية الجدول الأولى إلى ضعف عرضها وارتفاعها الافتراضيين، ويحفظ النتيجة كصورة PNG.
#include <DOM/IParagraph.h>
#include <DOM/Presentation.h>
#include <DOM/Table/ITable.h>
#include <IImage.h>
#include <ImageFormat.h>
#include <system/array.h>
#include <system/console.h>
using namespace Aspose::Slides;
using namespace System;
auto scaleX = 2.0f;
auto scaleY = 2.0f;
auto presentation = MakeObject<Presentation>();
auto slide = presentation->get_Slide(0);
auto table = slide->get_Shapes()->AddTable(50, 50, MakeArray<double>({300}), MakeArray<double>({80}));
auto paragraph = table->idx_get(0, 0)->get_TextFrame()->get_Paragraph(0);
paragraph->set_Text(u"Text in a table cell");
auto paragraphImage = paragraph->GetImage(scaleX, scaleY);
if (paragraphImage != nullptr)
{
paragraphImage->Save(u"table_paragraph.png", ImageFormat::Png);
paragraphImage->Dispose();
}
else
{
Console::WriteLine(u"The paragraph could not be rendered.");
}
presentation->Dispose();
عامل المقياس 1 يحافظ على ذلك البُعد بالحجم البكسلي الافتراضي. على سبيل المثال، يُنتج 2 لكلا العاملين صورة عرضها وارتفاعها تقريبًا ضعف الأبعاد الافتراضية، ما يعني أربعة أضعاف عدد البكسلات. عادةً ما تُنتج العوامل الأكبر نصًا أكثر حدة للقراءة أو للإخراج عالي الدقة، لكنها أيضًا تزيد من استهلاك الذاكرة وحجم الملف. العوامل دون 1 تُنتج صورًا أصغر مع تفاصيل أقل. استخدم عوامل متماثلة للحفاظ على نسبة عرض الفقرة إلى ارتفاعها؛ العوامل الأفقية والعمودية المختلفة تُشوّه الإخراج بشكل مستقل.
يظل تحويل شكل كامل باستخدام IShape::GetImage مفيدًا عندما يجب تضمين تعبئة الشكل، حدوده، أو سياقه البصري. للحصول على صورة للفقرة فقط، استخدم IParagraph::GetImage.
الأسئلة الشائعة
هل يمكنني تعطيل التفاف السطر بالكامل داخل إطار النص؟
نعم. استخدم ITextFrameFormat::set_WrapText لتعطيل التفاف السطر بحيث لا ينكسر عند حدود إطار النص.
كيف يمكنني الحصول على حدود الفقرة المحددة داخل الشريحة بدقة؟
استخدم IParagraph::GetRect لاسترجاع المستطيل المحيط بالفقرة. يوفر IPortion::GetRect حدود الجزء الفردي.
أين يتم التحكم في محاذاة الفقرة (يسار، يمين، وسط، ضبط)؟
IParagraphFormat::set_Alignment هو إعداد على مستوى الفقرة وينطبق على الفقرة بأكملها بغض النظر عن تنسيق الأجزاء الفردية.
لإنشاء محاذاة عمودية لأجزاء ذات أحجام خطوط مختلفة داخل كل سطر، راجع Align Fonts Within a Line.
هل يمكنني تعيين لغة التدقيق لجزء من الفقرة؟
نعم. استخدم IBasePortionFormat::set_LanguageId للأجزاء الفردية، بحيث يمكن لفقرة واحدة أن تحتوي على نصوص بعدة لغات.