چگونه متن را در .NET قالب‌بندی کنیم

چگونه متن را در .NET قالب‌بندی کنیم

Aspose.Slides FOSS برای .NET قالب‌بندی دقیق متن را از طریق کلاس PortionFormat فراهم می‌کند. یک Portion کوچک‌ترین واحد مستقل متن است؛ این واحد به یک اجرای قالب‌بندی منفرد درون یک پاراگراف نگاشت می‌شود. این راهنما نشان می‌دهد چگونه قالب‌بندی‌های بولد، ایتالیک، اندازه قلم و رنگ را بر متن در یک ارائه اعمال کنیم.

راهنمای گام به گام

مرحله 1: نصب بسته

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 frame

یک TextFrame شامل فهرستی از اشیاء Paragraph (tf.Paragraphs) است. هر Paragraph شامل اشیاء Portion (paragraph.Portions) می‌باشد.


مرحله ۴: اعمال قالب‌بندی بولد و ایتالیک

از PortionFormat.FontBold و PortionFormat.FontItalic استفاده کنید. این ویژگی‌ها NullableBool.True، NullableBool.False یا NullableBool.NotDefined را می‌پذیرند (از مستر ارث می‌برند).

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

مرحله 5: تنظیم اندازه و رنگ فونت

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 را برای هر کانال می‌پذیرد.


مرحله 6: چندین بخش در یک پاراگراف

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

همچنین ببینید

 فارسی