نحوه قالببندی متن در .NET
Aspose.Slides FOSS for .NET provides fine-grained text formatting through the PortionFormat کلاس. A Portion کوچکترین واحد مستقل متن است؛ به یک اجرای قالببندی تک در یک پاراگراف نگاشت میشود. این راهنما نشان میدهد چگونه قالببندی بولد، ایتالیک، اندازه قلم و رنگ را بر روی متن در یک ارائه اعمال کنید.
راهنمای گام به گام
مرحله ۱: نصب بسته
dotnet add package Aspose.Slides.Fossمرحله ۲: افزودن یک شکل با یک قاب متن
قبل از قالببندی متن، یک شکل باید شامل یک TextFrame. استفاده کنید shape.AddTextFrame() برای ایجاد یک مورد.
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Default text: will be formatted");
prs.Save("output.pptx", SaveFormat.Pptx);مرحله ۳: دسترسی به TextFrame
shape.AddTextFrame() باز میگرداند TextFrame شی. همچنین میتوانید آن را بعداً از طریق shape.TextFrame.
var tf = shape.TextFrame; // if the frame already exists
var tf = shape.AddTextFrame(""); // creates a new frameA TextFrame شامل یک فهرست از Paragraph اشیاء (tf.Paragraphs). هر Paragraph شامل Portion اشیاء (paragraph.Portions).
مرحله ۴: اعمال قالببندی Bold و Italic
استفاده کنید PortionFormat.FontBold و PortionFormat.FontItalic. این ویژگیها میپذیرند NullableBool.True, NullableBool.False,، یا NullableBool.NotDefined (از master به ارث میبرد).
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Bold and italic text");
var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontBold = NullableBool.True;
fmt.FontItalic = NullableBool.True;
prs.Save("bold-italic.pptx", SaveFormat.Pptx);مرحله ۵: تنظیم اندازه قلم و رنگ
تنظیم PortionFormat.FontHeight برای اندازه (به نقطه) و استفاده FillFormat برای رنگ.
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Large corporate-blue heading");
var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontHeight = 32; // 32pt font
fmt.FontBold = NullableBool.True;
fmt.FillFormat.FillType = FillType.Solid;
fmt.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 0, 70, 127);
prs.Save("colored-text.pptx", SaveFormat.Pptx);Color.FromArgb(alpha, red, green, blue) مقادیر 0-255 را برای هر کانال میپذیرد.
مرحله ۶: چندین Portion در یک پاراگراف
یک پاراگراف میتواند شامل چندین بخش با قالببندی متفاوت باشد. یک مورد جدید Portion به پاراگراف Portions مجموعه:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 600, 100);
var tf = shape.AddTextFrame(""); // start with empty frame
var paragraph = tf.Paragraphs[0];
// First portion: normal text
var portion1 = paragraph.Portions[0];
portion1.Text = "Normal text followed by ";
portion1.PortionFormat.FontHeight = 20;
// Second portion: bold red text
var portion2 = new Portion();
portion2.Text = "bold red text";
portion2.PortionFormat.FontHeight = 20;
portion2.PortionFormat.FontBold = NullableBool.True;
portion2.PortionFormat.FillFormat.FillType = FillType.Solid;
portion2.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 200, 0, 0);
paragraph.Portions.Add(portion2);
prs.Save("mixed-format.pptx", SaveFormat.Pptx);مشکلات رایج و راهحلها
متن پس از تنظیم رنگ همچنان بهصورت سیاه نمایش داده میشود
اطمینان حاصل کنید FillFormat.FillType = FillType.Solid قبل از اختصاص رنگ تنظیم شود. بدون تنظیم نوع پر، تغییر رنگ ممکن است هیچ اثری نداشته باشد.
NullableBool.True در مقابل true
PortionFormat.FontBold انتظار دارد NullableBool.True,، نه C# true. اختصاص C# true کامپایل نخواهد شد زیرا انواع ناسازگار هستند.
قلم در فایل ذخیرهشده ظاهر نمیشود
این LatinFont ویژگی خانواده فونت لاتین را تنظیم میکند. اگر تنظیم نشود، فونت تم ارائه استفاده میشود. فونتهای سفارشی باید جاسازی شوند یا بر روی دستگاه مشاهدهکننده موجود باشند.
سوالات متداول
چگونه میتوانم خانوادهٔ قلم را تغییر دهم؟?
تنظیم PortionFormat.LatinFont:
fmt.LatinFont = new FontData("Arial");FontData نام خانواده فونت را بهعنوان رشته میپذیرد.
چگونه میتوانم تراز پاراگراف را تنظیم کنم؟?
استفاده ParagraphFormat.Alignment:
tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;مقادیر پشتیبانیشده: Left, Center, Right, Justify.
چگونه میتوانم فاصلهٔ خطوط را تنظیم کنم؟?
استفاده ParagraphFormat.SpaceBefore (نقاط قبل از پاراگراف) یا ParagraphFormat.SpaceAfter (نقاط بعد از پاراگراف):
tf.Paragraphs[0].ParagraphFormat.SpaceBefore = 12; // 12pt before
tf.Paragraphs[0].ParagraphFormat.SpaceAfter = 6; // 6pt after