Android でプレゼンテーション プロパティを管理する

イントロダクション

Aspose.Slides for Android via Java は 組み込み と カスタム のドキュメント プロパティをサポートします。これらのプロパティを使用して、著者、タイトル、ユーザー定義の値などのプレゼンテーション メタデータを読み取りおよび更新します。

Aspose.Slides を使用すると、IDocumentProperties インターフェイスを通じてプレゼンテーションのドキュメント プロパティを操作できます。このインターフェイスのインスタンスは IPresentation.getDocumentProperties によって返されます。以下の例では、これらのプロパティの読み取り、変更、管理方法を示します。

プレゼンテーション プロパティの管理

ドキュメント プロパティは、プレゼンテーション コンテンツとともに記述情報を保存します。これらは 2 つのカテゴリに分かれます:

  • 組み込みプロパティ は、タイトル、著者、ドキュメントの統計情報などの標準メタデータを含みます。
  • カスタムプロパティ は、ユーザー定義の名前/値のペアを保存します。

Aspose.Slides for Android via Java は両方のカテゴリへのアクセスを提供します。PowerPoint では、File → Info → Properties → Advanced Properties を選択してドキュメント メタデータを確認します。ダイアログは情報を Summary、Statistics、Contents、Custom などのタブに分類します。Custom タブを使用してユーザー定義プロパティを管理します。

暗号化されたプレゼンテーションから公開プロパティを読み取る

開くパスワードは通常、プレゼンテーション コンテンツとドキュメント プロパティの両方を保護します。false を指定して IProtectionManager.setEncryptDocumentProperties を呼び出した後にプレゼンテーションが暗号化された場合、ドキュメント プロパティは公開されたままになります。アプリケーションはその後、true を指定して LoadOptions.setOnlyLoadDocumentProperties を呼び出すことで、開くパスワードを提供せずに公開メタデータを読み取れます。

LoadOptions.setOnlyLoadDocumentProperties は Aspose.Slides が読み込む内容を制御します。暗号化は行いません。プロパティが暗号化に含まれている場合、パスワードなしでの読み込みは失敗します。プレゼンテーションが暗号化されていない場合、このオプションは無視され、プレゼンテーション全体が読み込まれます。

この例では、ドキュメント プロパティが公開されたままの暗号化されたプレゼンテーションが必要です。IProtectionManager.isOnlyDocumentPropertiesLoaded でロード モードを確認し、続いて IPresentation.getDocumentProperties を使用して組み込みプロパティを読み取ります。

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;

LoadOptions loadOptions = new LoadOptions();
loadOptions.setOnlyLoadDocumentProperties(true);

Presentation presentation = new Presentation("public-properties-encrypted.pptx", loadOptions);
try {
    if (presentation.getProtectionManager().isOnlyDocumentPropertiesLoaded()) {
        IDocumentProperties documentProperties = presentation.getDocumentProperties();

        System.out.println("Author: " + documentProperties.getAuthor());
        System.out.println("Title: " + documentProperties.getTitle());
        System.out.println("Keywords: " + documentProperties.getKeywords());
    } else {
        System.out.println("The presentation was not loaded in document-properties-only mode.");
    }
} finally {
    presentation.dispose();
}

このモードでは、スライド コンテンツは読み込まれません。スライド、マスター、レイアウト、シェイプ、メディア、その他のプレゼンテーション オブジェクトは利用できません。完全なプレゼンテーション オブジェクト モデルが必要な操作を行う前に、必ず IProtectionManager.isOnlyDocumentPropertiesLoaded を確認してください。

暗号化されたプレゼンテーションのプロパティを更新する

暗号化された PPTX ファイルの場合、true を指定して LoadOptions.setOnlyLoadDocumentProperties を呼び出した後に読み込まれるプレゼンテーションは、公開メタデータの読み取りを目的としています。Aspose.Slides は、そのメタデータのみのオブジェクトから変更されたプロパティを保存できません。公開プロパティは暗号化されたプレゼンテーション内の対応データと一貫性を保つ必要があるため、更新には正しい開きパスワードと完全なロードが必要です。

この例では、公開ドキュメント プロパティを持ち、既知の開きパスワードが設定された暗号化された PPTX プレゼンテーションが必要です。LoadOptions.setPassword でプレゼンテーション全体を開き、公開組み込みプロパティを更新して結果を保存します。その後、IPresentationInfo.isEncrypted を使用して暗号化が保持されていることを確認し、パスワードなしで公開メタデータを再度開いて新しい値を検証します。

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.IPresentationInfo;
import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.PresentationFactory;
import com.aspose.slides.SaveFormat;

final String inputPath = "public-properties-encrypted.pptx";
final String outputPath = "updated-public-properties-encrypted.pptx";

LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("open_password");

Presentation presentation = new Presentation(inputPath, loadOptions);
try {
    IDocumentProperties documentProperties = presentation.getDocumentProperties();
    documentProperties.setTitle("Updated Product Roadmap");
    documentProperties.setKeywords("roadmap, planning, indexed");
    
    presentation.save(outputPath, SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

IPresentationInfo presentationInfo = PresentationFactory.getInstance().getPresentationInfo(outputPath);
System.out.println("The presentation is encrypted: " + presentationInfo.isEncrypted());

LoadOptions metadataLoadOptions = new LoadOptions();
metadataLoadOptions.setOnlyLoadDocumentProperties(true);

Presentation metadataPresentation = new Presentation(outputPath, metadataLoadOptions);
try {
    if (metadataPresentation.getProtectionManager().isOnlyDocumentPropertiesLoaded()) {
        System.out.println("Title: " + metadataPresentation.getDocumentProperties().getTitle());
        System.out.println("Keywords: " + metadataPresentation.getDocumentProperties().getKeywords());
    } else {
        System.out.println("The presentation was not loaded in document-properties-only mode.");
    }
} finally {
    metadataPresentation.dispose();
}

アプリケーションがプレゼンテーション コンテンツを復号またはロードできない場合、暗号化された PPTX ファイルの公開プロパティは読み取り専用として扱う必要があります。

組み込みプロパティにアクセスする

IDocumentProperties インターフェイスは、getter メソッドを通じて組み込みメタデータを公開します。例として getAuthor、getTitle、getCreatedTime があります。この例では既存のプレゼンテーションを開き、保存されたメタデータをコンソールに出力します。値は入力プレゼンテーションに依存し、いくつかのフィールドは空の場合があります。

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.Presentation;

Presentation presentation = new Presentation("AccessBuiltInProperties.pptx");
try {
    IDocumentProperties documentProperties = presentation.getDocumentProperties();

    System.out.println("Category: " + documentProperties.getCategory());
    System.out.println("Content status: " + documentProperties.getContentStatus());
    System.out.println("Creation date: " + documentProperties.getCreatedTime());
    System.out.println("Author: " + documentProperties.getAuthor());
    System.out.println("Comments: " + documentProperties.getComments());
    System.out.println("Keywords: " + documentProperties.getKeywords());
    System.out.println("Last modified by: " + documentProperties.getLastSavedBy());
    System.out.println("Manager: " + documentProperties.getManager());
    System.out.println("Modified date: " + documentProperties.getLastSavedTime());
    System.out.println("Presentation format: " + documentProperties.getPresentationFormat());
    System.out.println("Last print date: " + documentProperties.getLastPrinted());
    System.out.println("Is shared between producers: " + documentProperties.getSharedDoc());
    System.out.println("Subject: " + documentProperties.getSubject());
    System.out.println("Title: " + documentProperties.getTitle());
} finally {
    presentation.dispose();
}

組み込みプロパティを変更する

組み込みメタデータを更新するには、IPresentation.getDocumentProperties が返すオブジェクトの setter メソッドを使用します。この例では既存のプレゼンテーションを開き、setAuthor、setTitle、setSubject、setComments、setManager を呼び出し、Presentation.save で更新されたプレゼンテーションを保存します。保存された PPTX には新しいメタデータが含まれます。

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("ModifyBuiltInProperties.pptx");
try {
    IDocumentProperties documentProperties = presentation.getDocumentProperties();

    documentProperties.setAuthor("Aspose.Slides for Android via Java");
    documentProperties.setTitle("Manage PowerPoint Presentation Properties");
    documentProperties.setSubject("Modify Built-in Properties");
    documentProperties.setComments("Aspose description");
    documentProperties.setManager("Aspose manager");

    presentation.save("DocumentProperties_output.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

ハイパーリンク ベースの管理

IDocumentProperties.getHyperlinkBase は、相対ハイパーリンク用のドキュメント レベルのベースを返します。IDocumentProperties.setHyperlinkBase を使用して変更またはクリアできます。プレゼンテーションとリンクされたファイルを別の場所に移動する際に便利です。これは個々のハイパーリンクの IHyperlink.getExternalUrl とは別物で、ベースを変更しても保存されたリンクアドレスは書き換えられず、絶対的な宛先も変更されません。

この例では、target.html のような相対リンクと、https://example.org/absolute.html のような絶対リンクの両方を含むプレゼンテーションが必要です。既存のベースを読み取り、新しいベース アドレスを割り当て、プレゼンテーションを保存して再度開き、値を確認します。その後、空文字列でベースをクリアし、別の保存ファイルで空の値を検証します。例のベース アドレスは、リンクされたファイルの場所に置き換えてください。

import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("hyperlinks.pptx");
try {
    System.out.println("Original base: " + presentation.getDocumentProperties().getHyperlinkBase());

    final String newBase = "https://example.com/new/";
    presentation.getDocumentProperties().setHyperlinkBase(newBase);
    presentation.save("hyperlinks-updated.pptx", SaveFormat.Pptx);

    Presentation updatedPresentation = new Presentation("hyperlinks-updated.pptx");
    try {
        System.out.println("Base preserved: " + newBase.equals(updatedPresentation.getDocumentProperties().getHyperlinkBase()));

        updatedPresentation.getDocumentProperties().setHyperlinkBase("");
        updatedPresentation.save("hyperlinks-cleared.pptx", SaveFormat.Pptx);

        Presentation clearedPresentation = new Presentation("hyperlinks-cleared.pptx");
        try {
            System.out.println("Base cleared: " + "".equals(clearedPresentation.getDocumentProperties().getHyperlinkBase()));
        } finally {
            clearedPresentation.dispose();
        }
    } finally {
        updatedPresentation.dispose();
    }
} finally {
    presentation.dispose();
}

2 つの検証メッセージは true と出力されるはずです。ベースをクリアしても、個々のハイパーリンク アドレスはそのまま残ります。

target.pptx への相対リンクは、ベースが old フォルダーを指しているときは old/target.pptx を開きました。ベースを new フォルダーに変更し、保存してプレゼンテーションを再度開くと、同じリンクは new/target.pptx を開きます。絶対リンクは同じファイルを開き続けます。プレゼンテーションを移動したりベースを変更したりする前後で、PowerPoint で相対リンクをテストしてください。

カスタム プレゼンテーション プロパティの追加

カスタム プロパティは、組み込みフィールドに対応しないメタデータを保存します。IDocumentProperties.set_Item メソッドを使用すると、プロパティ名で値を割り当てられます。この例ではプレゼンテーションを作成し、2 つのテキスト値と整数のバージョン値を追加し、Presentation.save でプレゼンテーションを保存します。既存の名前に値を割り当てると、そのプロパティの値が置き換えられます。

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation();
try {
    IDocumentProperties documentProperties = presentation.getDocumentProperties();

    documentProperties.set_Item("Reviewed by", "John Smith");
    documentProperties.set_Item("Confidentiality level", "Internal");
    documentProperties.set_Item("Document version", 2);

    presentation.save("CustomDocumentProperties_output.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

カスタム プロパティへのアクセスと変更

この例ではカスタム プロパティを含むプレゼンテーションを開き、getCountOfCustomProperties を使用してそれらを反復処理します。getCustomPropertyName は 0 ベースのインデックスを受け取り、IDocumentProperties.set_Item で使用される名前を返します。例では各元の名前と値を出力し、元の型に関係なくすべての値を番号付き文字列に置き換えて変更を保存します。カスタム プロパティがない場合、ループは何も変更しません。

import com.aspose.slides.IDocumentProperties;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("AccessAndModifyProperties.pptx");
try {
    IDocumentProperties documentProperties = presentation.getDocumentProperties();

    for (int i = 0; i < documentProperties.getCountOfCustomProperties(); i++) {
        String propertyName = documentProperties.getCustomPropertyName(i);
        Object propertyValue = documentProperties.get_Item(propertyName);

        System.out.println("Custom property name: " + propertyName);
        System.out.println("Custom property value: " + propertyValue);

        documentProperties.set_Item(propertyName, "New Value " + (i + 1));
    }

    presentation.save("CustomProperties_output.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

ライブ例

Aspose.Slides API を使用してドキュメント プロパティを操作する方法を確認するには、オンライン アプリの PowerPoint メタデータの表示と編集 をお試しください:

PowerPoint メタデータの表示と編集

よくある質問

プレゼンテーションから組み込みプロパティを削除するにはどうすればよいですか?

組み込みプロパティはプレゼンテーションの不可欠な部分であり、完全に削除することはできません。ただし、特定のプロパティが許可している場合は、値を変更したり空に設定したりできます。

既に存在するカスタムプロパティを追加した場合はどうなりますか?

既に存在するカスタムプロパティを追加すると、既存の値は新しい値で上書きされます。プロパティを事前に削除したり確認したりする必要はありません。Aspose.Slides が自動的にプロパティの値を更新します。

プレゼンテーションを完全にロードせずにプロパティにアクセスできますか?

はい。PresentationFactory.getPresentationInfo を使用し、続いて IPresentationInfo.readDocumentProperties を呼び出すことで、Presentation インスタンスを作成せずに保存されたドキュメント メタデータを読み取れます。完全なレポート例とフォーマット固有の制限については、Build a Lightweight Presentation Inventory を参照してください。

暗号化されたプレゼンテーションの公開プロパティを開きパスワードなしで読み取れますか?

はい。プレゼンテーションは false を指定して IProtectionManager.setEncryptDocumentProperties を呼び出した後に暗号化され、かつ true を指定して LoadOptions.setOnlyLoadDocumentProperties を呼び出した後にロードされている必要があります。

ドキュメント プロパティのみモードで暗号化された PPTX ファイルを更新できますか?

いいえ。公開プロパティと暗号化されたプロパティのデータは一貫性を保つ必要があるため、暗号化された PPTX ファイルを更新するには、正しい開きパスワードでプレゼンテーション全体をロードする必要があります。