Enregistrer un Document
La plupart des tâches que vous devez effectuer avec Aspose.Words impliquent l’enregistrement d’un document. Pour enregistrer un document Aspose.Words fournit la méthode Save de la classe Document. Le document peut être enregistré dans n’importe quel format de sauvegarde pris en charge par Aspose.Words. Pour la liste de tous les formats d’enregistrement pris en charge, voir l’énumération SaveFormat.
Enregistrer dans un fichier
Utilisez simplement la méthode Save avec un nom de fichier. Aspose.Words déterminera le format de sauvegarde à partir de l’extension de fichier que vous spécifiez.
L’exemple de code suivant montre comment charger et enregistrer un document dans un fichier:
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); |
Enregistrer dans un flux
Passez un objet stream à la méthode Save. Il est nécessaire de spécifier explicitement le format de sauvegarde lors de l’enregistrement dans un flux.
L’exemple de code suivant montre comment charger et enregistrer un document dans un flux:
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()); |
Vous pouvez télécharger le fichier modèle de cet exemple à partir de Aspose.Words GitHub.
Enregistrer dans PCL
Aspose.Words prend en charge l’enregistrement d’un document en PCL (Langage de commande d’imprimante). Aspose.Words peut enregistrer des documents au format PCL 6 (PCL 6 amélioré ou PCL XL). La classe PclSaveOptions
peut être utilisée pour spécifier des options supplémentaires lors de l’enregistrement d’un document au format PCL.
L’exemple de code suivant montre comment enregistrer un document dans PCL à l’aide des options d’enregistrement:
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); |