Androidでプレゼンテーションのプレースホルダーを管理する

概要

プレースホルダーは、プレゼンテーションテンプレート内で特定の種類のコンテンツの位置を確保するシェイプです。一般的な例として、タイトル、本文、画像、チャート、汎用コンテンツのプレースホルダーがあります。通常のシェイプとは異なり、プレースホルダーはレイアウトスライドまたはマスタースライドから位置、サイズ、書式設定、その他の設定を継承できます。

Aspose.Slidesはプレースホルダー情報をIShape.getPlaceholderメソッドで提供します。このメソッドは通常のシェイプの場合はnull、それ以外の場合はIPlaceholderオブジェクトを返します。IPlaceholder.getTypeを使用して、プレースホルダーが何を含むことを意図しているかを判断します。

シェイプインターフェイスはプレースホルダーの型を把握した後も重要です:

  • 空のテキスト、画像、チャート、またはコンテンツプレースホルダーは、通常IAutoShapeで表されます。
  • 画像が設定されたプレースホルダーはIPictureFrameで表されます。
  • チャートが設定されたプレースホルダーはIChartで表されます。
  • コンテンツプレースホルダーは複数の種類のコンテンツを含むことができます。すべてのプレースホルダーがIAutoShapeであると仮定せず、IPlaceholder.getTypeとランタイムのシェイプインターフェイスの両方を確認してください。

プレースホルダー継承の理解

プレースホルダーは階層を形成します:

  1. マスタースライドは再利用可能なスタイルを定義し、場合によってはマスターレベルのプレースホルダーも定義します。
  2. レイアウトスライドは1つまたは複数の通常スライドで使用される配置を定義し、マスターから継承できます。
  3. 通常スライドはそのスライド用のプレースホルダーを保持し、レイアウトから継承できます。

IShape.getBasePlaceholderを呼び出すと、この階層で1レベル上のプレースホルダーに移動できます。スライドのプレースホルダーは通常、レイアウトプレースホルダーを返し、レイアウトプレースホルダーはマスタープレースホルダーを返すことがあります。シェイプにベースプレースホルダーがない場合、このメソッドはnullを返します。

以下の例は、最初のスライドのプレースホルダーを列挙し、それらのベースプレースホルダーを報告します:

import com.aspose.slides.*;

Presentation presentation = new Presentation("template.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);

    for (IShape shape : slide.getShapes()) {
        IPlaceholder placeholder = shape.getPlaceholder();
        if (placeholder == null) {
            continue;
        }

        byte placeholderType = placeholder.getType();
        String typeName = shape.getClass().getSimpleName();
        String slidePlaceholderMessage = "Slide placeholder: " + placeholderType + "; shape interface: " + typeName;
        System.out.println(slidePlaceholderMessage);

        IShape layoutPlaceholder = shape.getBasePlaceholder();
        if (layoutPlaceholder != null) {
            IPlaceholder layoutPlaceholderInfo = layoutPlaceholder.getPlaceholder();
            Byte layoutPlaceholderType = layoutPlaceholderInfo == null ? null : layoutPlaceholderInfo.getType();
            String layoutPlaceholderMessage = "  Layout placeholder: " + layoutPlaceholderType;
            System.out.println(layoutPlaceholderMessage);

            IShape masterPlaceholder = layoutPlaceholder.getBasePlaceholder();
            if (masterPlaceholder != null) {
                IPlaceholder masterPlaceholderInfo = masterPlaceholder.getPlaceholder();
                Byte masterPlaceholderType = masterPlaceholderInfo == null ? null : masterPlaceholderInfo.getType();
                String masterPlaceholderMessage = "  Master placeholder: " + masterPlaceholderType;
                System.out.println(masterPlaceholderMessage);
            }
        }
    }
} finally {
    presentation.dispose();
}

通常スライド上でプレースホルダーを編集すると、そのスライド用のローカルオーバーライドが作成または変更されます。関連するレイアウトやマスターを編集すると、その設定を継承しているすべてのスライドに影響を及ぼす可能性があります。ローカルの通常シェイプはベースプレースホルダーを持たず、同じ座標にあるだけで継承が開始されることはありません。

プレースホルダーのステータスを削除する

テンプレートを調整する際は、IShape.removePlaceholderを使用して既存のオブジェクトを通常のシェイプとして保持します。そのgetPlaceholderメソッドはnullを返しますが、シェイプはスライドのコレクションに残ります。対照的に、IShapeCollection.removeはそのコレクションからシェイプを削除します。

次の例は、少なくとも1枚のスライドと最初のスライドに空でないテキストプレースホルダーが含まれるtemplate.pptxを必要とします。レイアウトまたはマスターから書式設定を継承したプレースホルダーを使用して、関係を削除したときの効果を確認してください。例は同一のシェイプが残っていることをチェックし、普通のシェイプになった後に再度メソッドを呼び出し、結果を保存します。

import com.aspose.slides.*;

Presentation presentation = new Presentation("template.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    boolean found = false;

    for (int index = 0; index < slide.getShapes().size(); index++) {
        IShape candidate = slide.getShapes().get_Item(index);
        if (!(candidate instanceof IAutoShape)) {
            continue;
        }

        IAutoShape shape = (IAutoShape) candidate;
        ITextFrame textFrame = shape.getTextFrame();
        if (shape.getPlaceholder() == null ||
            textFrame == null ||
            textFrame.getText() == null ||
            textFrame.getText().isEmpty()) {
            continue;
        }

        int shapeCount = slide.getShapes().size();
        shape.removePlaceholder();

        System.out.println("Placeholder removed: " + (shape.getPlaceholder() == null));
        System.out.println("Same shape retained: " + (slide.getShapes().get_Item(index) == shape));
        System.out.println("Shape count unchanged: " + (slide.getShapes().size() == shapeCount));

        // 通常のシェイプに対してメソッドを呼び出しても効果がありません。
        shape.removePlaceholder();
        found = true;
        break;
    }

    if (found) {
        presentation.save("placeholder-status-removed.pptx", SaveFormat.Pptx);
    } else {
        System.out.println("No nonempty text placeholder was found on the first slide.");
    }
} finally {
    presentation.dispose();
}

一致するプレースホルダーの場合、3つのチェックはすべてtrueを出力します。テキストは残りますが、プレースホルダーの関係が削除されると、継承された位置、サイズ、回転、フォント、段落配置、テキストフレームの余白、垂直アンカリング、塗りつぶし、線の設定が変わることがあります。例えば、継承されたタイトルの書式設定は通常のテキスト書式にフォールバックすることがあります。レイアウトの詳細についてはスライド レイアウトをご参照ください。

意図した外観を保つには、呼び出し前に解決された位置、サイズ、回転を取得し、必要な実効書式設定値を個別の変数にコピーします。これらの値はIPortionFormat.getEffective、IParagraphFormat.getEffective、ITextFrameFormat.getEffective、IFillFormat.getEffective、ILineFormat.getEffectiveを通じて取得します。実効書式オブジェクトだけに依存しないでください。関係が削除された後にテキスト書式設定が変わる可能性があります。取得した値を普通のシェイプに明示的に適用します。フォントファミリ、サイズ、スタイル、色、段落設定、テキストフレーム設定、塗りつぶし、線など、テンプレートで使用されている設定をすべて適用してください。

継承されたシェイプのジオメトリも、IGeometryShape.setShapeTypeで明示的に設定する必要があります。矩形テキストプレースホルダーの場合、ShapeType.Rectangleを設定することを意味します。塗りつぶしと線だけを復元しても、見える矩形が復元されないことがあります。追加効果やより複雑な書式設定を持つテンプレートでは、これらの設定も同様に処理する必要があります。

プレースホルダー内のテキストを変更する

タイトル、センタリングタイトル、サブタイトル、本文、テキストのプレースホルダーは通常テキストをサポートします。IAutoShapeかどうかを確認してから、そのgetTextFrameメソッドを使用してください。

この例は、最初のスライドの最初のタイトルプレースホルダーを更新し、結果を保存します:

import com.aspose.slides.*;

Presentation presentation = new Presentation("template.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape titleShape = null;

    for (IShape shape : slide.getShapes()) {
        if (!(shape instanceof IAutoShape)) {
            continue;
        }

        IAutoShape autoShape = (IAutoShape) shape;
        IPlaceholder placeholder = autoShape.getPlaceholder();
        if (placeholder == null) {
            continue;
        }

        byte placeholderType = placeholder.getType();
        if (placeholderType == PlaceholderType.Title || placeholderType == PlaceholderType.CenteredTitle) {
            titleShape = autoShape;
            break;
        }
    }

    if (titleShape == null) {
        throw new IllegalStateException("The first slide does not contain a title placeholder.");
    }

    titleShape.getTextFrame().setText("Quarterly Business Review");
    presentation.save("title-placeholder-updated.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

このパターンは、画像、チャート、テーブル、メディアのプレースホルダーをIAutoShapeにキャストすることを回避します。また、脆弱なシェイプインデックスに依存せず、目的でプレースホルダーを識別します。

レイアウト上でプロンプトテキストを設定する

プロンプトテキストは、空のプレースホルダーに表示されるデザイン時の指示で、例えば Click to add title のようなものです。通常スライドのシェイプコレクションから取得しようとせず、レイアウトプレースホルダーにカスタムプロンプトテキストを設定してください。レイアウトはISlide.getLayoutSlideで取得し、ILayoutSlide.getShapesが返すコレクションを反復処理します。

以下の例は、最初のスライドで使用されるレイアウトのタイトルとサブタイトルのプロンプトを変更します:

import com.aspose.slides.*;

Presentation presentation = new Presentation("template.pptx");
try {
    ILayoutSlide layoutSlide = presentation.getSlides().get_Item(0).getLayoutSlide();

    for (IShape shape : layoutSlide.getShapes()) {
        if (!(shape instanceof IAutoShape)) {
            continue;
        }

        IAutoShape autoShape = (IAutoShape) shape;
        IPlaceholder placeholder = autoShape.getPlaceholder();
        if (placeholder == null) {
            continue;
        }

        byte placeholderType = placeholder.getType();

        if (placeholderType == PlaceholderType.Title || placeholderType == PlaceholderType.CenteredTitle) {
            autoShape.getTextFrame().setText("Enter a concise slide title");
        } else if (placeholderType == PlaceholderType.Subtitle) {
            autoShape.getTextFrame().setText("Enter a subtitle or reporting period");
        }
    }

    presentation.save("custom-placeholder-prompts.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

プロンプトテキストは通常のスライドコンテンツではありません。PowerPointなどの編集アプリケーションで空のプレースホルダーに表示されることを意図しています。ユーザーまたはプログラムが実際のコンテンツを提供すると、プロンプトは表示されなくなります。プロンプトを変更しても、レイアウトを使用しているスライド上の既存テキストは置き換えられません。

画像プレースホルダーを更新する

扱うべきケースは2つあります:

次の例は両方のケースに対応し、プレゼンテーションを保存します:

import com.aspose.slides.*;
import java.io.FileInputStream;

Presentation presentation = new Presentation("picture-template.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IShape picturePlaceholder = null;

    for (IShape shape : slide.getShapes()) {
        IPlaceholder placeholder = shape.getPlaceholder();
        if (placeholder != null && placeholder.getType() == PlaceholderType.Picture) {
            picturePlaceholder = shape;
            break;
        }
    }

    if (picturePlaceholder == null) {
        throw new IllegalStateException("The first slide does not contain a picture placeholder.");
    }

    IPPImage image;
    try (FileInputStream imageStream = new FileInputStream("replacement.png")) {
        image = presentation.getImages().addImage(imageStream);
    }

    if (picturePlaceholder instanceof IPictureFrame) {
        IPictureFrame pictureFrame = (IPictureFrame) picturePlaceholder;
        pictureFrame.getPictureFormat().getPicture().setImage(image);
    } else {
        slide.getShapes().addPictureFrame(ShapeType.Rectangle, picturePlaceholder.getX(), picturePlaceholder.getY(), picturePlaceholder.getWidth(), picturePlaceholder.getHeight(), image);
        slide.getShapes().remove(picturePlaceholder);
    }

    presentation.save("picture-placeholder-updated.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

空のプレースホルダー向けに作成された置き換えは、新しいプレースホルダーではなくローカルの画像フレームです。IShape.getPlaceholderにセッターがないためです。予約された位置は保持しますが、プレースホルダー固有の動作は継承しなくなります。プレースホルダーの関係を保持することが重要な場合は、まずPowerPointでプレースホルダーを用意して設定し、その後Aspose.Slidesで生成されたIPictureFrameを更新してください。

画像の透過、トリミング、その他画像固有の効果については画像フレームの管理をご参照ください。これらの操作は画像フレームまたは画像塗りつぶしに対するもので、プレースホルダーメタデータには関係しません。

チャートとコンテンツプレースホルダーの操作

設定されたチャートプレースホルダーはIChartで表されます。この例はプレースホルダータイプとランタイムインターフェイスの両方でチャートを見つけ、タイトルを変更し、ファイルを保存します:

import com.aspose.slides.*;

Presentation presentation = new Presentation("chart-template.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IChart placeholderChart = null;

    for (IShape shape : slide.getShapes()) {
        if (!(shape instanceof IChart)) {
            continue;
        }

        IChart chart = (IChart) shape;
        IPlaceholder placeholder = chart.getPlaceholder();
        if (placeholder != null && placeholder.getType() == PlaceholderType.Chart) {
            placeholderChart = chart;
            break;
        }
    }

    if (placeholderChart == null) {
        throw new IllegalStateException("The first slide does not contain a populated chart placeholder.");
    }

    placeholderChart.setTitle(true);
    placeholderChart.getChartTitle().addTextFrameForOverriding("Quarterly Revenue");
    presentation.save("chart-placeholder-updated.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

一般的なコンテンツプレースホルダーは通常PlaceholderType.Objectを持ちます。PowerPointでは、チャート、テーブル、図、画像、メディアなど複数のコンテンツタイプの起動点として機能します。コンテンツが設定された後は、実際のシェイプインターフェイスを調べて何が含まれているかを確認してください。特化したレイアウトではPlaceholderType.Chart、PlaceholderType.Table、PlaceholderType.Picture、PlaceholderType.Media、PlaceholderType.Diagramも公開されることがあります。

Aspose.Slidesは、IPlaceholder.getTypeを変更しただけで空のIAutoShapeプレースホルダーをIChartに変換することはできません。型はインターフェイスを通じて変更できません。空のチャートまたはコンテンツ領域にプログラムで内容を設定するには、プレースホルダーの座標に必要なオブジェクトを追加し、空のプレースホルダーを削除します。次の例はチャートに対してそれを行います:

import com.aspose.slides.*;

Presentation presentation = new Presentation("content-template.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IShape targetPlaceholder = null;

    for (IShape shape : slide.getShapes()) {
        IPlaceholder placeholder = shape.getPlaceholder();
        if (placeholder == null) {
            continue;
        }

        byte placeholderType = placeholder.getType();
        if (placeholderType == PlaceholderType.Chart || placeholderType == PlaceholderType.Object) {
            targetPlaceholder = shape;
            break;
        }
    }

    if (targetPlaceholder == null) {
        throw new IllegalStateException("The first slide does not contain a chart or content placeholder.");
    }

    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, targetPlaceholder.getX(), targetPlaceholder.getY(), targetPlaceholder.getWidth(), targetPlaceholder.getHeight());
    chart.setTitle(true);
    chart.getChartTitle().addTextFrameForOverriding("Quarterly Revenue");
    slide.getShapes().remove(targetPlaceholder);
    presentation.save("content-placeholder-replaced-with-chart.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

追加されたチャートは普通のローカルチャートです。プレースホルダーの領域を占めますが、レイアウトプレースホルダーからは継承しません。カテゴリ、シリーズ、またはワークブックデータを置き換える必要がある場合は、専用のチャート管理記事をご利用ください。

完全例: テキストまたは画像コンテンツの更新

以下のエンドツーエンドの例は、テンプレートを開き、最初のスライドでタイトルまたは画像プレースホルダーを検索し、プレースホルダーとシェイプの型を確認して適切なコンテンツを更新し、出力を保存します。この例はシェイプインデックスを仮定したり、すべてのプレースホルダーを同じインターフェイスにキャストしたりすることを意図的に回避しています。

import com.aspose.slides.*;
import java.io.FileInputStream;

Presentation presentation = new Presentation("template.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    boolean updated = false;

    for (IShape shape : slide.getShapes()) {
        IPlaceholder placeholder = shape.getPlaceholder();
        if (placeholder == null) {
            continue;
        }

        byte placeholderType = placeholder.getType();

        if ((placeholderType == PlaceholderType.Title || placeholderType == PlaceholderType.CenteredTitle) && shape instanceof IAutoShape) {
            IAutoShape titleShape = (IAutoShape) shape;
            titleShape.getTextFrame().setText("Quarterly Business Review");
            updated = true;
            break;
        }

        if (placeholderType == PlaceholderType.Picture) {
            IPPImage image;
            try (FileInputStream imageStream = new FileInputStream("replacement.png")) {
                image = presentation.getImages().addImage(imageStream);
            }

            if (shape instanceof IPictureFrame) {
                IPictureFrame pictureFrame = (IPictureFrame) shape;
                pictureFrame.getPictureFormat().getPicture().setImage(image);
            } else {
                slide.getShapes().addPictureFrame(ShapeType.Rectangle, shape.getX(), shape.getY(), shape.getWidth(), shape.getHeight(), image);
                slide.getShapes().remove(shape);
            }

            updated = true;
            break;
        }
    }

    if (!updated) {
        throw new IllegalStateException("No supported title or picture placeholder was found on the first slide.");
    }

    presentation.save("placeholder-content-updated.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

よくある質問

ベースプレースホルダーとは何ですか?

ベースプレースホルダーとは、他のプレースホルダーが継承するレイアウトまたはマスター上の対応するシェイプです。IShape.getBasePlaceholderを使用して取得します。通常のローカルシェイプはプレースホルダー階層の一部ではないためnullを返します。

レイアウトプレースホルダーを編集してすべてのスライドタイトルを変更できますか?

レイアウトを通じて継承された書式設定やプロンプトテキストは変更できますが、既存のタイトルコンテンツは通常のスライドに保存されています。プレゼンテーション全体の実際のタイトルテキストを置き換えるには、スライドを反復処理し、各タイトルプレースホルダーを更新してください。

日付、スライド番号、ヘッダー、フッターのプレースホルダーはどう管理しますか?

適切なスライド、レイアウト、マスター、ノート、またはハンドアウトのスコープでヘッダーおよびフッターマネージャーを使用します。完全な例についてはプレゼンテーション ヘッダーとフッターの管理をご参照ください。