ドキュメントを保存する

Aspose.Words を使用して実行する必要があるタスクのほとんどには、ドキュメントの保存が含まれます。ドキュメントを保存するために、Aspose.Words は Document クラスの Save メソッドを提供します。クライアント ブラウザーに送信するために、ドキュメントをファイル、ストリーム、または ASP.NET HttpResponse オブジェクトに保存できるオーバーロードがあります。ドキュメントは、Aspose.Words がサポートする任意の保存形式で保存できます。サポートされているすべての保存形式のリストについては、SaveFormat 列挙を参照してください。

ファイルに保存

ファイル名を指定して Save メソッドを使用するだけです。 Aspose.Words は、指定したファイル拡張子から保存形式を決定します。

次のコード例は、ドキュメントをファイルにロードして保存する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Document.doc");
doc.Save(ArtifactsDir + "BaseConversions.DocToDocx.docx");
view raw load-save.cs hosted with ❤ by GitHub

ストリーム{#save-a-document-to-a-stream}に保存

ストリーム オブジェクトを Save メソッドに渡します。ストリームに保存する場合は、保存形式を明示的に指定する必要があります。

次のコード例は、ドキュメントをストリームにロードして保存する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Read only access is enough for Aspose.Words to load a document.
Stream stream = File.OpenRead(MyDir + "Document.docx");
Document doc = new 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.
MemoryStream dstStream = new MemoryStream();
doc.Save(dstStream, SaveFormat.Rtf);
// Rewind the stream position back to zero so it is ready for the next reader.
dstStream.Position = 0;

ドキュメントをクライアント ブラウザ {#sending-a-document-to-a-client-browser} に送信する

ドキュメントをクライアント ブラウザに送信するには、ファイル名、保存形式、保存タイプ、ASP.NET HttpResponse オブジェクトの 4 つのパラメータを取る特別なオーバーロードを使用します。ドキュメントがユーザーに表示される方法は ContentDisposition 列挙によって表され、ブラウザに送信されたドキュメントがブラウザで直接開くオプションを提供するか、ファイル拡張子に関連付けられたアプリケーションで開くオプションを提供するかを決定します。

次のコード例は、ASP.NET コードからクライアント ブラウザにドキュメントを送信する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Document.docx");
// If this method overload is causing a compiler error then you are using the Client Profile DLL whereas
// the Aspose.Words .NET 2.0 DLL must be used instead.
doc.Save(ArtifactsDir + "BaseConversions.DocxToDocx.docx");

Save メソッドのこのオーバーロードは、.NET クライアント プロファイル DLL を使用する場合には使用できません。この DLL は net3.5_クライアントプロファイル フォルダーにあります。 .NET クライアント プロファイルでは System.Web などのアセンブリが除外されているため、HttpResponse は使用できません。これは完全に仕様によるものです。

これはエラーとして現れる場合があります。

「メソッド ‘Save’ のオーバーロードは ‘4’ パラメータを取ります。」

ASP.NET アプリケーションで Aspose.Words を使用する必要がある場合は、この記事で説明されているように、適切なオーバーロードが利用可能な .NET 2.0 DLL を使用することをお勧めします。

PCL {#save-a-document-to-pcl} に保存

Aspose.Words は、PCL (プリンター コマンド言語) へのドキュメントの保存をサポートしています。 Aspose.Words は、ドキュメントを PCL 6 (PCL 6 Enhanced または PCL XL) 形式で保存できます。 PclSaveOptions クラスを使用すると、ドキュメントを PCL 形式で保存するときに追加のオプションを指定できます。

次のコード例は、保存オプションを使用してドキュメントを PCL に保存する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Rendering.docx");
PclSaveOptions saveOptions = new PclSaveOptions { SaveFormat = SaveFormat.Pcl };
doc.Save(ArtifactsDir + "BaseConversions.DocxToPcl.pcl", saveOptions);
view raw docx-to-pcl.cs hosted with ❤ by GitHub

関連項目