Định dạng Văn bản Bản trình chiếu trong .NET

Tổng quan

Bài viết này hướng dẫn cách định dạng văn bản trong các bản trình chiếu PowerPoint và OpenDocument bằng Aspose.Slides cho .NET. Nó bao gồm các màu nền, độ trong suốt, khoảng cách ký tự, thuộc tính phông chữ, xoay, khoảng cách đoạn, hành vi tự động điều chỉnh, neo văn bản, vị trí tab và cài đặt ngôn ngữ.

Trong các ví dụ dưới đây, chúng tôi sẽ sử dụng tệp có tên “sample.pptx”, chứa một hộp văn bản duy nhất trên slide đầu tiên với nội dung sau:

Sample text

Để tìm và tô sáng văn bản nguyên mẫu hoặc các kết quả khớp biểu thức chính quy, xem Search and Replace Text.

Đặt Màu Nền Văn Bản

Sử dụng IParagraphFormat.DefaultPortionFormat để đặt màu tô sáng mặc định cho một đoạn, hoặc sử dụng IBasePortionFormat.HighlightColor cho các phần văn bản riêng lẻ.

Ví dụ mã sau cho thấy cách đặt màu nền cho toàn bộ đoạn:

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    // Đặt màu tô sáng cho toàn bộ đoạn.
    paragraph.ParagraphFormat.DefaultPortionFormat.HighlightColor.Color = Color.LightGray;

    presentation.Save("gray_paragraph.pptx", SaveFormat.Pptx);
}

Kết quả:

The gray paragraph

Ví dụ mã dưới đây minh họa cách đặt màu nền cho các phần văn bản có phông đậm:

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    foreach (var portion in paragraph.Portions)
    {
        if (portion.PortionFormat.GetEffective().FontBold)
        {
            // Đặt màu tô sáng cho phần văn bản.
            portion.PortionFormat.HighlightColor.Color = Color.LightGray;
        }
    }

    presentation.Save("gray_text_portions.pptx", SaveFormat.Pptx);
}

Kết quả:

The gray text portions

Căn Lề Đoạn Văn Bản

Sử dụng IParagraphFormat.Alignment để đặt căn lề đoạn trong khung văn bản. Giá trị có thể là căn giữa, căn trái, căn phải, căn đều, v.v.

Ví dụ mã sau cho thấy cách căn đoạn về giữa:

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    // Đặt căn chỉnh của đoạn văn ở vị trí trung tâm.
    paragraph.ParagraphFormat.Alignment = TextAlignment.Center;

    presentation.Save("aligned_paragraph.pptx", SaveFormat.Pptx);
}

Kết quả:

The aligned paragraph

Đặt Độ Trong Suốt Cho Văn Bản

Độ trong suốt văn bản được kiểm soát thông qua thành phần alpha của màu được gán cho IBasePortionFormat.FillFormat. Trong các ví dụ dưới đây, alpha = 50 là giá trị kênh alpha ARGB trên thang 0–255, không phải phần trăm trong suốt.

Ví dụ mã dưới đây cho thấy cách áp dụng độ trong suốt cho toàn bộ đoạn:

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

int alpha = 50;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    // Đặt màu nền của văn bản thành màu trong suốt.
    paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
    paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Black);

    presentation.Save("transparent_paragraph.pptx", SaveFormat.Pptx);
}

Kết quả:

The transparent paragraph

Ví dụ mã sau cho thấy cách áp dụng độ trong suốt cho các phần văn bản có phông đậm:

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

int alpha = 50;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    foreach (var portion in paragraph.Portions)
    {
        if (portion.PortionFormat.GetEffective().FontBold)
        {
            // Đặt độ trong suốt cho phần văn bản.
            portion.PortionFormat.FillFormat.FillType = FillType.Solid;
            portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Black);
        }
    }

    presentation.Save("transparent_text_portions.pptx", SaveFormat.Pptx);
}

Kết quả:

The transparent text portions

Đặt Khoảng Cách Ký Tự Cho Văn Bản

Sử dụng IBasePortionFormat.Spacing để mở rộng hoặc thu hẹp khoảng cách giữa các ký tự trong một hộp văn bản.

Mã C# sau cho thấy cách mở rộng khoảng cách ký tự trong toàn bộ đoạn:

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    // Lưu ý: Sử dụng giá trị âm để nén khoảng cách ký tự.
    paragraph.ParagraphFormat.DefaultPortionFormat.Spacing = 3;  // Mở rộng khoảng cách ký tự.

    presentation.Save("character_spacing_in_paragraph.pptx", SaveFormat.Pptx);
}

Kết quả:

The character spacing in the paragraph

Ví dụ mã dưới đây cho thấy cách mở rộng khoảng cách ký tự trong các phần văn bản có phông đậm:

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    foreach (var portion in paragraph.Portions)
    {
        if (portion.PortionFormat.GetEffective().FontBold)
        {
            // Lưu ý: Sử dụng giá trị âm để nén khoảng cách ký tự.
            portion.PortionFormat.Spacing = 3;  // Mở rộng khoảng cách ký tự.
        }
    }

    presentation.Save("character_spacing_in_text_portions.pptx", SaveFormat.Pptx);
}

Kết quả:

The character spacing in the text portions

Tắt Kerning cho Các Phông Cụ Thể Cụ Thể

Trong một số trường hợp, văn bản do Aspose.Slides hiển thị có thể trông hơi chặt hơn so với văn bản cùng loại trong PowerPoint. Điều này có thể xảy ra vì PowerPoint có thể bỏ qua dữ liệu kerning cho một số phông, ngay cả khi phông chứa thông tin kerning hợp lệ và kerning đã được bật trong cài đặt PowerPoint.

Để làm cho kết quả hiển thị gần với PowerPoint hơn trong các trường hợp này, bạn có thể tắt kerning cho các phần văn bản sử dụng phông bị ảnh hưởng. Đặt IBasePortionFormat.KerningMinimalSize thành giá trị lớn hơn đáng kể so với kích thước phông thực tế:

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("presentation.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var targetFont = "Roboto";

    foreach (var paragraph in autoShape.TextFrame.Paragraphs)
    {
        foreach (var portion in paragraph.Portions)
        {
            if ((portion.PortionFormat.LatinFont != null &&
                 portion.PortionFormat.LatinFont.FontName == targetFont) ||
                (portion.PortionFormat.EastAsianFont != null &&
                 portion.PortionFormat.EastAsianFont.FontName == targetFont) ||
                (portion.PortionFormat.ComplexScriptFont != null &&
                 portion.PortionFormat.ComplexScriptFont.FontName == targetFont))
            {
                portion.PortionFormat.KerningMinimalSize = 100;
            }
        }
    }

    presentation.Save("output.pptx", SaveFormat.Pptx);
}

Cài đặt này ngăn kerning được áp dụng cho các phần văn bản khớp và có thể giúp đồng bộ kết quả hiển thị của Aspose.Slides với PowerPoint đối với các phông bị ảnh hưởng bởi hành vi đặc thù của PowerPoint này.

Quản Lý Thuộc Tính Phông Văn Bản

Thuộc tính phông có thể được đặt ở mức đoạn thông qua IParagraphFormat.DefaultPortionFormat hoặc trên từng phần riêng lẻ thông qua IPortionFormat.

Mã sau thiết lập phông và kiểu văn bản cho toàn bộ đoạn: áp dụng kích thước phông, in đậm, in nghiêng, gạch chân chấm, và phông Times New Roman cho tất cả các phần trong đoạn.

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    // Đặt các thuộc tính phông cho đoạn văn.
    paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 12;
    paragraph.ParagraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
    paragraph.ParagraphFormat.DefaultPortionFormat.FontItalic = NullableBool.True;
    paragraph.ParagraphFormat.DefaultPortionFormat.FontUnderline = TextUnderlineType.Dotted;
    paragraph.ParagraphFormat.DefaultPortionFormat.LatinFont = new FontData("Times New Roman");

    presentation.Save("font_properties_for_paragraph.pptx", SaveFormat.Pptx);
}

Kết quả:

The font properties for the paragraph

Ví dụ mã dưới đây áp dụng các thuộc tính tương tự cho các phần văn bản có phông đậm:

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    foreach (var portion in paragraph.Portions)
    {
        if (portion.PortionFormat.GetEffective().FontBold)
        {
            // Đặt các thuộc tính phông cho phần văn bản.
            portion.PortionFormat.FontHeight = 13;
            portion.PortionFormat.FontItalic = NullableBool.True;
            portion.PortionFormat.FontUnderline = TextUnderlineType.Dotted;
            portion.PortionFormat.LatinFont = new FontData("Times New Roman");
        }
    }

    presentation.Save("font_properties_for_text_portions.pptx", SaveFormat.Pptx);
}

Kết quả:

The font properties for text portions

Đặt Xoay Văn Bản

Sử dụng ITextFrameFormat.TextVerticalType để đặt hướng văn bản định trước trong một hình dạng.

Mã dưới đây đặt hướng văn bản trong hình dạng thành Vertical270, xoay văn bản 90 độ ngược chiều kim đồng hồ:

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];

    autoShape.TextFrame.TextFrameFormat.TextVerticalType = TextVerticalType.Vertical270;

    presentation.Save("text_rotation.pptx", SaveFormat.Pptx);
}

Kết quả:

The text rotation

Đặt Xoay Tùy Chỉnh Cho Khung Văn Bản

Sử dụng ITextFrameFormat.RotationAngle để đặt góc xoay tùy chỉnh cho một ITextFrame.

Mã dưới đây xoay khung văn bản 3 độ theo chiều kim đồng hồ trong hình dạng:

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];

    autoShape.TextFrame.TextFrameFormat.RotationAngle = 3;

    presentation.Save("custom_text_rotation.pptx", SaveFormat.Pptx);
}

Kết quả:

The custom text rotation

Đặt Khoảng Cách Dòng Cho Các Đoạn

Aspose.Slides cung cấp IParagraphFormat.SpaceAfter, IParagraphFormat.SpaceBefore và IParagraphFormat.SpaceWithin để kiểm soát khoảng cách đoạn. Các thuộc tính này được sử dụng như sau:

  • Sử dụng giá trị dương để chỉ định khoảng cách dòng dưới dạng phần trăm của chiều cao dòng.
  • Sử dụng giá trị âm để chỉ định khoảng cách dòng bằng điểm.

Mã dưới đây cho thấy cách chỉ định khoảng cách dòng trong đoạn:

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    paragraph.ParagraphFormat.SpaceWithin = 200;

    presentation.Save("line_spacing.pptx", SaveFormat.Pptx);
}

Kết quả:

The line spacing within the paragraph

Đặt Kiểu Tự Động Điều Chỉnh Cho Khung Văn Bản

ITextFrameFormat.AutofitType xác định cách văn bản hành xử khi vượt quá giới hạn của vùng chứa. Sử dụng nó để kiểm soát liệu văn bản co lại, tràn ra ngoài, hay tự động thay đổi kích thước hình dạng.

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];

    autoShape.TextFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;

    presentation.Save("autofit_type.pptx", SaveFormat.Pptx);
}

Để đếm số dòng sau khi tự động ngắt và xem độ rộng văn bản hoặc hình dạng thay đổi như thế nào, xem Count Rendered Lines. Số lượng dòng chỉ đơn giản không cho biết liệu văn bản có tràn ra ngoài vùng chứa hay không.

Đặt Neo Cho Khung Văn Bản

ITextFrameFormat.AnchoringType xác định cách văn bản được định vị theo chiều dọc bên trong một hình dạng, chẳng hạn ở trên cùng, giữa hoặc dưới cùng.

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];

    autoShape.TextFrame.TextFrameFormat.AnchoringType = TextAnchorType.Bottom;

    presentation.Save("text_anchor.pptx", SaveFormat.Pptx);
}

Đặt Tabulation Cho Văn Bản

Sử dụng IParagraphFormat.DefaultTabSize và IParagraphFormat.Tabs để cấu hình vị trí tab trong một đoạn.

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    paragraph.ParagraphFormat.DefaultTabSize = 100;
    paragraph.ParagraphFormat.Tabs.Add(30, TabAlignment.Left);

    presentation.Save("paragraph_tabs.pptx", SaveFormat.Pptx);
}

Kết quả:

The paragraph tabs

Đặt Ngôn Ngữ Kiểm Tra Chính Tả

Aspose.Slides cung cấp IBasePortionFormat.LanguageId, cho phép bạn đặt ngôn ngữ kiểm tra chính tả cho một phần văn bản. Ngôn ngữ kiểm tra quyết định ngôn ngữ được dùng cho việc kiểm tra chính tả và ngữ pháp trong PowerPoint.

Mã dưới đây cho thấy cách đặt ngôn ngữ kiểm tra cho một phần văn bản:

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation("presentation.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];

    var paragraph = autoShape.TextFrame.Paragraphs[0];
    paragraph.Portions.Clear();

    var font = new FontData("SimSun");

    var textPortion = new Portion();
    textPortion.PortionFormat.ComplexScriptFont = font;
    textPortion.PortionFormat.EastAsianFont = font;
    textPortion.PortionFormat.LatinFont = font;

    // Đặt Id của ngôn ngữ kiểm tra chính tả.
    textPortion.PortionFormat.LanguageId = "zh-CN";

    textPortion.Text = "1。";
    paragraph.Portions.Add(textPortion);

    presentation.Save("proofing_language.pptx", SaveFormat.Pptx);
}

Đặt Ngôn Ngữ Mặc Định

Sử dụng LoadOptions.DefaultTextLanguage để xác định ngôn ngữ mặc định cho văn bản được tạo khi tải hoặc tạo một bản trình chiếu.

using Aspose.Slides;

var loadOptions = new LoadOptions();
loadOptions.DefaultTextLanguage = "en-US";

using (var presentation = new Presentation(loadOptions))
{
    var slide = presentation.Slides[0];

    // Thêm một hình chữ nhật mới có văn bản.
    var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 150, 50);
    shape.TextFrame.Text = "Sample text";

    // Kiểm tra ngôn ngữ của phần đầu tiên.
    var portion = shape.TextFrame.Paragraphs[0].Portions[0];
    Console.WriteLine(portion.PortionFormat.LanguageId);
}

Đặt Kiểu Văn Bản Mặc Định

Để áp dụng định dạng văn bản mặc định ở cấp độ bản trình chiếu, sử dụng IPresentation.DefaultTextStyle.

Mã dưới đây cho thấy cách đặt phông chữ đậm mặc định với kích thước 14 pt cho tất cả văn bản trên các slide trong một bản trình chiếu mới.

using Aspose.Slides;
using Aspose.Slides.Export;

using (var presentation = new Presentation())
{
    // Lấy định dạng đoạn ở cấp cao nhất.
    var paragraphFormat = presentation.DefaultTextStyle.GetLevel(0);

    if (paragraphFormat != null)
    {
        paragraphFormat.DefaultPortionFormat.FontHeight = 14;
        paragraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
    }

    presentation.Save("default_text_style.pptx", SaveFormat.Pptx);
}

Trích Xuất Văn Bản Với Hiệu Ứng In HOA

Trong PowerPoint, áp dụng hiệu ứng All Caps làm cho văn bản hiển thị dưới dạng chữ hoa trên slide ngay cả khi nó được gõ bằng chữ thường. Khi bạn truy xuất phần văn bản như vậy bằng Aspose.Slides, thư viện sẽ trả về văn bản đúng như khi nhập. Để khớp với văn bản hiển thị, kiểm tra TextCapType và chuyển chuỗi trả về thành chữ hoa khi giá trị là All.

Giả sử chúng ta có hộp văn bản sau trên slide đầu tiên của tệp sample2.pptx.

The All Caps effect

Mã dưới đây cho thấy cách trích xuất văn bản với hiệu ứng All Caps đã được áp dụng:

using Aspose.Slides;

using (var presentation = new Presentation("sample2.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var textPortion = autoShape.TextFrame.Paragraphs[0].Portions[0];

    Console.WriteLine($"Original text: {textPortion.Text}");

    var textFormat = textPortion.PortionFormat.GetEffective();
    if (textFormat.TextCapType == TextCapType.All)
    {
        var text = textPortion.Text.ToUpper();
        Console.WriteLine($"All-Caps effect: {text}");
    }
}

Kết quả:

Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!

Câu Hỏi Thường Gặp

Làm thế nào để sửa đổi văn bản trong bảng trên slide?

Để sửa đổi văn bản trong bảng trên slide, sử dụng ITable. Duyệt qua các ô và cập nhật từng ô thông qua ICell.TextFrame và định dạng đoạn qua IParagraph.ParagraphFormat.

Làm thế nào để áp dụng màu gradient cho văn bản trong slide PowerPoint?

Để áp dụng màu gradient cho văn bản, sử dụng IBasePortionFormat.FillFormat. Đặt IFillFormat.FillType thành FillType.Gradient và cấu hình các điểm dừng gradient, hướng và độ trong suốt.