จัดการกล่องข้อความในงานนำเสนอบน Android
บทนำ
ข้อความบนสไลด์ปกติจะอยู่ในกล่องข้อความหรือรูปทรง ดังนั้นเพื่อเพิ่มข้อความในสไลด์ คุณต้องเพิ่มกล่องข้อความแล้วใส่ข้อความบางส่วนลงในกล่องข้อความนั้น Aspose.Slides for Android via Java มีอินเทอร์เฟซ IAutoShape ที่ช่วยให้คุณเพิ่มรูปทรงที่มีข้อความได้
Info
Aspose.Slides ยังมีอินเทอร์เฟซ IShape ที่ช่วยให้คุณเพิ่มรูปทรงลงบนสไลด์ อย่างไรก็ตาม รูปทรงทั้งหมดที่เพิ่มผ่านอินเทอร์เฟซIShape ไม่สามารถเก็บข้อความได้ แต่รูปทรงที่เพิ่มผ่านอินเทอร์เฟซ IAutoShape อาจมีข้อความได้
Note
ดังนั้นเมื่อทำงานกับรูปทรงที่คุณต้องการเพิ่มข้อความ คุณอาจต้องตรวจสอบและยืนยันว่ามันถูกแคสผ่านอินเทอร์เฟซIAutoShape เท่านั้นจึงจะสามารถทำงานกับ TextFrame ซึ่งเป็นคุณสมบัติของ IAutoShape ได้ ดูส่วน Update Text ในหน้านี้
สร้างกล่องข้อความบนสไลด์
เพื่อสร้างกล่องข้อความบนสไลด์ ให้ทำตามขั้นตอนต่อไปนี้:
- สร้างอินสแตนซ์ของคลาส Presentation
- รับอ้างอิงของสไลด์แรกในงานพรีเซนเทชันที่เพิ่งสร้าง
- เพิ่มอ็อบเจกต์ IAutoShape ที่มี ShapeType ตั้งค่าเป็น
Rectangleที่ตำแหน่งที่กำหนดบนสไลด์ และรับอ้างอิงของอ็อบเจกต์IAutoShapeที่เพิ่งเพิ่ม - เพิ่มคุณสมบัติ
TextFrameให้กับอ็อบเจกต์IAutoShapeที่จะบรรจุข้อความ ในตัวอย่างด้านล่าง เราได้เพิ่มข้อความนี้: Aspose TextBox - สุดท้าย ให้บันทึกไฟล์ PPTX ผ่านอ็อบเจกต์
Presentation
โค้ด Java นี้—การทำตามขั้นตอนข้างต้น—แสดงวิธีการเพิ่มข้อความในสไลด์:
// สร้างอินสแตนซ์ Presentation
Presentation pres = new Presentation();
try {
// ดึงสไลด์แรกในงานนำเสนอ
ISlide sld = pres.getSlides().get_Item(0);
// เพิ่ม AutoShape ที่มีประเภทเป็น Rectangle
IAutoShape ashp = sld.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);
// เพิ่ม TextFrame ให้กับ Rectangle
ashp.addTextFrame(" ");
// เข้าถึง TextFrame
ITextFrame txtFrame = ashp.getTextFrame();
// สร้างอ็อบเจกต์ Paragraph สำหรับ TextFrame
IParagraph para = txtFrame.getParagraphs().get_Item(0);
// สร้างอ็อบเจกต์ Portion สำหรับ Paragraph
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 มีเมธอด isTextBox จากอินเทอร์เฟซ IAutoShape ที่ช่วยให้คุณตรวจสอบรูปทรงและระบุว่ากล่องข้อความหรือไม่

โค้ด 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();
}
โปรดทราบว่าหากคุณเพียงแค่เพิ่ม autoshape ด้วยเมธอด addAutoShape จากอินเทอร์เฟซ IShapeCollection เมธอด isTextBox ของ autoshape จะคืนค่า false อย่างไรก็ตาม เมื่อคุณเพิ่มข้อความให้กับ autoshape ด้วยเมธอด 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);
// เพิ่ม AutoShape โดยตั้งค่าชนิดเป็น Rectangle
IAutoShape aShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 300, 300);
// เพิ่ม TextFrame ให้กับ Rectangle
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();
}
เพิ่มคอลัมน์ใน Text Frame
Aspose.Slides for Android via Java มีคุณสมบัติ ColumnCount (จากอินเทอร์เฟซ ITextFrameFormat) ที่ช่วยให้คุณเพิ่มคอลัมน์ใน Text Frame ผ่านคุณสมบัตินี้ คุณสามารถระบุจำนวนคอลัมน์ที่ต้องการใน Text Frame ได้
โค้ด Java นี้แสดงวิธีเพิ่มคอลัมน์ภายใน Text Frame:
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 ในย่อหน้า
{
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 ที่เพิ่งเพิ่ม - เพิ่ม
TextFrameให้กับอ็อบเจกต์AutoShapeที่มี Aspose TextBox เป็นข้อความเริ่มต้น - สร้างอินสแตนซ์ของคลาส
IHyperlinkManager - กําหนดอ็อบเจกต์
IHyperlinkManagerให้กับคุณสมบัติ HyperlinkClick ที่เชื่อมโยงกับส่วนที่คุณต้องการของTextFrame - สุดท้าย ให้บันทึกไฟล์ PPTX ผ่านอ็อบเจกต์
Presentation
โค้ด Java นี้—การทำตามขั้นตอนข้างต้น—แสดงวิธีเพิ่มกล่องข้อความที่มีลิงก์ไปยังสไลด์:
// สร้างอินสแตนซ์ของคลาส Presentation ที่แสดงถึงไฟล์ PPTX
Presentation pres = new Presentation();
try {
// ดึงสไลด์แรกในงานนำเสนอ
ISlide slide = pres.getSlides().get_Item(0);
// เพิ่มอ็อบเจกต์ AutoShape โดยตั้งค่าชนิดเป็น Rectangle
IShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 150, 150, 150, 50);
// แคสต์รูปทรงเป็น AutoShape
IAutoShape pptxAutoShape = (IAutoShape)shape;
// เข้าถึงคุณสมบัติ ITextFrame ที่เชื่อมโยงกับ AutoShape
pptxAutoShape.addTextFrame("");
ITextFrame textFrame = pptxAutoShape.getTextFrame();
// เพิ่มข้อความบางส่วนลงในเฟรม
textFrame.getParagraphs().get_Item(0).getPortions().get_Item(0).setText("Aspose.Slides");
// ตั้งค่าลิงก์สำหรับข้อความ portion
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();
}
คำถามที่พบบ่อย
ความแตกต่างระหว่างกล่องข้อความและตัวเล่นข้อความเมื่อทำงานกับมาสเตอร์สไลด์คืออะไร?
A placeholder สืบทอดสไตล์/ตำแหน่งจาก master และสามารถถูกแทนที่ได้บน layouts ส่วนกล่องข้อความทั่วไปเป็นอ็อบเจกต์อิสระบนสไลด์เฉพาะและจะไม่เปลี่ยนแปลงเมื่อตามสลับเลย์เอาต์
ฉันจะทำการแทนที่ข้อความเป็นกลุ่มทั่วทั้งพรีเซนเทชันโดยไม่กระทบข้อความภายในแผนภูมิ ตาราง และ SmartArt ได้อย่างไร?
จำกัดการวนรอบของคุณให้กับ auto‑shapes ที่มี text frame เท่านั้นและไม่รวมวัตถุที่ฝังอยู่ (charts, tables, SmartArt) โดยการเดินทางผ่านคอลเลกชันของพวกมันแยกกันหรือข้ามประเภทวัตถุนั้น