Mở bản trình bày trong Java

Giới thiệu

Aspose.Slides for Java có thể tải các bản trình bày PowerPoint và OpenDocument từ tệp và luồng. Sau khi một bản trình bày được tải, bạn có thể kiểm tra cấu trúc của nó, chỉnh sửa các slide, quản lý tài nguyên và lưu nó ở định dạng gốc hoặc một định dạng được hỗ trợ khác.

Hành vi tải có thể được tùy chỉnh thông qua lớp LoadOptions. Ví dụ, bạn có thể cung cấp mật khẩu mở, giữ các đối tượng nhị phân lớn ngoài bộ nhớ heap của Java, kiểm soát tài nguyên bên ngoài, hoặc bỏ qua dữ liệu nhị phân nhúng.

Mở bản trình bày

Sau khi tải tệp hoặc luồng, bạn có thể xác định định dạng bản trình bày gốc để chọn cách ứng dụng của mình xử lý nó.

Để mở một bản trình bày hiện có, truyền đường dẫn tệp vào trình tạo Presentation. Giải phóng bản trình bày sau khi sử dụng để các tay cầm tệp, dữ liệu tạm và các tài nguyên khác được giải phóng kịp thời.

Ví dụ Java sau cho thấy cách mở một bản trình bày và lấy số lượng slide của nó:

import com.aspose.slides.Presentation;

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

Mở bản trình bày có mật khẩu bảo vệ

Mật khẩu mở mã hoá nội dung bản trình bày. Để tải toàn bộ bản trình bày, truyền mật khẩu đúng vào LoadOptions.setPassword và cung cấp các tùy chọn cho trình tạo Presentation. Việc tải sẽ thất bại khi mật khẩu bị thiếu hoặc không đúng.

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();
}

Đối với việc phát hiện, xác thực và quy trình mã hoá mật khẩu, xem Password-Protect Presentations. Nếu một bản trình bày đã được mã hoá nhưng được lưu có thuộc tính tài liệu công khai, các thuộc tính đó vẫn có thể đọc được mà không cần mật khẩu; xem Manage Presentation Properties.

Mở bản trình bày lớn

LoadOptions.getBlobManagementOptions trả về các tùy chọn kiểm soát cách Aspose.Slides xử lý các đối tượng nhị phân lớn như hình ảnh, âm thanh và video. Bạn có thể giữ tệp nguồn bị khóa, cho phép tệp tạm và giới hạn lượng dữ liệu BLOB được giữ trong bộ nhớ.

Mã Java sau minh họa việc tải một bản trình bày lớn (ví dụ, 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();
}

Kiểm soát tài nguyên bên ngoài

LoadOptions.setResourceLoadingCallback chấp nhận một triển khai IResourceLoadingCallback. Callback có thể cung cấp dữ liệu thay thế, chuyển hướng tài nguyên, sử dụng bộ tải mặc định, hoặc bỏ qua tài nguyên. Điều này hữu ích khi bản trình bày chứa các hình ảnh bên ngoài cần được giải quyết theo các quy tắc bảo mật hoặc lưu trữ riêng của ứng dụng.

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();
}

Tải bản trình bày mà không có đối tượng nhị phân nhúng

Một bản trình bày có thể chứa dữ liệu nhị phân nhúng mà ứng dụng không cần hoặc không muốn giữ lại. Các ví dụ bao gồm:

Đặt LoadOptions.setDeleteEmbeddedBinaryObjects thành true để loại bỏ dữ liệu nhị phân này khi tải. Lưu bản trình bày đã tải để giữ lại kết quả đã được làm sạch.

Tùy chọn này giảm thiểu việc tiếp xúc với các payload nhúng không mong muốn, nhưng không phải là một hệ thống phát hiện phần mềm độc hại hay làm sạch nội dung hoàn chỉnh.

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();
}

Câu hỏi thường gặp

Làm sao tôi biết một tệp bị hỏng và không thể mở?

Aspose.Slides ném ra ngoại lệ phân tích cú pháp hoặc định dạng trong quá trình tải. Xử lý lỗi này riêng biệt với lỗi mật khẩu không đúng để ứng dụng có thể báo cáo nguyên nhân một cách chính xác.

Điều gì sẽ xảy ra nếu các phông chữ bắt buộc bị thiếu?

Bản trình bày vẫn có thể tải, nhưng quá trình hiển thị và xuất có thể thay thế phông chữ. Bạn có thể cấu hình thay thế phông chữ hoặc cung cấp phông chữ tùy chỉnh để kết quả đầu ra dự đoán được hơn.

Việc tải một bản trình bày có đồng thời tải các phương tiện nhúng không?

Âm thanh và video nhúng sẽ khả dụng qua mô hình đối tượng của bản trình bày. Các tài nguyên bên ngoài được giải quyết theo hành vi tải tài nguyên đã cấu hình và có thể không khả dụng nếu không thể truy cập vị trí của chúng.