在 Java 中儲存簡報
概述
建立簡報或開啟已存在的簡報之後,使用 Presentation.save 方法寫入結果。Aspose.Slides for Java 可以將簡報儲存為檔案或串流,支援 PowerPoint、OpenDocument、PDF 等多種格式。以下章節說明標準儲存操作以及 PPTX 輸出可用的選項。
將簡報儲存為檔案
要將簡報儲存為檔案,將輸出路徑與一個 SaveFormat 值傳入 Presentation.save 方法。格式值決定 Aspose.Slides 產生的檔案類型。
以下範例建立簡報並將其儲存為 PPTX 檔案:
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
Presentation presentation = new Presentation();
try {
// 在此新增或修改簡報內容。
presentation.save("Output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
將簡報儲存為原始格式
有關檔案與串流偵測範例、新建立簡報的行為,以及來源與輸出格式之差異,請參閱 確定原始簡報格式。
在批次處理應用程式中,輸入格式可能事先未知。載入檔案後,從 IPresentation.getSourceFormat 方法讀取其原始格式。將取得的 SourceFormat 值傳給 SlideUtil.toSaveFormat 以取得相對應的 SaveFormat 值,然後使用 Presentation.save 寫入已修改的簡報。
以下完整範例處理輸入目錄中的每個檔案,更新其標題,並以載入時的格式儲存到輸出目錄:
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 會將 PPT、PPTX、ODP、PPTM、PPSX、PPSM、POTX、POTM、PPS、POT、OTP、FODP 以及 PowerPoint XML 映射到相對應的簡報儲存格式。它僅映射簡報來源格式;不應用於選擇 PDF、HTML、TIFF 或影像等匯出格式。傳入不支援或無效的 SourceFormat 會拋出 IllegalArgumentException。
舊版 PPT、PPS 與 POT 使用相同的二進位容器。如果此類簡報從沒有副檔名的串流載入,PPS 或 POT 檔案可能會被識別為 PPT。若需要保留這些舊版子類型,請另外保留原始檔名或格式中繼資料,並在選擇輸出檔名與格式時使用它們。
將簡報儲存為串流
若不想依賴最終檔案路徑寫入簡報,請將可寫入的串流與一個 SaveFormat 值傳入 Presentation.save 方法。此方式在需要從 Web 服務返回、存入資料庫或在記憶體中處理輸出時非常有用。
以下範例將新簡報儲存到檔案串流:
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();
}
以預先定義的檢視類型儲存簡報
您可以指定 PowerPoint 開啟已儲存簡報時的預設檢視。於儲存前使用 ViewProperties.setLastView 方法,傳入 ViewType 值。
以下範例將 Slide Master 檢視設定為初始檢視:
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();
}
以 Strict Office Open XML 格式儲存簡報
若要建立符合 Office Open XML Strict 檔案設定的 PPTX,先建立 PptxOptions 實例,並使用其 setConformance 方法,傳入 Conformance.Iso29500_2008_Strict。然後將此選項傳給 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();
}
以 Zip64 模式儲存 Office Open XML 格式簡報
標準 ZIP 壓縮檔對每個項目的壓縮與未壓縮大小、總檔案大小以及項目數量都有上限。因為 PPTX 本身即為 ZIP 壓縮檔,極大型的簡報可能會超過這些限制。ZIP64 延伸可提升相關大小與項目數上限。
使用 PptxOptions.setZip64Mode 方法控制 Aspose.Slides 是否寫入 ZIP64 延伸:
- IfNecessary 只在簡報超出標準 ZIP 限制時使用 ZIP64,為預設模式。
- Never 停用 ZIP64 延伸。
- Always 總是寫入 ZIP64 延伸。
以下範例於輸出簡報時始終啟用 ZIP64 延伸:
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();
}
以壓縮等級儲存 Office Open XML 格式簡報
對於 PPTX 輸出,您可以使用 PptxOptions.setCompressionLevel 方法在儲存速度與檔案大小之間取得平衡。CompressionLevel 類別提供以下值:
- None 不進行壓縮。
- Level1 壓縮最快,產生最大的壓縮檔。
- Level2 至 Level5 逐步偏好較小的輸出而犧牲儲存速度。
- Level6 在儲存速度與檔案大小間取得平衡,為預設等級。
- Level7 與 Level8 更偏好較小的輸出。
- Level9 提供最強的壓縮,需耗費最多處理時間。
以下範例在無壓縮的情況下儲存簡報:
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();
}
以下範例使用最高壓縮等級:
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();
}
儲存簡報時不重新整理縮圖
當簡報以 PPTX 格式儲存時,PptxOptions.setRefreshThumbnail 方法可控制文件縮圖:
true在儲存過程中重新產生縮圖,為預設值。false保留現有縮圖。如果簡報本身沒有縮圖,Aspose.Slides 不會產生新縮圖。
以下範例在儲存時不重新整理縮圖:
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
停用縮圖重新整理可以縮短 PPTX 檔案的儲存時間。以百分比方式接收儲存進度更新
若要監控儲存作業,請實作 IProgressCallback 介面,並將實作傳入 ISaveOptions.setProgressCallback 方法。Aspose.Slides 會在匯出期間呼叫 IProgressCallback.reporting 方法,傳回進度值。
以下範例在主控台上報告 PDF 匯出的進度:
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 提供免費的 PowerPoint Splitter 應用程式,內部使用 Aspose.Slides API。它可將簡報中的選定投影片另存為獨立的 PPT 或 PPTX 檔案。常見問題
Aspose.Slides 是否支援增量或「快速儲存」?
不支援。每次儲存都會寫入完整的輸出檔,而不是僅更新變更的部分。
多執行緒可以同時儲存相同的 Presentation 實例嗎?
不可以。Presentation 實例 非執行緒安全,請一次只由單一執行緒存取與儲存。
儲存簡報時,超連結與外部連結檔案會發生什麼事?
Hyperlinks 仍會保留在簡報中。Aspose.Slides 不會複製外部連結的檔案,因此儲存後的簡報仍須能存取這些位置。
我可以儲存文件的中繼資料(例如作者、標題、公司或建立日期)嗎?
可以。於儲存前設定相應的文件屬性,Aspose.Slides 會將它們寫入輸出檔。