Lưu Tài liệu

Hầu hết các nhiệm vụ bạn cần thực hiện với Aspose.Words liên quan đến việc lưu tài liệu. Để lưu tài liệu Aspose.Words cung cấp phương thức Save của lớp Document. Tài liệu có thể được lưu ở bất kỳ định dạng lưu nào được hỗ trợ bởi Aspose.Words. Để biết danh sách tất cả các định dạng lưu được hỗ trợ, hãy xem liệt kê SaveFormat.

Lưu Vào Tệp

Chỉ cần sử dụng phương thức Save với tên tệp. Aspose.Words sẽ xác định định dạng lưu từ phần mở rộng tệp mà bạn chỉ định.

Ví dụ mã sau đây cho thấy cách tải và lưu tài liệu vào tệp:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_LoadingAndSaving();
System::String outputDataDir = GetOutputDataDir_LoadingAndSaving();
// Load the document from the absolute path on disk.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.docx");
System::String outputPath = outputDataDir + u"LoadAndSaveToDisk.docx";
// Save the document as DOC document.");
doc->Save(outputPath);

Lưu Vào Luồng

Chuyển một đối tượng luồng sang phương thức Save. Nó cần thiết để chỉ định định dạng lưu rõ ràng khi lưu vào luồng.

Ví dụ mã sau đây cho thấy cách tải và lưu tài liệu vào luồng:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_LoadingAndSaving();
System::String outputDataDir = GetOutputDataDir_LoadingAndSaving();
// Open the stream. Read only access is enough for Aspose.Words to load a document.
System::SharedPtr<System::IO::Stream> stream = System::IO::File::OpenRead(inputDataDir + u"Document.docx");
// Load the entire document into memory.
System::SharedPtr<Document> doc = System::MakeObject<Document>(stream);
// You can close the stream now, it is no longer needed because the document is in memory.
stream->Close();
// ... do something with the document
// Convert the document to a different format and save to stream.
System::SharedPtr<System::IO::MemoryStream> dstStream = System::MakeObject<System::IO::MemoryStream>();
doc->Save(dstStream, SaveFormat::Doc);
// Rewind the stream position back to zero so it is ready for the next reader.
dstStream->set_Position(0);
// Save the document from stream, to disk. Normally you would do something with the stream directly,
// For example writing the data to a database.
System::String outputPath = outputDataDir + u"LoadAndSaveToStream.docx";
System::IO::File::WriteAllBytes(outputPath, dstStream->ToArray());

Bạn có thể tải xuống tệp mẫu của ví dụ này từ Aspose.Words GitHub.

Lưu vào PCL

Aspose.Words hỗ trợ lưu tài liệu vào PCL (Ngôn ngữ Lệnh Máy In). Aspose.Words có thể lưu tài liệu vào PCL 6 (PCL 6 định dạng nâng cao hoặc PCL XL). Lớp PclSaveOptions có thể được sử dụng để chỉ định các tùy chọn bổ sung khi lưu tài liệu vào định dạng PCL.

Ví dụ mã sau đây cho thấy cách lưu tài liệu thành PCL bằng cách sử dụng các tùy chọn lưu:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_LoadingAndSaving();
System::String outputDataDir = GetOutputDataDir_LoadingAndSaving();
// Load the document from disk.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Test File (docx).docx");
System::SharedPtr<PclSaveOptions> saveOptions = System::MakeObject<PclSaveOptions>();
saveOptions->set_SaveFormat(SaveFormat::Pcl);
saveOptions->set_RasterizeTransformedElements(false);
// Export the document as an PCL file.
doc->Save(outputDataDir + u"ConvertDocumentToPCL.pcl", saveOptions);