チュートリアル

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

チュートリアル»Java»Spire.Presentation for Java»SmartArt グラフィック»Java:PowerPoint で SmartArt を作成する方法
2022-11-07

Java:PowerPoint で SmartArt を作成する方法

SmartArt グラフィックは、情報やアイデアを視覚的に表現したものです。普通のテキストをあらかじめ定義されたグラフィックに変えることができ、テキスト情報をより理解しやすくします。この記事では、Spire.Presentation for Java を使用してプログラム的に PowerPoint で SmartArt を作成し、そのレイアウトをカスタマイズする方法を示しています。

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>7.10.1</version>
    </dependency>
</dependencies>

PowerPoint で SmartArt を作成する

詳細な手順は以下の通りです。

  • Presentation クラスオブジェクトを作成します。
  • Presentation.getSlides().get() メソッドを使用して、特定のスライドを取得します。
  • ISlide.getShapes().appendSmartArt() メソッドを使用して、SmartArt をスライドに挿入します。
  • ISmartArt.setStyle() メソッドと ISmartArt.setColorStyle() メソッドを使用して SmartArt のスタイルと色を設定します。
  • ISmartArt.getNodes().removeNode() メソッドを使用して、SmartArt のノードをループして、すべての既定のノードを削除します。
  • ISmartArt.getNodes().addNode() メソッドを使用して SmartArt に親ノードを追加し、ISmartArtNode.getChildNodes().addNode() メソッドを使用して4つの子ノードを追加しています。
  • ISmartArtNode.getTextFrame().setText() メソッドを使用して、各ノードにテキストを追加します。
  • ISmartArtNode.getTextFrame().getTextRange() メソッドを使用して追加したテキストの範囲を取得し、PortionEx.setFontHeight() メソッドを使用してフォントの大きさを設定します。
  • Presentation.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.diagrams.*;

public class addSmartArt {

    public static void main(String[] args) throws Exception {

        //Presentationクラスのオブジェクトを作成する
        Presentation presentation = new Presentation();

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

        //スライドにSmartArt(組織図)を挿入する
        ISmartArt smartArt = slide.getShapes().appendSmartArt(60, 60, 500, 400, SmartArtLayoutType.ORGANIZATION_CHART);

        //SmartArtのスタイルと色を設定する
        smartArt.setStyle(SmartArtStyleType.MODERATE_EFFECT);
        smartArt.setColorStyle(SmartArtColorType.COLORFUL_ACCENT_COLORS_4_TO_5);

        //すべての既定のノードを削除する
        for (Object a : smartArt.getNodes()) {
            smartArt.getNodes().removeNode(0);
        }

        //親ノードを追加する
        ISmartArtNode node1 = smartArt.getNodes().addNode();

        //4つの子ノードを追加する
        ISmartArtNode node1_1 = node1.getChildNodes().addNode();
        ISmartArtNode node1_2 = node1.getChildNodes().addNode();
        ISmartArtNode node1_3 = node1.getChildNodes().addNode();
        ISmartArtNode node1_4 = node1.getChildNodes().addNode();

        //各ノードにテキストを追加し、そのフォントの大きさを設定する
        node1.getTextFrame().setText("社長");
        node1.getTextFrame().getTextRange().setFontHeight(14f);
        node1_1.getTextFrame().setText("営業部長");
        node1_1.getTextFrame().getTextRange().setFontHeight(12f);
        node1_2.getTextFrame().setText("運営部長");
        node1_2.getTextFrame().getTextRange().setFontHeight(12f);
        node1_3.getTextFrame().setText("人事部長");
        node1_3.getTextFrame().getTextRange().setFontHeight(12f);
        node1_4.getTextFrame().setText("アカウントマネージャー");
        node1_4.getTextFrame().getTextRange().setFontHeight(12f);

        //ドキュメントを保存する
        presentation.saveToFile("SmartArtの追加.pptx", FileFormat.PPTX_2010);
        presentation.dispose();
    }
}

Java:PowerPoint で SmartArt を作成する方法

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

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

Read 475 times