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 provides fine-grained text formatting through the PortionFormat lớp. A Portion là đơn vị độc lập nhỏ nhất của văn bản; nó tương ứng với một chuỗi đị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 một 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 một 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ề TextFrame đối tượng. Bạn cũng có thể lấy lại nó sau này 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 Paragraph đối tượng (tf.Paragraphs). Mỗi Paragraph chứa Portion đối tượng (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 Phông chữ và Màu sắc

Đặt PortionFormat.FontHeight cho kích thước (đơn vị đ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 Portion trong một Đoạn văn

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

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 vẫn hiển thị màu đen ngay cả sau khi đã đặt màu

Đảm bảo FillFormat.FillType = FillType.Solid is set before assigning the color. Nếu không đặt fill type, 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 xuất hiện trong tệp đã lưu

Thuộc tính LatinFont property đặt họ phông chữ Latin. Nếu không được đặt, phông chữ chủ đề bản trình chiếu sẽ được sử dụng. 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 thế nào để 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 để thiết lập 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 văn):

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

Xem thêm

 Tiếng Việt