Quản lý các đoạn văn bản PowerPoint trong .NET
Tổng quan
Aspose.Slides for .NET biểu diễn văn bản dưới dạng một cấu trúc cây gồm các khung văn bản, đoạn văn và phần:
- ITextFrame đại diện cho vùng chứa văn bản trong một hình dạng và cung cấp quyền truy cập vào bộ sưu tập đoạn văn của nó.
- IParagraph đại diện cho một đoạn vă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 đại diện cho 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 vă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 Văn với Nhiều Phần
Các bước sau tạo một khung văn bản với ba đoạn, mỗi đoạn chứa ba phần:
- Tạo một thể hiện của lớp Presentation.
- Truy cập tham chiếu slide liên quan thông qua chỉ mục của nó.
- Thêm một IAutoShape hình chữ nhật vào slide.
- Truy cập ITextFrame của hình dạng.
- Sử dụng đoạn mặc định và thêm hai đối tượng IParagraph nữa vào khung văn bản.
- Thêm đủ đối tượng IPortion cho mỗi đoạn để chứa ba phần. Đoạn mặc định đã chứa một phần rỗng.
- Đặt văn bản cho mỗi phần.
- Áp dụng định dạng ký tự thông qua IPortion.PortionFormat.
- Lưu bản trình chiếu đã sửa đổi.
Ví dụ C# thực hiện các bước trê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, 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ố
Tạo Danh Sách Đánh Dấu hoặc Đánh Số
Các dấu đầu dòng và đánh số giúp người đọc dễ dàng quét các mục liên quan. Trong Aspose.Slides, cài đặt danh sách được xác định qua IBulletFormat.
- Tạo một thể hiện của lớp Presentation.
- Truy cập tham chiếu slide liên quan thông qua chỉ mục của nó.
- Thêm một IAutoShape vào slide đã chọn.
- Truy cập ITextFrame của hình dạng.
- Xóa đoạn mặc định khỏi khung văn bản.
- Tạo một Paragraph cho dấu đầu dòng ký hiệu.
- Đặt IBulletFormat.Type thành BulletType.Symbol và chỉ định ký tự dấu đầu dòng.
- Đặt văn bản đoạn, thụt lề, màu sắc dấu đầu dòng và chiều cao dấu đầu dòng.
- Thêm đoạn vào khung văn bản.
- Tạo đoạn thứ hai và đặt IBulletFormat.Type thành BulletType.Numbered.
- Cấu hình kiểu dấu đầu dòng đánh số và thêm đoạn vào khung văn bản.
- Lưu bản trình chiếu.
Ví dụ C# tạo một dấu đầu dòng ký hiệu và một dấu đầu dòng đánh số:
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 sử dụng ảnh tùy chỉnh thay vì ký hiệu hoặc số.
- Tạo một thể hiện của lớp Presentation.
- Truy cập tham chiếu slide liên quan thông qua chỉ mục của nó.
- Thêm một IAutoShape và truy cập ITextFrame của nó.
- Xóa đoạn mặc định khỏi khung văn bản.
- Tải ảnh dấu đầu dòng và thêm nó vào bộ sưu tập ảnh của bản trình chiếu dưới dạng IPPImage.
- Tạo một Paragraph và đặt văn bản cho nó.
- Đặt IBulletFormat.Type thành BulletType.Picture.
- Gán ảnh qua IBulletFormat.Picture và đặt chiều cao dấu đầu dòng.
- Thêm đoạn vào khung văn bản.
- Lưu bản trình chiếu đã sửa đổi.
Ví dụ C# tạo một dấu đầu dòng hình ảnh:
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 danh sách. Cấp độ cao nhất có độ sâu 0.
- Tạo một Presentation và truy cập một slide.
- Thêm một IAutoShape và xóa đoạn mặc định khỏi khung văn bản của nó.
- Tạo bốn đoạn và cấu hình các ký hiệu dấu đầu dòng của chúng.
- Đặt giá trị IParagraphFormat.Depth thành
0,1,2và3. - Thêm các đoạn vào khung văn bản và lưu bản trình chiếu.
Ví dụ C# tạo một danh sách có bốn mức độ dấu đầu dòng:
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 Đánh Số Từ Giá Trị Tùy Chỉnh
Sử dụng IBulletFormat.NumberedBulletStartWith để đặt số ban đầu hiển thị cho một đoạn đánh số.
- Tạo một Presentation và thêm một IAutoShape vào một slide.
- Xóa đoạn mặc định khỏi khung văn bản của hình dạng.
- Tạo ba đoạn đánh số.
- Đặt IBulletFormat.NumberedBulletStartWith thành
2,3và7cho các đoạn tương ứng. - Thêm các đoạn vào khung văn bản và lưu bản trình chiếu.
Ví dụ C# gán số bắt đầu tùy chỉnh cho mỗi đoạn:
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ăn và Thuộc Tính Kết Thúc
Đặt Thụt Dòng Dòng Đầu Tiên
Sử dụng thuộc tính IParagraphFormat.Indent để kiểm soát thụt dòng của dòng đầu tiên trong một đoạn. Thuộc tính này chỉ di chuyển dòng đầu tiên so với lề trái của đoạn. Giá trị dương dịch dòng đầu tiên sang bên 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. Sử dụng IParagraphFormat.Indent khi bạn chỉ muốn di chuyển dòng đầu tiên.
Ví dụ dưới đây tạo một số đoạn và áp dụng các giá trị IParagraphFormat.Indent khác nhau để minh họa cách thụt dòng đầu tiên ảnh hưởng đến bố cục đoạn.
- Tạo một thể hiện của lớp Presentation.
- Truy cập slide mục tiêu.
- Thêm một IAutoShape hình chữ nhật vào slide.
- Truy cập ITextFrame của hình dạng và xóa đoạn mặc định.
- Tạo một số đoạn và đặt các giá trị Indent khác nhau cho chúng.
- Thêm các đoạn vào khung văn bản.
- Lưu bản trình chiếu đã sửa đổi.
Mã này cho bạn thấy cách đặt thụt dòng cho một đoạ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 = "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 Dòng Treo
Thụt dòng treo là bố cục đoạn trong đó dòng đầu tiên bắt đầu ở phía trái hơn 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 tiên 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 tiên so với lề đó. Để tạo thụt dòng treo, đặt giá trị MarginLeft dương và Indent âm.
Định dạng này hữu ích cho các mục thư mục, tham khảo, mục từ điển và các đoạn khác mà các dòng gói cần căn dưới thân đoạn thay vì dưới ký tự đầu tiên của dòng đầu.
- Tạo một thể hiện của lớp Presentation.
- Truy cập slide mục tiêu.
- Thêm một IAutoShape hình chữ nhật vào slide.
- Truy cập ITextFrame của hình dạng và xóa đoạn mặc định.
- Tạo các đoạn và đặt giá trị MarginLeft dương cho mỗi đoạn.
- Đặt giá trị Indent âm để tạo hiệu ứng thụt dòng treo.
- Thêm các đoạn vào khung văn bản.
- Lưu bản trình chiếu đã sửa đổi.
Mã này cho bạn thấy cách đặt thụt dòng treo cho một đoạ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 Kết Thúc Đoạn Văn
Thuộc tính IParagraph.EndParagraphPortionFormat kiểm soát định dạng của ký tự kết thúc đoạn. Ví dụ sau gán kích thước phông và phông Latin cho ký tự kết thúc của đoạn thứ hai:
- Tải một Presentation và truy cập một slide.
- Thêm một IAutoShape và xóa đoạn mặc định của nó.
- Tạo hai đoạn và thêm các phần văn bản vào chúng.
- Tạo một PortionFormat cho ký tự kết thúc của đoạn thứ hai.
- Đặt IBasePortionFormat.FontHeight và IBasePortionFormat.LatinFont.
- Gán định dạng cho IParagraph.EndParagraphPortionFormat và lưu bản trình chiếu.
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 Số Dòng Được Kết Xuất
Đối với các quy tắc đoạn ảnh hưởng đến việc gói tự động và dấu câu ở cuối dòng, xem Control Line Breaking và Control Hanging Punctuation.
Sử dụng IParagraph.GetLinesCount để đếm số dòng chiếm bởi một đoạn sau khi bố trí văn bản, bao gồm cả việc gói tự động. Điều này hữu ích khi kiểm tra độ dài và bố cục văn bản trong các mẫu bản trình chiếu.
Một đoạn là một mục trong ITextFrame.Paragraphs, và nó có thể chiếm nhiều dòng được kết xuất. Một ngắt dòng rõ ràng trong một đoạn buộc tạo một 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 chiều 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, việc đếm số đoạn hoặc ký tự ngắt dòng không cho kết quả là số dòng được kết xuất.
Ví dụ dưới đây tạo một hình dạng văn bản, đếm các dòng, thu hẹp hình dạng, rồi thay thế văn bản bằng một chuỗi ngắn hơn. Việc gói được bật và tự động vừa không được bật để chiều rộng hình dạng 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 hình dạng. Kích thước hình dạng được 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, việc thu hẹp hình dạng làm tăng số dòng, trong khi thay thế bằng chuỗi ngắn làm giảm số dòng. Các số đếm chính xác có thể thay đổi tùy thuộc vào phông khả dụng và thay thế, kích thước phông, lề, thụt lề, gói và cài đặt tự động vừa. Hãy sử 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ột mẫu.
Số dòng một mình không quyết định liệu văn bản có tràn ra khỏi vùng chứa hay không. Chiều cao khả dụng, chiều cao dòng, khoảng cách giữa các đoạn và dòng, và hành vi tự động vừa 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 Văn
Nhập Văn Bản HTML vào Đoạn Văn
Sử dụng ParagraphCollection.AddFromHtml để chuyển đổi mã HTML thành các đoạn và phần trong một khung văn bản.
- Tạo một thể hiện của lớp Presentation.
- Truy cập một slide và thêm một IAutoShape.
- Truy cập ITextFrame của hình dạng và xóa đoạn mặc định.
- Đọc tệp HTML nguồn.
- Gửi chuỗi HTML tới ParagraphCollection.AddFromHtml.
- Lưu bản trình chiếu đã sửa đổi.
Ví dụ C# nhập HTML vào một khung văn bản:
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
Sử dụng ParagraphCollection.ExportToHtml để xuất một phạm vi đoạn đã chọn dưới dạng HTML.
- Tạo một thể hiện của lớp Presentation và tải bản trình chiếu mong muốn.
- Truy cập slide và tìm IAutoShape chứa văn bản.
- Truy cập ITextFrame của hình dạng.
- Gọi ParagraphCollection.ExportToHtml với chỉ mục đoạn bắt đầu và số lượng đoạn cần xuất.
- Ghi chuỗi HTML trả về vào tệp.
Ví dụ C# xuất tất cả các đoạn từ hình dạng văn bản đầu tiên:
using System;
using System.IO;
using System.Text;
using Aspose.Slides;
using var presentation = new Presentation("ExportingHTMLText.pptx");
var shape = presentation.Slides[0].Shapes[0];
if (shape is IAutoShape textShape && textShape.TextFrame != null)
{
var paragraphs = textShape.TextFrame.Paragraphs;
var html = paragraphs.ExportToHtml(0, paragraphs.Count, null);
using var writer = new StreamWriter("paragraphs.html", false, Encoding.UTF8);
writer.Write(html);
}
else
{
Console.WriteLine("The first shape is not a text shape.");
}
Kết Xuất Đoạn Văn Thành Hình Ảnh
IParagraph.GetImage kết xuất một đoạn riêng lẻ và trả về một IImage. Lưu kết quả vào tệp hoặc luồng bằng IImage.Save. Bạn không cần phải kết xuất hình dạng 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 kết xuất hợp lệ, hoặc không thể được kết xuất. Hãy kiểm tra kết quả trước khi lưu và giải phóng hình ảnh đã trả về sau khi dùng.
Kết Xuất Đoạn Văn với Tỷ Lệ Mặc Định
Giả sử chúng ta có một tệp trình chiếu tên sample.pptx với một slide, trong đó hình dạng đầu tiên là một hộp văn bản chứa ba đoạn.

Ví dụ sau kết xuất đoạn thứ hai trong một hình dạng văn bản thường tại tỷ lệ mặc định và lưu hình ảnh trả về ở định dạng PNG. Lệnh using đảm bảo hình ảnh được giải phóng đúng cách.
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ả:

Kết Xuất Đoạn Văn trong Ô Bảng với Tỷ Lệ Phóng To
Sử dụng phương thức IParagraph.GetImage có tham số float scaleX và float scaleY để đặt hệ số phóng to chiều ngang và chiều dọc. Ví dụ dưới tạo một bảng, kết xuất đ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ữ độ pixel mặc định cho trục tương ứng. Ví dụ, 2 cho cả hai hệ số tạo ra một hình ảnh có chiều rộng và chiều cao khoảng gấp đôi kích thước mặc định, dẫn tới bốn lần số pixel. Các hệ số lớn hơn thường tạo ra văn bản sắc nét hơn cho việc phóng to hoặc xuất ra độ phân giải cao, nhưng cũng tăng mức tiêu thụ bộ nhớ và kích thước tệp. Các hệ số dưới 1 tạo ra hình ảnh nhỏ hơn với chi tiết ít hơn. Sử dụng các hệ số bằng nhau để duy trì 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.
Việc kết xuất toàn bộ hình dạng 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 hình dạng. Đối với hình ảnh chỉ chứa đoạn, hãy dùng IParagraph.GetImage.
Câu Hỏi Thường Gặp
Tôi 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 sao tôi có thể lấy vị trí giới hạn trên slide chính xác của một đoạn cụ thể?
Sử dụng IParagraph.GetRect để lấy hình chữ nhật bao quanh của đoạn. IPortion.GetRect cung cấp giới hạn của một phần riêng lẻ.
Nơi nào kiểm soát việc căn chỉnh đoạn (trái, phải, giữa hoặc canh đều)?
IParagraphFormat.Alignment là cài đặt ở mức đoạn và áp dụng cho toàn bộ đoạn bất kể định dạng riêng lẻ của các phần.
Để căn chỉnh các phần có kích thước phông khác nhau theo chiều dọc trong mỗi dòng, xem Align Fonts Within a Line.
Tôi 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ẻ, vì vậy một đoạn có thể chứa văn bản bằng nhiều ngôn ngữ.