مدیریت پاراگرافهای متن PowerPoint در .NET
معرفی
Aspose.Slides تمام رابطها و کلاسهایی را که برای کار با متون، پاراگرافها و بخشهای PowerPoint در C# نیاز دارید، فراهم میکند.
- Aspose.Slides رابط ITextFrame را فراهم میکند تا بتوانید اشیائی که نمایانگر یک پاراگراف هستند اضافه کنید. یک شیء
ITextFameمیتواند یک یا چند پاراگراف داشته باشد (هر پاراگراف از طریق یک بازگشت carriage ایجاد میشود). - Aspose.Slides رابط IParagraph را فراهم میکند تا بتوانید اشیائی که نمایانگر بخشها هستند اضافه کنید. یک شیء
IParagraphمیتواند یک یا چند بخش داشته باشد (مجموعهای از اشیاء iPortions). - Aspose.Slides رابط IPortion را فراهم میکند تا بتوانید اشیائی که نمایانگر متون و ویژگیهای قالببندی آنها هستند اضافه کنید.
یک شیء IParagraph قادر است متونی با ویژگیهای قالببندی مختلف را از طریق اشیاء IPortion زیرین خود مدیریت کند.
اضافهکردن چندین پاراگراف شامل چندین بخش
این مراحل نشان میدهند چگونه یک فریم متن شامل ۳ پاراگراف و هر پاراگراف شامل ۳ بخش اضافه کنید:
- یک نمونه از کلاس Presentation ایجاد کنید.
- از طریق ایندکس، به مرجع اسلاید مربوطه دسترسی پیدا کنید.
- یک IAutoShape مستطیل به اسلاید اضافه کنید.
- ITextFrame مرتبط با IAutoShape را دریافت کنید.
- دو شیء IParagraph ایجاد کنید و آنها را به مجموعه
IParagraphsمربوط به ITextFrame اضافه کنید. - برای هر
IParagraphجدید، سه شیء IPortion ایجاد کنید (برای پاراگراف پیشفرض دو شیء Portion) و هر شیءIPortionرا به مجموعه IPortion مربوط به هرIParagraphاضافه کنید. - متنی برای هر بخش تنظیم کنید.
- ویژگیهای قالببندی موردنظر خود را با استفاده از خصوصیات قالببندی موجود در شیء
IPortionبر هر بخش اعمال کنید. - پرزنتیشن تغییر یافته را ذخیره کنید.
// یک شی از کلاس Presentation که نمایانگر یک فایل PPTX است را ایجاد میکند
using (Presentation pres = new Presentation())
{
// به اولین اسلاید دسترسی مییابد
ISlide slide = pres.Slides[0];
// یک IAutoShape مستطیل اضافه میکند
IAutoShape ashp = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
// به TextFrame شکل خودکار دسترسی مییابد
ITextFrame tf = ashp.TextFrame;
// پاراگرافها و بخشها را با قالببندیهای متنی مختلف ایجاد میکند
IParagraph para0 = tf.Paragraphs[0];
IPortion port01 = new Portion();
IPortion port02 = new Portion();
para0.Portions.Add(port01);
para0.Portions.Add(port02);
IParagraph para1 = new Paragraph();
tf.Paragraphs.Add(para1);
IPortion port10 = new Portion();
IPortion port11 = new Portion();
IPortion port12 = new Portion();
para1.Portions.Add(port10);
para1.Portions.Add(port11);
para1.Portions.Add(port12);
IParagraph para2 = new Paragraph();
tf.Paragraphs.Add(para2);
IPortion port20 = new Portion();
IPortion port21 = new Portion();
IPortion port22 = new Portion();
para2.Portions.Add(port20);
para2.Portions.Add(port21);
para2.Portions.Add(port22);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
tf.Paragraphs[i].Portions[j].Text = "Portion0" + j.ToString();
if (j == 0)
{
tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.FillType = FillType.Solid;
tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
tf.Paragraphs[i].Portions[j].PortionFormat.FontBold = NullableBool.True;
tf.Paragraphs[i].Portions[j].PortionFormat.FontHeight = 15;
}
else if (j == 1)
{
tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.FillType = FillType.Solid;
tf.Paragraphs[i].Portions[j].PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
tf.Paragraphs[i].Portions[j].PortionFormat.FontItalic = NullableBool.True;
tf.Paragraphs[i].Portions[j].PortionFormat.FontHeight = 18;
}
}
// پرزنتیشن تغییر یافته را ذخیره میکند
pres.Save("multiParaPort_out.pptx", SaveFormat.Pptx);
}
مدیریت گلولههای پاراگراف
فهرستهای گلولهای به شما کمک میکنند تا اطلاعات را بهسرعت و بهصورت مؤثر سازماندهی و ارائه کنید. پاراگرافهای دارای گلوله همیشه خواندن و درک آسانتری دارند.
- یک نمونه از کلاس Presentation ایجاد کنید.
- از طریق ایندکس، به مرجع اسلاید مربوطه دسترسی پیدا کنید.
- یک autoshape به اسلاید انتخابی اضافه کنید.
- به TextFrame autoshape دسترسی پیدا کنید.
- پاراگراف پیشفرض در
TextFrameرا حذف کنید. - اولین پاراگراف را با استفاده از کلاس Paragraph ایجاد کنید.
- نوع گلوله (
Type) پاراگراف را بهSymbolتنظیم کنید و کاراکتر گلوله را تعیین کنید. - متن پاراگراف (
Text) را تنظیم کنید. - تورفتگی پاراگراف (
Indent) برای گلوله را تنظیم کنید. - رنگی برای گلوله تعیین کنید.
- ارتفاع گلوله را تنظیم کنید.
- پاراگراف جدید را به مجموعه پاراگرافهای
TextFrameاضافه کنید. - پاراگراف دوم را اضافه کنید و فرآیند مراحل 7 تا 13 را تکرار کنید.
- پرزنتیشن را ذخیره کنید.
// یک شی از کلاس Presentation که نمایانگر یک فایل PPTX است را ایجاد میکند
using (Presentation pres = new Presentation())
{
// به اولین اسلاید دسترسی مییابد
ISlide slide = pres.Slides[0];
// یک Autoshape اضافه میکند و به آن دسترسی مییابد
IAutoShape aShp = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// به فریم متن autoshape دسترسی مییابد
ITextFrame txtFrm = aShp.TextFrame;
// پاراگراف پیشفرض را حذف میکند
txtFrm.Paragraphs.RemoveAt(0);
// یک پاراگراف ایجاد میکند
Paragraph para = new Paragraph();
// سبک و نماد گلوله پاراگراف را تنظیم میکند
para.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
// متن پاراگراف را تنظیم میکند
para.Text = "Welcome to Aspose.Slides";
// تورفتگی گلوله را تنظیم میکند
para.ParagraphFormat.Indent = 25;
// رنگ گلوله را تنظیم میکند
para.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
para.ParagraphFormat.Bullet.Color.Color = Color.Black;
para.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True; // مقدار IsBulletHardColor را به true تنظیم میکند تا از رنگ دلخواه برای گلوله استفاده شود
// ارتفاع گلوله را تنظیم میکند
para.ParagraphFormat.Bullet.Height = 100;
// پاراگراف را به فریم متن اضافه میکند
txtFrm.Paragraphs.Add(para);
// پاراگراف دوم را ایجاد میکند
Paragraph para2 = new Paragraph();
// نوع و سبک گلوله پاراگراف را تنظیم میکند
para2.ParagraphFormat.Bullet.Type = BulletType.Numbered;
para2.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletCircleNumWDBlackPlain;
// متن پاراگراف را اضافه میکند
para2.Text = "This is numbered bullet";
// تورفتگی گلوله را تنظیم میکند
para2.ParagraphFormat.Indent = 25;
para2.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
para2.ParagraphFormat.Bullet.Color.Color = Color.Black;
para2.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True; // مقدار IsBulletHardColor را به true تنظیم میکند تا از رنگ دلخواه برای گلوله استفاده شود
// ارتفاع گلوله را تنظیم میکند
para2.ParagraphFormat.Bullet.Height = 100;
// پاراگراف را به فریم متن اضافه میکند
txtFrm.Paragraphs.Add(para2);
// پرزنتیشن تغییر یافته را ذخیره میکند
pres.Save("Bullet_out.pptx", SaveFormat.Pptx);
}
مدیریت گلولههای تصویری
فهرستهای گلولهای به شما کمک میکنند تا اطلاعات را بهسرعت و بهصورت مؤثر سازماندهی و ارائه کنید. پاراگرافهای تصویری خواندن آسان و درک سادهای دارند.
- یک نمونه از کلاس Presentation ایجاد کنید.
- از طریق ایندکس، به مرجع اسلاید مربوطه دسترسی پیدا کنید.
- یک autoshape به اسلاید اضافه کنید.
- به TextFrame autoshape دسترسی پیدا کنید.
- پاراگراف پیشفرض در
TextFrameرا حذف کنید. - اولین پاراگراف را با استفاده از کلاس Paragraph ایجاد کنید.
- تصویر را در IPPImage بارگیری کنید.
- نوع گلوله را به Picture تنظیم کنید و تصویر را تعیین کنید.
- متن پاراگراف (
Text) را تنظیم کنید. - تورفتگی پاراگراف (
Indent) برای گلوله را تنظیم کنید. - رنگی برای گلوله تعیین کنید.
- ارتفاع گلوله را تنظیم کنید.
- پاراگراف جدید را به مجموعه پاراگرافهای
TextFrameاضافه کنید. - پاراگراف دوم را اضافه کنید و فرآیند بر اساس مراحل قبلی را تکرار کنید.
- پرزنتیشن تغییر یافته را ذخیره کنید.
// یک شی از کلاس Presentation که نمایانگر یک فایل PPTX است را ایجاد میکند
Presentation presentation = new Presentation();
// به اولین اسلاید دسترسی مییابد
ISlide slide = presentation.Slides[0];
// تصویر برای گلولهها را ایجاد میکند
IImage image = Images.FromFile("bullets.png");
IPPImage ippxImage = presentation.Images.AddImage(image);
image.Dispose();
// یک Autoshape اضافه میکند و به آن دسترسی مییابد
IAutoShape autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// به فریم متن autoshape دسترسی مییابد
ITextFrame textFrame = autoShape.TextFrame;
// پاراگراف پیشفرض را حذف میکند
textFrame.Paragraphs.RemoveAt(0);
// یک پاراگراف جدید ایجاد میکند
Paragraph paragraph = new Paragraph();
paragraph.Text = "Welcome to Aspose.Slides";
// سبک گلوله پاراگراف و تصویر را تنظیم میکند
paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
paragraph.ParagraphFormat.Bullet.Picture.Image = ippxImage;
// ارتفاع گلوله را تنظیم میکند
paragraph.ParagraphFormat.Bullet.Height = 100;
// پاراگراف را به فریم متن اضافه میکند
textFrame.Paragraphs.Add(paragraph);
// پرزنتیشن را به عنوان فایل PPTX مینویسد
presentation.Save("ParagraphPictureBulletsPPTX_out.pptx", SaveFormat.Pptx);
// پرزنتیشن را به عنوان فایل PPT مینویسد
presentation.Save("ParagraphPictureBulletsPPT_out.ppt", SaveFormat.Ppt);
مدیریت گلولههای چندسطحی
فهرستهای گلولهای به شما کمک میکنند تا اطلاعات را بهسرعت و بهصورت مؤثر سازماندهی و ارائه کنید. گلولههای چندسطحی خواندن آسان و درک سادهای دارند.
- یک نمونه از کلاس Presentation ایجاد کنید.
- از طریق ایندکس، به مرجع اسلاید مربوطه دسترسی پیدا کنید.
- یک autoshape در اسلاید جدید اضافه کنید.
- به TextFrame autoshape دسترسی پیدا کنید.
- پاراگراف پیشفرض در
TextFrameرا حذف کنید. - پاراگراف اول را با استفاده از کلاس Paragraph ایجاد کنید و عمق را به ۰ تنظیم کنید.
- پاراگراف دوم را با کلاس
Paragraphایجاد کنید و عمق را به ۱ تنظیم کنید. - پاراگراف سوم را با کلاس
Paragraphایجاد کنید و عمق را به ۲ تنظیم کنید. - پاراگراف چهارم را با کلاس
Paragraphایجاد کنید و عمق را به ۳ تنظیم کنید. - پاراگرافهای جدید را به مجموعه پاراگرافهای
TextFrameاضافه کنید. - پرزنتیشن تغییر یافته را ذخیره کنید.
// یک شی از کلاس Presentation که نمایانگر یک فایل PPTX است را ایجاد میکند
using (Presentation pres = new Presentation())
{
// به اولین اسلاید دسترسی مییابد
ISlide slide = pres.Slides[0];
// یک Autoshape اضافه میکند و به آن دسترسی مییابد
IAutoShape aShp = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// به فریم متن Autoshape ایجاد شده دسترسی مییابد
ITextFrame text = aShp.AddTextFrame("");
// پاراگراف پیشفرض را پاک میکند
text.Paragraphs.Clear();
// پاراگراف اول را اضافه میکند
IParagraph para1 = new Paragraph();
para1.Text = "Content";
para1.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para1.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
para1.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
para1.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
// سطح گلوله را تنظیم میکند
para1.ParagraphFormat.Depth = 0;
// پاراگراف دوم را اضافه میکند
IParagraph para2 = new Paragraph();
para2.Text = "Second Level";
para2.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para2.ParagraphFormat.Bullet.Char = '-';
para2.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
para2.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
// سطح گلوله را تنظیم میکند
para2.ParagraphFormat.Depth = 1;
// پاراگراف سوم را اضافه میکند
IParagraph para3 = new Paragraph();
para3.Text = "Third Level";
para3.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para3.ParagraphFormat.Bullet.Char = Convert.ToChar(8226);
para3.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
para3.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
// سطح گلوله را تنظیم میکند
para3.ParagraphFormat.Depth = 2;
// پاراگراف چهارم را اضافه میکند
IParagraph para4 = new Paragraph();
para4.Text = "Fourth Level";
para4.ParagraphFormat.Bullet.Type = BulletType.Symbol;
para4.ParagraphFormat.Bullet.Char = '-';
para4.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
para4.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
// سطح گلوله را تنظیم میکند
para4.ParagraphFormat.Depth = 3;
// پاراگرافها را به مجموعه اضافه میکند
text.Paragraphs.Add(para1);
text.Paragraphs.Add(para2);
text.Paragraphs.Add(para3);
text.Paragraphs.Add(para4);
// پرزنتیشن را به عنوان فایل PPTX مینویسد
pres.Save("MultilevelBullet.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
مدیریت پاراگراف با فهرست شمارهگذاری سفارشی
رابط IBulletFormat ویژگی NumberedBulletStartWith و سایر ویژگیها را فراهم میکند که به شما امکان مدیریت پاراگرافها با شمارهگذاری یا قالببندی سفارشی را میدهد.
- یک نمونه از کلاس Presentation ایجاد کنید.
- به اسلاید حاوی پاراگراف دسترسی پیدا کنید.
- یک autoshape به اسلاید اضافه کنید.
- به TextFrame autoshape دسترسی پیدا کنید.
- پاراگراف پیشفرض در
TextFrameرا حذف کنید. - پاراگراف اول را با استفاده از کلاس Paragraph ایجاد کنید و NumberedBulletStartWith را به ۲ تنظیم کنید.
- پاراگراف دوم را با کلاس
Paragraphایجاد کنید وNumberedBulletStartWithرا به ۳ تنظیم کنید. - پاراگراف سوم را با کلاس
Paragraphایجاد کنید وNumberedBulletStartWithرا به ۷ تنظیم کنید. - پاراگرافهای جدید را به مجموعه پاراگرافهای
TextFrameاضافه کنید. - پرزنتیشن تغییر یافته را ذخیره کنید.
using (var presentation = new Presentation())
{
var shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
// به فریم متن autoshape ایجاد شده دسترسی مییابد
ITextFrame textFrame = shape.TextFrame;
// پاراگراف پیشفرض موجود را حذف میکند
textFrame.Paragraphs.RemoveAt(0);
// فهرست اول
var paragraph1 = new Paragraph { Text = "bullet 2" };
paragraph1.ParagraphFormat.Depth = 4;
paragraph1.ParagraphFormat.Bullet.NumberedBulletStartWith = 2;
paragraph1.ParagraphFormat.Bullet.Type = BulletType.Numbered;
textFrame.Paragraphs.Add(paragraph1);
var paragraph2 = new Paragraph { Text = "bullet 3" };
paragraph2.ParagraphFormat.Depth = 4;
paragraph2.ParagraphFormat.Bullet.NumberedBulletStartWith = 3;
paragraph2.ParagraphFormat.Bullet.Type = BulletType.Numbered;
textFrame.Paragraphs.Add(paragraph2);
var paragraph5 = new Paragraph { Text = "bullet 7" };
paragraph5.ParagraphFormat.Depth = 4;
paragraph5.ParagraphFormat.Bullet.NumberedBulletStartWith = 7;
paragraph5.ParagraphFormat.Bullet.Type = BulletType.Numbered;
textFrame.Paragraphs.Add(paragraph5);
presentation.Save("SetCustomBulletsNumber-slides.pptx", SaveFormat.Pptx);
}
تنظیم تورفتگی خط اول برای یک پاراگراف
از ویژگی IParagraphFormat.Indent برای کنترل تورفتگی خط اول یک پاراگراف استفاده کنید. این ویژگی فقط خط اول را نسبت به حاشیه چپ پاراگراف جابهجا میکند. مقدار مثبت خط اول را به راست منتقل میکند، در حالی که خطوط باقیمانده به بدنه پاراگراف همراستا میمانند.
زمانی که نیاز به جابهجا کردن کل پاراگراف دارید، از IParagraphFormat.MarginLeft استفاده کنید. وقتی فقط خط اول را میخواهید جابهجا کنید، از IParagraphFormat.Indent استفاده کنید.
مثال زیر چندین پاراگراف ایجاد میکند و مقادیر مختلف Indent را اعمال مینماید تا نشان دهد تورفتگی خط اول چگونه بر چیدمان پاراگراف تأثیر میگذارد.
- یک نمونه از کلاس Presentation ایجاد کنید.
- به اسلاید هدف دسترسی پیدا کنید.
- یک AutoShape مستطیلی به اسلاید اضافه کنید.
- یک TextFrame خالی به شکل اضافه کنید و پاراگراف پیشفرض را حذف کنید.
- چندین پاراگراف ایجاد کنید و مقادیر متفاوت Indent را برای آنها تنظیم کنید.
- پاراگرافها را به فریم متن اضافه کنید.
- پرزنتیشن تغییر یافته را ذخیره کنید.
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
IAutoShape rectangleShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
rectangleShape.FillFormat.FillType = FillType.NoFill;
rectangleShape.LineFormat.FillFormat.FillType = FillType.Solid;
rectangleShape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
ITextFrame textFrame = rectangleShape.AddTextFrame(string.Empty);
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.RemoveAt(0);
Paragraph firstParagraph = new Paragraph();
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.Text = "No first-line indent. Wrapped lines start at the same position as the first line.";
firstParagraph.ParagraphFormat.MarginLeft = 20f;
firstParagraph.ParagraphFormat.Indent = 0f;
Paragraph secondParagraph = new Paragraph();
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.Text = "First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body.";
secondParagraph.ParagraphFormat.MarginLeft = 20f;
secondParagraph.ParagraphFormat.Indent = 20f;
Paragraph thirdParagraph = new Paragraph();
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.Text = "First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see.";
thirdParagraph.ParagraphFormat.MarginLeft = 20f;
thirdParagraph.ParagraphFormat.Indent = 40f;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("paragraph_indent.pptx", SaveFormat.Pptx);
}
نتیجه:

تنظیم تورفتگی معلق برای یک پاراگراف
تورفتگی معلق یک چیدمان پاراگراف است که در آن خط اول به سمت چپ خطوط باقیمانده شروع میشود. در Aspose.Slides، این اثر را با ویژگی IParagraphFormat.Indent ایجاد میکنید. Indent را به مقدار منفی تنظیم کنید تا خط اول نسبت به بدنه پاراگراف به چپ جابهجا شود.
در عمل، IParagraphFormat.MarginLeft موقعیت چپ بدنه پاراگراف را تعریف میکند و IParagraphFormat.Indent موقعیت خط اول را نسبت به آن حاشیه تعیین میکند. برای ایجاد تورفتگی معلق، مقدار مثبت MarginLeft و مقدار منفی Indent تنظیم کنید.
این قالببندی برای کتابشناسیها، مراجع، ورودیهای واژهنامه و سایر پاراگرافهایی که خطوط پیچیده باید زیر بدنه پاراگراف تراز شوند نه زیر اولین کاراکتر خط اول، مفید است.
- یک نمونه از کلاس Presentation ایجاد کنید.
- به اسلاید هدف دسترسی پیدا کنید.
- یک AutoShape مستطیلی به اسلاید اضافه کنید.
- یک TextFrame خالی به شکل اضافه کنید و پاراگراف پیشفرض را حذف کنید.
- پاراگرافها را ایجاد کنید و برای هر پاراگراف مقدار مثبت MarginLeft تنظیم کنید.
- مقدار منفی Indent تنظیم کنید تا اثر تورفتگی معلق ایجاد شود.
- پاراگرافها را به فریم متن اضافه کنید.
- پرزنتیشن تغییر یافته را ذخیره کنید.
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
IAutoShape rectangleShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
rectangleShape.FillFormat.FillType = FillType.NoFill;
rectangleShape.LineFormat.FillFormat.FillType = FillType.Solid;
rectangleShape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
ITextFrame textFrame = rectangleShape.AddTextFrame(string.Empty);
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.RemoveAt(0);
Paragraph firstParagraph = new Paragraph();
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.Text = "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.ParagraphFormat.MarginLeft = 40f;
firstParagraph.ParagraphFormat.Indent = -20f;
Paragraph secondParagraph = new Paragraph();
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.Text = "This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare.";
secondParagraph.ParagraphFormat.MarginLeft = 60f;
secondParagraph.ParagraphFormat.Indent = -30f;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("hanging_indent.pptx", SaveFormat.Pptx);
}
نتیجه:

مدیریت ویژگیهای End برای پاراگراف
- یک نمونه از کلاس Presentation ایجاد کنید.
- مرجع اسلاید حاوی پاراگراف را از طریق موقعیت آن دریافت کنید.
- یک autoshape مستطیل به اسلاید اضافه کنید.
- یک TextFrame با دو پاراگراف به مستطیل اضافه کنید.
FontHeightو نوع فونت را برای پاراگرافها تنظیم کنید.- ویژگیهای End را برای پاراگرافها تنظیم کنید.
- پرزنتیشن را به صورت فایل PPTX بنویسید.
using (Presentation pres = new Presentation("Test.pptx"))
{
IAutoShape shape = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
Paragraph para1 = new Paragraph();
para1.Portions.Add(new Portion("Sample text"));
Paragraph para2 = new Paragraph();
para2.Portions.Add(new Portion("Sample text 2"));
PortionFormat endParagraphPortionFormat = new PortionFormat();
endParagraphPortionFormat.FontHeight = 48;
endParagraphPortionFormat.LatinFont = new FontData("Times New Roman");
para2.EndParagraphPortionFormat = endParagraphPortionFormat;
shape.TextFrame.Paragraphs.Add(para1);
shape.TextFrame.Paragraphs.Add(para2);
pres.Save("pres.pptx", SaveFormat.Pptx);
}
وارد کردن متن HTML به پاراگرافها
Aspose.Slides پشتیبانی پیشرفتهای برای وارد کردن متن HTML به پاراگرافها فراهم میکند.
- یک نمونه از کلاس Presentation ایجاد کنید.
- از طریق ایندکس، به مرجع اسلاید مربوطه دسترسی پیدا کنید.
- یک autoshape به اسلاید اضافه کنید.
autoshapeرا به ITextFrame دسترسی پیدا کنید.- پاراگراف پیشفرض در
ITextFrameرا حذف کنید. - فایل HTML منبع را در یک TextReader بخوانید.
- پاراگراف اول را با استفاده از کلاس Paragraph ایجاد کنید.
- محتوای فایل HTML را از TextReader خوانده شده به ParagraphCollection فریم متن اضافه کنید.
- پرزنتیشن تغییر یافته را ذخیره کنید.
// یک نمونه خالی از پرزنتیشن ایجاد میکند
using (Presentation pres = new Presentation())
{
// به اسلاید پیشفرض اول پرزنتیشن دسترسی مییابد
ISlide slide = pres.Slides[0];
// یک AutoShape اضافه میکند تا محتویات HTML را در خود نگه دارد
IAutoShape ashape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, pres.SlideSize.Size.Width - 20, pres.SlideSize.Size.Height - 10);
ashape.FillFormat.FillType = FillType.NoFill;
// یک فریم متن به شکل اضافه میکند
ashape.AddTextFrame("");
// تمام پاراگرافهای فریم متن اضافهشده را پاک میکند
ashape.TextFrame.Paragraphs.Clear();
// فایل HTML را با استفاده از StreamReader بارگذاری میکند
TextReader tr = new StreamReader("file.html");
// متن خوانده شده از StreamReader HTML را به فریم متن اضافه میکند
ashape.TextFrame.Paragraphs.AddFromHtml(tr.ReadToEnd());
// پرزنتیشن را ذخیره میکند
pres.Save("output_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
صادر کردن متن پاراگراف به HTML
Aspose.Slides پشتیبانی پیشرفتهای برای صادر کردن متون (موجود در پاراگرافها) به HTML فراهم میکند.
- یک نمونه از کلاس Presentation ایجاد کنید و پرزنتیشن موردنظر را بارگذاری کنید.
- از طریق ایندکس، به مرجع اسلاید مربوطه دسترسی پیدا کنید.
- به شکل حاوی متنی که به HTML صادر خواهد شد دسترسی پیدا کنید.
- به TextFrame شکل دسترسی پیدا کنید.
- یک نمونه از
StreamWriterایجاد کنید و فایل HTML جدید را اضافه کنید. - یک ایندکس شروع به StreamWriter بدهید و پاراگرافهای موردنظر را صادر کنید.
// فایل پرزنتیشن را بارگذاری میکند
using (Presentation pres = new Presentation("ExportingHTMLText.pptx"))
{
// به اسلاید پیشفرض اول پرزنتیشن دسترسی مییابد
ISlide slide = pres.Slides[0];
// به ایندکس مورد نیاز دسترسی مییابد
int index = 0;
// به شکل اضافهشده دسترسی مییابد
IAutoShape ashape = (IAutoShape)slide.Shapes[index];
StreamWriter sw = new StreamWriter("output_out.html", false, Encoding.UTF8);
// دادههای پاراگرافها را به HTML مینویسد با مشخص کردن ایندکس شروع پاراگراف و تعداد پاراگرافهای قابل کپی
sw.Write(ashape.TextFrame.Paragraphs.ExportToHtml(0, ashape.TextFrame.Paragraphs.Count, null));
sw.Close();
}
ذخیره یک پاراگراف به عنوان تصویر
در این بخش، دو مثال را بررسی میکنیم که نشان میدهند چگونه یک پاراگراف متنی، نمایندگی شده توسط رابط IParagraph، به عنوان تصویر ذخیره میشود. هر دو مثال شامل دریافت تصویر یک شکل حاوی پاراگراف با استفاده از متدهای GetImage از رابط IShape، محاسبه محدوده پاراگراف داخل شکل، و صادر کردن آن به عنوان یک تصویر bitmap است. این روشها به شما امکان میدهند بخشهای خاصی از متن را از ارائههای PowerPoint استخراج کنید و بهصورت تصاویر جداگانه ذخیره کنید، که میتواند در سناریوهای مختلف مفید باشد.
فرض کنید فایلی به نام sample.pptx داریم که یک اسلاید دارد و اولین شکل آن یک جعبه متن است که سه پاراگراف دارد.

مثال 1
در این مثال، پاراگراف دوم را بهعنوان تصویر استخراج میکنیم. برای این کار، تصویر شکل را از اسلاید اول ارائه استخراج میکنیم و سپس محدوده پاراگراف دوم را در فریم متن شکل محاسبه میکنیم. سپس پاراگراف روی یک تصویر bitmap جدید رسم میشود و بهصورت PNG ذخیره میگردد. این روش بهویژه وقتی مفید است که بخواهید پاراگراف خاصی را بهصورت تصویر جداگانه ذخیره کنید و ابعاد و قالببندی دقیق متن حفظ شود.
using var presentation = new Presentation("sample.pptx");
var firstShape = presentation.Slides[0].Shapes[0] as IAutoShape;
// Save the shape in memory as a bitmap.
using var shapeImage = firstShape.GetImage();
using var shapeImageStream = new MemoryStream();
shapeImage.Save(shapeImageStream, ImageFormat.Png);
// Create a shape bitmap from memory.
shapeImageStream.Seek(0, SeekOrigin.Begin);
using var shapeBitmap = Image.FromStream(shapeImageStream);
// Calculate the boundaries of the second paragraph.
var secondParagraph = firstShape.TextFrame.Paragraphs[1];
var paragraphRectangle = secondParagraph.GetRect();
// Calculate the size for the output image (minimum size - 1x1 pixel).
var imageWidth = Math.Max(1, (int)Math.Ceiling(paragraphRectangle.Width));
var imageHeight = Math.Max(1, (int)Math.Ceiling(paragraphRectangle.Height));
// Prepare a bitmap for the paragraph.
using var paragraphBitmap = new Bitmap(imageWidth, imageHeight);
// Redraw the paragraph from the shape bitmap to the paragraph bitmap.
using var imageGraphics = Graphics.FromImage(paragraphBitmap);
var drawingRectangle = new RectangleF(0, 0, paragraphRectangle.Width, paragraphRectangle.Height);
imageGraphics.DrawImage(shapeBitmap, drawingRectangle, paragraphRectangle, GraphicsUnit.Pixel);
paragraphBitmap.Save("paragraph.png", System.Drawing.Imaging.ImageFormat.Png);
نتیجه:

مثال 2
در این مثال، روش قبلی را با افزودن عوامل مقیاس به تصویر پاراگراف گسترش میدهیم. شکل از ارائه استخراج میشود و با عامل مقیاس 2 بهعنوان تصویر ذخیره میشود. این امکان خروجی با وضوح بالاتر را هنگام صادر کردن پاراگراف فراهم میکند. سپس محدوده پاراگراف با در نظر گرفتن مقیاس محاسبه میشود. مقیاسگذاری میتواند زمانی مفید باشد که تصویر دقیقتری نیاز باشد، برای مثال برای استفاده در مواد چاپی با کیفیت بالا.
var imageScaleX = 2f;
var imageScaleY = imageScaleX;
using var presentation = new Presentation("sample.pptx");
var firstShape = presentation.Slides[0].Shapes[0] as IAutoShape;
// Save the shape in memory as a bitmap with scaling.
using var shapeImage = firstShape.GetImage(ShapeThumbnailBounds.Shape, imageScaleX, imageScaleY);
using var shapeImageStream = new MemoryStream();
shapeImage.Save(shapeImageStream, ImageFormat.Png);
// Create a shape bitmap from memory.
shapeImageStream.Seek(0, SeekOrigin.Begin);
using var shapeBitmap = Image.FromStream(shapeImageStream);
// Calculate the boundaries of the second paragraph.
var secondParagraph = firstShape.TextFrame.Paragraphs[1];
var paragraphRectangle = secondParagraph.GetRect();
paragraphRectangle.X *= imageScaleX;
paragraphRectangle.Y *= imageScaleY;
paragraphRectangle.Width *= imageScaleX;
paragraphRectangle.Height *= imageScaleY;
// Calculate the size for the output image (minimum size - 1x1 pixel).
var imageWidth = Math.Max(1, (int)Math.Ceiling(paragraphRectangle.Width));
var imageHeight = Math.Max(1, (int)Math.Ceiling(paragraphRectangle.Height));
// Prepare a bitmap for the paragraph.
using var paragraphBitmap = new Bitmap(imageWidth, imageHeight);
// Redraw the paragraph from the shape bitmap to the paragraph bitmap.
using var imageGraphics = Graphics.FromImage(paragraphBitmap);
var drawingRectangle = new RectangleF(0, 0, paragraphRectangle.Width, paragraphRectangle.Height);
imageGraphics.DrawImage(shapeBitmap, drawingRectangle, paragraphRectangle, GraphicsUnit.Pixel);
paragraphBitmap.Save("paragraph.png", System.Drawing.Imaging.ImageFormat.Png);
سؤالات متداول
آیا میتوانم بستهبندی خطوط داخل یک فریم متن را بهطور کامل غیرفعال کنم؟
بله. از تنظیمات بستهبندی فریم متن (WrapText) استفاده کنید تا بستهبندی را خاموش کنید تا خطوط در حاشیه فریم شکسته نشوند.
چگونه میتوانم محدوده دقیق روی اسلاید یک پاراگراف خاص را بدست آورم؟
میتوانید مستطیل محدوده (و حتی یک Portion واحد) پاراگراف را دریافت کنید تا موقعیت و اندازه دقیق آن را روی اسلاید بدانید.
کنترل ترازبندی پاراگراف (چپ/راست/وسط/توزیع) در کجا انجام میشود؟
Alignment تنظیمی در سطح پاراگراف در ParagraphFormat است؛ این تنظیم برای کل پاراگراف اعمال میشود بدون در نظر گرفتن قالببندی بخشهای جداگانه.
آیا میتوانم زبان بررسی املا را فقط برای بخشی از پاراگراف (مثلاً یک کلمه) تنظیم کنم؟
بله. زبان در سطح Portion تنظیم میشود (PortionFormat.LanguageId)، بنابراین میتوان چندین زبان را در یک پاراگراف ترکیب کرد.