Quản lý các đoạn văn bản PowerPoint trong JavaScript
Tổng quan
Aspose.Slides cho Node.js thông qua Java đại diện văn bản dưới dạng một cấu trúc phân cấp của các khung văn bản, đoạn văn và phần:
- TextFrame đại diện cho container văn bản trong một shape và cung cấp truy cập tới bộ sưu tập đoạn văn của nó.
- Paragraph đại diện cho một đoạn văn trong một khung văn bản và cung cấp truy cập tới các phần và định dạng cấp độ đoạn.
- Portion đại diện cho một đoạn chạy văn bản trong một đoạn. Mỗi phần có thể có văn bản và định dạng cấp ký tự riêng.
Do đó một đoạn có thể chứa văn bản với các phông chữ, màu sắc, kích thước và định dạng khác nhau bằng cách sử dụng nhiều phần.
Tạo và Định dạng Đoạn văn
Tạo đoạn văn trong khung văn bản của một shape và sử dụng các phần để định dạng các đoạn chạy văn bản riêng lẻ. Phần này cho thấy cách kết hợp nhiều phần trong mỗi đoạn và áp dụng các màu, kích thước phông và kiểu khác nhau.
Tạo Đoạn văn với Nhiều Phần
Ví dụ JavaScript sau tạo ba đoạn, mỗi đoạn chứa ba đoạn chạy văn bản trong bộ sưu tập được trả về bởi Paragraph.getPortions. 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 Portion.getPortionFormat: phần đầu tiên trong mỗi đoạn có màu đỏ, in đậm và kích thước 15 pt; phần thứ hai có màu xanh lam, in nghiêng và kích thước 18 pt. Phần thứ ba giữ nguyên định dạng mặc định:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 150, 300, 150);
const textFrame = shape.getTextFrame();
const firstParagraph = textFrame.getParagraphs().get_Item(0);
firstParagraph.getPortions().add(new aspose.slides.Portion());
firstParagraph.getPortions().add(new aspose.slides.Portion());
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.getPortions().add(new aspose.slides.Portion());
secondParagraph.getPortions().add(new aspose.slides.Portion());
secondParagraph.getPortions().add(new aspose.slides.Portion());
textFrame.getParagraphs().add(secondParagraph);
const thirdParagraph = new aspose.slides.Paragraph();
thirdParagraph.getPortions().add(new aspose.slides.Portion());
thirdParagraph.getPortions().add(new aspose.slides.Portion());
thirdParagraph.getPortions().add(new aspose.slides.Portion());
textFrame.getParagraphs().add(thirdParagraph);
const paragraphCount = textFrame.getParagraphs().getCount();
for (let paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++) {
const paragraph = textFrame.getParagraphs().get_Item(paragraphIndex);
const portionCount = paragraph.getPortions().getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = paragraph.getPortions().get_Item(portionIndex);
portion.setText("Portion " + (paragraphIndex + 1) + "." + (portionIndex + 1));
if (portionIndex === 0) {
portion.getPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
portion.getPortionFormat().setFontBold(java.newByte(aspose.slides.NullableBool.True));
portion.getPortionFormat().setFontHeight(15);
} else if (portionIndex === 1) {
portion.getPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLUE"));
portion.getPortionFormat().setFontItalic(java.newByte(aspose.slides.NullableBool.True));
portion.getPortionFormat().setFontHeight(18);
}
}
}
presentation.save("paragraphs_with_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Tạo Danh sách Dấu đầu dòng và Đánh số
Sắp xếp văn bản của đ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à bắt đầu số tùy chỉnh.
Tạo Danh sách Dấu đầu dòng hoặc Đánh số
Dấu đầu dòng và đánh số giúp người đọc nhanh chóng nhận biết các mục liên quan. Trong Aspose.Slides, cài đặt danh sách được xác định qua BulletFormat.
Ví dụ JavaScript này thêm một dấu đầu dòng ký hiệu và một mục đánh số vào cùng một khung văn bản. BulletFormat.setType chọn kiểu dấu đầu, trong khi BulletFormat.setChar cung cấp ký hiệu và BulletFormat.setNumberedBulletStyle chọn kiểu đánh số. Cả hai mục đều sử dụng dấu đầu màu đen với độ cao 100 % và thụt lề đoạn là 25 pt:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
const symbolParagraph = new aspose.slides.Paragraph();
symbolParagraph.setText("Welcome to Aspose.Slides");
symbolParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
symbolParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
symbolParagraph.getParagraphFormat().setIndent(25);
symbolParagraph.getParagraphFormat().getBullet().getColor().setColorType(aspose.slides.ColorType.RGB);
symbolParagraph.getParagraphFormat().getBullet().getColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
symbolParagraph.getParagraphFormat().getBullet().setBulletHardColor(java.newByte(aspose.slides.NullableBool.True));
symbolParagraph.getParagraphFormat().getBullet().setHeight(100);
textFrame.getParagraphs().add(symbolParagraph);
const numberedParagraph = new aspose.slides.Paragraph();
numberedParagraph.setText("This is a numbered item");
numberedParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
numberedParagraph.getParagraphFormat().getBullet().setNumberedBulletStyle(java.newByte(aspose.slides.NumberedBulletStyle.BulletCircleNumWDBlackPlain));
numberedParagraph.getParagraphFormat().setIndent(25);
numberedParagraph.getParagraphFormat().getBullet().getColor().setColorType(aspose.slides.ColorType.RGB);
numberedParagraph.getParagraphFormat().getBullet().getColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
numberedParagraph.getParagraphFormat().getBullet().setBulletHardColor(java.newByte(aspose.slides.NullableBool.True));
numberedParagraph.getParagraphFormat().getBullet().setHeight(100);
textFrame.getParagraphs().add(numberedParagraph);
presentation.save("bulleted_and_numbered_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
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 một hình ảnh tùy chỉnh thay vì ký hiệu hoặc số.
Ví dụ JavaScript này tải image.png từ thư mục làm việc của ứng dụng, thêm hình ảnh vào bộ sưu tập hình ảnh của bản trình chiếu dưới dạng một PPImage và sử dụng nó làm dấu đầu dòng cho đoạn. Nó gọi BulletFormat.setType để sử dụng dấu đầu hình ảnh và gán hình ảnh qua BulletFormat.getPicture. BulletFormat.setHeight được đặt thành 100 % để kích thước dấu đầu tương đối với văn bản đoạn:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const bulletImage = aspose.slides.Images.fromFile("image.png");
let presentationImage;
try {
presentationImage = presentation.getImages().addImage(bulletImage);
} finally {
bulletImage.dispose();
}
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
const paragraph = new aspose.slides.Paragraph();
paragraph.setText("Welcome to Aspose.Slides");
paragraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Picture));
paragraph.getParagraphFormat().getBullet().getPicture().setImage(presentationImage);
paragraph.getParagraphFormat().getBullet().setHeight(100);
textFrame.getParagraphs().add(paragraph);
presentation.save("picture_bullet.pptx", aspose.slides.SaveFormat.Pptx);
presentation.save("picture_bullet.ppt", aspose.slides.SaveFormat.Ppt);
} finally {
presentation.dispose();
}
Tạo Danh sách Đa cấp
Đặt ParagraphFormat.setDepth để đặt các đoạn ở các mức độ khác nhau của danh sách. Mức cao nhất có độ sâu 0.
Ví dụ JavaScript này gán các giá trị ParagraphFormat.setDepth 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 ký hiệu, với BulletFormat.setChar xen kẽ giữa dấu tròn và dấu gạch ngang để phân biệt các cấp liền kề:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
const firstParagraph = new aspose.slides.Paragraph();
firstParagraph.setText("Content");
firstParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
firstParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
firstParagraph.getParagraphFormat().setDepth(java.newShort(0));
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.setText("Second level");
secondParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
secondParagraph.getParagraphFormat().getBullet().setChar(java.newChar(45));
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
secondParagraph.getParagraphFormat().setDepth(java.newShort(1));
const thirdParagraph = new aspose.slides.Paragraph();
thirdParagraph.setText("Third level");
thirdParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
thirdParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
thirdParagraph.getParagraphFormat().setDepth(java.newShort(2));
const fourthParagraph = new aspose.slides.Paragraph();
fourthParagraph.setText("Fourth level");
fourthParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
fourthParagraph.getParagraphFormat().getBullet().setChar(java.newChar(45));
fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
fourthParagraph.getParagraphFormat().setDepth(java.newShort(3));
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
textFrame.getParagraphs().add(thirdParagraph);
textFrame.getParagraphs().add(fourthParagraph);
presentation.save("multilevel_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Bắt đầu Các mục Danh sách Đánh số với Giá trị Tùy chỉnh
Sử dụng BulletFormat.setNumberedBulletStartWith để đặt số ban đầu hiển thị cho một đoạn được đánh số.
Ví dụ JavaScript này đặt BulletFormat.setType để sử dụng đánh số cho ba đoạn và gán các giá trị BulletFormat.setNumberedBulletStartWith 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ị:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
const firstParagraph = new aspose.slides.Paragraph();
firstParagraph.setText("Start at 2");
firstParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
firstParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(2));
textFrame.getParagraphs().add(firstParagraph);
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.setText("Start at 3");
secondParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
secondParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(3));
textFrame.getParagraphs().add(secondParagraph);
const thirdParagraph = new aspose.slides.Paragraph();
thirdParagraph.setText("Start at 7");
thirdParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
thirdParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(7));
textFrame.getParagraphs().add(thirdParagraph);
presentation.save("custom_numbered_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kiểm soát Bố cục Đoạn vă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 ký hiệu kết thúc đoạn riêng biệt so với các phần văn bản hiển thị.
Đặt Thụt Lề Dòng Đầu tiên
Sử dụng phương thức ParagraphFormat.setIndent để kiểm soát thụt lề dòng đầu của một đoạn. Cài đặt này chỉ di chuyển dòng đầu so với lề trái của đoạn. Giá trị dương đẩy 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 ParagraphFormat.setMarginLeft khi bạn cần di chuyển toàn bộ đoạn. Sử dụng ParagraphFormat.setIndent khi bạn chỉ cần di chuyển dòng đầu.
Ví dụ sau đưa ba đoạn cùng một giá trị ParagraphFormat.setMarginLeft là 20 pt và các giá trị ParagraphFormat.setIndent là 0, 20 và 40 pt. Các dòng đầu bắt đầu ở các khoảng cách ngày càng lớn, trong khi các dòng gói giữ nguyên lề trái:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 420, 220);
shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
shape.getLineFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));
const textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
textFrame.getParagraphs().clear();
const firstParagraph = new aspose.slides.Paragraph();
firstParagraph.setText("No first-line indent. Wrapped lines start at the same position as the first line.");
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
firstParagraph.getParagraphFormat().setMarginLeft(20);
firstParagraph.getParagraphFormat().setIndent(0);
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.setText("First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body.");
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
secondParagraph.getParagraphFormat().setMarginLeft(20);
secondParagraph.getParagraphFormat().setIndent(20);
const thirdParagraph = new aspose.slides.Paragraph();
thirdParagraph.setText("First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see.");
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
thirdParagraph.getParagraphFormat().setMarginLeft(20);
thirdParagraph.getParagraphFormat().setIndent(40);
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
textFrame.getParagraphs().add(thirdParagraph);
presentation.save("paragraph_indent.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đặt Thụt Lề Treo
Thụt lề treo là bố cục đoạn trong đó dòng đầu bắt đầu ở bên trái các dòng còn lại. Trong Aspose.Slides, bạn tạo hiệu ứng này bằng phương thức ParagraphFormat.setIndent. Đặt thụt lề thành giá trị âm để di chuyển dòng đầu sang trái so với thân đoạn.
Trong thực tế, ParagraphFormat.setMarginLeft xác định vị trí bên trái của thân đoạn, và ParagraphFormat.setIndent xác định vị trí của dòng đầu so với lề đó. Để tạo thụt lề treo, đặt lề trái dương và thụt lề âm.
Định dạng này hữu ích cho thư mục, tài liệu tham khảo, mục chú giải và các đoạn khác nơi các dòng gói phải căn dưới thân đoạn thay vì dưới ký tự đầu tiên của dòng đầu.
Ví dụ JavaScript sau ghép các giá trị ParagraphFormat.setMarginLeft là 40 và 60 pt với các giá trị ParagraphFormat.setIndent là -20 và -30 pt. 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:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 420, 220);
shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
shape.getLineFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));
const textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
textFrame.getParagraphs().clear();
const firstParagraph = new aspose.slides.Paragraph();
firstParagraph.setText("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.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
firstParagraph.getParagraphFormat().setMarginLeft(40);
firstParagraph.getParagraphFormat().setIndent(-20);
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.setText("This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare.");
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
secondParagraph.getParagraphFormat().setMarginLeft(60);
secondParagraph.getParagraphFormat().setIndent(-30);
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
presentation.save("hanging_indent.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kết quả:

Đặt Thuộc tính Chạy Kết thúc Đoạn văn
Phương thức Paragraph.setEndParagraphPortionFormat kiểm soát định dạng của ký hiệu kết thúc đoạn. Ví dụ dưới tạo một PortionFormat với BasePortionFormat.setFontHeight là 48 pt và BasePortionFormat.setLatinFont là Times New Roman. Nó gán định dạng này cho ký hiệ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:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 10, 10, 200, 250);
const textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
const firstParagraph = new aspose.slides.Paragraph();
firstParagraph.getPortions().add(new aspose.slides.Portion("Sample text"));
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.getPortions().add(new aspose.slides.Portion("Sample text 2"));
const endParagraphFormat = new aspose.slides.PortionFormat();
endParagraphFormat.setFontHeight(48);
endParagraphFormat.setLatinFont(new aspose.slides.FontData("Times New Roman"));
secondParagraph.setEndParagraphPortionFormat(endParagraphFormat);
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
presentation.save("end_paragraph_format.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Đế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 Paragraph.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 TextFrame.getParagraphs, và nó có thể chiếm nhiều dòng được render. Một dấu ngắt dòng rõ ràng trong một đoạn buộc tạo dòng mới mà không tạo đoạn mới. Việc gói tự động tạo các dòng dựa trên chiều rộng khả dụng mà không chèn dấu ngắt dòng rõ ràng vào văn bản. Do đó, đếm số đoạn hoặc ký tự ngắt dòng không cho biết số dòng đã render.
Ví dụ dưới tạo một shape văn bản, đếm các dòng, thu hẹp shape, sau đó thay thế văn bản bằng chuỗi ngắn hơn. Ví dụ bật gói bằng TextFrameFormat.setWrapText và tắt autofit bằng TextFrameFormat.setAutofitType để chiều rộng shape kiểm soát việc gói mà không tự động thu nhỏ văn bản hoặc thay đổi kích thước shape. Mỗi phép đo sử dụng Paragraph.getLinesCount. Kích thước shape tính bằng điểm. Cuối cùng, ví dụ thêm một đoạn nữa và cộng tổng số dòng trong toàn bộ khung văn bản.
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 400, 200);
const textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setWrapText(java.newByte(aspose.slides.NullableBool.True));
textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.None));
const paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
paragraph.setText("This text demonstrates how automatic wrapping changes the number of rendered lines.");
console.log("Original width: " + paragraph.getLinesCount());
shape.setWidth(150);
console.log("Narrower shape: " + paragraph.getLinesCount());
paragraph.setText("Short text.");
console.log("Shorter text: " + paragraph.getLinesCount());
const secondParagraph = new aspose.slides.Paragraph();
secondParagraph.setText("Another paragraph.");
secondParagraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
textFrame.getParagraphs().add(secondParagraph);
let totalLineCount = 0;
for (let i = 0; i < textFrame.getParagraphs().getCount(); i++) {
const currentParagraph = textFrame.getParagraphs().get_Item(i);
totalLineCount += currentParagraph.getLinesCount();
}
console.log("Total lines in the text frame: " + totalLineCount);
} finally {
presentation.dispose();
}
Với văn bản và các kích thước này, thu hẹp shape làm tăng số dòng, trong khi thay thế bằng chuỗi ngắn làm giảm số dòng. Số đếm chính xác có thể thay đổi tùy thuộc vào tính khả dụng và thay thế phông, kích thước phông, lề, thụt lề, gói và cài đặt autofit. 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ẫu.
Số dòng một mình không quyết định liệu văn bản có tràn khỏi container hay không. Chiều cao khả dụng, chiều cao dòng, khoảng cách đoạn và dòng, và hành vi autofit cũng quan trọng; ngay cả một dòng duy nhất cũng có thể vượt quá chiều rộng khả dụng khi tắt gói.
Nhập và Xuất Nội dung Đoạn văn
Sử dụng HTML để đưa văn bản vào bản trình chiếu hoặc xuất các đoạn hiện có để sử dụng bên ngoài. Phần này cũng cho thấy cách render các đoạn riêng lẻ thành hình ảnh, bao gồm các đoạn trong ô bảng.
Nhập Văn bản HTML vào Đoạn văn
Sử dụng ParagraphCollection.addFromHtml để chuyển đổi markup HTML thành các đoạn và phần trong một khung văn bản.
Ví dụ JavaScript này đọc HTML từ file.html trong thư mục làm việc của ứng dụng và truyền markup cho ParagraphCollection.addFromHtml. Nó xóa đoạn mặc định của khung văn bản để khung chỉ chứa nội dung đã nhập. Shape hình chữ nhật để lại lề 10 pt ở mỗi phía của slide:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const fs = require("fs");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shapeWidth = presentation.getSlideSize().getSize().getWidth() - 20;
const shapeHeight = presentation.getSlideSize().getSize().getHeight() - 20;
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
shape.getTextFrame().getParagraphs().clear();
const html = fs.readFileSync("file.html", "utf8");
shape.getTextFrame().getParagraphs().addFromHtml(html);
presentation.save("html_text.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Xuất Văn bản Đoạn văn sang HTML
Sử dụng ParagraphCollection.exportToHtml để xuất một phạm vi đoạn được 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 website, bạn có thể muốn giữ lại việc nhấn mạnh cục bộ mà không mang theo phông, kích thước hoặc kiểu hộp văn bản của bản trình chiếu. Đặt TextToHtmlConversionOptions.setTextInheritanceLimit để kiểm soát mức độ exporter tuân theo kế thừa định dạng.
Tải về bản mẫu trình chiếu và đặt nó trong thư mục làm việc của ứng dụng. Slide đầu tiên chứa một shape văn bản với nội dung “Inherited text; explicit red 18pt”. Đầu vào cố ý sử dụng nhiều cấp định dạng:
- Kiểu văn bản mặc định của bản trình chiếu cung cấp Calibri và kích thước 28 pt.
- Kiểu văn bản cấp một của khung cung cấp màu xanh và in nghiêng.
- Đoạn đặt in đậm một cách rõ ràng.
- Phần thứ hai, “explicit red 18pt”, đặt màu đỏ và kích thước 18 pt một cách rõ ràng.
Ví dụ hoàn chỉnh dưới đây 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:
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const fs = require("fs");
const presentation = new aspose.slides.Presentation("text-inheritance.pptx");
try {
const shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
if (java.instanceOf(shape, "com.aspose.slides.IAutoShape")) {
const textFrame = shape.getTextFrame();
if (textFrame !== null) {
const paragraphs = textFrame.getParagraphs();
const modes = [aspose.slides.TextInheritanceLimit.All, aspose.slides.TextInheritanceLimit.TextBox, aspose.slides.TextInheritanceLimit.ParagraphOnly];
const fileNames = ["All.html", "TextBox.html", "ParagraphOnly.html"];
for (let i = 0; i < modes.length; i++) {
const options = new aspose.slides.TextToHtmlConversionOptions();
options.setTextInheritanceLimit(modes[i]);
const html = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
fs.writeFileSync(fileNames[i], html, "utf8");
}
} else {
console.log("The first shape does not contain a text frame.");
}
} else {
console.log("The first shape is not a text shape.");
}
} finally {
presentation.dispose();
}
HTML được tạo chứa CSS sau cho phần đầu tiên, “Inherited text; “. Bảng liệt kê các khai báo liên quan; các thuộc tính được tạo khác được lược bỏ để 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, nghiêng, 28 pt trong Calibri, nếu phông này có sẵn. |
TextBox.html |
color:#0000ff; font-style:italic; font-weight:bold |
Văn bản màu xanh, in đậm, nghiêng; trang hoặc trình duyệt cung cấp phông và kích thước. |
ParagraphOnly.html |
font-weight:bold |
Văn bản in đậm; trang hoặc trình duyệt cung cấp phông, kích thước, màu và kiểu nghiêng. |
Phần thứ hai vẫn giữ color:#ff0000, font-size:18pt và font-weight:bold trong cả ba kết quả. Nó cũng giữ kiểu nghiêng với All và TextBox, nhưng không giữ với ParagraphOnly. Giới hạn kế thừa do đó 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. Trong hai chế độ giới hạn, các thuộc tính CSS bị bỏ qua sẽ được giải quyết bởi kiểu của trang đích hoặc mặc định của trình duyệt, vì vậy giao diện cuối cùng phụ thuộc vào nơi HTML được sử dụng.
Tạo Đoạn Văn bản Clipboard HTML
Sử dụng TextToHtmlConversionOptions.setAddClipboardFragmentHeader để chọn đầu ra từ ParagraphCollection.exportToHtml. Với false, phương thức trả về HTML thông thường. Với true, chuỗi trả về bắt đầu bằng tiêu đề clipboard của Windows. Các trường StartHTML và EndHTML đưa ra offset byte UTF‑8 của HTML, trong khi StartFragment và EndFragment xác định nội dung cần dán.
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 clipboard HTML (clipboardHtml). Ứng dụng sau đó có thể ghi đoạn này vào clipboard hệ thống. Nếu API clipboard yêu cầu byte thô, mã hoá đoạn thành UTF‑8 mà không có BOM.
const slides = require("aspose.slides.via.java");
const presentation = new slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const shape = slide.getShapes().addAutoShape(slides.ShapeType.Rectangle, 0, 0, 400, 100);
const paragraphs = shape.getTextFrame().getParagraphs();
paragraphs.clear();
paragraphs.addFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");
const options = new slides.TextToHtmlConversionOptions();
options.setAddClipboardFragmentHeader(false);
const html = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
options.setAddClipboardFragmentHeader(true);
const clipboardHtml = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
} finally {
presentation.dispose();
}
Render Đoạn văn Thành Hình ảnh
Paragraph.getImage render một đoạn riêng lẻ trực tiếp và trả về một IImage. Lưu kết quả vào tệp hoặc stream bằng IImage.save. Bạn không cần render shape chứa hoặc cắt bitmap một cách thủ công.
Paragraph.getImage có thể trả về null nếu đoạn không tồn tại trong bộ sưu tập cha, không có giới hạn render hợp lệ, hoặc không thể render. Kiểm tra kết quả trước khi lưu và giải phóng ảnh đã trả về sau khi dùng.
Render Đoạn văn ở Quy mô Mặc định
Ví dụ tạo một bản trình chiếu với một slide, shape đầu tiên là một hộp văn bản chứa ba đoạn.

Ví dụ tiếp theo chọn đoạn thứ hai tại chỉ số 1 và gọi Paragraph.getImage mà không truyền tham số tỷ lệ. Nó kiểm tra shape có chứa đoạn cần thiết và render trả về hình ảnh trước khi gọi IImage.save để lưu dưới dạng PNG. Khối finally giải phóng ảnh sau khi dùng.
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const sourceShape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 400, 100);
const sourceTextFrame = sourceShape.getTextFrame();
sourceTextFrame.getParagraphs().clear();
for (const text of ["First paragraph", "Second paragraph", "Third paragraph"]) {
const sourceParagraph = new aspose.slides.Paragraph();
sourceParagraph.setText(text);
sourceTextFrame.getParagraphs().add(sourceParagraph);
}
const shape = slide.getShapes().get_Item(0);
if (java.instanceOf(shape, "com.aspose.slides.IAutoShape")) {
const textFrame = shape.getTextFrame();
if (textFrame !== null && textFrame.getParagraphs().getCount() > 1) {
const paragraph = textFrame.getParagraphs().get_Item(1);
const paragraphImage = paragraph.getImage();
if (paragraphImage !== null) {
try {
paragraphImage.save("paragraph.png", aspose.slides.ImageFormat.Png);
} finally {
paragraphImage.dispose();
}
} else {
console.log("The paragraph could not be rendered.");
}
} else {
console.log("The expected paragraph was not found.");
}
} else {
console.log("The first shape is not a text shape.");
}
} finally {
presentation.dispose();
}
Kết quả:

Render Đoạn văn trong Ô Bảng với Tỷ lệ
Sử dụng overload của Paragraph.getImage chấp nhận các tham số scaleX và scaleY để đặt hệ số tỷ lệ ngang và dọc. Ví dụ dưới tạo một bảng, render đoạn trong ô đầu tiên với độ rộng và chiều cao gấp đôi so với mặc định, và lưu kết quả dưới dạng PNG.
var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const scaleX = 2;
const scaleY = 2;
const presentation = new aspose.slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const columnWidths = java.newArray("double", [300]);
const rowHeights = java.newArray("double", [80]);
const table = slide.getShapes().addTable(50, 50, columnWidths, rowHeights);
const paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0);
paragraph.setText("Text in a table cell");
const paragraphImage = paragraph.getImage(scaleX, scaleY);
if (paragraphImage !== null) {
try {
paragraphImage.save("table_paragraph.png", aspose.slides.ImageFormat.Png);
} finally {
paragraphImage.dispose();
}
} else {
console.log("The paragraph could not be rendered.");
}
} finally {
presentation.dispose();
}
Hệ số 1 giữ trục đó ở kích thước pixel mặc định. Ví dụ, 2 cho cả hai hệ số tạo ra 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 phóng to hoặc đầu ra độ phân giải cao, nhưng cũng tăng việc dùng bộ nhớ và kích thước tệp. Các hệ số dưới 1 tạo hình ảnh nhỏ hơn với chi tiết í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ộ shape bằng Shape.getImage vẫn hữu ích khi đầu ra cần bao gồm nền, viền hoặc ngữ cảnh hình ảnh khác của shape. Đối với ảnh chỉ chứa đoạn, hãy dùng Paragraph.getImage.
Câu hỏi thường gặp
Tôi có thể hoàn toàn tắt việc ngắt dòng trong một khung văn bản không?
Có. Đặt TextFrameFormat.setWrapText để tắt gói, vì vậy các dòng không bị ngắt tại mép khung văn bản.
Làm thế nào để lấy giới hạn chính xác trên slide của một đoạn cụ thể?
Sử dụng Paragraph.getRect để lấy hình chữ nhật bao quanh của đoạn. Portion.getRect cung cấp giới hạn của một phần riêng lẻ.
Độ căn chỉnh đoạn (trái, phải, trung tâm hoặc căn đều) được kiểm soát ở đâu?
ParagraphFormat.setAlignment là cài đặt cấp độ đoạn và áp dụng cho toàn đ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 Căn chỉnh phông chữ trong một dòng.
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 BasePortionFormat.setLanguageId 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ữ.