PHPでプレゼンテーションのハイパーリンクを管理する
ハイパーリンクは、オブジェクトやデータ、または何かの場所への参照です。これらは PowerPoint プレゼンテーションで一般的なハイパーリンクです:
- テキスト、図形、またはメディア内のウェブサイトへのリンク
- スライドへのリンク
Aspose.Slides for PHP via Java を使用すると、プレゼンテーション内のハイパーリンクに関連するさまざまな操作を実行できます。
URL ハイパーリンクの追加
テキストへの URL ハイパーリンクの追加
この PHP コードは、テキストにウェブサイトのハイパーリンクを追加する方法を示しています:
$presentation = new Presentation();
try {
$shape1 = $presentation->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, 600, 50, false);
$shape1->addTextFrame("Aspose: File Format APIs");
$portionFormat = $shape1->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0)->getPortionFormat();
$portionFormat::setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
$portionFormat::getHyperlinkClick()->setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");
$portionFormat::setFontHeight(32);
$presentation->save("presentation-out.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($presentation)) {
$presentation->dispose();
}
}
図形またはフレームへの URL ハイパーリンクの追加
このサンプルコードは、図形にウェブサイトのハイパーリンクを追加する方法を示しています:
$pres = new Presentation();
try {
$shape = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, 600, 50);
$shape->setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
$shape->getHyperlinkClick()->setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");
$pres->save("pres-out.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
メディアへの URL ハイパーリンクの追加
Aspose.Slides を使用すると、画像、オーディオ、ビデオ ファイルにハイパーリンクを追加できます。
このサンプルコードは、画像にハイパーリンクを追加する方法を示しています:
$pres = new Presentation();
try {
# プレゼンテーションに画像を追加
$picture;
$image = Images->fromFile("image.png");
try {
$picture = $pres->getImages()->addImage($picture);
} finally {
if (!java_is_null($image)) {
$image->dispose();
}
}
# 以前に追加された画像に基づいてスライド1に画像フレームを作成
$pictureFrame = $pres->getSlides()->get_Item(0)->getShapes()->addPictureFrame(ShapeType::Rectangle, 10, 10, 100, 100, $picture);
$pictureFrame->setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
$pictureFrame->getHyperlinkClick()->setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");
$pres->save("pres-out.pptx", SaveFormat::Pptx);
} catch (JavaException $e) {
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
このサンプルコードは、オーディオ ファイルにハイパーリンクを追加する方法を示しています:
$pres = new Presentation();
try {
$Array = new JavaClass("java.lang.reflect.Array");
$Byte = (new JavaClass("java.lang.Byte"))->TYPE;
try {
$dis = new Java("java.io.DataInputStream", new Java("java.io.FileInputStream", "audio.mp3"));
$bytes = $Array->newInstance($Byte, $dis->available());
$dis->readFully($bytes);
} finally {
if (!java_is_null($dis)) $dis->close();
}
$audio = $pres->getAudios()->addAudio($bytes);
$audioFrame = $pres->getSlides()->get_Item(0)->getShapes()->addAudioFrameEmbedded(10, 10, 100, 100, $audio);
$audioFrame->setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
$audioFrame->getHyperlinkClick()->setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");
$pres->save("pres-out.pptx", SaveFormat::Pptx);
} catch (JavaException $e) {
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
このサンプルコードは、ビデオにハイパーリンクを追加する方法を示しています:
$pres = new Presentation();
try {
$Array = new JavaClass("java.lang.reflect.Array");
$Byte = (new JavaClass("java.lang.Byte"))->TYPE;
try {
$dis = new Java("java.io.DataInputStream", new Java("java.io.FileInputStream", "video.avi"));
$bytes = $Array->newInstance($Byte, $dis->available());
$dis->readFully($bytes);
} finally {
if (!java_is_null($dis)) $dis->close();
}
$video = $pres->getVideos()->addVideo($bytes);
$videoFrame = $pres->getSlides()->get_Item(0)->getShapes()->addVideoFrame(10, 10, 100, 100, $video);
$videoFrame->setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
$videoFrame->getHyperlinkClick()->setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");
$pres->save("pres-out.pptx", SaveFormat::Pptx);
} catch (JavaException $e) {
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Tip
ご覧ください 管理 OLE。ハイパーリンクを使用した目次の作成
ハイパーリンクはオブジェクトや場所への参照を追加できるため、目次を作成するために使用できます。
このサンプルコードは、ハイパーリンクを使用した目次の作成方法を示しています:
$pres = new Presentation();
try {
$firstSlide = $pres->getSlides()->get_Item(0);
$secondSlide = $pres->getSlides()->addEmptySlide($firstSlide->getLayoutSlide());
$contentTable = $firstSlide->getShapes()->addAutoShape(ShapeType::Rectangle, 40, 40, 300, 100);
$contentTable->getFillFormat()->setFillType(FillType::NoFill);
$contentTable->getLineFormat()->getFillFormat()->setFillType(FillType::NoFill);
$contentTable->getTextFrame()->getParagraphs()->clear();
$paragraph = new Paragraph();
$paragraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$paragraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLACK);
$paragraph->setText("Title of slide 2 .......... ");
$linkPortion = new Portion();
$linkPortion->setText("Page 2");
$linkPortion->getPortionFormat()->getHyperlinkManager()->setInternalHyperlinkClick($secondSlide);
$paragraph->getPortions()->add($linkPortion);
$contentTable->getTextFrame()->getParagraphs()->add($paragraph);
$pres->save("link_to_slide.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
ハイパーリンクの書式設定
色
IHyperlink インターフェイスの ColorSource プロパティを使用すると、ハイパーリンクの色を設定したり、ハイパーリンクから色情報を取得したりできます。この機能は PowerPoint 2019 で初めて導入されたため、プロパティに関する変更は古いバージョンの PowerPoint には適用されません。
このサンプルコードは、異なる色のハイパーリンクを同じスライドに追加する操作を示しています:
$pres = new Presentation();
try {
$shape1 = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, 450, 50, false);
$shape1->addTextFrame("This is a sample of colored hyperlink.");
$portionFormat = $shape1->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0)->getPortionFormat();
$portionFormat::setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
$portionFormat::getHyperlinkClick()->setColorSource(HyperlinkColorSource->PortionFormat);
$portionFormat::getFillFormat()->setFillType(FillType::Solid);
$portionFormat::getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->RED);
$shape2 = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 200, 450, 50, false);
$shape2->addTextFrame("This is a sample of usual hyperlink.");
$shape2->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0)->getPortionFormat()->setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
$pres->save("presentation-out-hyperlink.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
プレゼンテーションからハイパーリンクを削除する
テキストからハイパーリンクを削除する
この PHP コードは、プレゼンテーション スライドのテキストからハイパーリンクを削除する方法を示しています:
$pres = new Presentation();
try {
$slide = $pres->getSlides()->get_Item(0);
foreach($slide->getShapes() as $shape) {
$autoShape = $shape;
if (!java_is_null($autoShape)) {
foreach($autoShape->getTextFrame()->getParagraphs() as $paragraph) {
foreach($paragraph->getPortions() as $portion) {
$portion->getPortionFormat()->getHyperlinkManager()->removeHyperlinkClick();
}
}
}
}
$pres->save("pres-removed-hyperlinks.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
図形またはフレームからハイパーリンクを削除する
この PHP コードは、プレゼンテーション スライドの図形からハイパーリンクを削除する方法を示しています:
$pres = new Presentation();
try {
$slide = $pres->getSlides()->get_Item(0);
foreach($slide->getShapes() as $shape) {
$shape->getHyperlinkManager()->removeHyperlinkClick();
}
$pres->save("pres-removed-hyperlinks.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
可変ハイパーリンク
Hyperlink クラスは可変です。このクラスを使用すると、次のプロパティの値を変更できます:
- IHyperlink.setTargetFrame(String value)
- IHyperlink.setTooltip(String value)
- IHyperlink.setHistory(boolean value)
- IHyperlink.setHighlightClick(boolean value)
- IHyperlink.setStopSoundOnClick(boolean value)
このコード スニペットは、スライドにハイパーリンクを追加し、後でツールチップを編集する方法を示しています:
$pres = new Presentation();
try {
$shape1 = $pres->getSlides()->get_Item(0)->getShapes()->addAutoShape(ShapeType::Rectangle, 100, 100, 600, 50, false);
$shape1->addTextFrame("Aspose: File Format APIs");
$portionFormat = $shape1->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0)->getPortionFormat();
$portionFormat::setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
$portionFormat::getHyperlinkClick()->setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");
$portionFormat::setFontHeight(32);
$pres->save("presentation-out.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
IHyperlinkQueries でサポートされているプロパティ
ハイパーリンクが定義されているプレゼンテーション、スライド、またはテキストから IHyperlinkQueries にアクセスできます。
- IPresentation.getHyperlinkQueries()
- IBaseSlide.getHyperlinkQueries()
- ITextFrame.getHyperlinkQueries()
IHyperlinkQueries クラスは次のメソッドとプロパティをサポートしています:
- IHyperlinkQueries.getHyperlinkClicks()
- IHyperlinkQueries.getHyperlinkMouseOvers()
- IHyperlinkQueries.getAnyHyperlinks()
- IHyperlinkQueries.removeAllHyperlinks()
よくある質問
スライドだけでなく「セクション」やセクションの最初のスライドへ内部ナビゲーションを作成するにはどうすればよいですか?
PowerPoint のセクションはスライドのグループ化です。ナビゲーションは技術的に特定のスライドを対象とします。セクションへ「移動」するには、通常そのセクションの最初のスライドにリンクします。
マスタースライド要素にハイパーリンクを付与してすべてのスライドで機能させることはできますか?
はい。マスタースライドやレイアウト要素はハイパーリンクをサポートします。そのリンクは子スライド上に表示され、スライドショー中にクリック可能です。
ハイパーリンクは PDF、HTML、画像、またはビデオにエクスポートしたときに保持されますか?
PDF と HTML では、リンクは通常保持されます。画像 PNG やビデオ MP4 へのエクスポートでは、ラスターフレームやビデオはハイパーリンクをサポートしないため、クリック可能性は引き継がれません。