จัดการกล่องข้อความในงานนำเสนอด้วย .NET
บทนำ
ข้อความบนสไลด์มักอยู่ในกล่องข้อความหรือรูปร่าง ดังนั้นเพื่อเพิ่มข้อความไปยังสไลด์ คุณต้องเพิ่มกล่องข้อความก่อนแล้วจึงใส่ข้อความลงในกล่องข้อความ
เพื่อให้คุณสามารถเพิ่มรูปร่างที่สามารถบรรจุข้อความได้ Aspose.Slides for .NET มีอินเทอร์เฟซ IAutoShape ให้
Note
Aspose.Slides ยังให้บริการอินเทอร์เฟซ IShape เพื่อให้คุณเพิ่มรูปร่างไปยังสไลด์ได้ อย่างไรก็ตาม ไม่ใช่ทุกรูปร่างที่เพิ่มผ่านอินเทอร์เฟซ IShape สามารถบรรจุข้อความได้ รูปร่างที่เพิ่มผ่านอินเทอร์เฟซ IAutoShape มักจะมีข้อความ
ดังนั้นเมื่อทำงานกับรูปร่างที่มีอยู่แล้วซึ่งคุณต้องการใส่ข้อความ คุณอาจต้องตรวจสอบและยืนยันว่ามันถูกแคสต์ผ่านอินเทอร์เฟซ IAutoShape เท่านั้นจึงจะสามารถทำงานกับ TextFrame ซึ่งเป็นคุณสมบัติของ IAutoShape ได้ ดูส่วน Update Text ในหน้านี้
สร้างกล่องข้อความบนสไลด์
- สร้างอินสแตนซ์ของคลาส Presentation
- ดึงอ้างอิงของสไลด์แรกผ่านดัชนีของมัน
- เพิ่มอ็อบเจ็กต์ IAutoShape ที่มี ShapeType ตั้งค่าเป็น
Rectangleในตำแหน่งที่ระบุบนสไลด์และรับอ้างอิงของอ็อบเจ็กต์IAutoShapeที่เพิ่มใหม่ - เพิ่มคุณสมบัติ
TextFrameให้กับอ็อบเจ็กต์IAutoShapeที่จะบรรจุข้อความ ในตัวอย่างด้านล่าง เราได้เพิ่มข้อความนี้: Aspose TextBox - สุดท้าย เขียนไฟล์ PPTX ผ่านอ็อบเจ็กต์
Presentation
โค้ด C# นี้—การนำขั้นตอนข้างต้นไปใช้งาน—แสดงวิธีการเพิ่มข้อความไปยังสไลด์:
// สร้างอินสแตนซ์ PresentationEx
using (Presentation pres = new Presentation())
{
// ดึงสไลด์แรกในงานนำเสนอ
ISlide sld = pres.Slides[0];
// เพิ่ม AutoShape โดยตั้งค่าประเภทเป็น Rectangle
IAutoShape ashp = sld.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);
// เพิ่ม TextFrame ไปยัง Rectangle
ashp.AddTextFrame(" ");
// เข้าถึง text frame
ITextFrame txtFrame = ashp.TextFrame;
// สร้างอ็อบเจ็กต์ Paragraph สำหรับ text frame
IParagraph para = txtFrame.Paragraphs[0];
// สร้างอ็อบเจ็กต์ Portion สำหรับ paragraph
IPortion portion = para.Portions[0];
// ตั้งค่าข้อความ
portion.Text = "Aspose TextBox";
// บันทึกงานนำเสนอไปยังดิสก์
pres.Save("TextBox_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
ตรวจสอบรูปทรงกล่องข้อความ
Aspose.Slides มีคุณสมบัติ IsTextBox จากอินเทอร์เฟซ IAutoShape ให้คุณตรวจสอบรูปร่างและระบุว่ามันเป็นกล่องข้อความหรือไม่

โค้ด C# นี้แสดงวิธีการตรวจสอบว่ารูปร่างถูกสร้างเป็นกล่องข้อความหรือไม่:
using (Presentation presentation = new Presentation("sample.pptx"))
{
Aspose.Slides.LowCode.ForEach.Shape(presentation, (shape, slide, index) =>
{
if (shape is IAutoShape autoShape)
{
Console.WriteLine(autoShape.IsTextBox ? "shape is a text box" : "shape is not a text box");
}
});
}
โปรดทราบว่าหากคุณเพียงเพิ่มออโต้เชปโดยใช้เมธอด AddAutoShape จากอินเทอร์เฟซ IShapeCollection คุณสมบัติ IsTextBox ของออโต้เชปจะคืนค่า false อย่างไรก็ตาม หลังจากที่คุณเพิ่มข้อความลงในออโต้เชปโดยใช้เมธอด AddTextFrame หรือคุณสมบัติ Text คุณสมบัติ IsTextBox จะคืนค่า true
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
IAutoShape shape1 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 100, 40);
// shape1.IsTextBox เป็น false
shape1.AddTextFrame("shape 1");
// shape1.IsTextBox เป็น true
IAutoShape shape2 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 110, 100, 40);
// shape2.IsTextBox เป็น false
shape2.TextFrame.Text = "shape 2";
// shape2.IsTextBox เป็น true
IAutoShape shape3 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 210, 100, 40);
// shape3.IsTextBox เป็น false
shape3.AddTextFrame("");
// shape3.IsTextBox เป็น false
IAutoShape shape4 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 310, 100, 40);
// shape4.IsTextBox เป็น false
shape4.TextFrame.Text = "";
// shape4.IsTextBox เป็น false
}
เพิ่มคอลัมน์ให้กับกล่องข้อความ
Aspose.Slides มีคุณสมบัติ ColumnCount และ ColumnSpacing (จากอินเทอร์เฟซ ITextFrameFormat และคลาส TextFrameFormat) เพื่อให้คุณสามารถเพิ่มคอลัมน์ให้กับกล่องข้อความได้ คุณสามารถระบุจำนวนคอลัมน์ในกล่องข้อความและกำหนดระยะห่างเป็นจุดระหว่างคอลัมน์
โค้ดนี้ใน C# แสดงการดำเนินการที่อธิบายไว้:
using (Presentation presentation = new Presentation())
{
// ดึงสไลด์แรกในงานนำเสนอ
ISlide slide = presentation.Slides[0];
// เพิ่ม AutoShape โดยตั้งค่าประเภทเป็น Rectangle
IAutoShape aShape = slide.Shapes.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.TextFrame.TextFrameFormat;
// ระบุจำนวนคอลัมน์ใน TextFrame
format.ColumnCount = 3;
// ระบุระยะห่างระหว่างคอลัมน์
format.ColumnSpacing = 10;
// บันทึกงานนำเสนอ
presentation.Save("ColumnCount.pptx", SaveFormat.Pptx);
}
เพิ่มคอลัมน์ให้กับ Text Frame
Aspose.Slides for .NET มีคุณสมบัติ ColumnCount (จากอินเทอร์เฟซ ITextFrameFormat) ที่ช่วยให้คุณเพิ่มคอลัมน์ใน Text Frame ได้ ผ่านคุณสมบัตินี้คุณสามารถระบุจำนวนคอลัมน์ที่ต้องการใน Text Frame
โค้ด C# นี้แสดงวิธีการเพิ่มคอลัมน์ภายใน Text Frame:
string outPptxFileName = "ColumnsTest.pptx";
using (Presentation pres = new Presentation())
{
IAutoShape shape1 = pres.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 100, 100, 300, 300);
TextFrameFormat format = (TextFrameFormat)shape1.TextFrame.TextFrameFormat;
format.ColumnCount = 2;
shape1.TextFrame.Text = "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);
using (Presentation test = new Presentation(outPptxFileName))
{
Debug.Assert(2 == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnCount);
Debug.Assert(double.NaN == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnSpacing);
}
format.ColumnSpacing = 20;
pres.Save(outPptxFileName, SaveFormat.Pptx);
using (Presentation test = new Presentation(outPptxFileName))
{
Debug.Assert(2 == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnCount);
Debug.Assert(20 == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnSpacing);
}
format.ColumnCount = 3;
format.ColumnSpacing = 15;
pres.Save(outPptxFileName, SaveFormat.Pptx);
using (Presentation test = new Presentation(outPptxFileName))
{
Debug.Assert(3 == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnCount);
Debug.Assert(15 == ((AutoShape)test.Slides[0].Shapes[0]).TextFrame.TextFrameFormat.ColumnSpacing);
}
}
อัปเดตข้อความ
Aspose.Slides ให้คุณเปลี่ยนแปลงหรืออัปเดตข้อความที่อยู่ในกล่องข้อความหรือข้อความทั้งหมดที่อยู่ในงานนำเสนอ
โค้ด C# นี้แสดงการดำเนินการที่อัปเดตหรือเปลี่ยนแปลงข้อความทั้งหมดในงานนำเสนอ:
using(Presentation pres = new Presentation("text.pptx"))
{
foreach (ISlide slide in pres.Slides)
{
foreach (IShape shape in slide.Shapes)
{
if (shape is IAutoShape autoShape) //ตรวจสอบว่ารูปร่างรองรับ text frame (IAutoShape) หรือไม่.
{
foreach (IParagraph paragraph in autoShape.TextFrame.Paragraphs) //วนรอบผ่านย่อหน้าต่างใน text frame
{
foreach (IPortion portion in paragraph.Portions) //วนรอบผ่านแต่ละ portion ในย่อหน้า
{
portion.Text = portion.Text.Replace("years", "months"); //เปลี่ยนข้อความ
portion.PortionFormat.FontBold = NullableBool.True; //เปลี่ยนการจัดรูปแบบ
}
}
}
}
}
//บันทึกงานนำเสนอที่แก้ไขแล้ว
pres.Save("text-changed.pptx", SaveFormat.Pptx);
}
เพิ่มกล่องข้อความพร้อมลิงก์
คุณสามารถแทรกลิงก์ภายในกล่องข้อความได้ เมื่อคลิกที่กล่องข้อความ ผู้ใช้จะถูกนำไปเปิดลิงก์นั้น
- สร้างอินสแตนซ์ของคลาส
Presentation - ดึงอ้างอิงของสไลด์แรกผ่านดัชนีของมัน
- เพิ่มอ็อบเจ็กต์
AutoShapeที่มีShapeTypeตั้งค่าเป็นRectangleในตำแหน่งที่ระบุบนสไลด์และรับอ้างอิงของอ็อบเจ็กต์ AutoShape ที่เพิ่มใหม่ - เพิ่ม
TextFrameให้กับอ็อบเจ็กต์AutoShapeที่มีข้อความเริ่มต้นเป็น Aspose TextBox - สร้างอินสแตนซ์ของคลาส
IHyperlinkManager - กำหนดอ็อบเจ็กต์
IHyperlinkManagerให้กับคุณสมบัติ HyperlinkClick ที่เชื่อมกับส่วนที่คุณต้องการของTextFrame - สุดท้าย เขียนไฟล์ PPTX ผ่านอ็อบเจ็กต์
Presentation
โค้ด C# นี้—การนำขั้นตอนข้างต้นไปใช้งาน—แสดงวิธีการเพิ่มกล่องข้อความพร้อมลิงก์ไปยังสไลด์:
// สร้างอินสแตนซ์ของคลาส Presentation ที่เป็นตัวแทนของไฟล์ PPTX
Presentation pptxPresentation = new Presentation();
// ดึงสไลด์แรกในงานนำเสนอ
ISlide slide = pptxPresentation.Slides[0];
// เพิ่มอ็อบเจ็กต์ AutoShape โดยตั้งค่าประเภทเป็น Rectangle
IShape pptxShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 150, 150, 150, 50);
// แคสต์รูปร่างเป็น AutoShape
IAutoShape pptxAutoShape = (IAutoShape)pptxShape;
// เข้าถึงคุณสมบัติ ITextFrame ที่เชื่อมกับ AutoShape
pptxAutoShape.AddTextFrame("");
ITextFrame ITextFrame = pptxAutoShape.TextFrame;
// เพิ่มข้อความบางส่วนลงในเฟรม
ITextFrame.Paragraphs[0].Portions[0].Text = "Aspose.Slides";
// ตั้งค่า Hyperlink ให้กับข้อความ portion
IHyperlinkManager HypMan = ITextFrame.Paragraphs[0].Portions[0].PortionFormat.HyperlinkManager;
HypMan.SetExternalHyperlinkClick("http://www.aspose.com");
// บันทึกงานนำเสนอ PPTX
pptxPresentation.Save("hLinkPPTX_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
คำถามที่พบบ่อย
ความแตกต่างระหว่างกล่องข้อความและตัวรอข้อความเมื่อทำงานกับมาสเตอร์สไลด์คืออะไร?
placeholder สืบทอดสไตล์/ตำแหน่งจาก master และสามารถถูกเขียนทับได้บน layouts ในขณะที่กล่องข้อความทั่วไปเป็นอ็อบเจ็กต์อิสระบนสไลด์เฉพาะและจะไม่เปลี่ยนแปลงเมื่อคุณสลับเลย์เอาต์
ฉันจะทำการแทนที่ข้อความจำนวนมากในงานนำเสนอโดยไม่กระทบข้อความภายในแผนภูมิ ตาราง และ SmartArt อย่างไร?
จำกัดการวนซ้ำของคุณให้กับออโต้เชปที่มี Text Frame เท่านั้นและยกเว้นอ็อบเจ็กต์ที่ฝังอยู่ (charts, tables, SmartArt) โดยทำการเดินทางผ่านคอลเลกชันของพวกมันแยกกันหรือข้ามประเภทอ็อบเจ็กต์ดังกล่าว