จัดการกล่องข้อความในงานนำเสนอด้วย PHP

แนะนำ

ข้อความบนสไลด์มักจะอยู่ในกล่องข้อความหรือรูปทรง ดังนั้น เพื่อเพิ่มข้อความลงในสไลด์ คุณต้องเพิ่มกล่องข้อความก่อน แล้วจึงใส่ข้อความลงในกล่องนั้น Aspose.Slides for PHP via Java มีคลาส AutoShape ที่ช่วยให้คุณเพิ่มรูปทรงที่มีข้อความได้

สร้างกล่องข้อความบนสไลด์

เพื่อสร้างกล่องข้อความบนสไลด์ ทำตามขั้นตอนต่อไปนี้:

  1. สร้างอินสแตนซ์ของคลาส Presentation
  2. รับอ้างอิงของสไลด์แรกในพรีเซนเทชันที่สร้างใหม่
  3. เพิ่มอ็อบเจ็กต์ AutoShape โดยกำหนดประเภทรูปทรงเป็น Rectangle ที่ตำแหน่งที่กำหนดบนสไลด์ และรับอ้างอิงของอ็อบเจ็กต์ AutoShape ที่เพิ่มใหม่
  4. เพิ่ม TextFrame ให้กับอ็อบเจ็กต์ AutoShape เพื่อบรรจุข้อความ ในตัวอย่างด้านล่าง เราเพิ่มข้อความนี้: Aspose TextBox
  5. สุดท้าย เขียนไฟล์ PPTX ผ่านอ็อบเจ็กต์ Presentation

โค้ด PHP นี้—เป็นการดำเนินการตามขั้นตอนข้างต้น—จะแสดงวิธีเพิ่มข้อความลงในสไลด์:

  # สร้างอินสแตนซ์ Presentation
  $pres = new Presentation();
  try {
    # รับสไลด์แรกในพรีเซนเทชัน
    $sld = $pres->getSlides()->get_Item(0);
    # เพิ่ม AutoShape โดยกำหนดประเภทเป็น Rectangle
    $ashp = $sld->getShapes()->addAutoShape(ShapeType::Rectangle, 150, 75, 150, 50);
    # เพิ่ม TextFrame ให้กับ Rectangle
    $ashp->addTextFrame(" ");
    # เข้าถึง TextFrame
    $txtFrame = $ashp->getTextFrame();
    # สร้างอ็อบเจ็กต์ Paragraph สำหรับ TextFrame
    $para = $txtFrame->getParagraphs()->get_Item(0);
    # สร้างอ็อบเจ็กต์ Portion สำหรับ Paragraph
    $portion = $para->getPortions()->get_Item(0);
    # ตั้งค่าข้อความ
    $portion->setText("Aspose TextBox");
    # บันทึกพรีเซนเทชันลงดิสก์
    $pres->save("TextBox_out.pptx", SaveFormat::Pptx);
  } finally {
    if (!java_is_null($pres)) {
      $pres->dispose();
    }
  }

ตรวจสอบว่ารูปทรงเป็นกล่องข้อความหรือไม่

Aspose.Slides มีเมธอด isTextBox จากคลาส AutoShape เพื่อให้คุณตรวจสอบรูปทรงและระบุว่ามันเป็นกล่องข้อความหรือไม่

Text box and shape

โค้ด PHP นี้แสดงวิธีตรวจสอบว่ารูปทรงถูกสร้างเป็นกล่องข้อความหรือไม่:

class ShapeCallback {
    function invoke($shape, $slide, $index) {
        if (java_instanceof($shape, new JavaClass("com.aspose.slides.AutoShape"))) {
            $autoShape = $shape;
            echo(java_is_true($autoShape->isTextBox()) ? "shape is a text box" : "shape is not a text box");
        }
    }
}

$presentation = new Presentation("sample.pptx");
try {
    $forEachShapeCallback = java_closure(new ShapeCallback(), null, java("com.aspose.slides.ForEachSlideCallback"));
    ForEach::shape($presentation, $forEachShapeCallback);
} finally {
    $presentation->dispose();
}

โปรดทราบว่า หากคุณเพียงเพิ่ม AutoShape ผ่านเมธอด addAutoShape ของคลาส ShapeCollection เมธอด isTextBox ของ AutoShape จะคืนค่า false อย่างไรก็ตาม หลังจากคุณเพิ่มข้อความให้กับ AutoShape ผ่านเมธอด addTextFrame หรือเมธอด setText คุณสมบัติ isTextBox จะคืนค่า true

$presentation = new Presentation();
$slide = $presentation->getSlides()->get_Item(0);

$shape1 = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 10, 10, 100, 40);
// shape1->isTextBox() คืนค่า false
$shape1->addTextFrame("shape 1");
// shape1->isTextBox() คืนค่า true

$shape2 = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 10, 110, 100, 40);
// shape2->isTextBox() คืนค่า false
$shape2->getTextFrame()->setText("shape 2");
// shape2->isTextBox() คืนค่า true

$shape3 = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 10, 210, 100, 40);
// shape3->isTextBox() คืนค่า false
$shape3->addTextFrame("");
// shape3->isTextBox() คืนค่า false

$shape4 = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 10, 310, 100, 40);
// shape4->isTextBox() คืนค่า false
$shape4->getTextFrame()->setText("");
// shape4->isTextBox() คืนค่า false

เพิ่มคอลัมน์ให้กับกล่องข้อความ

Aspose.Slides มีเมธอด setColumnCount และ setColumnSpacing จากคลาส TextFrameFormat ซึ่งช่วยให้คุณเพิ่มคอลัมน์ให้กับกล่องข้อความ คุณสามารถกำหนดจำนวนคอลัมน์และระยะห่างระหว่างคอลัมน์เป็นจุดได้

โค้ดต่อไปนี้แสดงการดำเนินการตามที่อธิบายไว้:

  $pres = new Presentation();
  try {
    # รับสไลด์แรกในพรีเซนเทชัน
    $slide = $pres->getSlides()->get_Item(0);
    # เพิ่ม AutoShape โดยกำหนดประเภทเป็น Rectangle
    $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
    $format = $aShape->getTextFrame()->getTextFrameFormat();
    # กำหนดจำนวนคอลัมน์ใน TextFrame
    $format->setColumnCount(3);
    # กำหนดระยะห่างระหว่างคอลัมน์
    $format->setColumnSpacing(10);
    # บันทึกพรีเซนเทชัน
    $pres->save("ColumnCount.pptx", SaveFormat::Pptx);
  } finally {
    if (!java_is_null($pres)) {
      $pres->dispose();
    }
  }

เพิ่มคอลัมน์ให้กับ Text Frame

Aspose.Slides for PHP via Java มีเมธอด setColumnCount จากคลาส TextFrameFormat ที่ช่วยให้คุณเพิ่มคอลัมน์ใน Text Frame ผ่านคุณสมบัตินี้ คุณสามารถกำหนดจำนวนคอลัมน์ที่ต้องการใน Text Frame ได้

โค้ด PHP นี้แสดงวิธีการเพิ่มคอลัมน์ภายใน Text Frame:

  $outPptxFileName = "ColumnsTest.pptx";
  $pres = new Presentation();
  try {
    $shape1 = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, 300, 300);
    $format = $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);
    $test = new Presentation($outPptxFileName);
    try {
      $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 (!java_is_null($test)) {
        $test->dispose();
      }
    }
    $format->setColumnSpacing(20);
    $pres->save($outPptxFileName, SaveFormat::Pptx);
    $test1 = new Presentation($outPptxFileName);
    try {
      $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 (!java_is_null($test1)) {
        $test1->dispose();
      }
    }
    $format->setColumnCount(3);
    $format->setColumnSpacing(15);
    $pres->save($outPptxFileName, SaveFormat::Pptx);
    $test2 = new Presentation($outPptxFileName);
    try {
      $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 (!java_is_null($test2)) {
        $test2->dispose();
      }
    }
  } finally {
    if (!java_is_null($pres)) {
      $pres->dispose();
    }
  }

อัปเดตข้อความ

Aspose.Slides อนุญาตให้คุณเปลี่ยนหรืออัปเดตข้อความที่อยู่ในกล่องข้อความหรือข้อความทั้งหมดในพรีเซนเทชัน

โค้ด PHP นี้แสดงตัวอย่างการอัปเดตหรือเปลี่ยนข้อความทั้งหมดในพรีเซนเทชัน:

  $pres = new Presentation("text.pptx");
  try {
    foreach($pres->getSlides() as $slide) {
      foreach($slide->getShapes() as $shape) {
        # ตรวจสอบว่ารูปทรงสนับสนุน TextFrame (IAutoShape) หรือไม่.
        if (java_instanceof($shape, new JavaClass("com.aspose.slides.AutoShape"))) {
          $autoShape = $shape;
          # วนลูปผ่าน Paragraphs ใน TextFrame
          foreach($autoShape->getTextFrame()->getParagraphs() as $paragraph) {
            # วนลูปผ่าน Portion แต่ละส่วนใน Paragraph
            foreach($paragraph->getPortions() as $portion) {
              $portion->setText($portion->getText()->replace("years", "months"));// เปลี่ยนข้อความ

              $portion->getPortionFormat()->setFontBold(NullableBool::True);// เปลี่ยนการจัดรูปแบบ

            }
          }
        }
      }
    }
    # บันทึกงานนำเสนอที่แก้ไขแล้ว
    $pres->save("text-changed.pptx", SaveFormat::Pptx);
  } finally {
    if (!java_is_null($pres)) {
      $pres->dispose();
    }
  }

เพิ่มกล่องข้อความพร้อมไฮเปอร์ลิงก์

คุณสามารถแทรกลิงก์ภายในกล่องข้อความได้ เมื่อคลิกที่กล่องข้อความ ผู้ใช้จะถูกนำไปเปิดลิงก์

ขั้นตอนการเพิ่มกล่องข้อความที่มีลิงก์:

  1. สร้างอินสแตนซ์ของคลาส Presentation
  2. รับอ้างอิงของสไลด์แรกในพรีเซนเทชันที่สร้างใหม่
  3. เพิ่มอ็อบเจ็กต์ AutoShape โดยกำหนด ShapeType เป็น Rectangle ที่ตำแหน่งที่กำหนดบนสไลด์และรับอ้างอิงของอ็อบเจ็กต์ AutoShape ที่เพิ่มใหม่
  4. เพิ่ม TextFrame ให้กับอ็อบเจ็กต์ AutoShape โดยมีข้อความเริ่มต้น Aspose TextBox
  5. สร้างอินสแตนซ์ของคลาส HyperlinkManager
  6. กำหนดไฮเปอร์ลิงก์โดยใช้เมธอด setExternalHyperlinkClick กับส่วนที่คุณต้องการใน TextFrame
  7. สุดท้าย เขียนไฟล์ PPTX ผ่านอ็อบเจ็กต์ Presentation

โค้ด PHP นี้—เป็นการดำเนินการตามขั้นตอนข้างต้น—จะแสดงวิธีเพิ่มกล่องข้อความพร้อมไฮเปอร์ลิงก์ลงในสไลด์:

  # สร้างอินสแตนซ์ของคลาส Presentation ที่เป็นตัวแทนไฟล์ PPTX
  $pres = new Presentation();
  try {
    # รับสไลด์แรกในพรีเซนเทชัน
    $slide = $pres->getSlides()->get_Item(0);
    # เพิ่มอ็อบเจ็กต์ AutoShape โดยกำหนดประเภทเป็น Rectangle
    $shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 150, 150, 150, 50);
    # แคสต์รูปร่างเป็น AutoShape
    $pptxAutoShape = $shape;
    # เข้าถึงคุณสมบัติ ITextFrame ที่เกี่ยวข้องกับ AutoShape
    $pptxAutoShape->addTextFrame("");
    $textFrame = $pptxAutoShape->getTextFrame();
    # เพิ่มข้อความบางส่วนลงในเฟรม
    $textFrame->getParagraphs()->get_Item(0)->getPortions()->get_Item(0)->setText("Aspose.Slides");
    # ตั้งค่า Hyperlink สำหรับข้อความส่วน
    $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 (!java_is_null($pres)) {
      $pres->dispose();
    }
  }

คำถามที่พบบ่อย

ความแตกต่างระหว่างกล่องข้อความกับตัวกรอกรูปแบบข้อความเมื่อทำงานกับสไลด์มาสเตอร์คืออะไร?

placeholder สืบทอดสไตล์/ตำแหน่งจาก master และสามารถถูกเขียนทับใน layouts ได้ ในขณะที่กล่องข้อความทั่วไปเป็นออบเจ็กต์อิสระบนสไลด์เฉพาะและจะไม่เปลี่ยนแปลงเมื่อสลับเลย์เอาต์

ฉันจะทำการแทนที่ข้อความจำนวนมากทั่วทั้งพรีเซนเทชันโดยไม่กระทบข้อความในแผนภูมิ ตาราง และ SmartArt อย่างไร?

จำกัดการวนลูปเฉพาะออโต้-เชปที่มี TextFrame และละเว้นออบเจ็กต์ที่ฝังอยู่ (charts, tables, SmartArt) โดยแยกแยะคอลเลกชันของพวกมันออกจากกันหรือข้ามประเภทออบเจ็กต์เหล่านั้น