Định dạng văn bản trình chiếu trên Android
Tổng quan
Bài viết này mô tả 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 Android thông qua Java. Nội dung bao gồm làm nổi bật, 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 vừa, neo văn bản, tab stops và cài đặt ngôn ngữ.
Trong các ví dụ dưới đây, chúng tôi sẽ sử dụng một tệp có tên “sample.pptx”, trong đó có một hộp văn bản đơn trên slide đầu tiên với nội dung sau:

Đánh dấu văn bản
Sử dụng phương thức ITextFrame.highlightText khi bạn cần làm nổi bật văn bản khớp với một mẫu cụ thể trong một khung văn bản. Phương thức này áp dụng màu nền cho các đoạn văn bản khớp và có thể được sử dụng cùng với ITextSearchOptions để kiểm soát cách tìm kiếm, ví dụ như chỉ khớp toàn bộ từ.
Đoạn mã dưới đây làm nổi bật tất cả các lần xuất hiện của ký tự “try” và sau đó chỉ làm nổi bật toàn bộ từ “to”.
Presentation presentation = new Presentation("sample.pptx");
try {
// Lấy hình dạng đầu tiên từ slide đầu tiên.
IAutoShape shape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
// Làm nổi bật từ "try" trong hình dạng.
shape.getTextFrame().highlightText("try", Color.rgb(173, 216, 230));
TextSearchOptions searchOptions = new TextSearchOptions();
searchOptions.setWholeWordsOnly(true);
// Làm nổi bật từ "to" trong hình dạng.
int violetColor = Color.rgb(238, 130, 238);
shape.getTextFrame().highlightText("to", violetColor, searchOptions, null);
presentation.save("highlighted_text.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đánh dấu văn bản bằng biểu thức chính quy
Phương thức ITextFrame.highlightRegex làm nổi bật các kết quả khớp tìm được bằng biểu thức chính quy.
Đoạn mã dưới đây làm nổi bật tất cả các từ có bảy ký tự trở lên:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape shape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
java.util.regex.Pattern regex = java.util.regex.Pattern.compile("\\b[^\\s]{7,}\\b");
// Làm nổi bật tất cả các từ có bảy ký tự hoặc nhiều hơn.
shape.getTextFrame().highlightRegex(regex, Color.YELLOW, null);
presentation.save("highlighted_text_using_regex.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đặt màu nền cho văn bản
Sử dụng IParagraphFormat.getDefaultPortionFormat để đặt màu nền mặc định cho một đoạn, hoặc sử dụng IBasePortionFormat.getHighlightColor cho các phần văn bản riêng lẻ.
Đoạn mã sau cho thấy cách đặt màu nền cho toàn bộ đoạn:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Đặt màu nền cho toàn bộ đoạn.
paragraph.getParagraphFormat().getDefaultPortionFormat().getHighlightColor().setColor(Color.LTGRAY);
presentation.save("gray_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đoạn 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:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (int portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// Đặt màu nền cho phần văn bản.
portion.getPortionFormat().getHighlightColor().setColor(Color.LTGRAY);
}
}
presentation.save("gray_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Căn chỉnh các đoạn văn bản
Sử dụng IParagraphFormat.setAlignment để đặt căn chỉnh đoạn trong một 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.
Đoạn mã sau cho thấy cách căn đoạn về giữa:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Đặt căn chỉnh của đoạn văn thành trung tâm.
paragraph.getParagraphFormat().setAlignment(TextAlignment.Center);
presentation.save("aligned_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đặt độ trong suốt cho văn bản
Độ trong suốt của 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.getFillFormat. 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.
Đoạn mã dưới đây cho thấy cách áp dụng độ trong suốt cho toàn bộ đoạn:
int alpha = 50;
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Đặt màu nền của văn bản thành màu trong suốt.
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.argb(alpha, 0, 0, 0));
presentation.save("transparent_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đoạn 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:
int alpha = 50;
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (int portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// Đặt độ trong suốt của phần văn bản.
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.argb(alpha, 0, 0, 0));
}
}
presentation.save("transparent_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đặt khoảng cách ký tự cho văn bản
Sử dụng IBasePortionFormat.setSpacing để 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.
Đoạn mã Java sau cho thấy cách mở rộng khoảng cách ký tự trong toàn bộ đoạn:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Lưu ý: Sử dụng giá trị âm để nén khoảng cách ký tự.
paragraph.getParagraphFormat().getDefaultPortionFormat().setSpacing(3); // Mở rộng khoảng cách ký tự.
presentation.save("character_spacing_in_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đoạn 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:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (int portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// Lưu ý: Sử dụng giá trị âm để nén khoảng cách ký tự.
portion.getPortionFormat().setSpacing(3); // Mở rộng khoảng cách ký tự.
}
}
presentation.save("character_spacing_in_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Tắt kerning cho các phông chữ cụ thể
Trong một số trường hợp, văn bản được render bởi Aspose.Slides có thể trông hơi chặt hơn so với cùng văn bản hiển thị 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 chữ, ngay cả khi phông chữ 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ả render gần hơn với PowerPoint 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 chữ bị ảnh hưởng. Đặt IBasePortionFormat.setKerningMinimalSize thành giá trị lớn hơn đáng kể so với kích thước phông chữ thực tế:
Presentation presentation = new Presentation("presentation.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
String targetFont = "Roboto";
for (int paragraphIndex = 0; paragraphIndex < autoShape.getTextFrame().getParagraphs().getCount(); paragraphIndex++) {
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(paragraphIndex);
for (int portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
IFontData latinFont = portion.getPortionFormat().getLatinFont();
IFontData eastAsianFont = portion.getPortionFormat().getEastAsianFont();
IFontData complexScriptFont = portion.getPortionFormat().getComplexScriptFont();
boolean usesTargetFont =
latinFont != null && targetFont.equals(latinFont.getFontName()) ||
eastAsianFont != null && targetFont.equals(eastAsianFont.getFontName()) ||
complexScriptFont != null && targetFont.equals(complexScriptFont.getFontName());
if (usesTargetFont) {
portion.getPortionFormat().setKerningMinimalSize(100);
}
}
}
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
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 nhất việc render của Aspose.Slides với đầu ra trực quan của PowerPoint đối với các phông chữ bị ảnh hưởng bởi hành vi đặc thù này của PowerPoint.
Quản lý thuộc tính phông chữ văn bản
Các thuộc tính phông chữ có thể được đặt ở mức đoạn thông qua IParagraphFormat.getDefaultPortionFormat hoặc trên từng phần thông qua IPortionFormat.
Đoạn mã sau đặt phông chữ và kiểu văn bản cho toàn bộ đoạn: 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.
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Đặt các thuộc tính phông chữ cho đoạn văn.
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(12);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontBold(NullableBool.True);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontItalic(NullableBool.True);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontUnderline(TextUnderlineType.Dotted);
paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Times New Roman"));
presentation.save("font_properties_for_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đoạn 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:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (int portionIndex = 0; portionIndex < paragraph.getPortions().getCount(); portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// Đặt các thuộc tính phông chữ cho phần văn bản.
portion.getPortionFormat().setFontHeight(13);
portion.getPortionFormat().setFontItalic(NullableBool.True);
portion.getPortionFormat().setFontUnderline(TextUnderlineType.Dotted);
portion.getPortionFormat().setLatinFont(new FontData("Times New Roman"));
}
}
presentation.save("font_properties_for_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đặt xoay văn bản
Sử dụng ITextFrameFormat.setTextVerticalType để đặt hướng văn bản định sẵn trong một hình dạng.
Đoạn mã sau đặt hướng văn bản trong hình dạng thành Vertical270, làm cho văn bản quay 90 độ ngược chiều kim đồng hồ:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setTextVerticalType(TextVerticalType.Vertical270);
presentation.save("text_rotation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đặt xoay tùy chỉnh cho khung văn bản
Sử dụng ITextFrameFormat.setRotationAngle để đặt góc xoay tùy chỉnh cho một ITextFrame.
Đoạn mã dưới đây xoay khung văn bản 3 độ theo chiều kim đồng hồ trong hình dạng:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setRotationAngle(3);
presentation.save("custom_text_rotation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đặt khoảng cách dòng cho các đoạn văn
Aspose.Slides cung cấp các phương thức IParagraphFormat.setSpaceAfter, IParagraphFormat.setSpaceBefore và IParagraphFormat.setSpaceWithin để 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.
Đoạn mã sau cho thấy cách chỉ định khoảng cách dòng trong đoạn:
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setSpaceWithin(200);
presentation.save("line_spacing.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đặt loại tự động vừa cho khung văn bản
ITextFrameFormat.setAutofitType xác định cách văn bản hành xử khi vượt quá giới hạn của container. Sử dụng nó để kiểm soát việc văn bản co lại, tràn hoặc tự động thay đổi kích thước hình dạng.
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
presentation.save("autofit_type.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Đặt neo cho khung văn bản
ITextFrameFormat.setAnchoringType xác định cách văn bản được đặt vị trí theo chiều dọc bên trong một hình dạng, ví dụ ở trên, giữa hoặc dưới.
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Bottom);
presentation.save("text_anchor.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Đặt tabulation cho văn bản
Sử dụng IParagraphFormat.setDefaultTabSize và IParagraphFormat.getTabs để cấu hình các vị trí tab trong một đoạn.
Presentation presentation = new Presentation("sample.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setDefaultTabSize(100);
paragraph.getParagraphFormat().getTabs().add(30, TabAlignment.Left);
presentation.save("paragraph_tabs.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đặt ngôn ngữ kiểm tra chính tả
Aspose.Slides cung cấp IBasePortionFormat.setLanguageId, 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ữ này xác định ngôn ngữ được dùng cho việc kiểm tra chính tả và ngữ pháp trong PowerPoint.
Đoạn mã sau cho thấy cách đặt ngôn ngữ kiểm tra chính tả cho một phần văn bản:
Presentation presentation = new Presentation("presentation.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getPortions().clear();
FontData font = new FontData("SimSun");
Portion textPortion = new Portion();
textPortion.getPortionFormat().setComplexScriptFont(font);
textPortion.getPortionFormat().setEastAsianFont(font);
textPortion.getPortionFormat().setLatinFont(font);
// Đặt ID của ngôn ngữ kiểm tra chính tả.
textPortion.getPortionFormat().setLanguageId("zh-CN");
textPortion.setText("1。");
paragraph.getPortions().add(textPortion);
presentation.save("proofing_language.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Đặt ngôn ngữ mặc định
Sử dụng LoadOptions.setDefaultTextLanguage để xác định ngôn ngữ mặc định cho văn bản được tạo trong khi tải hoặc tạo một bản trình chiếu.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setDefaultTextLanguage("en-US");
Presentation presentation = new Presentation(loadOptions);
try {
ISlide slide = presentation.getSlides().get_Item(0);
// Thêm một hình chữ nhật mới với văn bản.
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 150, 50);
shape.getTextFrame().setText("Sample text");
// Kiểm tra ngôn ngữ của phần đầu tiên.
IPortion portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
System.out.println(portion.getPortionFormat().getLanguageId());
} finally {
presentation.dispose();
}
Đặt kiểu văn bản mặc định
Để áp dụng định dạng văn bản mặc định ở mức bản trình chiếu, sử dụng IPresentation.getDefaultTextStyle.
Đoạn 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.
Presentation presentation = new Presentation();
try {
// Lấy định dạng đoạn văn cấp cao nhất.
IParagraphFormat paragraphFormat = presentation.getDefaultTextStyle().getLevel(0);
if (paragraphFormat != null) {
paragraphFormat.getDefaultPortionFormat().setFontHeight(14);
paragraphFormat.getDefaultPortionFormat().setFontBold(NullableBool.True);
}
presentation.save("default_text_style.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Trích xuất văn bản với hiệu ứng chữ HOA
Trong PowerPoint, áp dụng hiệu ứng All Caps khiến 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 lấy phần văn bản như vậy bằng Aspose.Slides, thư viện sẽ trả về văn bản chính xác 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.

Đoạn mã dưới đây cho thấy cách trích xuất văn bản có hiệu ứng All Caps được áp dụng:
Presentation presentation = new Presentation("sample2.pptx");
try {
IAutoShape autoShape = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IPortion textPortion = autoShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
System.out.println("Original text: " + textPortion.getText());
IPortionFormatEffectiveData textFormat = textPortion.getPortionFormat().getEffective();
if (textFormat.getTextCapType() == TextCapType.All) {
String text = textPortion.getText().toUpperCase();
System.out.println("All-Caps effect: " + text);
}
} finally {
presentation.dispose();
}
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 để chỉnh sửa văn bản trong bảng trên một slide?
Để chỉnh sửa văn bản trong bảng trên một slide, sử dụng ITable. Duyệt qua các ô và cập nhật mỗi ô thông qua ICell.getTextFrame và định dạng đoạn qua IParagraph.getParagraphFormat.
Làm thế nào để áp dụng màu gradient cho văn bản trong một slide PowerPoint?
Để áp dụng màu gradient cho văn bản, sử dụng IBasePortionFormat.getFillFormat. Đặt IFillFormat.setFillType thành FillType.Gradient và cấu hình các điểm dừng gradient, hướng và độ trong suốt.