مدیریت متن پاراگرافهای پاورپوینت در C++
نمای کلی
Aspose.Slides for C++ متن را به صورت یک سلسلهمراتبی از فریمهای متن، پاراگرافها و بخشها نمایش میدهد:
- ITextFrame محفظه متن در یک شکل را نمایان میکند و دسترسی به مجموعه پاراگرافهای آن را فراهم میکند.
- IParagraph یک پاراگراف در فریم متن را نمایان میکند و دسترسی به بخشها و قالببندی سطح پاراگراف را فراهم میکند.
- IPortion یک بخش متنی داخل پاراگراف را نشان میدهد. هر بخش میتواند متن و قالببندی کاراکتری خود را داشته باشد.
بنابراین یک پاراگراف میتواند متنی با فونتها، رنگها، اندازهها و قالببندیهای مختلف داشته باشد که از طریق چندین بخش ایجاد میشود.
ایجاد و قالببندی پاراگرافها
ایجاد پاراگرافها با چندین بخش
مراحل زیر یک فریم متن با سه پاراگراف، که هر کدام شامل سه بخش هستند، ایجاد میکند:
- یک نمونه از کلاس Presentation ایجاد کنید.
- از طریق ایندکس، به ارجاع اسلاید مرتبط دسترسی پیدا کنید.
- یک IAutoShape مستطیلی به اسلاید اضافه کنید.
- به ITextFrame شکل دسترسی پیدا کنید.
- از پاراگراف پیشفرض استفاده کنید و دو شیء دیگر از IParagraph را به فریم متن اضافه کنید.
- به اندازه کافی شیء IPortion برای هر پاراگراف اضافه کنید تا هر کدام سه بخش داشته باشند. پاراگراف پیشفرض از قبل یک بخش خالی دارد.
- متن هر بخش را تنظیم کنید.
- قالببندی در سطح کاراکتر را از طریق IPortion::get_PortionFormat اعمال کنید.
- ارائه اصلاحشده را ذخیره کنید.
این مثال C++ مراحل فوق را پیادهسازی میکند:
#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 (int paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++)
{
auto paragraph = textFrame->get_Paragraph(paragraphIndex);
auto portionCount = paragraph->get_Portions()->get_Count();
for (int 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 تعریف میشود.
- یک نمونه از کلاس Presentation ایجاد کنید.
- از طریق ایندکس، به ارجاع اسلاید مرتبط دسترسی پیدا کنید.
- یک IAutoShape به اسلاید انتخابی اضافه کنید.
- به ITextFrame شکل دسترسی پیدا کنید.
- پاراگراف پیشفرض را از فریم متن حذف کنید.
- یک Paragraph برای یک بولت نماد ایجاد کنید.
- متد IBulletFormat::set_Type را به BulletType::Symbol تنظیم کرده و کاراکتر بولت را مشخص کنید.
- متن پاراگراف، تورفتگی، رنگ بولت و ارتفاع بولت را تنظیم کنید.
- پاراگراف را به فریم متن اضافه کنید.
- یک پاراگراف دوم ایجاد کرده و IBulletFormat::set_Type را به BulletType::Numbered تنظیم کنید.
- سبک بولت عددی را پیکربندی کنید و پاراگراف را به فریم متن اضافه کنید.
- ارائه را ذخیره کنید.
این مثال C++ یک بولت نماد و یک بولت عددی ایجاد میکند:
#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();
استفاده از بولتهای تصویری
بولتهای تصویری به شما اجازه میدهد به جای نماد یا عدد از یک تصویر سفارشی استفاده کنید.
- یک نمونه از کلاس Presentation ایجاد کنید.
- از طریق ایندکس، به ارجاع اسلاید مرتبط دسترسی پیدا کنید.
- یک IAutoShape اضافه کنید و به ITextFrame آن دسترسی پیدا کنید.
- پاراگراف پیشفرض را از فریم متن حذف کنید.
- تصویر بولت را بارگیری کرده و به مجموعه تصاویر ارائه به عنوان یک IPPImage اضافه کنید.
- یک Paragraph ایجاد کنید و متن آن را تنظیم کنید.
- متد IBulletFormat::set_Type را به BulletType::Picture تنظیم کنید.
- تصویر را از طریق ISlidesPicture::set_Image اختصاص داده و ارتفاع بولت را تنظیم کنید.
- پاراگراف را به فریم متن اضافه کنید.
- ارائه اصلاحشده را ذخیره کنید.
این مثال C++ یک بولت تصویری ایجاد میکند:
#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 دارد.
- یک Presentation ایجاد کرده و به یک اسلاید دسترسی پیدا کنید.
- یک IAutoShape اضافه کنید و پاراگراف پیشفرض را از فریم متن آن پاک کنید.
- چهار پاراگراف ایجاد کنید و نمادهای بولت آنها را پیکربندی کنید.
- مقدارهای IParagraphFormat::set_Depth آنها را به ترتیب
0،1،2و3تنظیم کنید. - پاراگرافها را به فریم متن اضافه کرده و ارائه را ذخیره کنید.
این مثال C++ یک فهرست بولتدار چهار سطحی ایجاد میکند:
#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 برای تعیین عدد اولیه نمایشدادهشده برای پاراگراف عددی استفاده کنید.
- یک Presentation ایجاد کنید و یک IAutoShape به یک اسلاید اضافه کنید.
- پاراگراف پیشفرض را از فریم متن شکل حذف کنید.
- سه پاراگراف عددی ایجاد کنید.
- برای پاراگرافهای مربوطه، IBulletFormat::set_NumberedBulletStartWith را به ترتیب به
2،3و7تنظیم کنید. - پاراگرافها را به فریم متن اضافه کرده و ارائه را ذخیره کنید.
این مثال C++ عدد شروع سفارشی را برای هر پاراگراف تعیین میکند:
#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_Indent را برای نشان دادن تأثیر تورفتگی خط اول بر چیدمان پاراگراف اعمال میکند.
- یک نمونه از کلاس Presentation ایجاد کنید.
- به اسلاید هدف دسترسی پیدا کنید.
- یک IAutoShape مستطیلی به اسلاید اضافه کنید.
- به ITextFrame شکل دسترسی پیدا کنید و پاراگراف پیشفرض را حذف کنید.
- چندین پاراگراف ایجاد کنید و مقادیر مختلف IParagraphFormat::set_Indent را برای آنها تنظیم کنید.
- پاراگرافها را به فریم متن اضافه کنید.
- ارائه اصلاحشده را ذخیره کنید.
این کد نشان میدهد چگونه تورفتگی پاراگراف را تنظیم کنید:
#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 موقعیت خط اول را نسبت به آن حاشیه تعیین میکند. برای ایجاد تورفتگی معلق، مقدار مثبت margin-left و مقدار منفی indent را تنظیم کنید.
این قالببندی برای کتابنامهها، مراجع، واژهنامهها و سایر پاراگرافهایی که خطوط بستهبندی شده باید زیر بدنه پاراگراف همتراز شوند مفید است.
- یک نمونه از کلاس Presentation ایجاد کنید.
- به اسلاید هدف دسترسی پیدا کنید.
- یک IAutoShape مستطیلی به اسلاید اضافه کنید.
- به ITextFrame شکل دسترسی پیدا کنید و پاراگراف پیشفرض را حذف کنید.
- پاراگرافها ایجاد کنید و برای هر پاراگراف مقدار مثبت IParagraphFormat::set_MarginLeft تنظیم کنید.
- برای ایجاد اثر تورفتگی معلق، مقدار منفی IParagraphFormat::set_Indent تنظیم کنید.
- پاراگرافها را به فریم متن اضافه کنید.
- ارائه اصلاحشده را ذخیره کنید.
این کد نشان میدهد چگونه تورفتگی معلق را برای یک پاراگراف تنظیم کنید:
#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 قالببندی علامت انتهای پاراگراف را کنترل میکند. مثال زیر اندازه فونت و فونت لاتین را برای علامت انتهای پاراگراف دوم اختصاص میدهد:
- یک Presentation بارگذاری کنید و به یک اسلاید دسترسی پیدا کنید.
- یک IAutoShape اضافه کنید و پاراگراف پیشفرض آن را پاک کنید.
- دو پاراگراف ایجاد کنید و بخشهای متنی به آنها اضافه نمایید.
- یک PortionFormat برای علامت انتهای پاراگراف دوم ایجاد کنید.
- IBasePortionFormat::set_FontHeight و IBasePortionFormat::set_LatinFont را تنظیم کنید.
- قالب را با IParagraph::set_EndParagraphPortionFormat اختصاص دهید و ارائه را ذخیره کنید.
#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 است و میتواند چندین خط رندر شده اشغال کند. یک شکست خط صریح داخل پاراگراف، خط جدیدی ایجاد میکند بدون اینکه پاراگراف جدیدی ساخته شود. بستهبندی خودکار بر اساس عرض موجود خطوط را ایجاد میکند بدون اینکه علامتهای شکست خط صریح به متن اضافه شود. بنابراین شمارش پاراگرافها یا کاراکترهای شکست خط، شمارش واقعی خطوط رندر شده را نمیدهد.
مثال زیر یک شکل متن ایجاد میکند، خطوط آن را میشمارد، شکل را باریک میسازد و سپس متن را با یک رشته کوتاهتر جایگزین میکند. بستهبندی فعال است و تنظیم خودکار اندازه (autofit) غیرفعال است تا عرض شکل کنترل بستهبندی را بدون کوچک کردن خودکار متن یا تغییر اندازه شکل انجام دهد. ابعاد شکل بر حسب پوینت است. در انتها، مثال یک پاراگراف دیگر اضافه میکند و مجموع خطوط را در فریم متن محاسبه میکند.
#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();
با این متن و این ابعاد، باریک کردن شکل تعداد خطوط را افزایش میدهد، در حالی که جایگزینی متن با رشته کوتاهتر آن را کاهش میدهد. شمارش دقیق ممکن است بسته به در دسترس بودن فونت، جایگزینی، اندازه فونت، حاشیهها، تورفتگی، بستهبندی و تنظیمات autofit متفاوت باشد. هنگام بررسی یک قالب، از فونتها و تنظیمات چیدمان موردنظر برای محیط هدف استفاده کنید.
تعداد خطوط به تنهایی تعیین نمیکند که آیا متن از محفظهاش سرریز میشود یا نه. ارتفاع در دسترس، ارتفاع خطوط، فاصله بین پاراگرافها و خطوط، و رفتار autofit نیز مؤثر هستند؛ حتی یک خط واحد میتواند عرض در دسترس را هنگام غیرفعال بودن بستهبندی تجاوز کند.
واردات و صادرات محتوای پاراگراف
وارد کردن متن HTML به پاراگرافها
از متد IParagraphCollection::AddFromHtml برای تبدیل نشانهگذاری HTML به پاراگرافها و بخشها در یک فریم متن استفاده کنید.
- یک نمونه از کلاس Presentation ایجاد کنید.
- به یک اسلاید دسترسی پیدا کنید و یک IAutoShape اضافه کنید.
- به ITextFrame شکل دسترسی پیدا کنید و پاراگراف پیشفرض آن را پاک کنید.
- فایل HTML منبع را بخوانید.
- رشته HTML را به IParagraphCollection::AddFromHtml منتقل کنید.
- ارائه اصلاحشده را ذخیره کنید.
این مثال C++ HTML را به یک فریم متن وارد میکند:
#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
از متد IParagraphCollection::ExportToHtml برای صادر کردن یک بازه از پاراگرافها به صورت HTML استفاده کنید.
- یک نمونه از کلاس Presentation ایجاد کنید و ارائه موردنظر را بارگذاری کنید.
- به اسلاید دسترسی پیدا کنید و IAutoShape حاوی متن را پیدا کنید.
- به ITextFrame شکل دسترسی پیدا کنید.
- متد IParagraphCollection::ExportToHtml را با اندیس پاراگراف شروع و تعداد پاراگرافهای موردنظر برای صادرات فراخوانی کنید.
- رشته HTML بازگشتی را در یک فایل بنویسید.
این مثال C++ تمام پاراگرافهای اولین شکل متن را صادر میکند:
#include <DOM/IAutoShape.h>
#include <DOM/IParagraphCollection.h>
#include <DOM/Presentation.h>
#include <system/console.h>
#include <system/io/stream_writer.h>
#include <system/object_ext.h>
#include <system/text/encoding.h>
using namespace Aspose::Slides;
using namespace System;
using namespace System::IO;
using namespace System::Text;
auto presentation = MakeObject<Presentation>(u"ExportingHTMLText.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 html = paragraphs->ExportToHtml(0, paragraphs->get_Count(), nullptr);
auto writer = MakeObject<StreamWriter>(u"paragraphs.html", false, Encoding::get_UTF8());
writer->Write(html);
writer->Close();
}
else
{
Console::WriteLine(u"The first shape is not a text shape.");
}
presentation->Dispose();
رندر کردن یک پاراگراف به عنوان تصویر
متد IParagraph::GetImage یک پاراگراف منفرد را مستقیماً رندر میکند و یک IImage برمیگرداند. نتیجه را با IImage::Save در یک فایل یا جریان ذخیره کنید. نیازی به رندر کردن شکل حاوی متن یا بریدن بیتمپ بهصورت دستی ندارید.
IParagraph::GetImage میتواند nullptr بازگرداند اگر پاراگراف در مجموعه والد خود پیدا نشود، محدوده رندر معتبری نداشته باشد یا قابل رندر نباشد. قبل از ذخیرهسازی نتیجه را بررسی کنید و پس از استفاده تصویر بازگردانده شده را آزاد کنید.
رندر کردن پاراگراف با مقیاس پیشفرض
فرض کنید فایلی به نام sample.pptx داریم که یک اسلاید دارد و اولین شکل آن یک جعبه متن با سه پاراگراف است.

مثال زیر پاراگراف دوم را در یک شکل متن معمولی با مقیاس پیشفرض رندر میکند و تصویر بازگشتی را در قالب 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();
نتیجه:

رندر کردن پاراگراف در سلول جدول با مقیاسبندی
از بارگذاری 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 برای بخشهای منفرد استفاده کنید، بهطوریکه یک پاراگراف میتواند متنی با چندین زبان داشته باشد.