ذخیره یک سند
بیشتر کارهایی که باید با Aspose.Words انجام دهید شامل ذخیره یک سند است. برای ذخیره یک سند Aspose.Words روش Save کلاس Document را فراهم می کند. این سند را می توان در هر فرمت ذخیره ای که توسط Aspose.Words پشتیبانی می شود ذخیره کرد. برای لیست تمام فرمت های ذخیره شده پشتیبانی شده، به SaveFormat enumeration مراجعه کنید.
ذخیره در یک فایل
به سادگی از روش 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); |