文書を保存する

Aspose.Wordsで実行する必要があるタスクのほとんどには、文書の保存が含まれます。 ドキュメントAspose.Wordsを保存するには、DocumentクラスのSaveメソッドを提供します。 文書はAspose.Wordsでサポートされている任意の保存形式で保存できます。 サポートされているすべての保存形式の一覧については、SaveFormat列挙体を参照してください。

ファイル{#save-a-document-to-a-file}に保存する

ファイル名を指定して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-a-document-to-a-stream}に保存する

Streamオブジェクトを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{#save-a-document-to-pcl}に保存する

Aspose.Words文書をPCL(Printer Command Language)に保存することをサポートします。 Aspose.Wordsは文書をPCL6(PCL6Enhancedまたは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);