Запис на документ

Повечето от задачите, които трябва да изпълните с Aspose.Words, включват записване на документ. За да запишете документ Aspose.Words предоставя метода Save на класа Document. Документът може да бъде записан във всеки формат, поддържан от Aspose.Words. За списъка с всички поддържани формати за записване вижте изброяването SaveFormat.

Запис във файл

Просто използвайте метода Save с име на файл. Aspose.Words ще определи формата на записване от разширението на файла, който сте задали.

Следващият пример за код показва как да заредите и запишете документ във файл:

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

Запази в поток

Подаване на обект на поток към метода Save. Необходимо е да зададете изрично формата за записване, когато записвате в поток.

Следващият пример за код показва как да заредите и запишете документ в поток:

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

Можете да изтеглите шаблонния файл на този пример от Aspose.Words GitHub.

Запази в PCL

Aspose.Words поддържа запис на документ в PCL (Език на командата за принтер). Aspose.Words може да записва документи в PCL 6 (PCL 6 подобрен или PCL XL) формат. Клас PclSaveOptions може да се използва за задаване на допълнителни опции при записване на документ във формат PCL.

Следният пример за код показва как да запишете документ до PCL, като използвате опциите за записване:

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