Quản lý các đoạn văn bản PowerPoint trong .NET
Tổng quan
Aspose.Slides for .NET đại diện cho văn bản dưới dạng một phân cấp các khung văn bản, đoạn văn và phần:
- ITextFrame là container văn bản trong một shape và cung cấp quyền truy cập vào bộ sưu tập đoạn văn của nó.
- IParagraph là một đoạn trong khung văn bản và cung cấp quyền truy cập vào các phần và định dạng cấp đoạn.
- IPortion là một chuỗi văn bản trong một đoạn. Mỗi phần có thể có văn bản và định dạng ký tự riêng.
Do đó một đoạn có thể chứa văn bản với các phông chữ, màu sắc, kích thước và định dạng khác nhau bằng cách sử dụng nhiều phần.
Tạo và Định dạng Đoạn văn
Tạo đoạn trong khung văn bản của shape và sử dụng các phần để định dạng các chuỗi văn bản riêng lẻ. Phần này cho thấy cách kết hợp nhiều phần trong mỗi đoạn và áp dụng các màu, kích thước phông và kiểu khác nhau.
Tạo Đoạn với Nhiều Phần
Ví dụ C# sau tạo ba đoạn, mỗi đoạn chứa ba chuỗi văn bản trong bộ sưu tập IParagraph.Portions. Nó tái sử dụng đoạn mặc định và phần rỗng của nó, sau đó thêm các đoạn và phần còn lại. Định dạng được áp dụng qua IPortion.PortionFormat: phần đầu tiên trong mỗi đoạn có màu đỏ, in đậm và 15 điểm; phần thứ hai có màu xanh lam, in nghiêng và 18 điểm. Phần thứ ba giữ định dạng mặc định:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 150, 300, 150);
var textFrame = shape.TextFrame;
var firstParagraph = textFrame.Paragraphs[0];
firstParagraph.Portions.Add(new Portion());
firstParagraph.Portions.Add(new Portion());
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
secondParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph();
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
thirdParagraph.Portions.Add(new Portion());
textFrame.Paragraphs.Add(thirdParagraph);
var paragraphCount = textFrame.Paragraphs.Count;
for (var paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++)
{
var paragragaph = textFrame.Paragraphs[paragraphIndex];
var portionCount = paragragaph.Portions.Count;
for (var portionIndex = 0; portionIndex < portionCount; portionIndex++)
{
var portion = paragragaph.Portions[portionIndex];
portion.Text = $"Portion {paragraphIndex + 1}.{portionIndex + 1}";
if (portionIndex == 0)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;
portion.PortionFormat.FontBold = NullableBool.True;
portion.PortionFormat.FontHeight = 15;
}
else if (portionIndex == 1)
{
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Blue;
portion.PortionFormat.FontItalic = NullableBool.True;
portion.PortionFormat.FontHeight = 18;
}
}
}
presentation.Save("paragraphs_with_portions.pptx", SaveFormat.Pptx);
Tạo Danh sách Đánh dấu và Đánh số
Sắp xếp văn bản đoạn thành các danh sách với ký hiệu, số hoặc hình ảnh tùy chỉnh. Các ví dụ trong phần này bao gồm định dạng dấu đầu dòng, danh sách đa cấp và số bắt đầu tùy chỉnh.
Tạo Danh sách Đánh dấu hoặc Đánh số
Dấu đầu dòng và đánh số giúp người đọc nhanh chóng nắm bắt các mục liên quan. Trong Aspose.Slides, cài đặt danh sách được định nghĩa qua IBulletFormat.
Ví dụ C# này thêm một dấu đầu dòng ký hiệu và một mục đánh số vào cùng một khung văn bản. IBulletFormat.Type chọn loại dấu đầu dòng, trong khi IBulletFormat.Char cung cấp ký hiệu và IBulletFormat.NumberedBulletStyle chọn kiểu đánh số. Cả hai mục đều sử dụng dấu đầu dòng màu đen với chiều cao 100 % và thụt vào đoạn 25 điểm:
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var symbolParagraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
symbolParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
symbolParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
symbolParagraph.ParagraphFormat.Indent = 25;
symbolParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
symbolParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
symbolParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
symbolParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(symbolParagraph);
var numberedParagraph = new Paragraph { Text = "This is a numbered item" };
numberedParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
numberedParagraph.ParagraphFormat.Bullet.NumberedBulletStyle = NumberedBulletStyle.BulletCircleNumWDBlackPlain;
numberedParagraph.ParagraphFormat.Indent = 25;
numberedParagraph.ParagraphFormat.Bullet.Color.ColorType = ColorType.RGB;
numberedParagraph.ParagraphFormat.Bullet.Color.Color = Color.Black;
numberedParagraph.ParagraphFormat.Bullet.IsBulletHardColor = NullableBool.True;
numberedParagraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(numberedParagraph);
presentation.Save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx);
Sử dụng Dấu đầu dòng Hình ảnh
Dấu đầu dòng hình ảnh cho phép bạn dùng hình ảnh tùy chỉnh thay cho ký hiệu hoặc số.
Ví dụ C# này thêm một hình ảnh vào bộ sưu tập hình ảnh của bản trình bày dưới dạng IPPImage và sử dụng nó làm dấu đầu dòng cho đoạn. Nó đặt IBulletFormat.Type để dùng dấu đầu dòng hình ảnh và gán hình ảnh qua IBulletFormat.Picture. IBulletFormat.Height được đặt thành 100 % để kích thước dấu đầu dòng tương đối với văn bản đoạn:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
using var bulletImage = Images.FromFile("bullets.png");
var presentationImage = presentation.Images.AddImage(bulletImage);
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var paragraph = new Paragraph { Text = "Welcome to Aspose.Slides" };
paragraph.ParagraphFormat.Bullet.Type = BulletType.Picture;
paragraph.ParagraphFormat.Bullet.Picture.Image = presentationImage;
paragraph.ParagraphFormat.Bullet.Height = 100;
textFrame.Paragraphs.Add(paragraph);
presentation.Save("picture_bullet.pptx", SaveFormat.Pptx);
presentation.Save("picture_bullet.ppt", SaveFormat.Ppt);
Tạo Danh sách Đa cấp
Đặt IParagraphFormat.Depth để đặt các đoạn ở các cấp độ khác nhau của một danh sách. Cấp cao nhất có độ sâu 0.
Ví dụ C# này gán các giá trị IParagraphFormat.Depth từ 0 đến 3 cho bốn đoạn, tạo một phân cấp trong một khung văn bản. Mỗi đoạn sử dụng dấu đầu dòng ký hiệu, với IBulletFormat.Char xen kẽ giữa dấu đầu dòng tròn và dấu gạch ngang để phân biệt các cấp liền kề:
using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Content" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
firstParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.Depth = 0;
var secondParagraph = new Paragraph { Text = "Second level" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
secondParagraph.ParagraphFormat.Bullet.Char = '-';
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.Depth = 1;
var thirdParagraph = new Paragraph { Text = "Third level" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
thirdParagraph.ParagraphFormat.Bullet.Char = Convert.ToChar(0x2022);
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.Depth = 2;
var fourthParagraph = new Paragraph { Text = "Fourth level" };
fourthParagraph.ParagraphFormat.Bullet.Type = BulletType.Symbol;
fourthParagraph.ParagraphFormat.Bullet.Char = '-';
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
fourthParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
fourthParagraph.ParagraphFormat.Depth = 3;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
textFrame.Paragraphs.Add(fourthParagraph);
presentation.Save("multilevel_list.pptx", SaveFormat.Pptx);
Bắt đầu các Mục Danh sách Đánh số với Giá trị Tùy chỉnh
Sử dụng IBulletFormat.NumberedBulletStartWith để đặt số ban đầu hiển thị cho một đoạn được đánh số.
Ví dụ C# này đặt IBulletFormat.Type để dùng đánh số cho ba đoạn và gán các giá trị IBulletFormat.NumberedBulletStartWith là 2, 3 và 7. Giá trị bắt đầu được cấu hình riêng cho mỗi đoạn, cho phép các số hiển thị bỏ qua một số giá trị:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "Start at 2" };
firstParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
firstParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 2;
textFrame.Paragraphs.Add(firstParagraph);
var secondParagraph = new Paragraph { Text = "Start at 3" };
secondParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
secondParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 3;
textFrame.Paragraphs.Add(secondParagraph);
var thirdParagraph = new Paragraph { Text = "Start at 7" };
thirdParagraph.ParagraphFormat.Bullet.Type = BulletType.Numbered;
thirdParagraph.ParagraphFormat.Bullet.NumberedBulletStartWith = 7;
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("custom_numbered_list.pptx", SaveFormat.Pptx);
Kiểm soát Bố cục Đoạn và Thuộc tính Kết thúc
Điều chỉnh lề và thụt vào đoạn để kiểm soát vị trí của dòng đầu và các dòng gói. Bạn cũng có thể định dạng dấu kết thúc đoạn riêng biệt so với các phần văn bản hiển thị.
Đặt Thụt vào Dòng Đầu
Sử dụng thuộc tính IParagraphFormat.Indent để kiểm soát thụt vào dòng đầu của đoạn. Thuộc tính này chỉ di chuyển dòng đầu so với lề trái của đoạn. Giá trị dương dịch dòng đầu sang phải, trong khi các dòng còn lại vẫn căn chỉnh với thân đoạn.
Sử dụng IParagraphFormat.MarginLeft khi bạn cần di chuyển toàn bộ đoạn. Dùng IParagraphFormat.Indent khi bạn chỉ muốn di chuyển dòng đầu.
Ví dụ sau đặt ba đoạn cùng có IParagraphFormat.MarginLeft là 20 điểm và các giá trị IParagraphFormat.Indent là 0, 20 và 40 điểm. Các dòng đầu bắt đầu ở các khoảng cách ngày càng lớn, trong khi các dòng gói giữ cùng lề trái:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "No first-line indent. Wrapped lines start at the same position as the first line." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 20;
firstParagraph.ParagraphFormat.Indent = 0;
var secondParagraph = new Paragraph { Text = "First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 20;
secondParagraph.ParagraphFormat.Indent = 20;
var thirdParagraph = new Paragraph { Text = "First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see." };
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
thirdParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
thirdParagraph.ParagraphFormat.MarginLeft = 20;
thirdParagraph.ParagraphFormat.Indent = 40;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
textFrame.Paragraphs.Add(thirdParagraph);
presentation.Save("paragraph_indent.pptx", SaveFormat.Pptx);
Kết quả:

Đặt Thụt vào Treo
Thụt vào treo là bố cục đoạn mà dòng đầu bắt đầu ở bên trái các dòng còn lại. Trong Aspose.Slides, bạn tạo hiệu ứng này bằng thuộc tính IParagraphFormat.Indent. Đặt Indent thành giá trị âm để di chuyển dòng đầu sang trái so với thân đoạn.
Trong thực tế, IParagraphFormat.MarginLeft xác định vị trí trái của thân đoạn, và IParagraphFormat.Indent xác định vị trí của dòng đầu so với lề đó. Để tạo thụt vào treo, đặt giá trị MarginLeft dương và Indent âm.
Định dạng này hữu ích cho thư mục, tài liệu tham khảo, mục từ vựng và các đoạn khác mà các dòng gói phải căn dưới thân đoạn thay vì dưới ký tự đầu tiên của dòng đầu.
Ví dụ C# sau ghép các giá trị IParagraphFormat.MarginLeft 40 và 60 điểm với các giá trị IParagraphFormat.Indent -20 và -30 điểm. Trong mỗi đoạn, dòng đầu bắt đầu bên trái các dòng gói; đoạn thứ hai có thụt vào treo sâu hơn:
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 420, 220);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Gray;
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph { Text = "A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body." };
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
firstParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
firstParagraph.ParagraphFormat.MarginLeft = 40;
firstParagraph.ParagraphFormat.Indent = -20;
var secondParagraph = new Paragraph { Text = "This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
secondParagraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
secondParagraph.ParagraphFormat.MarginLeft = 60;
secondParagraph.ParagraphFormat.Indent = -30;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("hanging_indent.pptx", SaveFormat.Pptx);
Kết quả:

Đặt Thuộc tính Chạy Kết thúc Đoạn
Thuộc tính IParagraph.EndParagraphPortionFormat kiểm soát định dạng dấu kết thúc đoạn. Ví dụ sau tạo một PortionFormat với IBasePortionFormat.FontHeight đặt thành 48 điểm và IBasePortionFormat.LatinFont là Times New Roman. Nó gán định dạng này cho dấu kết thúc của đoạn thứ hai, để các phần văn bản hiển thị không thay đổi:
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("Test.pptx");
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
var textFrame = shape.TextFrame;
textFrame.Paragraphs.Clear();
var firstParagraph = new Paragraph();
firstParagraph.Portions.Add(new Portion("Sample text"));
var secondParagraph = new Paragraph();
secondParagraph.Portions.Add(new Portion("Sample text 2"));
var endParagraphFormat = new PortionFormat();
endParagraphFormat.FontHeight = 48;
endParagraphFormat.LatinFont = new FontData("Times New Roman");
secondParagraph.EndParagraphPortionFormat = endParagraphFormat;
textFrame.Paragraphs.Add(firstParagraph);
textFrame.Paragraphs.Add(secondParagraph);
presentation.Save("end_paragraph_format.pptx", SaveFormat.Pptx);
Đếm Các Dòng Được Vẽ
Đối với các quy tắc đoạn ảnh hưởng tới việc tự động gói và dấu câu ở cuối dòng, xem Control Line Breaking và Control Hanging Punctuation.
Dùng IParagraph.GetLinesCount để đếm số dòng mà một đoạn chiếm sau khi bố trí văn bản, bao gồm việc tự động gói. Điều này hữu ích khi kiểm tra độ dài và bố cục văn bản trong mẫu bản trình bày.
Một đoạn là một mục trong ITextFrame.Paragraphs, và nó có thể chiếm nhiều dòng được vẽ. Một dấu ngắt dòng tường minh trong đoạn buộc tạo dòng mới mà không tạo đoạn mới. Việc gói tự động tạo các dòng dựa trên độ rộng có sẵn mà không chèn ký tự ngắt dòng vào văn bản. Vì vậy, đếm số đoạn hoặc ký tự ngắt dòng không cho kết quả số dòng được vẽ.
Ví dụ sau tạo một shape văn bản, đếm các dòng, thu hẹp shape, rồi thay thế văn bản bằng một chuỗi ngắn hơn. Ví dụ bật ITextFrameFormat.WrapText và tắt ITextFrameFormat.AutofitType để độ rộng shape kiểm soát việc gói mà không tự động thu nhỏ văn bản hoặc thay đổi kích thước shape. Mỗi phép đo sử dụng IParagraph.GetLinesCount. Kích thước shape tính bằng điểm. Cuối cùng, ví dụ thêm một đoạn khác và tổng hợp số dòng trên toàn bộ khung văn bản.
using System;
using Aspose.Slides;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
var textFrame = shape.TextFrame;
textFrame.TextFrameFormat.WrapText = NullableBool.True;
textFrame.TextFrameFormat.AutofitType = TextAutofitType.None;
var paragraph = textFrame.Paragraphs[0];
paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
paragraph.Text = "This text demonstrates how automatic wrapping changes the number of rendered lines.";
Console.WriteLine($"Original width: {paragraph.GetLinesCount()}");
shape.Width = 150;
Console.WriteLine($"Narrower shape: {paragraph.GetLinesCount()}");
paragraph.Text = "Short text.";
Console.WriteLine($"Shorter text: {paragraph.GetLinesCount()}");
var secondParagraph = new Paragraph { Text = "Another paragraph." };
secondParagraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 20;
textFrame.Paragraphs.Add(secondParagraph);
var totalLineCount = 0;
foreach (var currentParagraph in textFrame.Paragraphs)
{
totalLineCount += currentParagraph.GetLinesCount();
}
Console.WriteLine($"Total lines in the text frame: {totalLineCount}");
Với văn bản và các kích thước này, thu hẹp shape làm tăng số dòng, trong khi thay thế bằng chuỗi ngắn hơn làm giảm số dòng. Các số đếm chính xác có thể thay đổi tùy vào sự khả dụng và thay thế phông chữ, kích thước phông, lề, thụt vào, gói và cài đặt autofit. Hãy dùng các phông và cài đặt bố cục dự định cho môi trường đích khi kiểm tra mẫu.
Chỉ số dòng không quyết định liệu văn bản có tràn khỏi container hay không. Chiều cao khả dụng, chiều cao dòng, khoảng cách đoạn và dòng, và hành vi autofit cũng quan trọng; ngay cả một dòng duy nhất cũng có thể vượt quá chiều rộng khả dụng khi tắt gói.
Nhập và Xuất Nội dung Đoạn
Sử dụng HTML để đưa văn bản vào bản trình bày hoặc xuất các đoạn hiện có để dùng bên ngoài. Phần này cũng cho thấy cách render các đoạn riêng lẻ thành hình ảnh, bao gồm các đoạn trong ô bảng.
Nhập Văn bản HTML vào Đoạn
Dùng ParagraphCollection.AddFromHtml để chuyển đổi markup HTML thành các đoạn và phần trong một khung văn bản.
Ví dụ C# này đọc HTML từ tệp và truyền markup cho ParagraphCollection.AddFromHtml. Nó xóa đoạn mặc định của khung văn bản để khung chỉ chứa nội dung đã nhập. Shape hình chữ nhật để lại lề 10 điểm ở mỗi phía của slide:
using System.IO;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shapeWidth = presentation.SlideSize.Size.Width - 20;
var shapeHeight = presentation.SlideSize.Size.Height - 20;
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
shape.FillFormat.FillType = FillType.NoFill;
shape.TextFrame.Paragraphs.Clear();
using var reader = new StreamReader("file.html");
var html = reader.ReadToEnd();
shape.TextFrame.Paragraphs.AddFromHtml(html);
presentation.Save("html_text.pptx", SaveFormat.Pptx);
Xuất Văn bản Đoạn sang HTML
Dùng ParagraphCollection.ExportToHtml để xuất một phạm vi đoạn đã chọn dưới dạng HTML. Khi tái sử dụng văn bản bản trình bày trên website, bạn có thể muốn giữ nguyên nhấn mạnh cục bộ mà không mang theo phông, kích thước hoặc kiểu hộp văn bản của bản trình bày. Đặt TextToHtmlConversionOptions.TextInheritanceLimit để kiểm soát mức độ kế thừa định dạng mà trình xuất tuân theo.
Tải về bản trình chiếu mẫu và đặt nó trong thư mục làm việc của ứng dụng. Slide đầu tiên chứa một shape văn bản với nội dung “Inherited text; explicit red 18pt”. Nội dung mẫu cố ý sử dụng nhiều mức định dạng:
- Kiểu văn bản mặc định của bản trình bày cung cấp Calibri và kích thước 28 pt.
- Kiểu văn bản cấp một của khung cung cấp màu xanh và chữ nghiêng.
- Đoạn đặt rõ ràng chữ in đậm.
- Phần thứ hai, “explicit red 18pt”, đặt rõ màu đỏ và kích thước 18 pt.
Ví dụ hoàn chỉnh sau xuất cùng các đoạn ba lần. Nó truyền TextToHtmlConversionOptions cho phương thức xuất và ghi mỗi kết quả vào một tệp HTML UTF‑8 riêng:
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation("text-inheritance.pptx");
var textShape = (IAutoShape)presentation.Slides[0].Shapes[0];
var paragraphs = textShape.TextFrame.Paragraphs;
var modes = new[] { TextInheritanceLimit.All, TextInheritanceLimit.TextBox, TextInheritanceLimit.ParagraphOnly };
foreach (var mode in modes)
{
var options = new TextToHtmlConversionOptions { TextInheritanceLimit = mode };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText($"{mode}.html", html, Encoding.UTF8);
}
HTML được tạo chứa CSS sau cho phần đầu tiên, “Inherited text; ”. Bảng liệt kê các khai báo liên quan; các thuộc tính được tạo khác được bỏ qua để rõ ràng.
| Output | CSS liên quan cho phần đầu tiên | Appearance |
|---|---|---|
All.html |
font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold |
Văn bản màu xanh, in đậm, nghiêng 28 pt trong Calibri, nếu phông này khả dụng. |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
Văn bản màu xanh, in đậm, nghiêng; trang hoặc trình duyệt cung cấp phông và kích thước. |
ParagraphOnly.html |
font-weight:bold |
Văn bản in đậm; trang hoặc trình duyệt cung cấp phông, kích thước, màu và kiểu nghiêng. |
Phần thứ hai giữ color:#ff0000, font-size:18pt và font-weight:bold trong cả ba đầu ra. Nó cũng giữ chữ nghiêng trong All và TextBox, nhưng không trong ParagraphOnly. Hạn chế kế thừa do đó không xóa định dạng được gán rõ ràng cho đoạn hoặc phần.
Mở các tệp trong cùng một trình duyệt để so sánh. Trong hai chế độ hạn chế, các thuộc tính CSS bị bỏ qua sẽ được giải quyết bởi kiểu của trang đích hoặc mặc định của trình duyệt, vì vậy giao diện cuối cùng phụ thuộc vào nơi HTML được dùng.
Lưu các Đoạn Đã Xuất dưới dạng HTML UTF‑8
ITextToHtmlConversionOptions.EncodingName đặt charset được khai báo trong chuỗi HTML trả về bởi ParagraphCollection.ExportToHtml. Khi bạn lưu chuỗi này, mã hóa bạn truyền cho File.WriteAllText quyết định các byte thực tế. Hãy dùng cùng một mã hóa cho cả hai để trình duyệt có thể giải mã đúng văn bản đa ngôn ngữ.
using System.IO;
using System.Text;
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
shape.TextFrame.Text = "Café | Ωω | 日本語 | 😀";
var options = new TextToHtmlConversionOptions
{
EncodingName = "utf-8",
AddClipboardFragmentHeader = false
};
var paragraphs = shape.TextFrame.Paragraphs;
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
File.WriteAllText("paragraphs.html", html, new UTF8Encoding(false));
Tệp đã lưu khai báo charset=utf-8 và chứa các byte UTF‑8. Mở paragraphs.html trong trình duyệt sẽ giữ nguyên văn bản “Café | Ωω | 日本語 | 😀”.
Tạo Đoạn HTML Clipboard Fragment
Đặt AddClipboardFragmentHeader thành false để tạo HTML thông thường hoặc true để bao gồm tiêu đề clipboard. Cả hai lời gọi đều trả về một chuỗi. Tiêu đề cung cấp các offset byte UTF‑8 của HTML xung quanh (StartHTML và EndHTML) và nội dung cần dán (StartFragment và EndFragment).
Ví dụ này xuất hai đoạn đã định dạng dưới dạng HTML thông thường (html) và dưới dạng fragment clipboard HTML (clipboardHtml). Việc ghi fragment vào clipboard hệ thống là bước ứng dụng riêng. API clipboard chấp nhận raw HTML clipboard bytes yêu cầu UTF‑8 không có BOM.
using Aspose.Slides;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 0, 0, 400, 100);
var paragraphs = shape.TextFrame.Paragraphs;
paragraphs.Clear();
paragraphs.AddFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
var options = new TextToHtmlConversionOptions { AddClipboardFragmentHeader = false };
var html = paragraphs.ExportToHtml(0, paragraphs.Count, options);
options.AddClipboardFragmentHeader = true;
var clipboardHtml = paragraphs.ExportToHtml(0, paragraphs.Count, options);
Render Đoạn dưới dạng Hình ảnh
IParagraph.GetImage render một đoạn riêng lẻ và trả về một IImage. Lưu kết quả vào tệp hoặc stream bằng IImage.Save. Bạn không cần render shape chứa hoặc cắt bitmap thủ công.
IParagraph.GetImage có thể trả về null nếu đoạn không tồn tại trong bộ sưu tập cha, không có giới hạn render hợp lệ, hoặc không thể render. Kiểm tra kết quả trước khi lưu và giải phóng ảnh trả về sau khi dùng.
Render Đoạn với Tỷ lệ Mặc định
Ví dụ sử dụng một bản trình bày có một slide, shape đầu tiên là textbox chứa ba đoạn.

Ví dụ sau chọn đoạn thứ hai (chỉ mục 1) và gọi IParagraph.GetImage mà không truyền tham số tỷ lệ. Nó kiểm tra shape có chứa đoạn yêu cầu và việc render trả về ảnh trước khi gọi IImage.Save để lưu dưới dạng PNG. Câu lệnh using giải phóng ảnh sau khi dùng.
using System;
using Aspose.Slides;
using var presentation = new Presentation("sample.pptx");
var shape = presentation.Slides[0].Shapes[0];
if (shape is IAutoShape textShape &&
textShape.TextFrame != null &&
textShape.TextFrame.Paragraphs.Count > 1)
{
var paragraph = textShape.TextFrame.Paragraphs[1];
using var paragraphImage = paragraph.GetImage();
if (paragraphImage != null)
{
paragraphImage.Save("paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
}
else
{
Console.WriteLine("The expected text shape or paragraph was not found.");
}
Kết quả:

Render Đoạn trong Ô Bảng với Tỷ lệ
Sử dụng overload của IParagraph.GetImage nhận các tham số float scaleX và float scaleY để đặt hệ số tỷ lệ ngang và dọc. Ví dụ sau tạo một bảng, render đoạn trong ô đầu tiên với độ rộng và chiều cao gấp đôi mặc định, và lưu kết quả dưới dạng PNG.
using System;
using Aspose.Slides;
var scaleX = 2f;
var scaleY = 2f;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var table = slide.Shapes.AddTable(50, 50, new[] { 300d }, new[] { 80d });
var paragraph = table[0, 0].TextFrame.Paragraphs[0];
paragraph.Text = "Text in a table cell";
using var paragraphImage = paragraph.GetImage(scaleX, scaleY);
if (paragraphImage != null)
{
paragraphImage.Save("table_paragraph.png", ImageFormat.Png);
}
else
{
Console.WriteLine("The paragraph could not be rendered.");
}
Hệ số 1 giữ kích thước pixel mặc định cho trục tương ứng. Ví dụ, 2 cho cả hai hệ số tạo ảnh có chiều rộng và chiều cao khoảng gấp đôi kích thước mặc định, tương đương với bốn lần pixel. Các hệ số lớn hơn thường cho văn bản nét hơn cho việc phóng to hoặc xuất độ phân giải cao, nhưng cũng tăng dùng bộ nhớ và kích thước tệp. Hệ số dưới 1 tạo ảnh nhỏ hơn với ít chi tiết hơn. Dùng các hệ số bằng nhau để giữ tỉ lệ khung hình đoạn; các hệ số ngang và dọc khác nhau sẽ kéo giãn đầu ra một cách độc lập.
Render toàn bộ shape bằng IShape.GetImage vẫn hữu ích khi đầu ra cần bao gồm nền, viền hoặc ngữ cảnh hình ảnh khác của shape. Đối với ảnh chỉ chứa đoạn, hãy dùng IParagraph.GetImage.
Câu hỏi thường gặp
Có thể tắt hoàn toàn việc gói dòng bên trong khung văn bản không?
Có. Đặt ITextFrameFormat.WrapText để tắt gói, vì vậy các dòng sẽ không ngắt ở cạnh khung văn bản.
Làm thế nào để lấy giới hạn trên slide chính xác của một đoạn cụ thể?
Dùng IParagraph.GetRect để lấy hình chữ nhật bao quanh đoạn. IPortion.GetRect cung cấp giới hạn của một phần riêng lẻ.
Độ căn chỉnh đoạn (trái, phải, giữa, hoặc canh đều) được kiểm soát ở đâu?
IParagraphFormat.Alignment là cài đặt cấp đoạn và áp dụng cho toàn bộ đoạn bất kể định dạng riêng của các phần.
Để căn chỉnh dọc các phần có kích thước phông khác nhau trong mỗi dòng, xem Align Fonts Within a Line.
Có thể đặt ngôn ngữ kiểm tra chính tả cho một phần của đoạn không?
Có. Đặt IBasePortionFormat.LanguageId cho các phần riêng lẻ, để một đoạn có thể chứa văn bản bằng nhiều ngôn ngữ.