在 JavaScript 中對簡報設定密碼保護

概述

開啟密碼會加密簡報。必須提供正確的密碼才能載入和檢視簡報內容,因此此保護提供機密性。

開啟密碼不同於寫入保護密碼。寫入保護限制修改,但不會加密內容或阻止載入簡報。若要管理修改簡報的密碼,請參閱Write-Protect Presentations

以下工作流程適用於 PPT 與 PPTX 簡報。範例同時使用兩種格式,以說明檔案與串流行為的重要性。

使用開啟密碼加密簡報

使用ProtectionManager.encrypt指派開啟密碼。然後使用Presentation.save將加密的簡報保存下來。

以下範例會加密 PPTX 簡報:

const slides = require("aspose.slides.via.java");

const presentation = new slides.Presentation("pres.pptx");
try {
    presentation.getProtectionManager().encrypt("open_password");
    presentation.save("encrypted-pres.pptx", slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

保持文件屬性公開

預設情況下,Aspose.Slides 會將文件屬性納入簡報加密。ProtectionManager.setEncryptDocumentProperties 方法可獨立於投影片內容加密來控制此行為。在必須讓索引、分類、搜尋或文件管理系統在未提供開啟密碼的情況下讀取中繼資料時,於呼叫ProtectionManager.encrypt之前傳入 false

以下範例會加密 PPTX 簡報,同時將內建文件屬性保留為公開:

const slides = require("aspose.slides.via.java");

const presentation = new slides.Presentation();
try {
    const properties = presentation.getDocumentProperties();
    properties.setAuthor("Contoso Knowledge Management");
    properties.setTitle("Quarterly Product Roadmap");
    properties.setKeywords("roadmap, planning, internal");

    presentation.getSlides().get_Item(0).setName("Encrypted presentation content");
    presentation.getProtectionManager().setEncryptDocumentProperties(false);
    presentation.getProtectionManager().encrypt("open_password");
    presentation.save("public-properties-encrypted.pptx", slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

傳入 falseProtectionManager.setEncryptDocumentProperties 不會使投影片、母片、版面配置、形狀、媒體或其他簡報內容公開。它僅影響文件屬性。若要在不載入加密內容的情況下讀取這些屬性,請參閱Manage Presentation Properties

載入加密的簡報

LoadOptions.setPassword設定為開啟密碼,並在載入檔案時將此選項傳遞給Presentation。若需要開啟密碼但未提供密碼或密碼不正確,載入將失敗。

const slides = require("aspose.slides.via.java");

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

const presentation = new slides.Presentation("encrypted-pres.pptx", loadOptions);
try {
    // 處理已解密的簡報。
} finally {
    presentation.dispose();
}

移除簡報的加密

使用開啟密碼載入簡報,呼叫ProtectionManager.removeEncryption,然後保存結果。保存後的簡報即可在不輸入密碼的情況下載入。

const slides = require("aspose.slides.via.java");

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

const presentation = new slides.Presentation("encrypted-pres.pptx", loadOptions);
try {
    presentation.getProtectionManager().removeEncryption();
    presentation.save("encryption-removed.pptx", slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

在載入前驗證開啟密碼

使用PresentationFactory.getPresentationInfo取得PresentationInfo而不必建立完整的簡報實例。於請求或驗證密碼之前先檢查PresentationInfo.isPasswordProtected。若存在保護,使用PresentationInfo.checkPassword驗證提供的值。

檔案路徑工作流程

以下範例驗證 PPTX 檔案的開啟密碼,將驗證後的值傳遞給LoadOptions.setPassword,然後載入完整的簡報:

const slides = require("aspose.slides.via.java");

const filePath = "protected-presentation.pptx";
const password = "open_password";
const presentationInfo = slides.PresentationFactory.getInstance().getPresentationInfo(filePath);

if (!presentationInfo.isPasswordProtected()) {
    console.log("The presentation does not have an opening password.");
} else if (!presentationInfo.checkPassword(password)) {
    console.log("The opening password is incorrect.");
} else {
    const loadOptions = new slides.LoadOptions();
    loadOptions.setPassword(password);

    const presentation = new slides.Presentation(filePath, loadOptions);
    try {
        console.log("The presentation was validated and loaded successfully.");
    } finally {
        presentation.dispose();
    }
}

串流工作流程

使用PresentationFactory.getPresentationInfoFromStream檢查 Node.js 可讀串流。檢查完畢且串流已被消耗後,請在使用Presentation.createPresentationFromStream載入完整簡報之前建立新的串流。

以下範例使用 PPT 檔案:

const slides = require("aspose.slides.via.java");
const fs = require("fs");

const filePath = "protected-presentation.ppt";
const password = "open_password";
const presentationFactory = slides.PresentationFactory.getInstance();
const infoStream = fs.createReadStream(filePath);

slides.PresentationFactory.getPresentationInfoFromStream(presentationFactory, infoStream, function(infoError, presentationInfo) {
    if (infoError) {
        console.log("The presentation information could not be read: " + infoError.message);
    } else if (!presentationInfo.isPasswordProtected()) {
        console.log("The presentation does not have an opening password.");
    } else if (!presentationInfo.checkPassword(password)) {
        console.log("The opening password is incorrect.");
    } else {
        const loadOptions = new slides.LoadOptions();
        loadOptions.setPassword(password);
        const presentationStream = fs.createReadStream(filePath);

        slides.Presentation.createPresentationFromStream(presentationStream, loadOptions, function(loadError, presentation) {
            if (loadError) {
                console.log("The presentation could not be loaded: " + loadError.message);
            } else {
                try {
                    console.log("The presentation was validated and loaded successfully.");
                } finally {
                    presentation.dispose();
                }
            }
        });
    }
});

checkPassword 回傳值

PresentationInfo.checkPassword僅在簡報具備開啟密碼且提供的密碼正確時回傳 true。在以下情況皆會回傳 false

  • 密碼不正確。
  • 簡報沒有設定開啟密碼。
  • 提供的密碼為 null 或空字串。

此行為對 PPT 與 PPTX 簡報皆相同。

檢查已載入的簡報是否已加密

使用正確密碼載入簡報後,檢查ProtectionManager.isEncrypted以確認來源簡報已被加密。若要在載入前偵測開啟密碼保護,請如上使用[PresentationInfo.isPasswordProtected]。

const slides = require("aspose.slides.via.java");

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

const presentation = new slides.Presentation("encrypted-pres.pptx", loadOptions);
try {
    const isEncrypted = presentation.getProtectionManager().isEncrypted();
    console.log("The presentation is encrypted: " + isEncrypted);
} finally {
    presentation.dispose();
}

安全性建議

在線上為簡報設定密碼保護

  1. 開啟 Aspose.Slides Lock 應用程式。
  2. 選取或上傳簡報。
  3. 輸入觀看保護的密碼。
  4. (可選)為編輯保護輸入另一組密碼。
  5. 套用保護並下載產生的檔案。

常見問題

開啟密碼與寫入保護密碼有何不同?

開啟密碼會加密簡報,且必須提供才能載入其內容。寫入保護密碼僅限制修改,並不加密內容。

我可以在不載入所有投影片的情況下驗證開啟密碼嗎?

可以。取得簡報資訊,檢查是否存在開啟密碼保護,然後在建立完整簡報實例之前驗證密碼。

應用程式可以在沒有開啟密碼的情況下讀取中繼資料嗎?

可以,但僅限於簡報在停用文件屬性加密的情況下。此時應用程式必須使用在Manage Presentation Properties中描述的僅文件屬性載入模式。

密碼檢查工作流程同時支援 PPT 與 PPTX 嗎?

是的。檔案路徑與串流式的密碼偵測與驗證對 PPT 與 PPTX 簡報的行為相同。