在 Android 上開啟簡報

簡介

Aspose.Slides for Android via Java 可以從檔案和串流載入 PowerPoint 與 OpenDocument 簡報。載入簡報後,您可以檢查其結構、編輯投影片、管理資源,並以原始或其他支援的格式儲存。

可以透過 LoadOptions 類別自訂載入行為。例如,您可以提供開啟密碼、將大型二進位物件保留在 Java 堆外、控制外部資源,或省略內嵌的二進位資料。

開啟簡報

載入檔案或串流後,您可以確定其原始簡報格式,以決定應用程式的處理方式。

要開啟現有簡報,將其檔案路徑傳遞給 Presentation 建構函式。使用完畢後請釋放簡報,以便即時釋放檔案句柄、暫存資料及其他資源。

以下 Java 程式碼示範如何開啟簡報並取得投影片數量:

import com.aspose.slides.Presentation;

Presentation presentation = new Presentation("sample.pptx");
try {
    System.out.println("Slide count: " + presentation.getSlides().size());
} finally {
    presentation.dispose();
}

開啟受密碼保護的簡報

開啟密碼會加密簡報內容。若要載入完整簡報,請將正確的密碼傳遞給 LoadOptions.setPassword,並將選項提供給 Presentation 建構函式。若密碼遺漏或不正確,載入將失敗。

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

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

Presentation presentation = new Presentation("encrypted-presentation.pptx", loadOptions);
try {
    System.out.println("Slide count: " + presentation.getSlides().size());
} finally {
    presentation.dispose();
}

若需密碼偵測、驗證與加密工作流程,請參閱密碼保護簡報。如果已加密的簡報故意以公開文件屬性儲存,這些屬性可在不提供密碼的情況下讀取;請參閱管理簡報屬性。

開啟大型簡報

LoadOptions.getBlobManagementOptions 會回傳控制 Aspose.Slides 處理二進位大型物件(如影像、音訊與視訊)的選項。您可以將來源檔案保持鎖定、允許暫存檔案,並限制保留在記憶體中的 BLOB 資料量。

以下 Java 程式碼示範如何載入大型簡報(例如 2 GB):

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

final String filePath = "large-presentation.pptx";

LoadOptions loadOptions = new LoadOptions();
loadOptions.getBlobManagementOptions().setPresentationLockingBehavior(PresentationLockingBehavior.KeepLocked);
loadOptions.getBlobManagementOptions().setTemporaryFilesAllowed(true);
loadOptions.getBlobManagementOptions().setMaxBlobsBytesInMemory(10 * 1024 * 1024);

Presentation presentation = new Presentation(filePath, loadOptions);
try {
    presentation.getSlides().get_Item(0).setName("Large presentation");
    presentation.save("large-presentation-copy.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

控制外部資源

LoadOptions.setResourceLoadingCallback 接受 IResourceLoadingCallback 的實作。回呼函式可以提供替代資料、重新導向資源、使用預設載入器,或跳過該資源。當簡報包含必須依據應用程式特定安全或儲存規則解析的外部影像時,此功能相當有用。

import com.aspose.slides.IResourceLoadingArgs;
import com.aspose.slides.IResourceLoadingCallback;
import com.aspose.slides.LoadOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.ResourceLoadingAction;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;

class ImageLoadingHandler implements IResourceLoadingCallback {
    public int resourceLoading(IResourceLoadingArgs args) {
        boolean isJpeg = args.getOriginalUri().toLowerCase(Locale.ROOT).endsWith(".jpg");
        Path approvedImagePath = Paths.get("approved-image.jpg");
        if (!isJpeg || !Files.exists(approvedImagePath)) {
            return ResourceLoadingAction.Skip;
        }

        try {
            byte[] imageData = Files.readAllBytes(approvedImagePath);
            args.setData(imageData);
            return ResourceLoadingAction.UserProvided;
        } catch (IOException exception) {
            System.err.println("The approved replacement image could not be read.");
            return ResourceLoadingAction.Skip;
        }
    }
}

LoadOptions loadOptions = new LoadOptions();
loadOptions.setResourceLoadingCallback(new ImageLoadingHandler());

Presentation presentation = new Presentation("presentation-with-external-images.pptx", loadOptions);
try {
    System.out.println("Slide count: " + presentation.getSlides().size());
} finally {
    presentation.dispose();
}

載入不含嵌入二進位物件的簡報

簡報可能包含應用程式不需要或不想保留的嵌入二進位資料。例如:

將 LoadOptions.setDeleteEmbeddedBinaryObjects 設為 true,即可在載入時移除這些二進位資料。將載入後的簡報儲存以保留已清理的結果。

此選項可降低不必要的嵌入式負載風險,但其並非完整的惡意程式偵測或內容清理系統。

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

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

Presentation presentation = new Presentation("presentation-with-embedded-data.pptx", loadOptions);
try {
    presentation.save("presentation-without-embedded-data.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

常見問題

如何判斷檔案已損毀且無法開啟?

Aspose.Slides 會在載入期間拋出解析或格式例外。請將此失敗與密碼錯誤的例外分開處理,以便應用程式能正確回報原因。

如果缺少所需字體會發生什麼情況?

簡報仍可載入,但在渲染與匯出時可能會替換字體。您可以設定字體替換或提供自訂字體,以使輸出更可預測。

載入簡報時是否同時載入其嵌入媒體?

嵌入的音訊與視訊可透過簡報物件模型存取。外部資源會依照設定的資源載入行為解析,若無法存取其位置則可能無法使用。