在 Android 上管理簡報中的文字方塊
簡介
投影片上的文字通常位於文字方塊或圖案中。因此,要在投影片上新增文字,必須先新增文字方塊,然後將文字放入文字方塊中。Aspose.Slides for Android via Java 提供 IAutoShape 介面,允許您新增包含文字的圖形。
資訊
Aspose.Slides 亦提供 IShape 介面,允許您向投影片新增圖形。然而,透過IShape 介面新增的並非所有圖形都能容納文字。但透過 IAutoShape 介面新增的圖形可能包含文字。
注意
因此,當處理想要加入文字的圖形時,您可能需要檢查並確認它是透過IAutoShape 介面轉型的。只有這樣,您才能使用屬於 IAutoShape 的 TextFrame。請參閱本頁面的 Update Text 章節。
在投影片上建立文字方塊
若要在投影片上建立文字方塊,請依照以下步驟操作:
- 建立 Presentation 類別的實例。
- 取得新建立的簡報中第一張投影片的參考。
- 在投影片的指定位置新增一個 IAutoShape 物件,將 ShapeType 設為
Rectangle,並取得新加入的IAutoShape物件的參考。 - 為
IAutoShape物件新增TextFrame屬性,以容納文字。在下方範例中,我們加入了以下文字:Aspose TextBox - 最後,使用
Presentation物件寫入 PPTX 檔案。
以下 Java 程式碼—上述步驟的實作範例—示範如何在投影片上加入文字:
// 實例化 Presentation
Presentation pres = new Presentation();
try {
// 取得簡報中的第一張投影片
ISlide sld = pres.getSlides().get_Item(0);
// 新增一個類型設定為 Rectangle 的 AutoShape
IAutoShape ashp = sld.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);
// 為 Rectangle 新增 TextFrame
ashp.addTextFrame(" ");
// 存取文字框
ITextFrame txtFrame = ashp.getTextFrame();
// 為文字框建立 Paragraph 物件
IParagraph para = txtFrame.getParagraphs().get_Item(0);
// 為段落建立 Portion 物件
IPortion portion = para.getPortions().get_Item(0);
// 設定文字
portion.setText("Aspose TextBox");
// 將簡報儲存至磁碟
pres.save("TextBox_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
檢查文字方塊圖形
Aspose.Slides 提供 IAutoShape 介面的 isTextBox 方法,讓您檢查圖形並辨識文字方塊。

以下 Java 程式碼示範如何檢查圖形是否是以文字方塊建立的:
Presentation presentation = new Presentation("sample.pptx");
try {
ForEach.shape(presentation, (shape, slide, index) -> {
if (shape instanceof IAutoShape) {
IAutoShape autoShape = (IAutoShape) shape;
System.out.println(autoShape.isTextBox() ? "shape is a text box" : "shape is not a text box");
}
});
} finally {
presentation.dispose();
}
請注意,如果僅使用 IShapeCollection 介面的 addAutoShape 方法新增自動圖形,該自動圖形的 isTextBox 方法將回傳 false。然而,當您使用 addTextFrame 方法或 setText 方法為自動圖形加入文字後,isTextBox 屬性會回傳 true。
Presentation presentation = new Presentation();
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape1 = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, 100, 40);
// shape1.isTextBox() 回傳 false
shape1.addTextFrame("shape 1");
// shape1.isTextBox() 回傳 true
IAutoShape shape2 = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 110, 100, 40);
// shape2.isTextBox() 回傳 false
shape2.getTextFrame().setText("shape 2");
// shape2.isTextBox() 回傳 true
IAutoShape shape3 = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 210, 100, 40);
// shape3.isTextBox() 回傳 false
shape3.addTextFrame("");
// shape3.isTextBox() 回傳 false
IAutoShape shape4 = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 310, 100, 40);
// shape4.isTextBox() 回傳 false
shape4.getTextFrame().setText("");
// shape4.isTextBox() 回傳 false
在文字方塊中加入欄位
Aspose.Slides 提供 ColumnCount 與 ColumnSpacing 屬性(分別來自 ITextFrameFormat 介面與 TextFrameFormat 類別),讓您可以為文字方塊新增欄位。您可以指定文字方塊的欄位數量,並設定欄位之間的點數間距。
以下 Java 程式碼示範上述操作:
Presentation pres = new Presentation();
try {
// 取得簡報中的第一張投影片
ISlide slide = pres.getSlides().get_Item(0);
// 新增一個類型設定為 Rectangle 的 AutoShape
IAutoShape aShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 300, 300);
// 為 Rectangle 新增 TextFrame
aShape.addTextFrame("All these columns are limited to be within a single text container -- " +
"you can add or delete text and the new or remaining text automatically adjusts " +
"itself to flow within the container. You cannot have text flow from one container " +
"to other though -- we told you PowerPoint's column options for text are limited!");
// 取得 TextFrame 的文字格式
ITextFrameFormat format = aShape.getTextFrame().getTextFrameFormat();
// 指定 TextFrame 中的欄位數量
format.setColumnCount(3);
// 指定欄位之間的間距
format.setColumnSpacing(10);
// 儲存簡報
pres.save("ColumnCount.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
在文字框中加入欄位
Aspose.Slides for Android via Java 提供 ColumnCount 屬性(來自 ITextFrameFormat 介面),讓您在文字框中新增欄位。透過此屬性,您可以指定文字框中欲使用的欄位數量。
以下 Java 程式碼示範如何在文字框內加入欄位:
String outPptxFileName = "ColumnsTest.pptx";
Presentation pres = new Presentation();
try {
IAutoShape shape1 = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 300, 300);
TextFrameFormat format = (TextFrameFormat)shape1.getTextFrame().getTextFrameFormat();
format.setColumnCount(2);
shape1.getTextFrame().setText("All these columns are forced to stay within a single text container -- " +
"you can add or delete text - and the new or remaining text automatically adjusts " +
"itself to stay within the container. You cannot have text spill over from one container " +
"to other, though -- because PowerPoint's column options for text are limited!");
pres.save(outPptxFileName, SaveFormat.Pptx);
Presentation test = new Presentation(outPptxFileName);
try {
IAutoShape autoShape = ((AutoShape)test.getSlides().get_Item(0).getShapes().get_Item(0));
Assert.assertTrue(2 == autoShape.getTextFrame().getTextFrameFormat().getColumnCount());
Assert.assertTrue(Double.NaN == autoShape.getTextFrame().getTextFrameFormat().getColumnSpacing());
} finally {
if (test != null) test.dispose();
}
format.setColumnSpacing(20);
pres.save(outPptxFileName, SaveFormat.Pptx);
Presentation test1 = new Presentation(outPptxFileName);
try {
IAutoShape autoShape = ((AutoShape)test1.getSlides().get_Item(0).getShapes().get_Item(0));
Assert.assertTrue(2 == autoShape.getTextFrame().getTextFrameFormat().getColumnCount());
Assert.assertTrue(20 == autoShape.getTextFrame().getTextFrameFormat().getColumnSpacing());
} finally {
if (test1 != null) test1.dispose();
}
format.setColumnCount(3);
format.setColumnSpacing(15);
pres.save(outPptxFileName, SaveFormat.Pptx);
Presentation test2 = new Presentation(outPptxFileName);
try {
IAutoShape autoShape = ((AutoShape)test2.getSlides().get_Item(0).getShapes().get_Item(0));
Assert.assertTrue(3 == autoShape.getTextFrame().getTextFrameFormat().getColumnCount());
Assert.assertTrue(15 == autoShape.getTextFrame().getTextFrameFormat().getColumnSpacing());
} finally {
if (test2 != null) test2.dispose();
}
} finally {
if (pres != null) pres.dispose();
}
更新文字
Aspose.Slides 允許您變更或更新文字方塊中的文字,或簡報中所有文字。
以下 Java 程式碼示範將簡報中所有文字更新或變更的操作:
Presentation pres = new Presentation("text.pptx");
try {
for (ISlide slide : pres.getSlides())
{
for (IShape shape : slide.getShapes())
{
if (shape instanceof IAutoShape) // 檢查形狀是否支援文字框 (IAutoShape)。
{
IAutoShape autoShape = (IAutoShape)shape;
for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) // 遍歷文字框中的段落
{
for (IPortion portion : paragraph.getPortions()) // 遍歷段落中的每個文字片段
{
portion.setText(portion.getText().replace("years", "months")); // 變更文字
portion.getPortionFormat().setFontBold(NullableBool.True); // 變更格式
}
}
}
}
}
// 儲存已修改的簡報
pres.save("text-changed.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
加入含超連結的文字方塊
您可以在文字方塊內插入連結。當使用者點擊文字方塊時,會導向開啟該連結。
若要加入包含連結的文字方塊,請依照以下步驟操作:
- 建立
Presentation類別的實例。 - 取得新建立的簡報中第一張投影片的參考。
- 在投影片的指定位置新增
AutoShape物件,將ShapeType設為Rectangle,並取得新加入的 AutoShape 物件的參考。 - 為
AutoShape物件新增TextFrame,其預設文字為 Aspose TextBox。 - 建立
IHyperlinkManager類別的實例。 - 將
IHyperlinkManager物件指派給與TextFrame中您選取的部分相關的 HyperlinkClick 屬性。 - 最後,使用
Presentation物件寫入 PPTX 檔案。
以下 Java 程式碼—上述步驟的實作範例—示範如何在投影片上加入含超連結的文字方塊:
// 實例化一個代表 PPTX 的 Presentation 類別
Presentation pres = new Presentation();
try {
// 取得簡報中的第一張投影片
ISlide slide = pres.getSlides().get_Item(0);
// 新增一個類型設定為 Rectangle 的 AutoShape 物件
IShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 150, 150, 150, 50);
// 將形狀轉型為 AutoShape
IAutoShape pptxAutoShape = (IAutoShape)shape;
// 存取與 AutoShape 相關聯的 ITextFrame 屬性
pptxAutoShape.addTextFrame("");
ITextFrame textFrame = pptxAutoShape.getTextFrame();
// 向框架加入一些文字
textFrame.getParagraphs().get_Item(0).getPortions().get_Item(0).setText("Aspose.Slides");
// 為文字片段設定超連結
IHyperlinkManager hyperlinkManager = textFrame.getParagraphs().get_Item(0).getPortions().get_Item(0).
getPortionFormat().getHyperlinkManager();
hyperlinkManager.setExternalHyperlinkClick("http://www.aspose.com");
// 儲存 PPTX 簡報
pres.save("hLink_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
常見問題
在使用母片時,文字方塊與文字佔位符有何差異?
佔位符 會從 母片 繼承樣式/位置,且可在 版面配置 上覆寫;相較之下,普通的文字方塊是特定投影片上的獨立物件,切換版面配置時不會改變。
如何在整份簡報中批次取代文字,同時不影響圖表、表格與 SmartArt 內的文字?
將遍歷限制在具有文字框的自動圖形,並排除嵌入式物件(圖表、表格、SmartArt),可以分別遍歷其集合或直接跳過這些物件類型。