Javaでプレゼンテーションのハイパーリンクを管理する
ハイパーリンクは、オブジェクトやデータ、または何かの場所への参照です。これらは PowerPoint プレゼンテーションで一般的に使用されるハイパーリンクです:
- テキスト、図形、またはメディア内のウェブサイトへのリンク
- スライドへのリンク
Aspose.Slides for Java を使用すると、プレゼンテーション内のハイパーリンクに関するさまざまな操作を実行できます。
URL ハイパーリンクの追加
テキストへの URL ハイパーリンクの追加
この Java コードは、テキストにウェブサイトへのハイパーリンクを追加する方法を示しています。
Presentation presentation = new Presentation();
try {
IAutoShape shape1 = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
shape1.addTextFrame("Aspose: File Format APIs");
IPortionFormat 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 (presentation != null) presentation.dispose();
}
図形またはフレームへの URL ハイパーリンクの追加
この Java のサンプルコードは、図形にウェブサイトへのハイパーリンクを追加する方法を示しています。
Presentation pres = new Presentation();
try {
IShape 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 (pres != null) pres.dispose();
}
メディアへの URL ハイパーリンクの追加
Aspose.Slides を使用すると、画像、音声、ビデオ ファイルにハイパーリンクを追加できます。
このサンプルコードは、画像にハイパーリンクを追加する方法を示しています。
Presentation pres = new Presentation();
try {
// プレゼンテーションに画像を追加します
IPPImage picture;
IImage image = Images.fromFile("image.png");
try {
picture = pres.getImages().addImage(picture);
} finally {
if (image != null) image.dispose();
}
// 以前に追加した画像を基にスライド1に画像フレームを作成します
IPictureFrame 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(IOException e) {
} finally {
if (pres != null) pres.dispose();
}
このサンプルコードは、音声ファイルにハイパーリンクを追加する方法を示しています。
Presentation pres = new Presentation();
try {
IAudio audio = pres.getAudios().addAudio(Files.readAllBytes(Paths.get("audio.mp3")));
IAudioFrame 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(IOException e) {
} finally {
if (pres != null) pres.dispose();
}
このサンプルコードは、ビデオにハイパーリンクを追加する方法を示しています。
Presentation pres = new Presentation();
try {
IVideo video = pres.getVideos().addVideo(Files.readAllBytes(Paths.get("video.avi")));
IVideoFrame 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(IOException e) {
} finally {
if (pres != null) pres.dispose();
}
Tip
次をご覧ください OLE の管理。ハイパーリンクを使用して目次を作成する
ハイパーリンクはオブジェクトや場所への参照を追加できるため、目次の作成に利用できます。
このサンプルコードは、ハイパーリンク付きの目次を作成する方法を示しています。
Presentation pres = new Presentation();
try {
ISlide firstSlide = pres.getSlides().get_Item(0);
ISlide secondSlide = pres.getSlides().addEmptySlide(firstSlide.getLayoutSlide());
IAutoShape 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 paragraph = new Paragraph();
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
paragraph.setText("Title of slide 2 .......... ");
Portion 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 (pres != null) pres.dispose();
}
ハイパーリンクの書式設定
色
IHyperlink インターフェイスの ColorSource プロパティを使用すると、ハイパーリンクの色を設定したり、ハイパーリンクから色情報を取得したりできます。この機能は PowerPoint 2019 で初めて導入されたため、プロパティに関する変更は古いバージョンの PowerPoint には適用されません。
このサンプルコードは、異なる色のハイパーリンクを同じスライドに追加する操作を示しています。
Presentation pres = new Presentation();
try {
IAutoShape shape1 = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 450, 50, false);
shape1.addTextFrame("This is a sample of colored hyperlink.");
IPortionFormat 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(Color.RED);
IAutoShape 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 (pres != null) pres.dispose();
}
プレゼンテーションからハイパーリンクを削除する
テキストからハイパーリンクを削除する
この Java コードは、プレゼンテーションのスライド内のテキストからハイパーリンクを削除する方法を示しています。
Presentation pres = new Presentation();
try {
ISlide slide = pres.getSlides().get_Item(0);
for (IShape shape : slide.getShapes())
{
IAutoShape autoShape = (IAutoShape)shape;
if (autoShape != null)
{
for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs())
{
for (IPortion portion : paragraph.getPortions())
{
portion.getPortionFormat().getHyperlinkManager().removeHyperlinkClick();
}
}
}
}
pres.save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
図形またはフレームからハイパーリンクを削除する
この Java コードは、プレゼンテーションのスライド内の図形からハイパーリンクを削除する方法を示しています。
Presentation pres = new Presentation();
try {
ISlide slide = pres.getSlides().get_Item(0);
for (IShape shape : slide.getShapes())
{
shape.getHyperlinkManager().removeHyperlinkClick();
}
pres.save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
可変ハイパーリンク
Hyperlink クラスは可変です。このクラスを使用すると、以下のプロパティの値を変更できます:
- IHyperlink.setTargetFrame(String value)
- IHyperlink.setTooltip(String value)
- IHyperlink.setHistory(boolean value)
- IHyperlink.setHighlightClick(boolean value)
- IHyperlink.setStopSoundOnClick(boolean value)
このコード スニペットは、スライドにハイパーリンクを追加し、後でツールチップを編集する方法を示しています。
Presentation pres = new Presentation();
try {
IAutoShape shape1 = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, false);
shape1.addTextFrame("Aspose: File Format APIs");
IPortionFormat 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 (pres != null) pres.dispose();
}
IHyperlinkQueries のサポートされているプロパティ
ハイパーリンクが定義されているプレゼンテーション、スライド、またはテキストから IHyperlinkQueries を取得できます。
- IPresentation.getHyperlinkQueries()
- IBaseSlide.getHyperlinkQueries()
- ITextFrame.getHyperlinkQueries()
[IHyperlinkQueries] クラスは、以下のメソッドとプロパティをサポートしています。
- IHyperlinkQueries.getHyperlinkClicks()
- IHyperlinkQueries.getHyperlinkMouseOvers()
- IHyperlinkQueries.getAnyHyperlinks()
- IHyperlinkQueries.removeAllHyperlinks()
FAQ
スライドだけでなく「セクション」やセクションの最初のスライドへの内部ナビゲーションを作成するにはどうすればよいですか?
PowerPoint のセクションはスライドのグループです。ナビゲーションは技術的には特定のスライドを対象とします。「セクションへ移動」するには、通常その最初のスライドにリンクします。
マスタースライドの要素にハイパーリンクを付けて、すべてのスライドで機能させることはできますか?
はい。マスタースライドやレイアウト要素はハイパーリンクをサポートしています。これらのリンクは子スライドに表示され、スライドショー中にクリック可能です。
PDF、HTML、画像、またはビデオへエクスポートしたときにハイパーリンクは保持されますか?
PDF と HTML では、リンクは通常保持されます。画像 や ビデオ へエクスポートする場合、これらの形式はハイパーリンクをサポートしないため、クリック可能性は失われます。