チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»Java»Spire.Presentation for Java»画像と図形»Java:PowerPoint に画像を追加または抽出する方法
2023-01-29

Java:PowerPoint に画像を追加または抽出する方法

スピーチの内容が非常に抽象的な場合、言葉だけでメッセージを聴衆に明確に伝えるのは難しい場合があります。 PowerPoint プレゼンテーションに画像を追加すると、意見をより明確に伝え、聴衆との間の誤解を最小限に抑えることができます。 必要に応じて、PowerPoint から既存の画像を抽出することもできます。この記事では、Spire.Presentation for Java を使用して PowerPoint に画像を追加または抽出する方法を紹介します。

Spire.Presentation for Java をインストールします

まず、Spire.Presentation for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url> https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>8.1.2</version>
    </dependency>
</dependencies>

Java でスライドに画像を追加する

Spire.Presentation が提供する ISlide.getShapes().appendEmbedImage() メソッドは、特定のスライドに画像の追加をサポートします。詳細な手順は次のとおりです。

  • Presentation クラスのインスタンスを初期化します。
  • Presentation.loadFromFile() メソッドを使用して PowerPoint ドキュメントをロードします。
  • Presentation.getSlides().get(int) メソッドを使用して、インデックスで特定のスライドを取得します。
  • ISlide.getShapes().appendEmbedImage() メソッドを使用してスライドに画像を追加します。
  • Presentation.saveToFile() メソッドを使用して結果ドキュメントを保存します。
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class AddImageToSlide {
    public static void main(String []args) throws Exception {
        //Presentationクラスのインスタンスを初期化する
        Presentation presentation = new Presentation();
        //PowerPointドキュメントをロードする
        presentation.loadFromFile("Input.pptx");

        //最初のスライドを取得する
        ISlide slide = presentation.getSlides().get(0);

        //スライドに画像を追加する
        String imageFile = "image.png";
        Rectangle2D.Double rect1 = new Rectangle2D.Double(presentation.getSlideSize().getSize().getWidth() / 2 - 280, 140, 120, 120);
        IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, imageFile, rect1);
        image.getLine().setFillType(FillFormatType.NONE);

        //結果ドキュメントを保存する
        presentation.saveToFile("AddImageToSlide.pptx", FileFormat.PPTX_2013);
    }
}

Java:PowerPoint に画像を追加または抽出する方法

Java でスライドマスターに画像を追加する

スライド マスターは、スライドのテーマ、レイアウト、背景、色、フォントなどの情報を制御できる一番上のスライドです。 スライド マスターの情報は他のスライドにも影響します。 つまり、スライド マスターの情報を変更すると、後で追加されたスライドを含め、他のスライドもそれに応じて変更されます。 画像をすべてのスライドに表示する場合は、スライドマスターに追加できます。

Spire.Presentationが提供する IMasterSlide.getShapes().appendEmbedImage() メソッドは、スライド マスターに画像の追加をサポートします。詳細な手順は次のとおりです。

  • Presentation.loadFromFile() メソッドを使用して PowerPoint ドキュメントをロードします。
  • Presentation.getMasters().get(int) メソッドを使用して、インデックスでスライド マスターを取得します。
  • IMasterSlide.getShapes().appendEmbedImage() メソッドを使用して、スライド マスターに画像を追加します。
  • Presentation.saveToFile() メソッドを使用して結果ドキュメントを保存します。
  • Java
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class AddImageToSlideMaster {
    public static void main(String []args) throws Exception {
        //Presentationクラスのインスタンスを初期化する
        Presentation presentation = new Presentation();
        //PowerPointドキュメントをロードする
        presentation.loadFromFile("Sample.pptx");

        //最初のスライドマスターを取得する
        IMasterSlide master = presentation.getMasters().get(0);

        //スライドマスターに画像を挿入する
        String image = "logo.png";
        Rectangle2D.Double rect = new Rectangle2D.Double(40, 40, 80, 80);
        IEmbedImage pic = master.getShapes().appendEmbedImage(ShapeType.RECTANGLE, image, rect);
        pic.getLine().getFillFormat().setFillType(FillFormatType.NONE);

        //プレゼンテーションへに新しいスライドを追加する
        presentation.getSlides().append();

        //PowerPointドキュメントをロードする
        presentation.saveToFile("AddImageToSlideMaster.pptx", FileFormat.PPTX_2013);
    }
}

Java:PowerPoint に画像を追加または抽出する方法

Java でスライドから画像を抽出する

特定のスライドから画像を抽出するには、スライドのすべての図形をループします。SlidePicture または PictureShape タイプの図形をを検索し、SlidePicture.getPictureFill().getPicture().getEmbedImage().getImage() または PictureShape.getEmbedImage().getImage() メソッドを使用して画像を取得します。詳細な手順は次のとおりです。

  • Presentation クラスのインスタンスを初期化します。
  • Presentation.loadFromFile() メソッドを使用して PowerPoint ドキュメントをロードします。
  • Presentation.getSlides().get(int) メソッドを使用して、インデックスで特定のスライドを取得します。
  • スライドのすべての図形をループします。
  • 図形が SlidePicture または PictureShape タイプであるかどうかを確認します。結果がtrueの場合は、SlidePicture.getPictureFill().getPicture().getEmbedImage().getImage() または PictureShape.getEmbedImage().getImage() メソッドを使用して画像を取得します。
  • 画像を PNG に保存します。
  • Java
import com.spire.presentation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ExtractImageFromSlide {
    public static void main(String []args) throws Exception {
        //Presentationクラスのインスタンスを初期化する
        Presentation ppt = new Presentation();
        //PowerPointドキュメントをロードする
        ppt.loadFromFile("Images.pptx");

        //最初のスライドを取得する
        ISlide slide = ppt.getSlides().get(0);

        //スライドのすべての図形をループする
        for(int i = 0; i< slide.getShapes().getCount(); i++)
        {
            IShape shape = slide.getShapes().get(i);
            //スライドから画像を抽出する
            if(shape instanceof SlidePicture)
            {
                SlidePicture pic = (SlidePicture) shape;
                BufferedImage image = pic.getPictureFill().getPicture().getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(String.format("slide/" + "extractImage-%1$s.png", i)));
            }
            if(shape instanceof PictureShape)
            {
                PictureShape ps = (PictureShape) shape;
                BufferedImage image = ps.getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(String.format("slide/" + "extractImage-%1$s.png", i)));
            }
        }
    }
}

Java:PowerPoint に画像を追加または抽出する方法

Java で PowerPoint からすべての画像を抽出する

PowerPoint からすべての画像を抽出するには、Presentation.getImages() メソッドを使用してドキュメントの画像コレクションを取得します。その画像コレクションをループし、ImageCollection.get(int).getImage() メソッドを使用して画像を取得します。詳細な手順は次のとおりです。

  • Presentation クラスのインスタンスを初期化します。
  • Presentation.loadFromFile() メソッドを使用して PowerPoint ドキュメントをロードします。
  • Presentation.getImages() メソッドを使用して、ドキュメントの画像コレクションを取得します。
  • 画像 コレクションをループし、ImageCollection.get(int).getImage() メソッドを使用して画像を取得します。
  • 画像を PNG に保存します。
  • Java
import com.spire.presentation.Presentation;
import com.spire.presentation.collections.ImageCollection;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ExtractAllImagesFromPowerPoint {
    public static void main(String []args) throws Exception {
        //Presentationクラスのインスタンスを初期化する
        Presentation ppt = new Presentation();
        //PowerPointドキュメントをロードする
        ppt.loadFromFile("Images.pptx");

        //ドキュメントの画像コレクションを取得する
        ImageCollection collection = ppt.getImages();
        
        //画像コレクションをループする
        for (int i = 0; i < collection.getCount(); i++) {
            //コレクションから画像を取得する 
            BufferedImage image = collection.get(i).getImage();
            ImageIO.write(image, "PNG",  new File(String.format("presentation/" + "extractImage-%1$s.png", i)));
        }
    }
}

Java:PowerPoint に画像を追加または抽出する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 441 times