مدیریت پاراگرافهای متنی پاورپوینت در .NET
بررسی کلی
Aspose.Slides for .NET متن را به صورت سلسلهمراتبی از فریمهای متنی، پاراگرافها و بخشها (Portions) نمایش میدهد:
- ITextFrame محفظه متنی در یک شکل را نشان میدهد و دسترسی به مجموعه پاراگرافهای آن را فراهم میکند.
- IParagraph یک پاراگراف را در فریم متنی نمایش میدهد و دسترسی به بخشها و قالببندی سطح پاراگراف را فراهم میکند.
- IPortion یک بخش متنی داخل پاراگراف را نمایان میسازد. هر بخش میتواند متن و قالببندی کاراکتری خود را داشته باشد.
بدین ترتیب یک پاراگراف میتواند متنی با فونتها، رنگها، اندازهها و سایر قالببندیهای مختلف داشته باشد که با استفاده از بخشهای متعدد اعمال میشود.
ایجاد و قالببندی پاراگرافها
پاراگرافها را در فریم متنی یک شکل ایجاد کنید و از بخشها برای قالببندی اجراهای متنی منفرد استفاده کنید. این بخش نشان میدهد چگونه بخشهای متعدد را در هر پاراگراف ترکیب کرده و رنگها، اندازههای فونت و سبکهای مختلف را اعمال کنیم.
ایجاد پاراگرافها با بخشهای متعدد
مثال زیر به زبان C# سه پاراگراف ایجاد میکند که هر کدام شامل سه اجرای متنی در مجموعهٔ IParagraph.Portions خود هستند. این مثال پاراگراف پیشفرض و بخش خالی آن را مجدداً استفاده میکند، سپس پاراگرافها و بخشهای باقیمانده را اضافه میکند. قالببندی از طریق IPortion.PortionFormat اعمال میشود: اولین بخش در هر پاراگراف قرمز، بولد و 15 پوینت است؛ دومین بخش آبی، ایتالیک و 18 پوینت؛ بخش سوم قالببندی پیشفرض خود را حفظ میکند:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
var textFrame = shape.TextFrame;
var firstParagraph = textFrame.Paragraphs[0];
firstParagraph.Portions.Add(new Portion());
firstParagraph.Portions.Add(new Portion());
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph();
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(thirdParagraph);
var paragraphCount = textFrame.Paragraphs.Count;
for (var paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++)
{
var paragragaph = textFrame.Paragraphs[paragraphIndex];
var portionCount = paragragaph.Portions.Count;
for (var portionIndex = 0; portionIndex < portionCount; portionIndex++)
{
var portion = paragragaph.Portions[portionIndex];
portion.Text = $"Portion {paragraphIndex + 1}.{portionIndex + 1}";
if (portionIndex == 0)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
portion.PortionFormat.FontBold = NullableBool.True;
portion.PortionFormat.FontHeight = 15;
}
else if (portionIndex == 1)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
portion.PortionFormat.FontItalic = NullableBool.True;
portion.PortionFormat.FontHeight = 18;
}
}
}
presentation.Save("paragraphs_with_portions.pptx", SaveFormat.Pptx);
ایجاد فهرستهای گلولهای و شمارهدار
متن پاراگرافها را به فهرستهایی با نمادها، اعداد یا تصاویر سفارشی سازماندهی کنید. مثالهای این بخش شامل قالببندی گلوله، فهرستهای چندسطحی و شروع شمارههای سفارشی میشود.
ایجاد فهرست گلولهای یا شمارهدار
گلولهها و شمارهگذاری موارد مرتبط را برای اسکن سریعتر میکند. در Aspose.Slides تنظیمات فهرست از طریق IBulletFormat تعریف میشوند.
این مثال به زبان C# یک گلوله نمادین و یک آیتم شمارهدار را به همان فریم متنی اضافه میکند. IBulletFormat.Type نوع گلوله را انتخاب میکند، در حالی که IBulletFormat.Char نماد را فراهم میکند و IBulletFormat.NumberedBulletStyle سبک شمارهگذاری را انتخاب میکند. هر دو آیتم از گلولههای سیاه با ارتفاع 100 درصد و تو رفتگی پاراگراف 25 پوینت استفاده میکنند:
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var symbolParagraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
symbolParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
symbolParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
symbolParagraph.ParagraphFormat.Indent = 25;
symbolParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
symbolParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
symbolParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
symbolParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(symbolParagraph);
var numberedParagraph = new Paragraph { Text = "This is a numbered item" };
numberedParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
numberedParagraph.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletCircleNumWDBlackPlain;
numberedParagraph.ParagraphFormat.Indent = 25;
numberedParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
numberedParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
numberedParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
numberedParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(numberedParagraph);
presentation.Save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx);
استفاده از گلولههای تصویری
گلولههای تصویری به شما اجازه میدهند به جای نماد یا عدد از یک تصویر سفارشی استفاده کنید.
این مثال به زبان C# یک تصویر را به مجموعهٔ تصاویر ارائه اضافه میکند بهعنوان یک IPPImage و آن را به عنوان گلولهٔ پاراگراف استفاده میکند. IBulletFormat.Type برای استفاده از گلولهٔ تصویری تنظیم میشود و تصویر از طریق IBulletFormat.Picture اختصاص داده میشود. IBulletFormat.Height به 100 درصد تنظیم میشود تا اندازه گلوله نسبت به متن پاراگراف تنظیم شود:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
using var bulletImage = Images.FromFile("bullets.png");
var presentationImage = presentation.Images.AddImage(bulletImage);
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var paragraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
paragraph.ParagraphFormat.Bullet.Picture.Image = presentationImage;
paragraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(paragraph);
presentation.Save("picture_bullet.pptx", SaveFormat.Pptx);
presentation.Save("picture_bullet.ppt", SaveFormat.Ppt);
ایجاد فهرست چندسطحی
IParagraphFormat.Depth را تنظیم کنید تا پاراگرافها در سطوح مختلف فهرست قرار گیرند. سطح بالایی دارای عمق 0 است.
این مثال به زبان C# مقادیر IParagraphFormat.Depth را از 0 تا 3 به چهار پاراگراف اختصاص میدهد و سلسلهمراتبی در یک فریم متنی ایجاد میکند. هر پاراگراف از یک گلولهٔ نماد استفاده میکند؛ در حالی که IBulletFormat.Char بین یک گلولهٔ دایرهای و یک خط تیره برای تمایز سطوح مجاور جابجا میشود:
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Content" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
firstParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.Depth = 0;
var secondParagraph = new Paragraph { Text = "Second level" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
secondParagraph.ParagraphFormat.Bullet.Char = '-';
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.Depth = 1;
var thirdParagraph = new Paragraph { Text = "Third level" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
thirdParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.Depth = 2;
var fourthParagraph = new Paragraph { Text = "Fourth level" };
fourthParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
fourthParagraph.ParagraphFormat.Bullet.Char = '-';
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
fourthParagraph.ParagraphFormat.Depth = 3;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
textFrame.Paragraphs.Add(fourthParagraph);
presentation.Save("multilevel_list.pptx", SaveFormat.Pptx);
شروع آیتمهای فهرست شمارهدار با مقادیر سفارشی
از IBulletFormat.NumberedBulletStartWith برای تعیین عدد اولیه نمایش داده شده برای یک پاراگراف شمارهدار استفاده کنید.
این مثال به زبان C# IBulletFormat.Type را طوری تنظیم میکند که برای سه پاراگراف شمارهگذاری استفاده شود و مقادیر IBulletFormat.NumberedBulletStartWith را به ترتیب 2، 3 و 7 اختصاص میدهد. مقدار شروع برای هر پاراگراف بهصورت جداگانه پیکربندی میشود تا اعداد نمایش داده شده بتوانند مقادیر را نادیده بگیرند:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Start at 2" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
firstParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 2;
textFrame.Paragraphs.Add(firstParagraph);
var secondParagraph = new Paragraph { Text = "Start at 3" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
secondParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 3;
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph { Text = "Start at 7" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
thirdParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 7;
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("custom_numbered_list.pptx", SaveFormat.Pptx);
کنترل چینش پاراگراف و ویژگیهای انتهای آن
حاشیهها و تو رفتگیهای پاراگراف را تنظیم کنید تا موقعیت خط اول و خطوط بستهشده را کنترل کنید. همچنین میتوانید علامت پایان پاراگراف را جدا از بخشهای متنی قابل مشاهده قالببندی کنید.
تنظیم تو رفتگی خط اول
از ویژگی IParagraphFormat.Indent برای کنترل تو رفتگی خط اول یک پاراگراف استفاده کنید. این ویژگی تنها خط اول را نسبت به حاشیه چپ پاراگراف جابجا میکند. مقدار مثبت تو رفتگی خط اول را به سمت راست میبرد، در حالی که خطوط باقیمانده در راستای بدنهٔ پاراگراف میمانند.
از IParagraphFormat.MarginLeft زمانی که نیاز به جابجایی کل پاراگراف دارید استفاده کنید. از IParagraphFormat.Indent زمانی که فقط نیاز به جابجایی خط اول دارید استفاده کنید.
مثال زیر سه پاراگراف را با همان IParagraphFormat.MarginLeft برابر 20 پوینت و مقادیر IParagraphFormat.Indent برابر 0، 20 و 40 پوینت تنظیم میکند. خطوط اول بهصورت تدریجی از افست بزرگتری آغاز میشوند، در حالی که خطوط بستهشده همان حاشیهٔ چپ را حفظ میکنند:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "No first-line indent. Wrapped lines start at the same position as the first line." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 20;
firstParagraph.ParagraphFormat.Indent = 0;
var secondParagraph = new Paragraph { 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.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 20;
secondParagraph.ParagraphFormat.Indent = 20;
var thirdParagraph = new Paragraph { Text = "First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see." };
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.MarginLeft = 20;
thirdParagraph.ParagraphFormat.Indent = 40;
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 را تنظیم کنید.
این قالببندی برای کتابشناسیها، منابع، ورودیهای واژهنامه و سایر پاراگرافهایی که خطوط بستهشده باید زیر بدنهٔ پاراگراف و نه زیر اولین حرف خط اول قرار گیرند، مفید است.
مثال C# زیر مقادیر IParagraphFormat.MarginLeft برابر 40 و 60 پوینت را با مقادیر IParagraphFormat.Indent برابر -20 و -30 پوینت ترکیب میکند. در هر پاراگراف، خط اول به سمت چپ خطوط بستهشده شروع میشود؛ پاراگراف دوم تو رفتگی معلق عمیقتری دارد:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { 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.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 40;
firstParagraph.ParagraphFormat.Indent = -20;
var secondParagraph = new Paragraph { 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.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 60;
secondParagraph.ParagraphFormat.Indent = -30;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("hanging_indent.pptx", SaveFormat.Pptx);
نتیجه:

تنظیم ویژگیهای انتهای پاراگراف
ویژگی IParagraph.EndParagraphPortionFormat قالببندی علامت پایان پاراگراف را کنترل میکند. مثال زیر یک PortionFormat با IBasePortionFormat.FontHeight برابر 48 پوینت و IBasePortionFormat.LatinFont برابر Times New Roman ایجاد میکند. این قالب به علامت پایان پاراگراف دوم اختصاص مییابد و بخشهای متنی قابل مشاهده بدون تغییر باقی میمانند:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("Test.pptx");
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph();
firstParagraph.Portions.Add(new Portion("Sample text"));
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion("Sample text 2"));
var endParagraphFormat = new PortionFormat();
endParagraphFormat.FontHeight = 48;
endParagraphFormat.LatinFont = new FontData("Times New Roman");
secondParagraph.EndParagraphPortionFormat = endParagraphFormat;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("end_paragraph_format.pptx", SaveFormat.Pptx);
شمارش خطوط رندرشده
برای قوانین پاراگراف که بر بسته شدن خودکار و نقطهگذاری در انتهای خطوط تاثیر میگذارند، به بخشهای Control Line Breaking و Control Hanging Punctuation مراجعه کنید.
از IParagraph.GetLinesCount برای شمارش خطوطی که یک پاراگراف پس از چیدمان متن اشغال میکند (از جمله بسته شدن خودکار) استفاده کنید. این روش برای بررسی طول متن و چیدمان در قالبهای ارائه مفید است.
یک پاراگراف یکی از آیتمهای ITextFrame.Paragraphs است و میتواند چندین خط رندرشده را اشغال کند. یک شکست خط صریح داخل پاراگراف، خط جدیدی ایجاد میکند بدون اینکه پاراگراف دیگری ساخته شود. بسته شدن خودکار خطوط را بر اساس عرض در دسترس ایجاد میکند بدون اینکه شکستهای خط صریح به متن اضافه شود. بنابراین شمارش پاراگرافها یا کاراکترهای شکست خط، شمارش خطوط رندرشده را نمیدهد.
مثال زیر یک شکل متنی میسازد، خطوط آن را میشمارد، شکل را باریک میکند و سپس متن را با رشتهٔ کوتاهتری جایگزین میکند. این مثال ITextFrameFormat.WrapText را فعال و ITextFrameFormat.AutofitType را غیرفعال میکند تا عرض شکل بسته شدن خطوط را کنترل کند بدون اینکه متن بهصورت خودکار کوچک یا شکل تغییر اندازه دهد. هر اندازهگیری از IParagraph.GetLinesCount استفاده میکند. ابعاد shape به پوینت است. در نهایت، مثال یک پاراگراف دیگر اضافه میکند و مجموع شمارش خطوط را در فریم متنی محاسبه میکند:
using System;
using Aspose.Slides;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.WrapText = NullableBool.True;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.None;
var paragraph = textFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
paragraph.Text = "This text demonstrates how automatic wrapping changes the number of rendered lines.";
Console.WriteLine($"Original width: {paragraph.GetLinesCount()}");
shape.Width = 150;
Console.WriteLine($"Narrower shape: {paragraph.GetLinesCount()}");
paragraph.Text = "Short text.";
Console.WriteLine($"Shorter text: {paragraph.GetLinesCount()}");
var secondParagraph = new Paragraph { Text = "Another paragraph." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
textFrame.Paragraphs.Add(secondParagraph);
var totalLineCount = 0;
foreach (var currentParagraph in textFrame.Paragraphs)
{
totalLineCount += currentParagraph.GetLinesCount();
}
Console.WriteLine($"Total lines in the text frame: {totalLineCount}");
با این متن و این ابعاد، باریک کردن shape تعداد خطوط را افزایش میدهد، در حالی که جایگزینی متن با رشتهٔ کوتاهتر آن را کاهش میدهد. شمارش دقیق میتواند بسته به دسترسی به فونت، جایگزینی، اندازهٔ فونت، حاشیهها، تو رفتگی، بسته شدن و تنظیمات autofit متفاوت باشد. از فونتها و تنظیمات چیدمان موردنظر برای محیط هدف هنگام بررسی یک قالب استفاده کنید.
تنها شمارش خطوط تعیینکنندهٔ سرریز متن در ظرف نیست. ارتفاع موجود، ارتفاع خطوط، فاصلهٔ پاراگراف و خط و رفتار autofit نیز مهم هستند؛ حتی یک خط واحد میتواند عرض موجود را در حالت غیرفعال بودن بسته شدن تجاوز کند.
واردات و صادرات محتوای پاراگراف
از HTML برای وارد کردن متن به یک ارائه یا صادرات پاراگرافهای موجود برای استفاده خارج از آن استفاده کنید. این بخش همچنین نشان میدهد چگونه پاراگرافهای منفرد را بهعنوان تصویر رندر کنید، از جمله پاراگرافهای موجود در سلولهای جدول.
وارد کردن متن HTML به پاراگرافها
از ParagraphCollection.AddFromHtml برای تبدیل markup HTML به پاراگرافها و بخشها در یک فریم متنی استفاده کنید.
این مثال به زبان C# HTML را از یک فایل میخواند و markup را به ParagraphCollection.AddFromHtml میسپارد. پاراگراف پیشفرض فریم متنی را پاک میکند تا فریم فقط شامل محتوای وارد شده باشد. شکل مستطیلی حاشیهٔ 10 پوینت در هر سمت اسلاید را باقی میگذارد:
using System.IO;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shapeWidth = presentation.SlideSize.Size.Width - 20;
var shapeHeight = presentation.SlideSize.Size.Height - 20;
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
shape.FillFormat.FillType = FillType.NoFill;
shape.TextFrame.Paragraphs.Clear();
using var reader = new StreamReader("file.html");
var html = reader.ReadToEnd();
shape.TextFrame.Paragraphs.AddFromHtml(html);
presentation.Save("html_text.pptx", SaveFormat.Pptx);
صادرات متن پاراگراف به HTML
از ParagraphCollection.ExportToHtml برای صادرات یک بازهٔ انتخابی از پاراگرافها به HTML استفاده کنید. هنگام استفاده مجدد از متن ارائه در یک وبسایت، ممکن است بخواهید تأکید محلی را بدون انتقال فونت، اندازه یا سبکهای جعبهٔ متن ارائه حفظ کنید. برای کنترل عمق ارثبری قالببندی، TextToHtmlConversionOptions.TextInheritanceLimit را تنظیم کنید.
ارائه نمونه را دانلود کنید و در دایرکتوری کاری برنامه بگذارید. اسلاید اول یک شکل متنی شامل متن «Inherited text; explicit red 18pt» دارد. ورودی بهصورت عمدی چندین سطح قالببندی دارد:
- سبک متن پیشفرض ارائه Calibri با اندازهٔ 28 پوینت است.
- سبک متن سطح اول فریم متنی متن آبی و ایتالیک ارائه میدهد.
- پاراگراف بهطور صریح متن بولد تنظیم میکند.
- بخش دوم «explicit red 18pt» بهصورت صریح متن قرمز و اندازهٔ 18 پوینت تنظیم میکند.
مثال کامل زیر همان پاراگرافها را سه بار صادر میکند. TextToHtmlConversionOptions به متد صادرات پاس داده میشود و هر نتیجه به عنوان یک فایل HTML UTF‑8 جداگانه نوشته میشود:
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("text-inheritance.pptx");
var textShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraphs = textShape.TextFrame.Paragraphs;
var modes = new[] { TextInheritanceLimit.All, TextInheritanceLimit.TextBox, TextInheritanceLimit.ParagraphOnly };
foreach (var mode in modes)
{
var options = new TextToHtmlConversionOptions { TextInheritanceLimit = mode };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText($"{mode}.html", html, Encoding.UTF8);
}
HTML تولید شده CSS زیر را برای اولین بخش «Inherited text; » شامل میشود. جدول موارد مربوطه را نشان میدهد؛ سایر ویژگیهای تولید شده برای وضوح حذف شدهاند.
| خروجی | CSS مربوط به اولین بخش | ظاهر |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
متن آبی، بولد، ایتالیک با 28 پوینت در 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 UTF‑8
ITextToHtmlConversionOptions.EncodingName کاراکتر مجموعهای را که در رشتهٔ HTML بازگشتی توسط ParagraphCollection.ExportToHtml اعلام میشود تنظیم میکند. وقتی این رشته را ذخیره میکنید، رمزگذاریای که به File.WriteAllText میسپارید بایتهای واقعی را تعیین میکند. برای اطمینان از قابلیت رمزگشایی صحیح متن چندزبانه توسط مرورگر، هر دو را یکسان تنظیم کنید.
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
shape.TextFrame.Text = "Café | Ωω | 日本語 | 😀";
var options = new TextToHtmlConversionOptions
{
EncodingName = "utf-8",
AddClipboardFragmentHeader = false
};
var paragraphs = shape.TextFrame.Paragraphs;
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText("paragraphs.html", html, new UTF8Encoding(false));
فایل ذخیرهشده charset=utf-8 را اعلام میکند و حاوی بایتهای UTF‑8 است. باز کردن paragraphs.html در مرورگر متن «Café | Ωω | 日本語 | 😀» را حفظ میکند.
تولید یک قطعه کلیپبورد HTML
AddClipboardFragmentHeader را برای HTML عادی false یا برای شامل شدن هدر کلیپبورد true تنظیم کنید. هر دو فراخوانی یک رشته برمیگردانند. هدر آفستهای بایتی UTF‑8 HTML اطراف (StartHTML و EndHTML) و محتوای قرارگیری (StartFragment و EndFragment) را میدهد.
این مثال دو پاراگراف قالببندیشده را به عنوان HTML عادی (html) و به عنوان یک قطعه کلیپبورد HTML (clipboardHtml) صادر میکند. نوشتن قطعه به کلیپبورد سیستم گامی جداگانه است. API کلیپبوردی که بایتهای خام HTML را میپذیرد، نیاز به UTF‑8 بدون BOM دارد.
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
var paragraphs = shape.TextFrame.Paragraphs;
paragraphs.Clear();
paragraphs.AddFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
var options = new TextToHtmlConversionOptions { AddClipboardFragmentHeader = false };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
options.AddClipboardFragmentHeader = true;
var clipboardHtml = paragraphs.ExportToHtml(0, paragraphs.Count, options);
رندر یک پاراگراف بهعنوان تصویر
IParagraph.GetImage یک پاراگراف منفرد را مستقیماً رندر میکند و یک IImage برمیگرداند. نتیجه را با IImage.Save به یک فایل یا جریان ذخیره کنید. نیازی به رندر کردن شکل حاوی یا برش دستی یک bitmap ندارید.
IParagraph.GetImage ممکن است null برگرداند اگر پاراگراف در مجموعهٔ والد پیدا نشود، مرزهای رندر معتبر نداشته باشد یا نتواند رندر شود. پیش از ذخیرهسازی نتیجه را بررسی کنید و پس از استفاده تصویر برگرداندهشده را آزاد کنید.
رندر پاراگراف در مقیاس پیشفرض
این مثال یک ارائه با یک اسلاید دارد که شکل اول آن یک جعبهٔ متن شامل سه پاراگراف است.

مثال زیر پاراگراف دوم را با ایندکس 1 انتخاب میکند و بدون پارامترهای مقیاس به IParagraph.GetImage فراخوانی میکند. ابتدا بررسی میکند که شکل شامل پاراگراف مورد نیاز است و رندر تصویر را پیش از فراخوانی IImage.Save برای ذخیره به فرمت PNG تأیید میکند. عبارت using تصویر را پس از استفاده آزاد میکند.
using System;
using Aspose.Slides;
using var presentation = new Presentation("sample.pptx");
var shape = presentation.Slides[0].Shapes[0];
if (shape is IAutoShape textShape &&
textShape.TextFrame != null &&
textShape.TextFrame.Paragraphs.Count > 1)
{
var paragraph = textShape.TextFrame.Paragraphs[1];
using var paragraphImage = paragraph.GetImage();
if (paragraphImage != null)
{
paragraphImage.Save("paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
}
else
{
Console.WriteLine("The expected text shape or paragraph was not found.");
}
نتیجه:

رندر پاراگراف در سلول جدول با مقیاس
از overload float scaleX و float scaleY متد IParagraph.GetImage برای تنظیم عوامل مقیاس افقی و عمودی استفاده کنید. مثال زیر یک جدول میسازد، پاراگراف را در اولین سلول با دو برابر عرض و ارتفاع پیشفرض رندر میکند و نتیجه را به صورت تصویر PNG ذخیره مینماید.
using System;
using Aspose.Slides;
var scaleX = 2f;
var scaleY = 2f;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var table = slide.Shapes.AddTable(50, 50, new[] { 300d }, new[] { 80d });
var paragraph = table[0, 0].TextFrame.Paragraphs[0];
paragraph.Text = "Text in a table cell";
using var paragraphImage = paragraph.GetImage(scaleX, scaleY);
if (paragraphImage != null)
{
paragraphImage.Save("table_paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
یک عامل مقیاس 1 آن محور را در اندازهٔ پیکسل پیشفرض نگه میدارد. برای مثال، 2 برای هر دو عامل تصویری با عرض و ارتفاع تقریباً دو برابر ابعاد پیشفرض تولید میکند که چهار برابر پیکسل دارد. عوامل بزرگتر معمولاً متن شفافتری برای زوم یا خروجی با وضوح بالا میسازند، اما حافظه و حجم فایل را نیز افزایش میدهند. عوامل زیر 1 تصاویر کوچکتری با جزئیات کمتر تولید میکنند. برای حفظ نسبت تصویر پاراگراف از عوامل مساوی استفاده کنید؛ عوامل متفاوت افقی و عمودی خروجی را بهصورت مستقل کش میدهند.
رندر کل شکل با IShape.GetImage زمانی مفید است که خروجی نیاز به شامل پر شدن، حاشیه یا سایر زمینههای بصری شکل داشته باشد. برای تصویر فقط پاراگراف، از IParagraph.GetImage استفاده کنید.
سوالات متداول
آیا میتوانم بسته شدن خطوط داخل فریم متنی را بهطور کامل غیرفعال کنم؟
بله. ITextFrameFormat.WrapText را تنظیم کنید تا بسته شدن غیرفعال شود و خطوط در لبههای فریم متنی شکسته نشوند.
چگونه میتوانم حدود دقیق روی اسلاید یک پاراگراف خاص را بهدست آورم؟
از IParagraph.GetRect برای دریافت مستطیل محصور پاراگراف استفاده کنید. IPortion.GetRect حدود یک بخش منفرد را ارائه میدهد.
کنترل تراز پاراگراف (چپ، راست، مرکز یا روبهتوزیع) در کجا انجام میشود؟
IParagraphFormat.Alignment یک تنظیم سطح پاراگراف است و بر کل پاراگراف اعمال میشود صرفنظر از قالببندی بخشهای منفرد.
برای تراز عمودی بخشهای مختلف با اندازهٔ فونت متفاوت در هر خط، به بخش Align Fonts Within a Line مراجعه کنید.
آیا میتوانم زبان تصحیح نوشتاری را برای بخشی از پاراگراف تنظیم کنم؟
بله. برای بخشهای منفرد IBasePortionFormat.LanguageId را تنظیم کنید تا یک پاراگراف بتواند متنی با چندین زبان مختلف داشته باشد.