Quản lý các đoạn văn bản PowerPoint trong PHP
Tổng quan
Aspose.Slides for PHP via Java biểu diễn văn bản dưới dạng một cấu trúc phân cấp gồm các khung văn bản, đoạn và phần:
- TextFrame đạ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.
- Paragraph đại diện cho một đoạ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.
- Portion đạ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 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
Tạo Đoạn với Nhiều Phần
Các bước sau tạo một khung văn bản với ba đoạn, mỗi đoạn chứa ba phần:
- Tạo một thể hiện của lớp Presentation.
- Truy cập slide tương ứng thông qua chỉ số của nó.
- Thêm một AutoShape hình chữ nhật vào slide.
- Truy cập TextFrame của hình.
- Sử dụng đoạn mặc định và thêm hai đối tượng Paragraph nữa vào khung văn bản.
- Thêm đủ các đối tượng Portion để mỗi đoạn chứa ba phần. Đoạn mặc định đã có một phần rỗng.
- Đặt văn bản cho mỗi phần.
- Áp dụng định dạng cấp ký tự qua Portion::getPortionFormat.
- Lưu bản trình chiếu đã sửa đổi.
Ví dụ PHP này thực hiện các bước trên:
use aspose\slides\FillType;
use aspose\slides\NullableBool;
use aspose\slides\Paragraph;
use aspose\slides\Portion;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 150, 300, 150);
$textFrame = $shape->getTextFrame();
$firstParagraph = $textFrame->getParagraphs()->get_Item(0);
$firstParagraph->getPortions()->add(new Portion());
$firstParagraph->getPortions()->add(new Portion());
$secondParagraph = new Paragraph();
$secondParagraph->getPortions()->add(new Portion());
$secondParagraph->getPortions()->add(new Portion());
$secondParagraph->getPortions()->add(new Portion());
$textFrame->getParagraphs()->add($secondParagraph);
$thirdParagraph = new Paragraph();
$thirdParagraph->getPortions()->add(new Portion());
$thirdParagraph->getPortions()->add(new Portion());
$thirdParagraph->getPortions()->add(new Portion());
$textFrame->getParagraphs()->add($thirdParagraph);
$paragraphCount = java_values($textFrame->getParagraphs()->getCount());
for ($paragraphIndex = 0; $paragraphIndex < $paragraphCount; $paragraphIndex++) {
$paragraph = $textFrame->getParagraphs()->get_Item($paragraphIndex);
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
$portion->setText("Portion " . ($paragraphIndex + 1) . "." . ($portionIndex + 1));
if ($portionIndex == 0) {
$portion->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$portion->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->RED);
$portion->getPortionFormat()->setFontBold(NullableBool::True);
$portion->getPortionFormat()->setFontHeight(15);
} else if ($portionIndex == 1) {
$portion->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$portion->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
$portion->getPortionFormat()->setFontItalic(NullableBool::True);
$portion->getPortionFormat()->setFontHeight(18);
}
}
}
$presentation->save("paragraphs_with_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Tạo Danh sách Dấu đầu dòng và Đánh số
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 dễ dàng quét các mục liên quan. Trong Aspose.Slides, cài đặt danh sách được định nghĩa qua BulletFormat.
- Tạo một thể hiện của lớp Presentation.
- Truy cập slide tương ứng thông qua chỉ số của nó.
- Thêm một AutoShape vào slide đã chọn.
- Truy cập TextFrame của hình.
- Xóa đoạn mặc định khỏi khung văn bản.
- Tạo một Paragraph cho dấu đầu dòng ký hiệu.
- Đặt BulletFormat::setType thành BulletType::Symbol và chỉ định ký tự dấu đầu dòng.
- Đặt văn bản đoạn, thụt lề, màu dấu đầu dòng và chiều cao dấu đầu dòng.
- Thêm đoạn vào khung văn bản.
- Tạo một đoạn thứ hai và đặt BulletFormat::setType thành BulletType::Numbered.
- Cấu hình kiểu dấu đầu dòng đánh số và thêm đoạn vào khung văn bản.
- Lưu bản trình chiếu.
Ví dụ PHP này tạo một dấu đầu dòng ký hiệu và một dấu đầu dòng đánh số:
use aspose\slides\BulletType;
use aspose\slides\ColorType;
use aspose\slides\NullableBool;
use aspose\slides\NumberedBulletStyle;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
$symbolParagraph = new Paragraph();
$symbolParagraph->setText("Welcome to Aspose.Slides");
$symbolParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$symbolParagraph->getParagraphFormat()->getBullet()->setChar("•");
$symbolParagraph->getParagraphFormat()->setIndent(25);
$symbolParagraph->getParagraphFormat()->getBullet()->getColor()->setColorType(ColorType::RGB);
$symbolParagraph->getParagraphFormat()->getBullet()->getColor()->setColor(java("java.awt.Color")->BLACK);
$symbolParagraph->getParagraphFormat()->getBullet()->setBulletHardColor(NullableBool::True);
$symbolParagraph->getParagraphFormat()->getBullet()->setHeight(100);
$textFrame->getParagraphs()->add($symbolParagraph);
$numberedParagraph = new Paragraph();
$numberedParagraph->setText("This is a numbered item");
$numberedParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$numberedParagraph->getParagraphFormat()->getBullet()->setNumberedBulletStyle(NumberedBulletStyle::BulletCircleNumWDBlackPlain);
$numberedParagraph->getParagraphFormat()->setIndent(25);
$numberedParagraph->getParagraphFormat()->getBullet()->getColor()->setColorType(ColorType::RGB);
$numberedParagraph->getParagraphFormat()->getBullet()->getColor()->setColor(java("java.awt.Color")->BLACK);
$numberedParagraph->getParagraphFormat()->getBullet()->setBulletHardColor(NullableBool::True);
$numberedParagraph->getParagraphFormat()->getBullet()->setHeight(100);
$textFrame->getParagraphs()->add($numberedParagraph);
$presentation->save("bulleted_and_numbered_list.pptx", 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 hình ảnh tùy chỉnh thay cho ký hiệu hoặc số.
- Tạo một thể hiện của lớp Presentation.
- Truy cập slide tương ứng thông qua chỉ số của nó.
- Thêm một AutoShape và truy cập TextFrame của nó.
- Xóa đoạn mặc định khỏi khung văn bản.
- Tải hình ảnh dấu đầu dòng và thêm vào bộ sưu tập hình ảnh của bản trình chiếu dưới dạng PPImage.
- Tạo một Paragraph và đặt văn bản cho nó.
- Đặt BulletFormat::setType thành BulletType::Picture.
- Gán hình ảnh qua BulletFormat::getPicture và đặt chiều cao dấu đầu dòng.
- Thêm đoạn vào khung văn bản.
- Lưu bản trình chiếu đã sửa đổi.
Ví dụ PHP này tạo một dấu đầu dòng hình ảnh:
use aspose\slides\BulletType;
use aspose\slides\Images;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$bulletImage = Images::fromFile("bullets.png");
try {
$presentationImage = $presentation->getImages()->addImage($bulletImage);
} finally {
$bulletImage->dispose();
}
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
$paragraph = new Paragraph();
$paragraph->setText("Welcome to Aspose.Slides");
$paragraph->getParagraphFormat()->getBullet()->setType(BulletType::Picture);
$paragraph->getParagraphFormat()->getBullet()->getPicture()->setImage($presentationImage);
$paragraph->getParagraphFormat()->getBullet()->setHeight(100);
$textFrame->getParagraphs()->add($paragraph);
$presentation->save("picture_bullet.pptx", SaveFormat::Pptx);
$presentation->save("picture_bullet.ppt", SaveFormat::Ppt);
} finally {
$presentation->dispose();
}
Tạo Danh sách Đa cấp
Đặt ParagraphFormat::setDepth để đặt các đoạn ở các cấp độ khác nhau của danh sách. Cấp cao nhất có độ sâu 0.
- Tạo một Presentation và truy cập một slide.
- Thêm một AutoShape và xóa đoạn mặc định khỏi khung văn bản của nó.
- Tạo bốn đoạn và cấu hình các ký hiệu dấu đầu dòng của chúng.
- Đặt giá trị ParagraphFormat::setDepth thành
0,1,2và3. - Thêm các đoạn vào khung văn bản và lưu bản trình chiếu.
Ví dụ PHP này tạo một danh sách dấu đầu dòng bốn cấp:
use aspose\slides\BulletType;
use aspose\slides\FillType;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
$firstParagraph = new Paragraph();
$firstParagraph->setText("Content");
$firstParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$firstParagraph->getParagraphFormat()->getBullet()->setChar("•");
$firstParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$firstParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$firstParagraph->getParagraphFormat()->setDepth(0);
$secondParagraph = new Paragraph();
$secondParagraph->setText("Second level");
$secondParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$secondParagraph->getParagraphFormat()->getBullet()->setChar('-');
$secondParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$secondParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$secondParagraph->getParagraphFormat()->setDepth(1);
$thirdParagraph = new Paragraph();
$thirdParagraph->setText("Third level");
$thirdParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$thirdParagraph->getParagraphFormat()->getBullet()->setChar("•");
$thirdParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$thirdParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$thirdParagraph->getParagraphFormat()->setDepth(2);
$fourthParagraph = new Paragraph();
$fourthParagraph->setText("Fourth level");
$fourthParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Symbol);
$fourthParagraph->getParagraphFormat()->getBullet()->setChar('-');
$fourthParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$fourthParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$fourthParagraph->getParagraphFormat()->setDepth(3);
$textFrame->getParagraphs()->add($firstParagraph);
$textFrame->getParagraphs()->add($secondParagraph);
$textFrame->getParagraphs()->add($thirdParagraph);
$textFrame->getParagraphs()->add($fourthParagraph);
$presentation->save("multilevel_list.pptx", 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ố đầu tiên hiển thị cho một đoạn đánh số.
- Tạo một Presentation và thêm một AutoShape vào một slide.
- Xóa đoạn mặc định khỏi khung văn bản của hình.
- Tạo ba đoạn đánh số.
- Đặt BulletFormat::setNumberedBulletStartWith thành
2,3và7cho các đoạn tương ứng. - Thêm các đoạn vào khung văn bản và lưu bản trình chiếu.
Ví dụ PHP này gán số bắt đầu tùy chỉnh cho mỗi đoạn:
use aspose\slides\BulletType;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$shape = $presentation->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 200, 200, 400, 200);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
$firstParagraph = new Paragraph();
$firstParagraph->setText("Start at 2");
$firstParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$firstParagraph->getParagraphFormat()->getBullet()->setNumberedBulletStartWith(2);
$textFrame->getParagraphs()->add($firstParagraph);
$secondParagraph = new Paragraph();
$secondParagraph->setText("Start at 3");
$secondParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$secondParagraph->getParagraphFormat()->getBullet()->setNumberedBulletStartWith(3);
$textFrame->getParagraphs()->add($secondParagraph);
$thirdParagraph = new Paragraph();
$thirdParagraph->setText("Start at 7");
$thirdParagraph->getParagraphFormat()->getBullet()->setType(BulletType::Numbered);
$thirdParagraph->getParagraphFormat()->getBullet()->setNumberedBulletStartWith(7);
$textFrame->getParagraphs()->add($thirdParagraph);
$presentation->save("custom_numbered_list.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Kiểm soát Bố cục Đoạn và Thuộc tính Kết thúc
Đặt Thụt Lề Dòng Đầu
Sử dụng ParagraphFormat::setIndent để kiểm soát thụt lề dòng đầu của một đoạn. Phương pháp này chỉ di chuyển dòng đầu tiên so với lề trái của đoạn. Giá trị dương làm dịch dòng đầu tiên sang phải, trong khi các dòng còn lại vẫn căn theo 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 tiên.
Ví dụ dưới đây tạo một số đoạn và áp dụng các giá trị khác nhau của ParagraphFormat::setIndent để minh họa cách thụt lề dòng đầu ảnh hưởng đến bố cục đoạn.
- Tạo một thể hiện của lớp Presentation.
- Truy cập slide mục tiêu.
- Thêm một AutoShape hình chữ nhật vào slide.
- Truy cập TextFrame của hình và xóa đoạn mặc định.
- Tạo một số đoạn và đặt các giá trị khác nhau của ParagraphFormat::setIndent cho chúng.
- Thêm các đoạn vào khung văn bản.
- Lưu bản trình chiếu đã sửa đổi.
Mã PHP này cho bạn thấy cách đặt thụt lề đoạn:
use aspose\slides\FillType;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\TextAutofitType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 420, 220);
$shape->getFillFormat()->setFillType(FillType::NoFill);
$shape->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
$shape->getLineFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$textFrame = $shape->getTextFrame();
$textFrame->getTextFrameFormat()->setAutofitType(TextAutofitType::Shape);
$textFrame->getParagraphs()->clear();
$firstParagraph = new Paragraph();
$firstParagraph->setText("No first-line indent. Wrapped lines start at the same position as the first line.");
$firstParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$firstParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$firstParagraph->getParagraphFormat()->setMarginLeft(20.0);
$firstParagraph->getParagraphFormat()->setIndent(0.0);
$secondParagraph = new 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(FillType::Solid);
$secondParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$secondParagraph->getParagraphFormat()->setMarginLeft(20.0);
$secondParagraph->getParagraphFormat()->setIndent(20.0);
$thirdParagraph = new 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(FillType::Solid);
$thirdParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$thirdParagraph->getParagraphFormat()->setMarginLeft(20.0);
$thirdParagraph->getParagraphFormat()->setIndent(40.0);
$textFrame->getParagraphs()->add($firstParagraph);
$textFrame->getParagraphs()->add($secondParagraph);
$textFrame->getParagraphs()->add($thirdParagraph);
$presentation->save("paragraph_indent.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Kết quả:

Đặt Thụt Lề Treo
Thụt lề treo là một bố cục đoạn trong đó dòng đầu tiên 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 ParagraphFormat::setIndent. Cung cấp giá trị âm để di chuyển dòng đầu tiên sang trái so với thân đoạn.
Trong thực tế, ParagraphFormat::setMarginLeft xác định vị trí trái của thân đoạn, và ParagraphFormat::setIndent xác định vị trí của dòng đầu tiên so với lề đó. Để tạo thụt lề treo, cung cấp giá trị dương cho setMarginLeft và giá trị âm cho setIndent.
Định dạng này hữu ích cho thư mục, tài liệu tham khảo, mục mục lục và các đoạn khác nơi các dòng gập phải căn dưới thân đoạn thay vì dưới ký tự đầu tiên của dòng đầu.
- Tạo một thể hiện của lớp Presentation.
- Truy cập slide mục tiêu.
- Thêm một AutoShape hình chữ nhật vào slide.
- Truy cập TextFrame của hình và xóa đoạn mặc định.
- Tạo các đoạn và cung cấp giá trị dương cho ParagraphFormat::setMarginLeft cho mỗi đoạn.
- Cung cấp giá trị âm cho ParagraphFormat::setIndent để tạo hiệu ứng thụt lề treo.
- Thêm các đoạn vào khung văn bản.
- Lưu bản trình chiếu đã sửa đổi.
Mã PHP này cho bạn thấy cách đặt thụt lề treo cho một đoạn:
use aspose\slides\FillType;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
use aspose\slides\TextAutofitType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 420, 220);
$shape->getFillFormat()->setFillType(FillType::NoFill);
$shape->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
$shape->getLineFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$textFrame = $shape->getTextFrame();
$textFrame->getTextFrameFormat()->setAutofitType(TextAutofitType::Shape);
$textFrame->getParagraphs()->clear();
$firstParagraph = new 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(FillType::Solid);
$firstParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$firstParagraph->getParagraphFormat()->setMarginLeft(40.0);
$firstParagraph->getParagraphFormat()->setIndent(-20.0);
$secondParagraph = new 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(FillType::Solid);
$secondParagraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$secondParagraph->getParagraphFormat()->setMarginLeft(60.0);
$secondParagraph->getParagraphFormat()->setIndent(-30.0);
$textFrame->getParagraphs()->add($firstParagraph);
$textFrame->getParagraphs()->add($secondParagraph);
$presentation->save("hanging_indent.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Kết quả:

Đặt Thuộc tính Kết thúc Đoạn
Paragraph::setEndParagraphPortionFormat điều khiển định dạng của ký tự kết thúc đoạn. Ví dụ PHP sau gán kích thước phông chữ và phông chữ Latin cho ký tự kết thúc của đoạn thứ hai:
- Tải một Presentation và truy cập một slide.
- Thêm một AutoShape và xóa đoạn mặc định của nó.
- Tạo hai đoạn và thêm các phần văn bản vào chúng.
- Tạo một PortionFormat cho ký tự kết thúc của đoạn thứ hai.
- Đặt BasePortionFormat::setFontHeight và BasePortionFormat::setLatinFont.
- Gán định dạng bằng Paragraph::setEndParagraphPortionFormat và lưu bản trình chiếu.
use aspose\slides\FontData;
use aspose\slides\Paragraph;
use aspose\slides\Portion;
use aspose\slides\PortionFormat;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation("Test.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 10, 10, 200, 250);
$textFrame = $shape->getTextFrame();
$textFrame->getParagraphs()->clear();
$firstParagraph = new Paragraph();
$firstParagraph->getPortions()->add(new Portion("Sample text"));
$secondParagraph = new Paragraph();
$secondParagraph->getPortions()->add(new Portion("Sample text 2"));
$endParagraphFormat = new PortionFormat();
$endParagraphFormat->setFontHeight(48);
$endParagraphFormat->setLatinFont(new FontData("Times New Roman"));
$secondParagraph->setEndParagraphPortionFormat($endParagraphFormat);
$textFrame->getParagraphs()->add($firstParagraph);
$textFrame->getParagraphs()->add($secondParagraph);
$presentation->save("end_paragraph_format.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Đếm Số Dòng Được Kết Xuất
Đối với các quy tắc đoạn ảnh hưởng đến việc gập 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 mà một đoạn chiếm sau khi bố cục văn bản, bao gồm việc gập 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 TextFrame::getParagraphs, và nó có thể chiếm nhiều dòng đã kết xuất. 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. Gập tự động tạo các dòng dựa trên độ rộng có sẵn mà không chèn dấu ngắt dòng rõ ràng vào văn bản. Vì vậy, đếm số đoạn hoặc ký tự ngắt dòng không cung cấp số dòng đã kết xuất.
Ví dụ sau tạo một hình dạng văn bản, đếm các dòng của nó, thu hẹp hình dạng, và sau đó thay thế văn bản bằng một chuỗi ngắn hơn. Gập được bật và tự động vừa (autofit) bị tắt để độ rộng hình dạng kiểm soát việc gập mà không tự động thu nhỏ văn bản hoặc thay đổi kích thước hình dạng. Kích thước hình dạng tính bằng điểm. Cuối cùng, ví dụ thêm một đoạn khác và tổng hợp số dòng qua khung văn bản.
use aspose\slides\NullableBool;
use aspose\slides\Paragraph;
use aspose\slides\Presentation;
use aspose\slides\ShapeType;
use aspose\slides\TextAutofitType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 50, 50, 400, 200);
$textFrame = $shape->getTextFrame();
$textFrame->getTextFrameFormat()->setWrapText(NullableBool::True);
$textFrame->getTextFrameFormat()->setAutofitType(TextAutofitType::None);
$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.");
echo "Original width: " . java_values($paragraph->getLinesCount()) . PHP_EOL;
$shape->setWidth(150);
echo "Narrower shape: " . java_values($paragraph->getLinesCount()) . PHP_EOL;
$paragraph->setText("Short text.");
echo "Shorter text: " . java_values($paragraph->getLinesCount()) . PHP_EOL;
$secondParagraph = new Paragraph();
$secondParagraph->setText("Another paragraph.");
$secondParagraph->getParagraphFormat()->getDefaultPortionFormat()->setFontHeight(20);
$textFrame->getParagraphs()->add($secondParagraph);
$totalLineCount = 0;
for ($i = 0; $i < java_values($textFrame->getParagraphs()->getCount()); $i++) {
$currentParagraph = $textFrame->getParagraphs()->get_Item($i);
$totalLineCount += java_values($currentParagraph->getLinesCount());
}
echo "Total lines in the text frame: " . $totalLineCount . PHP_EOL;
} finally {
$presentation->dispose();
}
Với văn bản này và các kích thước này, 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 nó. Các số đếm chính xác có thể thay đổi tùy vào việc có sẵn phông chữ và việc thay thế, kích thước phông, lề, thụt lề, gập và cài đặt autofit. Sử dụng phông chữ 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 chỉ không quyết định liệu văn bản có tràn ra ngoà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ập.
Nhập và Xuất Nội dung Đoạn
Nhập Văn bản HTML vào Đoạ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.
- Tạo một thể hiện của Presentation.
- Truy cập một slide và thêm một AutoShape.
- Truy cập TextFrame của hình và xóa đoạn mặc định.
- Đọc tệp HTML nguồn.
- Gửi chuỗi HTML tới ParagraphCollection::addFromHtml.
- Lưu bản trình chiếu đã sửa đổi.
Ví dụ PHP này nhập HTML vào một khung văn bản:
use aspose\slides\FillType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\ShapeType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$shapeWidth = java_values($presentation->getSlideSize()->getSize()->getWidth()) - 20;
$shapeHeight = java_values($presentation->getSlideSize()->getSize()->getHeight()) - 20;
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 10, 10, $shapeWidth, $shapeHeight);
$shape->getFillFormat()->setFillType(FillType::NoFill);
$shape->getTextFrame()->getParagraphs()->clear();
$html = file_get_contents("file.html");
if ($html !== false) {
$shape->getTextFrame()->getParagraphs()->addFromHtml($html);
$presentation->save("html_text.pptx", SaveFormat::Pptx);
} else {
echo "The HTML file could not be read.";
}
} finally {
$presentation->dispose();
}
Xuất Văn bản Đoạn ra HTML
Sử dụng ParagraphCollection::exportToHtml để xuất một phạm vi đã chọn của các đoạn dưới dạng HTML.
- Tạo một thể hiện của Presentation và tải bản trình chiếu mong muốn.
- Truy cập slide và tìm AutoShape chứa văn bản.
- Truy cập TextFrame của hình.
- Gọi ParagraphCollection::exportToHtml với chỉ số đoạn bắt đầu và số đoạn cần xuất.
- Ghi chuỗi HTML trả về vào tệp.
Ví dụ PHP này xuất tất cả các đoạn từ hình dạng văn bản đầu tiên:
use aspose\slides\Presentation;
$presentation = new Presentation("ExportingHTMLText.pptx");
try {
$shape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
if (java_instanceof($shape, new JavaClass("com.aspose.slides.AutoShape"))) {
$textFrame = $shape->getTextFrame();
if (!java_is_null($textFrame)) {
$paragraphs = $textFrame->getParagraphs();
$html = $paragraphs->exportToHtml(0, $paragraphs->getCount(), null);
if (file_put_contents("paragraphs.html", $html) === false) {
echo "The HTML file could not be written.";
}
} else {
echo "The first shape does not contain a text frame.";
}
} else {
echo "The first shape is not a text shape.";
}
} finally {
$presentation->dispose();
}
Kết xuất Đoạn dưới dạng Hình ảnh
Paragraph::getImage kết xuất 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 luồng bằng IImage::save. Bạn không cần phải kết xuất hình dạng chứa hoặc cắt bitmap thủ công.
Paragraph::getImage có thể trả về null nếu đoạn không thể tìm thấy trong bộ sưu tập cha, không có ràng buộc kết xuất hợp lệ, hoặc không thể kết xuất. 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.
Kết xuất Đoạn ở Tỷ lệ Mặc định
Giả sử chúng ta có một tệp trình chiếu có tên sample.pptx với một slide, trong đó hình dạng đầu tiên là một hộp văn bản chứa ba đoạn.

Ví dụ PHP sau kết xuất đoạn thứ hai trong một hình dạng văn bản thường ở tỷ lệ mặc định và lưu hình ảnh trả về ở định dạng PNG. Khối finally đảm bảo rằng hình ảnh được giải phóng đúng cách.
use aspose\slides\ImageFormat;
use aspose\slides\Presentation;
$presentation = new Presentation("sample.pptx");
try {
$shape = $presentation->getSlides()->get_Item(0)->getShapes()->get_Item(0);
if (java_instanceof($shape, new JavaClass("com.aspose.slides.AutoShape"))) {
$textFrame = $shape->getTextFrame();
if (!java_is_null($textFrame) && java_values($textFrame->getParagraphs()->getCount()) > 1) {
$paragraph = $textFrame->getParagraphs()->get_Item(1);
$paragraphImage = $paragraph->getImage();
if (!java_is_null($paragraphImage)) {
try {
$paragraphImage->save("paragraph.png", ImageFormat::Png);
} finally {
$paragraphImage->dispose();
}
} else {
echo "The paragraph could not be rendered.";
}
} else {
echo "The expected paragraph was not found.";
}
} else {
echo "The first shape is not a text shape.";
}
} finally {
$presentation->dispose();
}
Kết quả:

Kết xuất Đoạn trong Ô Bảng với Tỷ lệ
Sử dụng Paragraph::getImage để đặt các hệ số tỷ lệ ngang và dọc $scaleX và $scaleY. Ví dụ PHP sau tạo một bảng, kết xuất đoạn trong ô đầu tiên với độ rộng và chiều cao gấp đôi so với mặc định, và lưu kết quả dưới dạng PNG.
use aspose\slides\ImageFormat;
use aspose\slides\Presentation;
$scaleX = 2;
$scaleY = 2;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$table = $slide->getShapes()->addTable(50, 50, array(300), array(80));
$paragraph = $table->get_Item(0, 0)->getTextFrame()->getParagraphs()->get_Item(0);
$paragraph->setText("Text in a table cell");
$paragraphImage = $paragraph->getImage($scaleX, $scaleY);
if (!java_is_null($paragraphImage)) {
try {
$paragraphImage->save("table_paragraph.png", ImageFormat::Png);
} finally {
$paragraphImage->dispose();
}
} else {
echo "The paragraph could not be rendered.";
}
} finally {
$presentation->dispose();
}
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 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 đến bốn lần nhiều pixel. Các hệ số lớn hơn thường tạo ra văn bản sắc nét hơn cho việc phóng to hoặc xuất khẩu độ phân giải cao, nhưng chúng cũng tăng sử dụng bộ nhớ và kích thước tệp. Các hệ số dưới 1 tạo ra hình ảnh nhỏ hơn với í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.
Việc kết xuất toàn bộ hình dạng với Shape::getImage vẫn hữu ích khi đầu ra cần bao gồm việc tô màu, viền hoặc ngữ cảnh hình ảnh khác của hình dạng. Đối với hình ảnh chỉ chứa đoạn, hãy sử dụng Paragraph::getImage.
Câu hỏi thường gặp
Can I completely disable line wrapping inside a text frame?
Có. Đặt TextFrameFormat::setWrapText để tắt gập, để các dòng không ngắt tại các cạnh của khung văn bản.
How can I get the exact on-slide bounds of a specific paragraph?
Sử dụng Paragraph::getRect để lấy hình chữ nhật bao quanh đoạn. Portion::getRect cung cấp kích thước của một phần riêng lẻ.
Where is paragraph alignment (left, right, center, or justify) controlled?
ParagraphFormat::setAlignment là một cài đặt mức độ đoạn và áp dụng cho toàn bộ đoạn bất kể định dạng phần riêng lẻ.
Để căn chỉnh các phần có kích thước phông chữ khác nhau theo chiều dọc trong mỗi dòng, xem Căn chỉnh phông chữ trong một dòng.
Can I set the proofing language for part of a paragraph?
Có. Đặt BasePortionFormat::setLanguageId cho các phần riêng lẻ, vì vậy một đoạn có thể chứa văn bản bằng nhiều ngôn ngữ.