保存文档
您需要使用Aspose.Words执行的大多数任务都涉及保存文档。 保存文档Aspose.Words提供Document类的Save方法。 文档可以以Aspose.Words支持的任何保存格式保存。 有关所有支持的保存格式的列表,请参阅SaveFormat枚举。
保存到文件
只需使用带有文件名的Save方法。 Aspose.Words将从您指定的文件扩展名确定保存格式。
下面的代码示例演示如何将文档加载并保存到文件中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(LoadAndSave.class); | |
Document doc = new Document(dataDir+ "Test File (doc).doc"); | |
// Save the finished document to disk. | |
doc.save(dataDir + "Test File (doc)_out.doc", SaveFormat.PNG); |
保存到流
将stream对象传递给Save方法。 在保存到流时,有必要显式指定保存格式。
下面的代码示例演示如何将文档加载并保存到流中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(LoadAndSaveToStream.class); | |
String inputFile = "Test File (doc).doc"; | |
String outputFile = "output.png"; | |
InputStream in = new FileInputStream(dataDir + inputFile); | |
OutputStream out = new FileOutputStream(dataDir + outputFile); | |
Document doc = new Document(in); | |
// Save the finished document to disk. | |
doc.save(out, SaveFormat.PNG); |
您可以从以下位置下载此示例的模板文件 Aspose.Words GitHub.
保存到PCL
Aspose.Words支持将文档保存为PCL(打印机命令语言)。 Aspose.Words可以将文档保存为PCL6(PCL6增强或PCLXL)格式。 在将文档保存为PCL格式时,可以使用PclSaveOptions
类指定其他选项。
下面的代码示例演示如何使用保存选项将文档保存到PCL:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ConvertDocumentToPCL.class); | |
// Load the document from disk. | |
Document doc = new Document(dataDir + "Document.doc"); | |
PclSaveOptions saveOptions = new PclSaveOptions(); | |
saveOptions.setSaveFormat(SaveFormat.PCL); | |
saveOptions.setRasterizeTransformedElements(false); | |
// Export the document as an PCL file. | |
doc.save(dataDir + "Document.PclConversion_out.pcl", saveOptions); |