Cách Định Dạng Văn Bản trong .NET

Cách Định Dạng Văn Bản trong .NET

Aspose.Slides FOSS for .NET cung cấp khả năng định dạng văn bản chi tiết thông qua lớp PortionFormat. Một Portion là đơn vị độc lập nhỏ nhất của văn bản; nó tương ứng với một đoạn định dạng duy nhất trong một đoạn văn. Hướng dẫn này cho thấy cách áp dụng định dạng in đậm, in nghiêng, kích thước phông chữ và màu sắc cho văn bản trong bản trình chiếu.

Hướng Dẫn Từng Bước

Bước 1: Cài đặt gói

dotnet add package Aspose.Slides.Foss

Bước 2: Thêm một Hình dạng với Khung văn bản

Trước khi định dạng văn bản, một hình dạng phải chứa TextFrame. Sử dụng shape.AddTextFrame() để tạo một.

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

Bước 3: Truy cập TextFrame

shape.AddTextFrame() trả về đối tượng TextFrame. Bạn cũng có thể lấy lại nó sau này thông qua shape.TextFrame.

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

Một TextFrame chứa một danh sách các đối tượng Paragraph (tf.Paragraphs). Mỗi Paragraph chứa các đối tượng Portion (paragraph.Portions).


Bước 4: Áp dụng định dạng in đậm và in nghiêng

Sử dụng PortionFormat.FontBoldPortionFormat.FontItalic. Các thuộc tính này chấp nhận NullableBool.True, NullableBool.False hoặc NullableBool.NotDefined (kế thừa từ 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);

Bước 5: Đặt kích thước và màu sắc phông chữ

Đặt PortionFormat.FontHeight cho kích thước (theo điểm) và sử dụng FillFormat cho màu.

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) chấp nhận các giá trị 0-255 cho mỗi kênh.


Bước 6: Nhiều phần trong một đoạn văn

Một đoạn văn có thể chứa nhiều phần với định dạng khác nhau. Thêm một Portion mới vào bộ sưu tập Portions của đoạn văn:

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

Các vấn đề thường gặp và cách khắc phục

Văn bản hiển thị màu đen ngay cả khi đã đặt màu

Đảm bảo FillFormat.FillType = FillType.Solid được đặt trước khi gán màu. Nếu không đặt loại tô, việc thay đổi màu có thể không có hiệu lực.

NullableBool.True vs true

PortionFormat.FontBold mong đợi NullableBool.True, không phải C# true. Gán C# true sẽ không biên dịch được vì các kiểu không tương thích.

Phông chữ không hiển thị trong tệp đã lưu

Thuộc tính LatinFont thiết lập họ phông chữ Latin. Nếu không được thiết lập, phông chữ của giao diện trình chiếu sẽ được sử dụng. Các phông chữ tùy chỉnh phải được nhúng hoặc có sẵn trên máy xem.


Câu hỏi thường gặp

Làm thế nào để thay đổi họ phông chữ?

Đặt PortionFormat.LatinFont:

fmt.LatinFont = new FontData("Arial");

FontData chấp nhận tên họ phông chữ dưới dạng chuỗi.

Làm sao để thiết lập căn chỉnh đoạn văn?

Sử dụng ParagraphFormat.Alignment:

tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;

Các giá trị được hỗ trợ: Left, Center, Right, Justify.

Làm thế nào để đặt khoảng cách dòng?

Sử dụng ParagraphFormat.SpaceBefore (điểm trước đoạn) hoặc ParagraphFormat.SpaceAfter (điểm sau đoạn):

tf.Paragraphs[0].ParagraphFormat.SpaceBefore = 12;   // 12pt before
tf.Paragraphs[0].ParagraphFormat.SpaceAfter = 6;     // 6pt after

Xem thêm

 Tiếng Việt