مدیریت پاراگراف‌های متن PowerPoint در .NET

بررسی کلی

Aspose.Slides for .NET متن را به صورت سلسله‌مراتبی از فریم‌های متن، پاراگراف‌ها و بخش‌ها نمایان می‌کند:

  • ITextFrame نمایانگر محفظه متن در یک شکل است و دسترسی به مجموعه پاراگراف‌های آن را فراهم می‌کند.
  • IParagraph نمایانگر یک پاراگراف در فریم متن است و دسترسی به بخش‌ها و قالب‌بندی سطح پاراگراف را ارائه می‌دهد.
  • IPortion نمایانگر یک تکه متنی درون یک پاراگراف است. هر بخش می‌تواند متن و قالب‌بندی سطح کاراکتر خود را داشته باشد.

بنابراین یک پاراگراف می‌تواند متنی با قلم‌ها، رنگ‌ها، اندازه‌ها و قالب‌بندی‌های مختلف داشته باشد که با استفاده از چندین بخش ساخته می‌شود.

ایجاد و قالب‌بندی پاراگراف‌ها

ایجاد پاراگراف‌ها با بخش‌های متعدد

مراحل زیر یک فریم متن با سه پاراگراف، هرکدام شامل سه بخش، ایجاد می‌کند:

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. از طریق ایندکس به اسلاید مورد نظر دسترسی پیدا کنید.
  3. یک IAutoShape مستطیلی به اسلاید اضافه کنید.
  4. به ITextFrame شکل دسترسی پیدا کنید.
  5. از پاراگراف پیش‌فرض استفاده کنید و دو IParagraph دیگر به فریم متن اضافه کنید.
  6. برای هر پاراگراف به اندازه کافی IPortion اضافه کنید تا هر کدام سه بخش داشته باشند. پاراگراف پیش‌فرض در حال حاضر یک بخش خالی دارد.
  7. متن هر بخش را تنظیم کنید.
  8. قالب‌بندی سطح کاراکتر را از طریق IPortion.PortionFormat اعمال کنید.
  9. ارائه اصلاح‌شده را ذخیره کنید.

این مثال C# مراحل فوق را پیاده‌سازی می‌کند:

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 تعریف می‌شوند.

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. از طریق ایندکس به اسلاید مورد نظر دسترسی پیدا کنید.
  3. یک IAutoShape به اسلاید انتخاب‌شده اضافه کنید.
  4. به ITextFrame شکل دسترسی پیدا کنید.
  5. پاراگراف پیش‌فرض را از فریم متن حذف کنید.
  6. یک Paragraph برای یک نقطه نمادین ایجاد کنید.
  7. IBulletFormat.Type را به BulletType.Symbol تنظیم کنید و کاراکتر نقطه را مشخص کنید.
  8. متن پاراگراف، تو رفتگی، رنگ نقطه و ارتفاع نقطه را تنظیم کنید.
  9. پاراگراف را به فریم متن اضافه کنید.
  10. یک پاراگراف دوم ایجاد کنید و IBulletFormat.Type را به BulletType.Numbered تنظیم کنید.
  11. سبک نقطه شماره‌دار را پیکربندی کنید و پاراگراف را به فریم متن اضافه کنید.
  12. ارائه را ذخیره کنید.

این مثال C# یک نقطه نمادین و یک نقطه شماره‌دار ایجاد می‌کند:

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);

استفاده از نقطه‌های تصویری

نقطه‌های تصویری به شما امکان می‌دهند به‌جای نماد یا عدد، یک تصویر سفارشی استفاده کنید.

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. از طریق ایندکس به اسلاید مورد نظر دسترسی پیدا کنید.
  3. یک IAutoShape اضافه کنید و به ITextFrame آن دسترسی پیدا کنید.
  4. پاراگراف پیش‌فرض را از فریم متن حذف کنید.
  5. تصویر نقطه را بارگذاری کرده و به مجموعه تصاویر ارائه به عنوان یک IPPImage اضافه کنید.
  6. یک Paragraph ایجاد کنید و متن آن را تنظیم کنید.
  7. IBulletFormat.Type را به BulletType.Picture تنظیم کنید.
  8. تصویر را از طریق IBulletFormat.Picture اختصاص دهید و ارتفاع نقطه را تنظیم کنید.
  9. پاراگراف را به فریم متن اضافه کنید.
  10. ارائه اصلاح‌شده را ذخیره کنید.

این مثال C# یک نقطه تصویری ایجاد می‌کند:

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 دارد.

  1. یک Presentation ایجاد کنید و به یک اسلاید دسترسی پیدا کنید.
  2. یک IAutoShape اضافه کنید و پاراگراف پیش‌فرض را از فریم متن آن پاک کنید.
  3. چهار پاراگراف ایجاد کنید و نمادهای نقطه آنها را پیکربندی کنید.
  4. مقدار IParagraphFormat.Depth آنها را به ترتیب 0، 1، 2 و 3 تنظیم کنید.
  5. پاراگراف‌ها را به فریم متن اضافه کنید و ارائه را ذخیره کنید.

این مثال C# یک فهرست چهارسطحی نقطه‌دار ایجاد می‌کند:

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 برای تنظیم عدد اولیه نمایش داده شده برای یک پاراگراف شماره‌دار استفاده کنید.

  1. یک Presentation ایجاد کنید و یک IAutoShape به اسلاید اضافه کنید.
  2. پاراگراف پیش‌فرض را از فریم متن شکل پاک کنید.
  3. سه پاراگراف شماره‌دار ایجاد کنید.
  4. IBulletFormat.NumberedBulletStartWith را به ترتیب برای پاراگراف‌ها به 2، 3 و 7 تنظیم کنید.
  5. پاراگراف‌ها را به فریم متن اضافه کنید و ارائه را ذخیره کنید.

این مثال C# عدد شروع دلخواه را برای هر پاراگراف تنظیم می‌کند:

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.Indent را برای نشان دادن تأثیر تورفتگی اولین خط اعمال می‌نماید.

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. اسلاید هدف را دسترسی پیدا کنید.
  3. یک IAutoShape مستطیلی به اسلاید اضافه کنید.
  4. به ITextFrame shape دسترسی پیدا کنید و پاراگراف پیش‌فرض را حذف کنید.
  5. چند پاراگراف ایجاد کنید و مقادیر مختلف Indent را برای آنها تنظیم کنید.
  6. پاراگراف‌ها را به فریم متن اضافه کنید.
  7. ارائه اصلاح‌شده را ذخیره کنید.

این کد نحوه تنظیم تورفتگی یک پاراگراف را نشان می‌دهد:

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 تنظیم کنید.

این قالب‌بندی برای کتابشناسی‌ها، مراجع، واژه‌نامه‌ها و سایر پاراگراف‌هایی که خطوط پیچیده باید زیر بدنه پاراگراف هم‌راستا شوند مفید است.

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. اسلاید هدف را دسترسی پیدا کنید.
  3. یک IAutoShape مستطیلی به اسلاید اضافه کنید.
  4. به ITextFrame shape دسترسی پیدا کنید و پاراگراف پیش‌فرض را حذف کنید.
  5. پاراگراف‌ها را ایجاد کنید و برای هر یک مقدار مثبت MarginLeft تنظیم کنید.
  6. مقدار منفی Indent را تنظیم کنید تا اثر تورفتگی آویز ایجاد شود.
  7. پاراگراف‌ها را به فریم متن اضافه کنید.
  8. ارائه اصلاح‌شده را ذخیره کنید.

این کد نحوه تنظیم تورفتگی آویز برای یک پاراگراف را نشان می‌دهد:

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 قالب‌بندی علامت انتهای پاراگراف را کنترل می‌کند. مثال زیر اندازه قلم و قلم لاتین را برای علامت انتهای پاراگراف دوم تنظیم می‌کند:

  1. یک Presentation بارگذاری کنید و به یک اسلاید دسترسی پیدا کنید.
  2. یک IAutoShape اضافه کنید و پاراگراف پیش‌فرض آن را پاک کنید.
  3. دو پاراگراف ایجاد کنید و به آنها بخش‌های متنی اضافه کنید.
  4. یک PortionFormat برای علامت انتهای پاراگراف دوم ایجاد کنید.
  5. IBasePortionFormat.FontHeight و IBasePortionFormat.LatinFont را تنظیم کنید.
  6. این قالب را به IParagraph.EndParagraphPortionFormat اختصاص دهید و ارائه را ذخیره کنید.
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 است و می‌تواند چند خط رندر شده اشغال کند. شکست خط صریح داخل پاراگراف یک خط جدید ایجاد می‌کند بدون اینکه پاراگراف جدیدی ساخته شود. بسته شدن خودکار خطوط را بر اساس عرض موجود تولید می‌کند بدون افزودن کاراکترهای شکست صریح. بنابراین شمارش پاراگراف‌ها یا کاراکترهای شکست خط، شمارش خطوط رندر شده را نمی‌دهد.

مثال زیر یک شکل متنی ایجاد می‌کند، خطوط آن را می‌شمارد، شکل را باریک می‌کند و سپس متن را با رشته کوتاه‌تری جایگزین می‌کند. بسته شدن فعال است و AutoFit غیرفعال، بنابراین عرض شکل بسته شدن را کنترل می‌کند بدون اینکه متن به طور خودکار کوچک یا شکل تغییر اندازه دهد. ابعاد شکل بر حسب پوینت است. در نهایت، یک پاراگراف دیگر اضافه می‌شود و تعداد خطوط در فریم متن جمع می‌شود.

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}");

با این متن و این ابعاد، باریک کردن شکل تعداد خطوط را افزایش می‌دهد، در حالی که جایگزینی متن با رشته کوتاه تعداد خطوط را کاهش می‌دهد. شمارش دقیق می‌تواند بسته به دسترسی به قلم‌ها و جایگزینی‌ها، اندازه قلم، حاشیه‌ها، تورفتگی، بسته شدن و تنظیمات AutoFit متفاوت باشد. برای بررسی یک قالب، از قلم‌ها و تنظیمات چیدمانی مورد انتظار در محیط هدف استفاده کنید.

تک‌تنهایی شمارش خطوط تعیین‌کننده این نیست که متن از محفظه‌اش تجاوز می‌کند یا نه. ارتفاع موجود، ارتفاع خطوط، فواصل پاراگراف و خط، و رفتار AutoFit نیز مؤثر هستند؛ حتی یک خط می‌تواند عرض موجود را هنگام غیرفعال بودن بسته شدن تجاوز کند.

واردات و صادرات محتوای پاراگراف

واردات متن HTML به پاراگراف‌ها

از ParagraphCollection.AddFromHtml برای تبدیل نشانه‌گذاری HTML به پاراگراف‌ها و بخش‌ها در یک فریم متن استفاده کنید.

  1. یک نمونه از کلاس Presentation ایجاد کنید.
  2. به یک اسلاید دسترسی پیدا کنید و یک IAutoShape اضافه کنید.
  3. به ITextFrame شکل دسترسی پیدا کنید و پاراگراف پیش‌فرض را پاک کنید.
  4. فایل HTML منبع را بخوانید.
  5. رشته HTML را به ParagraphCollection.AddFromHtml پاس دهید.
  6. ارائه اصلاح‌شده را ذخیره کنید.

این مثال C# HTML را به فریم متن وارد می‌کند:

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 استفاده کنید.

  1. یک نمونه از کلاس Presentation ایجاد کنید و ارائه مورد نظر را بارگذاری کنید.
  2. به اسلاید دسترسی پیدا کنید و IAutoShape که متن را دارد پیدا کنید.
  3. به ITextFrame شکل دسترسی پیدا کنید.
  4. ParagraphCollection.ExportToHtml را با ایندکس پاراگراف شروع و تعداد پاراگراف‌های مورد نیاز فراخوانی کنید.
  5. رشته HTML برگردانده‌شده را در فایلی بنویسید.

این مثال C# تمام پاراگراف‌های اولین شکل متنی را صادر می‌کند:

using System;
using System.IO;
using System.Text;
using Aspose.Slides;

using var presentation = new Presentation("ExportingHTMLText.pptx");
var shape = presentation.Slides[0].Shapes[0];

if (shape is IAutoShape textShape && textShape.TextFrame != null)
{
    var paragraphs = textShape.TextFrame.Paragraphs;
    var html = paragraphs.ExportToHtml(0, paragraphs.Count, null);
    using var writer = new StreamWriter("paragraphs.html", false, Encoding.UTF8);
    writer.Write(html);
}
else
{
    Console.WriteLine("The first shape is not a text shape.");
}

رندر یک پاراگراف به‌عنوان تصویر

IParagraph.GetImage یک پاراگراف منفرد را مستقیماً رندر می‌کند و یک IImage برمی‌گرداند. نتیجه را با IImage.Save در یک فایل یا جریان ذخیره کنید. نیازی به رندر شکل شامل‌کننده یا برش دستی bitmap ندارید.

IParagraph.GetImage می‌تواند null برگرداند اگر پاراگراف در مجموعه والد یافت نشود، حدود رندر معتبری نداشته باشد یا قابل رندر نباشد. قبل از ذخیره نتیجه را بررسی کنید و پس از استفاده تصویر برگردانده‌شده را آزاد کنید.

رندر پاراگراف با مقیاس پیش‌فرض

فرض کنید فایلی به نام sample.pptx داریم که یک اسلاید دارد و اولین شکل آن یک جعبه متن با سه پاراگراف است.

جعبه متن با سه پاراگراف

مثال زیر پاراگراف دوم را در یک شکل متنی عادی با مقیاس پیش‌فرض رندر می‌کند و تصویر برگردانده‌شده را در فرمت 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 IParagraph.GetImage که پارامترهای float scaleX و float scaleY را می‌پذیرد برای تنظیم عوامل مقیاس افقی و عمودی استفاده کنید. مثال زیر یک جدول ایجاد می‌کند، پاراگراف را در اولین سلول آن با دو برابر عرض و ارتفاع پیش‌فرض رندر می‌کند و نتیجه را به‌صورت تصویر 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 تنظیم سطح پاراگرافی است و بر تمام پاراگراف صرف‌نظر از قالب‌بندی بخش‌های منفرد اعمال می‌شود.

آیا می‌توانم زبان proofreading را برای بخشی از پاراگراف تنظیم کنم؟

بله. IBasePortionFormat.LanguageId را برای بخش‌های منفرد تنظیم کنید، به‌طوری که یک پاراگراف بتواند متن‌های چند زبانه داشته باشد.