JavaのPowerPointPPTにハイパーリンクを追加または削除する

さまざまな場合、WebページのURLを提供するためなど、PowerPointプレゼンテーションにハイパーリンクを挿入する必要があります。 PowerPoint PPTスライドで、テキスト、画像、図形、またはメディア要素のハイパーリンクを作成できます。この記事では、JavaでPowerPoint PPT/PPTXにハイパーリンクを追加する方法を学習します。最後に、プログラムでPPTスライドからハイパーリンクを削除する方法についても説明します。

PowerPointプレゼンテーションでハイパーリンクを挿入または削除するには、Aspose.Slides for Javaを使用します。 APIは、PPT/PPTXプレゼンテーションをシームレスに作成および操作するための一連の機能を提供します。また、プレゼンテーションを他のドキュメントまたは画像形式に変換することもできます。そのJARをダウンロードするか、以下のMaven構成を使用してインストールできます。

リポジトリ:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

依存:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>22.2</version>
    <classifier>jdk16</classifier>
</dependency>

PowerPointプレゼンテーションのハイパーリンクは、テキスト、画像、図形、音声、またはビデオ要素に追加できます。次のセクションでは、Javaを使用してPPT/PPTXプレゼンテーションでこれらの要素にハイパーリンクを追加する方法について説明します。

以下は、JavaのPowerPoint PPT/PPTXにテキストハイパーリンクを追加する手順です。

次のコードサンプルは、Javaを使用してPowerPoint PPTXにテキストハイパーリンクを追加する方法を示しています。

// プレゼンテーションを作成する
Presentation presentation = new Presentation();
try {
	// オートシェイプを追加する
	IAutoShape shape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100,
			100, 600, 50, false);
	
	// ハイパーリンクを追加
	shape.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
	shape.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");

	// プレゼンテーションを保存する
	presentation.save("add-text-hyperlink.pptx", SaveFormat.Pptx);
} finally {
	if (presentation != null)
		presentation.dispose();
}

次の手順は、JavaのPowerPointプレゼンテーションに図形のハイパーリンクを挿入する方法を示しています。

次のコードサンプルは、Javaを使用してPowerPointで図形のハイパーリンクを作成する方法を示しています。

// プレゼンテーションを作成する
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("add-shape-hyperlink.pptx", SaveFormat.Pptx);
} finally {
	if (presentation != null)
		presentation.dispose();
}

以下は、JavaのPowerPointPPTに画像ハイパーリンクを挿入する手順です。

次のコードサンプルは、Javaを使用してPowerPointで画像のハイパーリンクを作成する方法を示しています。

// プレゼンテーションを作成する
Presentation presentation = new Presentation();
try {
	// プレゼンテーションに画像を追加
	IPPImage image = presentation.getImages().addImage(Files.readAllBytes(Paths.get("image.png")));
	
	// 以前に追加した画像に基づいて、スライド1に額縁を作成します
	IPictureFrame pictureFrame = presentation.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, image);

	// ハイパーリンクを作成する
	pictureFrame.setHyperlinkClick(new Hyperlink("https://www.aspose.com/"));
	pictureFrame.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs");

	// プレゼンテーションを保存する
	presentation.save("add-image-hyperlink.pptx", SaveFormat.Pptx);
} finally {
	if (presentation != null)
		presentation.dispose();
}

以下の手順に従って、PowerPoint PPT/PPTXにオーディオハイパーリンクを追加できます。

次のコードスニペットは、JavaのPowerPointでオーディオハイパーリンクを追加する方法を示しています。

// プレゼンテーションを作成する
Presentation presentation = new Presentation();
try {
	// オーディオ要素を追加する
	IAudio audio = presentation.getAudios().addAudio(Files.readAllBytes(Paths.get("audio.mp3")));
	IAudioFrame audioFrame = presentation.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");

	// プレゼンテーションを保存する
	presentation.save("add-audio-hyperlink.pptx", SaveFormat.Pptx);
} finally {
	if (presentation != null)
		presentation.dispose();
}

PowerPoint PPT / PPTXにビデオハイパーリンクを追加するには、次の手順に従います。

次のコードスニペットは、JavaのPowerPointでビデオハイパーリンクを追加する方法を示しています。

// プレゼンテーションを作成する
Presentation presentation = new Presentation();
try {
	// ビデオを追加
	IVideo video = presentation.getVideos().addVideo(Files.readAllBytes(Paths.get("video.avi")));
	IVideoFrame videoFrame = presentation.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");

	// プレゼンテーションを保存する
	presentation.save("add-video-hyperlink.pptx", SaveFormat.Pptx);
} finally {
	if (presentation != null)
		presentation.dispose();
}

このセクションでは、PowerPointスライドからハイパーリンクを削除する方法を示します。これらのハイパーリンクは、上記で説明した任意のタイプにすることができます。この操作を実行する手順は次のとおりです。

次のコードサンプルは、JavaのPowerPointPPTでスライドからハイパーリンクを削除する方法を示しています。

// プレゼンテーションを読み込む
Presentation presentation = new Presentation("presentation.pptx");
try {
	// テキストからハイパーリンクを削除する
	ISlide slide = presentation.getSlides().get_Item(0);
	for (IShape shape : slide.getShapes())
	{
		// IAutoShapeにキャスト
		IAutoShape autoShape = (IAutoShape)shape;
		if (autoShape != null)
		{
			// 段落をループする
			for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs())
			{
				// テキスト部分をループする
				for (IPortion portion : paragraph.getPortions())
				{
					portion.getPortionFormat().getHyperlinkManager().removeHyperlinkClick();
				}
			}
		}
	}
	
	// 図形からハイパーリンクを削除する
	for (IShape shape : slide.getShapes())
	{
		shape.getHyperlinkManager().removeHyperlinkClick();
	}

	// プレゼンテーションを保存する
	presentation.save("remove-hyperlink.pptx", SaveFormat.Pptx);
} finally {
	if (presentation != null)
		presentation.dispose();
}

無料ライセンスを取得する

無料の一時ライセンスを取得して、評価の制限なしにAspose.Slides for Javaを使用します。

結論

この記事では、JavaのPowerPointPPTでハイパーリンクを追加または削除する方法を学習しました。特に、テキスト、画像、形状、オーディオ、およびビデオ要素のハイパーリンクを作成する方法を見てきました。また、テキストを削除したり、ハイパーリンクを明示的に形作ったりする方法についても説明しました。さらに、ドキュメントにアクセスすると、Aspose.Slides for Javaの詳細を調べることができます。また、フォーラムにクエリを投稿することもできます。

関連項目