在 Java 中管理簡報屬性

簡介

Aspose.Slides for Java 支援 內建 和 自訂 文件屬性。使用這些屬性來讀取和更新簡報的中繼資料,例如作者、標題和使用者自訂的值。

Aspose.Slides 允許您透過 IDocumentProperties 介面來操作簡報的文件屬性。此介面的實例是由 IPresentation.getDocumentProperties 回傳。以下範例說明如何讀取、修改與管理這些屬性。

管理簡報屬性

文件屬性在簡報內容旁邊儲存描述性資訊。它們分為兩類:

  • 內建屬性 包含標準中繼資料,例如標題、作者和文件統計資訊。
  • 自訂屬性 儲存使用者自訂的名稱/值對。

Aspose.Slides for Java 提供對兩種屬性的存取。在 PowerPoint 中,選取 File → Info → Properties → Advanced Properties 以檢視文件中繼資料。對話方塊將資訊分組於 Summary、Statistics、Contents 與 Custom 等分頁。使用 Custom 分頁來管理使用者自訂屬性。

從加密的簡報讀取公開屬性

開啟密碼通常會保護簡報內容與文件屬性。當簡報在呼叫 IProtectionManager.setEncryptDocumentProperties 並將參數設為 false 後再進行加密,其文件屬性將保持公開。此時應用程式可呼叫 LoadOptions.setOnlyLoadDocumentProperties 並將參數設為 true,在不提供開啟密碼的情況下讀取公開的中繼資料。

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 檔案,在呼叫 LoadOptions.setOnlyLoadDocumentProperties 並將參數設為 true 後載入的簡報,旨在讀取公開的中繼資料。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 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();
}

兩個驗證訊息應印出 true。清除基礎不會影響個別超連結位址。

當基礎指向 old 資料夾時,指向 target.pptx 的相對連結會開啟 old/target.pptx。將基礎更改為 new 資料夾,儲存並重新開啟簡報後,同一連結會開啟 new/target.pptx。絕對連結則仍然開啟相同的檔案。請在 PowerPoint 中於移動簡報或變更基礎前後測試您的相對連結。

新增自訂簡報屬性

自訂屬性儲存沒有對應內建欄位的中繼資料。IDocumentProperties.set_Item 方法允許您依屬性名稱指派值。此範例建立一個簡報,加入兩個文字值與一個整數版本值,並使用 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 接受從零開始的索引,並回傳用於 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();
}

即時範例

請嘗試線上應用程式 View & Edit PowerPoint Metadata 以了解如何使用 Aspose.Slides API 處理文件屬性:

檢視與編輯 PowerPoint 中繼資料

常見問題

如何從簡報中移除內建屬性?

內建屬性是簡報的整體組成部分,無法完全移除。然而,您可以變更其值,或者在該屬性允許的情況下將其設為空值。

如果我新增的自訂屬性已存在會發生什麼?

如果您新增的自訂屬性已存在,其現有值會被新值覆蓋。您無需事先移除或檢查該屬性,因為 Aspose.Slides 會自動更新屬性的值。

我能在不完整載入簡報的情況下存取簡報屬性嗎?

可以。使用 PresentationFactory.getPresentationInfo,然後使用 IPresentationInfo.readDocumentProperties 讀取儲存的文件中繼資料,而無需建立 Presentation 實例。請參閱 建立輕量級簡報清查 以取得完整的報告範例與格式特定的限制。

我能在不提供開啟密碼的情況下讀取加密簡報的公開屬性嗎?

可以。簡報必須在呼叫 IProtectionManager.setEncryptDocumentProperties 並將參數設為 false 後進行加密,且在呼叫 LoadOptions.setOnlyLoadDocumentProperties 並將參數設為 true 後載入。

我能在僅載入文件屬性的模式下更新加密的 PPTX 檔案嗎?

不能。公開與加密的屬性資料必須保持一致,因此更新加密的 PPTX 檔案需要以正確的開啟密碼完成完整的載入。