Salvare un documento
La maggior parte delle attività che è necessario eseguire con Aspose.Words comporta il salvataggio di un documento. Per salvare un documento Aspose.Words fornisce il metodo Save della classe Document. Il documento può essere salvato in qualsiasi formato di salvataggio supportato da Aspose.Words. Per l’elenco di tutti i formati di salvataggio supportati, vedere l’enumerazione SaveFormat.
Salva in un file
Basta usare il metodo Save con un nome di file. Aspose.Words determinerà il formato di salvataggio dall’estensione del file specificata.
Il seguente esempio di codice mostra come caricare e salvare un documento in un file:
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); |
Salva in un flusso
Passare un oggetto stream al metodo Save. È necessario specificare esplicitamente il formato di salvataggio quando si salva in un flusso.
Il seguente esempio di codice mostra come caricare e salvare un documento in un flusso:
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()); |
È possibile scaricare il file modello di questo esempio da Aspose.Words GitHub.
Salva in PCL
Aspose.Words supporta il salvataggio di un documento in PCL (Printer Command Language). Aspose.Words può salvare i documenti in formato PCL 6 (PCL 6 Enhanced o PCL XL). La classe PclSaveOptions
può essere utilizzata per specificare opzioni aggiuntive quando si salva un documento nel formato PCL.
Il seguente esempio di codice mostra come salvare un documento in PCL utilizzando le opzioni di salvataggio:
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); |