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

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

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

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

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

dotnet add package Aspose.Slides.Foss

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

ก่อนทำการจัดรูปแบบข้อความ, shape ต้องมี a TextFrame. Use 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 object. คุณยังสามารถดึงคืนได้ในภายหลังผ่าน shape.TextFrame.

var tf = shape.TextFrame;          // if the frame already exists
var tf = shape.AddTextFrame("");   // creates a new frame

A TextFrame มีรายการของ Paragraph objects (tf.Paragraphs). แต่ละ Paragraph มี Portion objects (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 ในย่อหน้าเดียว

ย่อหน้าเดียวสามารถมีหลายส่วนที่มีการจัดรูปแบบต่างกันได้ เพิ่มใหม่ 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 ต้องตั้งค่าให้เสร็จก่อนกำหนดสี หากไม่ได้ตั้งค่า fill type การเปลี่ยนสีอาจไม่มีผล.

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

ดูเพิ่มเติม

 ภาษาไทย