Quản lý các đoạn văn bản PowerPoint trong .NET

Tổng quan

Aspose.Slides cho .NET biểu diễn văn bản như một cây phân cấp gồm 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 các đoạn vă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 ở mức độ đoạn.
  • IPortion đại diện cho một đoạn 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 các đoạn văn trong khung văn bản của một hình dạng và sử dụng các phần để định dạng các đoạn văn bản riêng lẻ. Phần này trình bày cách kết hợp nhiều phần trong mỗi đoạn và áp dụng các màu sắc, kích thước phông chữ và kiểu dáng khác nhau.

Tạo Đoạn văn với Nhiều Phần

Ví dụ C# dưới đây tạo ba đoạn văn, mỗi đoạn chứa ba đoạn 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à kích thước 15 điểm; phần thứ hai có màu xanh lam, in nghiêng và 18 điểm. Phần thứ ba giữ nguyên đị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 các mục liên quan dễ dàng quét. 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 có 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 lề đoạn là 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 sử dụng hình ảnh tùy chỉnh thay vì 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 để sử 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 mức độ khác nhau của một danh sách. Mức 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 cây 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 thay đổi xen kẽ giữa dấu đầu dòng tròn và dấu gạch nối để phân biệt các cấp gần nhau:

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 có 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 để sử 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 lề của đ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 với các phần văn bản hiển thị.

Đặt Thụt Lề Dòng Đầu

Sử dụng thuộc tính IParagraphFormat.Indent để kiểm soát thụt lề dòng đầu của một đ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. Sử dụng IParagraphFormat.Indent khi bạn chỉ cần di chuyển dòng đầu.

Ví dụ sau cung cấp ba đoạn cùng 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 độ lệch ngày càng lớn, trong khi các dòng gói giữ nguyên 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ả:

Thụt lề dòng đầu của các đoạn

Đặt Thụt Lề Treo

Một thụt lề treo là bố cục đoạn trong đó dòng đầu bắt đầu ở phía trái của 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.

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 lề treo, đặt giá trị MarginLeft dương và giá trị Indent âm.

Định dạng này hữu ích cho thư mục, tài liệu tham khảo, mục bối cảnh 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.

Ví dụ C# sau ghép các giá trị IParagraphFormat.MarginLeft là 40 và 60 điểm với các giá trị IParagraphFormat.Indent là -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 lề 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ả:

Thụt lề treo của các đoạn

Đặ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 của 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 đặt thành Times New Roman. Nó gán định dạng này cho dấu kết thúc của đoạn thứ hai, để lại 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 Số Dòng Được Render

Đố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 mà một đoạn chiếm sau khi bố trí văn bản, bao gồm 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 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 render. Một ký 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. 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. Do đó, đếm số đoạn hoặc ký tự ngắt dòng không cho ra số dòng đã render.

Ví dụ sau tạo một hình dạng văn bản, đếm số dòng của nó, 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. Ví dụ bật ITextFrameFormat.WrapText và tắt ITextFrameFormat.AutofitType để chiều rộng hình dạng kiểm soát việc gói mà không tự động co thu văn bản hoặc thay đổi kích thước hình dạng. Mỗi phép đo sử dụng IParagraph.GetLinesCount. Kích thước hình dạng tính bằng điểm. Cuối cùng, ví dụ thêm một đoạn khác và tính tổng số dòng trong 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ế văn bản bằng chuỗi ngắn làm giảm số dòng. Số đếm chính xác có thể thay đổi tùy vào sẵn có và thay thế phông chữ, kích thước phông, lề, thụt lề, gói và cài đặt autofit. Sử dụng phông và cài đặt bố cục dự định cho môi trường mục tiêu khi kiểm tra mẫu.

Số dòng một mình không quyết định liệu văn bản có tràn khỏi vùng chứa 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 một bản trình chiếu hoặc xuất các đoạn hiện có để dùng bên ngoài. Phần này cũng trình bà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

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.

Ví dụ C# này đọc HTML từ một tệp và truyền mã markup vào 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. 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 thành HTML

Sử dụng ParagraphCollection.ExportToHtml để xuất một phạm vi các đoạn đã chọn dưới dạng HTML. Khi tái sử dụng văn bản bản trình chiếu trên một trang web, bạn có thể muốn giữ nguyên các nhấn mạnh nội địa mà không mang theo phông chữ, kích thước hoặc kiểu hộp văn bản của bản trình chiếu. Đặt TextToHtmlConversionOptions.TextInheritanceLimit để kiểm soát mức độ mà bộ xuất theo kế thừa định dạng.

Tải xuống the sample presentation và đặt nó trong thư mục làm việc của ứng dụng. Slide đầu tiên của nó chứa một hình dạng văn bản với văn bản “Inherited text; explicit red 18pt”. Đầu vào 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 chiếu cung cấp phông Calibri và kích thước 28 pt.
  • Kiểu văn bản cấp một của khung văn bản cung cấp văn bản màu xanh dương và in nghiêng.
  • Đoạn này đặt rõ ràng văn bản in đậm.
  • Phần thứ hai, “explicit red 18pt”, đặt rõ ràng văn bản 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 vào phương thức xuất và ghi mỗi kết quả thành 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 bị bỏ qua để rõ ràng.

Đầu ra CSS liên quan cho phần đầu tiên Hiển thị
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, in nghiêng tại 28 pt trong Calibri, nếu phông chữ đó có sẵn.
TextBox.html color:#0000ff; font-style:italic; font-weight:bold Văn bản màu xanh, in đậm, in nghiêng; trang hoặc trình duyệt bao quanh cung cấp phông chữ và kích thước.
ParagraphOnly.html font-weight:bold Văn bản in đậm; trang hoặc trình duyệt bao quanh cung cấp phông chữ, kích thước, màu và kiểu in nghiêng.

Phần thứ hai giữ nguyên color:#ff0000, font-size:18pt, và font-weight:bold trong cả ba đầu ra. Nó cũng giữ nguyên kiểu in nghiêng với All và TextBox, nhưng không giữ với ParagraphOnly. Việc hạn chế kế thừa vì vậy không loại bỏ định dạng được gán rõ ràng cho một đoạn hoặc phần.

Mở các tệp trong cùng một trình duyệt để so sánh chúng. Trong hai chế độ bị 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 kết quả phụ thuộc vào nơi HTML được sử dụng.

Tạo Một Đoạn HTML Cho Bảng Nhớ

Đặt AddClipboardFragmentHeader thành false cho HTML thông thường hoặc true để bao gồm tiêu đề bảng nhớ. Cả hai lời gọi trả về một chuỗi. Tiêu đề cung cấp các vị trí byte UTF-8 của HTML bao 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 đoạn HTML cho bảng nhớ (clipboardHtml). Việc ghi đoạn vào bảng nhớ hệ thống là một bước ứng dụng riêng. Một API bảng nhớ chấp nhận byte HTML thô 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 một Đoạn dưới dạng Hình ảnh

IParagraph.GetImage render một đoạn riêng trực tiếp và trả về một IImage. Lưu kết quả vào tệp hoặc luồng với IImage.Save. Bạn không cần render hình dạng chứa hoặc cắt bitmap bằng tay.

IParagraph.GetImage có thể trả về null nếu đoạn không tìm thấy 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 hình ảnh trả về sau khi sử dụng.

Render một Đoạn ở Tỷ lệ Mặc định

Ví dụ sử dụng một bản trình chiếu 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.

Hộp văn bản với ba đoạn

Ví dụ sau chọn đoạn thứ hai ở chỉ mục 1 và gọi IParagraph.GetImage mà không có tham số tỷ lệ. Nó kiểm tra rằng hình dạng chứa đoạn yêu cầu và việc render trả về một hình ảnh trước khi gọi IImage.Save để lưu ở định dạng PNG. Câu lệnh using giải phóng hình ảnh sau khi sử 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ả:

Hình ảnh đoạn

Render một Đoạn trong Ô Bảng với Tỷ lệ

Sử dụng phiên bản 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 ảnh 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ố tỷ lệ 1 giữ trục đó ở kích thước pixel mặc định. Ví dụ, 2 cho cả hai hệ số tạo ra một ảnh có chiều rộng và chiều cao khoảng gấp đôi kích thước mặc định, dẫn đến 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 đầu ra độ phân giải cao, nhưng chúng cũng tăng lượng bộ nhớ và kích thước tệp. Các hệ số dưới 1 tạo ảnh nhỏ hơn với ít chi tiết hơn. Sử dụng các hệ số bằng nhau để giữ tỉ lệ khung hình của đoạn; các hệ số ngang và dọc khác nhau sẽ kéo dài đầu ra một cách độc lập.

Render 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 của hình dạng. Đối với hình ảnh chỉ chứa đoạn, sử dụng IParagraph.GetImage.

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

Tôi có thể hoàn toàn tắt việc gói dòng trong một khung văn bản không?

Có. Đặt ITextFrameFormat.WrapText để tắt gói, để các dòng không ngắt tại cạnh của khung văn bản.

Làm sao tôi có thể lấy kích thước chính xác trên slide 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ẻ.

Caǹh sắp xểp đoạn (trái, phải, giữa, hoặc căn đều) được kiểm soát ở đâ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 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.

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ẻ, để một đoạn có thể chứa văn bản bằng nhiều ngôn ngữ.