วิธีจัดรูปแบบข้อความใน .NET

วิธีจัดรูปแบบข้อความใน .NET

Aspose.Slides FOSS for .NET ให้การจัดรูปแบบข้อความแบบละเอียดผ่านคลาส PortionFormat. Portion คือหน่วยข้อความอิสระที่เล็กที่สุด; มันแมปไปยังการรันการจัดรูปแบบเดียวภายในย่อหน้า. คู่มือนี้แสดงวิธีการใช้การจัดรูปแบบตัวหนา, ตัวเอียง, ขนาดฟอนต์, และสีกับข้อความในงานนำเสนอ.

คู่มือแบบขั้นตอนต่อขั้นตอน

ขั้นตอนที่ 1: ติดตั้งแพคเกจ

dotnet add package Aspose.Slides.Foss

ขั้นตอนที่ 2: เพิ่มรูปทรงพร้อมกรอบข้อความ

ก่อนที่จะจัดรูปแบบข้อความ รูปร่างต้องมี 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);

ขั้นตอนที่ 3: เข้าถึง 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).


ขั้นตอนที่ 4: ใช้การจัดรูปแบบตัวหนาและตัวเอียง

ใช้ 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);

ขั้นตอนที่ 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 กำหนดฟอนต์ตระกูล Latin. หากไม่ได้ตั้งค่า จะใช้ฟอนต์ของธีมการนำเสนอ. ฟอนต์ที่กำหนดเองต้องฝังไว้หรือมีอยู่บนเครื่องที่ดู.


คำถามที่พบบ่อย

ฉันจะเปลี่ยนตระกูลฟอนต์ได้อย่างไร?

ตั้งค่า 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

ดูเพิ่มเติม

 ภาษาไทย