Lưu bản trình chiếu trên Android
Tổng quan
Sau khi bạn tạo một bản trình chiếu hoặc mở một bản hiện có, sử dụng phương thức Presentation.save để ghi kết quả. Aspose.Slides for Android qua Java có thể lưu một bản trình chiếu vào tệp hoặc luồng dưới dạng PowerPoint, OpenDocument, PDF và các định dạng khác. Các phần sau đây đề cập đến các thao tác lưu tiêu chuẩn và các tùy chọn có sẵn cho đầu ra PPTX.
Lưu bản trình chiếu vào tệp
Để lưu một bản trình chiếu vào tệp, truyền đường dẫn đầu ra và một giá trị SaveFormat vào phương thức Presentation.save. Giá trị định dạng xác định loại tệp mà Aspose.Slides tạo.
Ví dụ sau tạo một bản trình chiếu và lưu nó dưới dạng tệp PPTX:
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
Presentation presentation = new Presentation();
try {
// Thêm hoặc chỉnh sửa nội dung bản trình chiếu tại đây.
presentation.save("Output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Lưu bản trình chiếu ở định dạng gốc
Đối với các ví dụ về phát hiện tệp và luồng, hành vi của các bản trình chiếu mới tạo, và sự phân biệt giữa định dạng nguồn và đầu ra, xem Determine the Original Presentation Format.
Trong một ứng dụng xử lý hàng loạt, định dạng đầu vào có thể không được biết trước. Sau khi tải một tệp, đọc định dạng gốc của nó bằng phương thức IPresentation.getSourceFormat. Truyền giá trị SourceFormat thu được vào SlideUtil.toSaveFormat để lấy giá trị SaveFormat tương ứng, sau đó sử dụng Presentation.save để ghi bản trình chiếu đã chỉnh sửa.
Ví dụ hoàn chỉnh sau xử lý mọi tệp trong thư mục đầu vào, cập nhật tiêu đề của chúng và lưu vào thư mục đầu ra ở định dạng mà chúng đã được tải:
import com.aspose.slides.Presentation;
import com.aspose.slides.SlideUtil;
import java.io.File;
File inputDirectory = new File("Input");
File outputDirectory = new File("Output");
if (!outputDirectory.exists() && !outputDirectory.mkdirs()) {
System.err.println("Cannot create the output directory.");
}
File[] inputFiles = inputDirectory.listFiles(File::isFile);
if (inputFiles != null && outputDirectory.isDirectory()) {
for (File inputFile : inputFiles) {
try {
Presentation presentation = new Presentation(inputFile.getPath());
try {
int saveFormat = SlideUtil.toSaveFormat(presentation.getSourceFormat());
presentation.getDocumentProperties().setTitle("Processed by the batch application");
File outputFile = new File(outputDirectory, inputFile.getName());
presentation.save(outputFile.getPath(), saveFormat);
} finally {
presentation.dispose();
}
} catch (IllegalArgumentException exception) {
System.err.println("Cannot map the source format of '" + inputFile.getPath() + "': " + exception.getMessage());
} catch (Exception exception) {
System.err.println("Cannot process '" + inputFile.getPath() + "': " + exception.getMessage());
}
}
}
SlideUtil.toSaveFormat ánh xạ PPT, PPTX, ODP, PPTM, PPSX, PPSM, POTX, POTM, PPS, POT, OTP, FODP và PowerPoint XML sang các định dạng lưu bản trình chiếu tương ứng. Nó chỉ ánh xạ các định dạng nguồn của bản trình chiếu; không được dùng để chọn các định dạng xuất như PDF, HTML, TIFF hoặc hình ảnh. Truyền một giá trị SourceFormat không được hỗ trợ hoặc không hợp lệ sẽ gây ra một IllegalArgumentException.
Các tệp PPT, PPS và POT cổ điển sử dụng cùng một container nhị phân. Khi một bản trình chiếu như vậy được tải từ luồng mà không có phần mở rộng tệp, một tệp PPS hoặc POT có thể bị nhận dạng là PPT. Nếu cần giữ nguyên các kiểu phụ cổ này, hãy giữ lại tên tệp gốc hoặc siêu dữ liệu định dạng riêng và sử dụng chúng khi chọn tên tệp và định dạng đầu ra.
Lưu bản trình chiếu vào luồng
Để ghi một bản trình chiếu mà không dựa vào đường dẫn tệp cuối cùng, truyền một luồng có thể ghi và một giá trị SaveFormat vào phương thức Presentation.save. Cách này hữu ích khi đầu ra cần được trả về từ dịch vụ web, lưu vào cơ sở dữ liệu hoặc xử lý trong bộ nhớ.
Ví dụ sau lưu một bản trình chiếu mới vào luồng tệp:
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import java.io.FileOutputStream;
import java.io.OutputStream;
Presentation presentation = new Presentation();
try {
OutputStream outputStream = new FileOutputStream("Output.pptx");
try {
presentation.save(outputStream, SaveFormat.Pptx);
} finally {
outputStream.close();
}
} finally {
presentation.dispose();
}
Lưu bản trình chiếu với Kiểu hiển thị đã xác định trước
Bạn có thể chỉ định chế độ xem mà PowerPoint sẽ mở bản trình chiếu đã lưu ban đầu. Sử dụng phương thức ViewProperties.setLastView với một giá trị ViewType trước khi lưu.
Ví dụ sau cấu hình chế độ xem Slide Master làm chế độ xem ban đầu:
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.ViewType;
Presentation presentation = new Presentation();
try {
presentation.getViewProperties().setLastView(ViewType.SlideMasterView);
presentation.save("SlideMasterView.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Lưu bản trình chiếu ở định dạng Office Open XML Strict
Để tạo một tệp PPTX tuân thủ hồ sơ Strict của Office Open XML, tạo một thể hiện PptxOptions và sử dụng phương thức setConformance của nó với Conformance.Iso29500_2008_Strict. Sau đó truyền các tùy chọn này vào phương thức Presentation.save.
import com.aspose.slides.Conformance;
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
PptxOptions options = new PptxOptions();
options.setConformance(Conformance.Iso29500_2008_Strict);
Presentation presentation = new Presentation();
try {
presentation.save("StrictOfficeOpenXml.pptx", SaveFormat.Pptx, options);
} finally {
presentation.dispose();
}
Lưu bản trình chiếu ở định dạng Office Open XML ở chế độ Zip64
Một tệp ZIP tiêu chuẩn giới hạn kích thước nén và chưa nén của từng mục, tổng kích thước kho lưu trữ và số lượng mục. Vì tệp PPTX là một tệp ZIP, một bản trình chiếu rất lớn có thể vượt quá các giới hạn này. Các phần mở rộng ZIP64 nâng cao các giới hạn kích thước và số lượng mục áp dụng.
Sử dụng phương thức PptxOptions.setZip64Mode để kiểm soát việc Aspose.Slides ghi các phần mở rộng ZIP64:
- IfNecessary chỉ sử dụng ZIP64 khi bản trình chiếu vượt quá giới hạn ZIP tiêu chuẩn. Đây là chế độ mặc định.
- Never vô hiệu hoá các phần mở rộng ZIP64.
- Always luôn ghi các phần mở rộng ZIP64.
Ví dụ sau luôn bật các phần mở rộng ZIP64 cho bản trình chiếu đầu ra:
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.Zip64Mode;
Presentation presentation = new Presentation("Sample.pptx");
try {
PptxOptions options = new PptxOptions();
options.setZip64Mode(Zip64Mode.Always);
presentation.save("OutputZip64.pptx", SaveFormat.Pptx, options);
} finally {
presentation.dispose();
}
Warning
Nếu sử dụng Zip64Mode.Never và bản trình chiếu không thể nằm trong giới hạn ZIP tiêu chuẩn, thao tác lưu sẽ ném ra một PptxException.Lưu bản trình chiếu ở định dạng Office Open XML với mức nén
Đối với đầu ra PPTX, bạn có thể cân bằng tốc độ lưu và kích thước tệp bằng cách sử dụng phương thức PptxOptions.setCompressionLevel. Lớp CompressionLevel cung cấp các giá trị sau:
- None lưu dữ liệu không nén.
- Level1 cung cấp nén nhanh nhất và đầu ra nén lớn nhất.
- Level2 đến Level5 dần ưu tiên đầu ra nhỏ hơn hơn tốc độ lưu.
- Level6 cân bằng tốc độ lưu và kích thước tệp. Đây là mức mặc định.
- Level7 và Level8 tiếp tục ưu tiên đầu ra nhỏ hơn hơn tốc độ lưu.
- Level9 cung cấp mức nén mạnh nhất và yêu cầu thời gian xử lý lâu nhất.
Ví dụ sau lưu một bản trình chiếu mà không nén:
import com.aspose.slides.CompressionLevel;
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
Presentation presentation = new Presentation("Sample.pptx");
try {
PptxOptions options = new PptxOptions();
options.setCompressionLevel(CompressionLevel.None);
presentation.save("OutputNoCompression.pptx", SaveFormat.Pptx, options);
} finally {
presentation.dispose();
}
Ví dụ sau sử dụng mức nén tối đa:
import com.aspose.slides.CompressionLevel;
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
Presentation presentation = new Presentation("Sample.pptx");
try {
PptxOptions options = new PptxOptions();
options.setCompressionLevel(CompressionLevel.Level9);
presentation.save("OutputMaximumCompression.pptx", SaveFormat.Pptx, options);
} finally {
presentation.dispose();
}
Lưu bản trình chiếu mà không làm mới ảnh thu nhỏ
Khi một bản trình chiếu được lưu dưới dạng PPTX, phương thức PptxOptions.setRefreshThumbnail kiểm soát ảnh thu nhỏ của tài liệu:
truetạo lại ảnh thu nhỏ trong quá trình lưu. Đây là giá trị mặc định.falsegiữ nguyên ảnh thu nhỏ hiện có. Nếu bản trình chiếu không có ảnh thu nhỏ, Aspose.Slides sẽ không tạo.
Ví dụ sau lưu một bản trình chiếu mà không làm mới ảnh thu nhỏ của nó:
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
Presentation presentation = new Presentation("Sample.pptx");
try {
PptxOptions options = new PptxOptions();
options.setRefreshThumbnail(false);
presentation.save("Output.pptx", SaveFormat.Pptx, options);
} finally {
presentation.dispose();
}
Note
Vô hiệu hoá việc làm mới ảnh thu nhỏ có thể giảm thời gian cần thiết để lưu tệp PPTX.Cập nhật tiến độ lưu theo phần trăm
Để giám sát một thao tác lưu, triển khai giao diện IProgressCallback và truyền triển khai này vào phương thức ISaveOptions.setProgressCallback. Aspose.Slides sau đó sẽ gọi phương thức IProgressCallback.reporting với các giá trị tiến độ trong quá trình xuất.
Ví dụ sau báo cáo tiến độ xuất PDF lên console:
import com.aspose.slides.IProgressCallback;
import com.aspose.slides.PdfOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
class ExportProgressHandler implements IProgressCallback {
public void reporting(double progressValue) {
int progress = (int) progressValue;
System.out.println(progress + "% of the file has been converted.");
}
}
PdfOptions options = new PdfOptions();
options.setProgressCallback(new ExportProgressHandler());
Presentation presentation = new Presentation("Sample.pptx");
try {
presentation.save("Output.pdf", SaveFormat.Pdf, options);
} finally {
presentation.dispose();
}
Note
Aspose cung cấp một công cụ PowerPoint Splitter miễn phí được xây dựng bằng API Aspose.Slides. Nó lưu các slide đã chọn từ một bản trình chiếu dưới dạng các tệp PPT hoặc PPTX riêng biệt.Câu hỏi thường gặp
Aspose.Slides có hỗ trợ lưu tăng dần hoặc “lưu nhanh” không?
Không. Mỗi thao tác lưu ghi một tệp đầu ra hoàn chỉnh thay vì chỉ cập nhật các phần đã thay đổi.
Nhiều luồng có thể lưu cùng một thể hiện Presentation không?
Không. Một thể hiện Presentation không an toàn với đa luồng. Truy cập và lưu mỗi thể hiện chỉ từ một luồng tại một thời điểm.
Điều gì xảy ra với siêu liên kết và các tệp được liên kết bên ngoài khi tôi lưu một bản trình chiếu?
Hyperlinks vẫn còn trong bản trình chiếu. Aspose.Slides không sao chép các tệp được liên kết bên ngoài, vì vậy bản trình chiếu đã lưu vẫn phải có khả năng truy cập tới vị trí của chúng.
Tôi có thể lưu siêu dữ liệu tài liệu như tác giả, tiêu đề, công ty và ngày tạo không?
Có. Đặt các thuộc tính tài liệu phù hợp trước khi lưu, và Aspose.Slides sẽ ghi chúng vào tệp đầu ra.