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

はじめに

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

Aspose.Slides を使用すると、DocumentProperties クラスを介してプレゼンテーションのドキュメント プロパティを操作できます。このクラスのインスタンスは Presentation::getDocumentProperties によって返されます。以下の例は、これらのプロパティを読み取り、変更し、管理する方法を示します。これらは PHP/Java Bridge と Aspose.Slides PHP ラッパーが初期化されていることを前提としています。

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

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

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

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

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

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

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

この例では、ドキュメント プロパティがパブリックのままの暗号化されたプレゼンテーションが必要です。ProtectionManager::isOnlyDocumentPropertiesLoaded でロード モードを確認し、その後 Presentation::getDocumentProperties で組み込みプロパティを読み取ります:

use aspose\slides\LoadOptions;
use aspose\slides\Presentation;

$loadOptions = new LoadOptions();
$loadOptions->setOnlyLoadDocumentProperties(true);

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

        echo("Author: " . $documentProperties->getAuthor() . "\n");
        echo("Title: " . $documentProperties->getTitle() . "\n");
        echo("Keywords: " . $documentProperties->getKeywords() . "\n");
    } else {
        echo("The presentation was not loaded in document-properties-only mode." . "\n");
    }
} finally {
    $presentation->dispose();
}

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

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

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

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

use aspose\slides\LoadOptions;
use aspose\slides\Presentation;
use aspose\slides\PresentationFactory;
use aspose\slides\SaveFormat;

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

$loadOptions = new LoadOptions();
$loadOptions->setPassword("open_password");

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

$presentationInfo = PresentationFactory::getInstance()->getPresentationInfo($outputPath);
echo("The presentation is encrypted: " . (java_values($presentationInfo->isEncrypted()) ? "true" : "false") . "\n");

$metadataLoadOptions = new LoadOptions();
$metadataLoadOptions->setOnlyLoadDocumentProperties(true);

$metadataPresentation = new Presentation($outputPath, $metadataLoadOptions);
try {
    if (java_values($metadataPresentation->getProtectionManager()->isOnlyDocumentPropertiesLoaded())) {
        echo("Title: " . $metadataPresentation->getDocumentProperties()->getTitle() . "\n");
        echo("Keywords: " . $metadataPresentation->getDocumentProperties()->getKeywords() . "\n");
    } else {
        echo("The presentation was not loaded in document-properties-only mode." . "\n");
    }
} finally {
    $metadataPresentation->dispose();
}

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

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

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

use aspose\slides\Presentation;

$presentation = new Presentation("AccessBuiltInProperties.pptx");
try {
    $documentProperties = $presentation->getDocumentProperties();

    echo("Category: " . $documentProperties->getCategory() . "\n");
    echo("Content status: " . $documentProperties->getContentStatus() . "\n");
    echo("Creation date: " . $documentProperties->getCreatedTime() . "\n");
    echo("Author: " . $documentProperties->getAuthor() . "\n");
    echo("Comments: " . $documentProperties->getComments() . "\n");
    echo("Keywords: " . $documentProperties->getKeywords() . "\n");
    echo("Last modified by: " . $documentProperties->getLastSavedBy() . "\n");
    echo("Manager: " . $documentProperties->getManager() . "\n");
    echo("Modified date: " . $documentProperties->getLastSavedTime() . "\n");
    echo("Presentation format: " . $documentProperties->getPresentationFormat() . "\n");
    echo("Last print date: " . $documentProperties->getLastPrinted() . "\n");
    echo("Is shared between producers: " . (java_values($documentProperties->getSharedDoc()) ? "true" : "false") . "\n");
    echo("Subject: " . $documentProperties->getSubject() . "\n");
    echo("Title: " . $documentProperties->getTitle() . "\n");
} finally {
    $presentation->dispose();
}

リビジョン番号と編集時間のレポート

presentations フォルダー内の各 PPTX ファイルのリビジョン番号と総編集時間をレポートするには、PresentationInfo::readDocumentProperties を使用してスライドをロードせずにその DocumentProperties を読み取ります。PHP via Java API は編集時間をミリ秒で返します。この例では、時間、分、秒に変換し、24 時間以上の場合は日数も含めます。

use aspose\slides\PresentationFactory;

echo "File | RevisionNumber | TotalEditingTime\n";
foreach (new GlobIterator("presentations/*.pptx") as $file) {
    if (!$file->isFile()) {
        continue;
    }

    $info = PresentationFactory::getInstance()->getPresentationInfo($file->getRealPath());
    $properties = $info->readDocumentProperties();
    $totalSeconds = (int) (java_values($properties->getTotalEditingTime()) / 1000);
    $days = intdiv($totalSeconds, 86400);
    $time = sprintf("%02d:%02d:%02d", intdiv($totalSeconds, 3600) % 24, intdiv($totalSeconds, 60) % 60, $totalSeconds % 60);
    $editingTime = $days > 0 ? $days . "." . $time : $time;
    printf("%s | %d | %s\n", $file->getFilename(), java_values($properties->getRevisionNumber()), $editingTime);
}

リビジョン番号が 17、記録された編集時間が 1 日 2 時間 5 分のプレゼンテーションの場合、レポートは次の出力を示します。

File | RevisionNumber | TotalEditingTime
long.pptx | 17 | 1.02:05:00

DocumentProperties::getRevisionNumber は、アプリケーションが DocumentProperties::setRevisionNumber で変更できる値を取得します。リビジョン番号だけでは、プレゼンテーションが何回保存されたかやバージョン履歴を再構築することはできません。

DocumentProperties::getTotalEditingTime はファイルに記録された編集時間を取得します。誰かが編集した場合でも、プレゼンテーションの記録時間がゼロ (00:00:00) になることがあります。

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

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

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("ModifyBuiltInProperties.pptx");
try {
    $documentProperties = $presentation->getDocumentProperties();

    $documentProperties->setAuthor("Aspose.Slides for PHP 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();
}

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

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

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

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("hyperlinks.pptx");
try {
    echo("Original base: " . $presentation->getDocumentProperties()->getHyperlinkBase() . "\n");

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

    $updatedPresentation = new Presentation("hyperlinks-updated.pptx");
    try {
        echo("Base preserved: " . ((string) $updatedPresentation->getDocumentProperties()->getHyperlinkBase() === $newBase ? "true" : "false") . "\n");

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

        $clearedPresentation = new Presentation("hyperlinks-cleared.pptx");
        try {
            echo("Base cleared: " . ((string) $clearedPresentation->getDocumentProperties()->getHyperlinkBase() === "" ? "true" : "false") . "\n");
        } finally {
            $clearedPresentation->dispose();
        }
    } finally {
        $updatedPresentation->dispose();
    }
} finally {
    $presentation->dispose();
}

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

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

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

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

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation();
try {
    $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 から始まるインデックスを受け取り、DocumentProperties::set_Item で使用される名前を返します。例では各元の名前と値を出力し、元の型に関係なくすべての値を番号付き文字列に置き換えて変更を保存します。カスタム プロパティがない場合、ループは何も変更しません。

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("AccessAndModifyProperties.pptx");
try {
    $documentProperties = $presentation->getDocumentProperties();

    for ($i = 0; $i < java_values($documentProperties->getCountOfCustomProperties()); $i++) {
        $propertyName = $documentProperties->getCustomPropertyName($i);
        $propertyValue = $documentProperties->get_Item($propertyName);

        echo("Custom property name: " . $propertyName . "\n");
        echo("Custom property value: " . $propertyValue . "\n");

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

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

ライブ例

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

FAQ

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

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

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

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

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

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

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

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

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

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