在 JavaScript 中開啟簡報
簡介
Aspose.Slides for Node.js via Java 可以從檔案和資料流載入 PowerPoint 與 OpenDocument 簡報。載入簡報後,您可以檢查其結構、編輯投影片、管理資源,並以原始或其他支援的格式儲存。
可以透過 LoadOptions 類別自訂載入行為。例如,您可以提供開啟密碼、將大型二進位物件保留在 Node.js 記憶體之外、控制外部資源,或省略嵌入的二進位資料。
開啟簡報
載入檔案或資料流後,您可以判斷其原始簡報格式 來決定應用程式如何處理它。
若要開啟現有簡報,將其檔案路徑傳遞給 Presentation 建構函式。使用完畢後請釋放簡報,以便即時釋放檔案處理指標、暫存資料與其他資源。
以下 JavaScript 範例示範如何開啟簡報並取得投影片數量:
const slides = require("aspose.slides.via.java");
const presentation = new slides.Presentation("sample.pptx");
try {
console.log("Slide count: " + presentation.getSlides().size());
} finally {
presentation.dispose();
}
開啟受密碼保護的簡報
開啟密碼會加密簡報內容。若要載入完整簡報,請將正確的密碼傳遞給 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-presentation.pptx", loadOptions);
try {
console.log("Slide count: " + presentation.getSlides().size());
} finally {
presentation.dispose();
}
有關密碼偵測、驗證與加密工作流程,請參閱受密碼保護的簡報。如果已加密的簡報刻意以公開文件屬性儲存,則可在未提供密碼的情況下讀取這些屬性;請參閱管理簡報屬性。
開啟大型簡報
LoadOptions.getBlobManagementOptions 會回傳控制 Aspose.Slides 如何處理二進位大型物件(例如圖片、音訊與影片)的選項。您可以保持來源檔案被鎖定、允許使用暫存檔,並限制保留於記憶體中的 BLOB 資料量。
以下 JavaScript 程式碼示範載入大型簡報(例如 2 GB):
const slides = require("aspose.slides.via.java");
const filePath = "large-presentation.pptx";
const loadOptions = new slides.LoadOptions();
loadOptions.getBlobManagementOptions().setPresentationLockingBehavior(slides.PresentationLockingBehavior.KeepLocked);
loadOptions.getBlobManagementOptions().setTemporaryFilesAllowed(true);
loadOptions.getBlobManagementOptions().setMaxBlobsBytesInMemory(10 * 1024 * 1024);
const presentation = new slides.Presentation(filePath, loadOptions);
try {
presentation.getSlides().get_Item(0).setName("Large presentation");
presentation.save("large-presentation-copy.pptx", slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Note
使用 PresentationLockingBehavior.KeepLocked 時,來源檔案會保持鎖定狀態,直至簡報實例被釋放。請勿在該實例存活期間搬移、覆寫或刪除來源檔案。
Aspose.Slides 可能在載入時複製輸入資料流的內容。對於大型簡報,檔案路徑通常比資料流更有效率。請參閱管理 BLOB 以取得其他儲存與記憶體管理選項。
控制外部資源
LoadOptions.setResourceLoadingCallback 接受一個 IResourceLoadingCallback 實作。回呼可以提供替代資料、重新導向資源、使用預設載入器,或跳過該資源。當簡報包含必須依據應用程式特定安全或儲存規則解析的外部圖片時,這項功能非常有用。
const slides = require("aspose.slides.via.java");
const fs = require("fs");
const java = require("java");
const imageLoadingHandler = java.newProxy("com.aspose.slides.IResourceLoadingCallback", {
resourceLoading: function(args) {
const isJpeg = args.getOriginalUri().toLowerCase().endsWith(".jpg");
const approvedImagePath = "approved-image.jpg";
if (!isJpeg || !fs.existsSync(approvedImagePath)) {
return slides.ResourceLoadingAction.Skip;
}
try {
const imageData = fs.readFileSync(approvedImagePath);
args.setData(imageData);
return slides.ResourceLoadingAction.UserProvided;
} catch (error) {
console.error("The approved replacement image could not be read.");
return slides.ResourceLoadingAction.Skip;
}
}
});
const loadOptions = new slides.LoadOptions();
loadOptions.setResourceLoadingCallback(imageLoadingHandler);
const presentation = new slides.Presentation("presentation-with-external-images.pptx", loadOptions);
try {
console.log("Slide count: " + presentation.getSlides().size());
} finally {
presentation.dispose();
}
載入不含嵌入二進位物件的簡報
簡報可能包含應用程式不需要或不想保留的嵌入二進位資料。範例包括:
- VBA 專案,可透過 Presentation.getVbaProject 取得;
- 嵌入的 OLE 資料,可透過 OleEmbeddedDataInfo.getEmbeddedFileData 取得;
- ActiveX 控制項資料,可透過 Control.getActiveXControlBinary 取得。
將 LoadOptions.setDeleteEmbeddedBinaryObjects 設為 true,即可在載入時移除這些二進位資料。將載入的簡報儲存以保留清理後的結果。
此選項可降低不需要的嵌入式有效負載風險,但它並非完整的惡意程式偵測或內容淨化系統。
const slides = require("aspose.slides.via.java");
const loadOptions = new slides.LoadOptions();
loadOptions.setDeleteEmbeddedBinaryObjects(true);
const presentation = new slides.Presentation("presentation-with-embedded-data.pptx", loadOptions);
try {
presentation.save("presentation-without-embedded-data.pptx", slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
常見問題
如何判斷檔案已損毀且無法開啟?
Aspose.Slides 會在載入期間拋出解析或格式例外。請將此失敗與密碼錯誤的例外分開處理,以便應用程式能正確報告原因。
如果缺少必要的字型會發生什麼情況?
簡報仍可載入,但在渲染與匯出時可能會替代字型。您可以設定字型替代或提供自訂字型,以使輸出更易預測。
載入簡報時也會載入其嵌入的媒體嗎?
嵌入的音訊與影片可透過簡報物件模型取得。外部資源會依照設定的資源載入行為解析,若無法存取其位置,則可能無法取得。