The Save method is crucial for saving the document. Its overloads allow saving the document to a file, stream or ASP .NET HttpResponse object for sending to a client browser. The document can be saved in DOC, DOCX, RTF, WordprocessingML, HTML, plain text or PDF.
Saving to a File
Simply use the Save method accepting a file name.
Example SaveToFile
Saves a document to a file.
[C#]
doc.Save(MyDir + "Document.OpenFromFile Out.doc");
[Visual Basic]
doc.Save(MyDir & "Document.OpenFromFile Out.doc")
[Java]
doc.save(getMyDir() + "Document.OpenFromFile Out.doc");
Saving to a Stream
You pass a stream object to the Save method. Also, specify a save format.
Example SaveToStream
Shows how to save a document to a stream.
[C#]
Document doc = new Document(MyDir + "Document.doc");
MemoryStream dstStream = new MemoryStream();
doc.Save(dstStream, SaveFormat.Docx);
// Rewind the stream position back to zero so it is ready for next reader.
dstStream.Position = 0;
[Visual Basic]
Dim doc As Document = New Document(MyDir & "Document.doc")
Dim dstStream As MemoryStream = New MemoryStream()
doc.Save(dstStream, SaveFormat.Docx)
' Rewind the stream position back to zero so it is ready for next reader.
dstStream.Position = 0
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
OutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, SaveFormat.DOCX);
Sending to a Client Browser
[This feature is available in Aspose.Words for .NET only.]
In order to send the document to a client browser, use a special overload that accepts four parameters: file name, save format, save type, and an ASP .NET HttpResponse object. Save type is represented by the SaveType enumeration that determines whether the document being sent to the browser will provide an option to open itself directly in the browser or in an application associated with .doc extension.
Example SaveToBrowser
Shows how to send a document to the client browser from an ASP.NET code.
[C#]
Document doc = new Document(MyDir + "Document.doc");
doc.Save("Report Out.doc", SaveFormat.Doc, SaveType.OpenInBrowser, Response);
[Visual Basic]
Dim doc As Document = New Document(MyDir & "Document.doc")
doc.Save("Report Out.doc", SaveFormat.Doc, SaveType.OpenInBrowser, Response)
Converting to PDF
The simplest way to convert a document to PDF is to just invoke the Save method and specify a file name with the “.PDF” extension.
This is the recommended method for converting to PDF because it uses the new rendering engine built into Aspose.Words and does not use Aspose.Pdf.
Example Doc2PdfSave
Converts a whole document from DOC to PDF using default options.
[C#]
Document doc = new Document(MyDir + "Document.doc");
doc.Save(MyDir + "Document.Doc2PdfSave Out.pdf");
[Visual Basic]
Dim doc As Document = New Document(MyDir & "Document.doc")
doc.Save(MyDir & "Document.Doc2PdfSave Out.pdf")
Converting to PDF using Aspose.Words and Aspose.Pdf (Legacy)
[This feature is available in Aspose.Words for .NET only.]
[This is the “legacy” method of converting to PDF and will be deprecated. See the new method in Convert to PDF Directly.]
Saving to PDF needs further description because the document is first saved in the intermediate XML format, not in Adobe PDF directly. After saving in this format, you need to invoke Aspose.Pdf to produce the resulting PDF document as described below.
Please note that some formatting specified in Microsoft Word documents might be lost when converting into PDF.
If the document contains images, Aspose.Words will save every image into a separate file and include the file name in the produced XML file. The image files are created in the same folder where the XML file is saved. If you are saving the XML file to a stream, Aspose.Words will save images to the Windows temporary folder. You can avoid creation of the intermediate XML file on the disk by saving into a MemoryStream, but if the document contains images, they will still be saved as disk files.
Please note that Aspose.Words does not automatically delete the image files. The responsibility to delete the image files lies with Aspose.Pdf. To instruct Aspose.Pdf to delete image files, set Pdf.IsImagesInXmlDeleteNeeded property to true.
Example Doc2PdfViaFile
Converts from DOC to PDF format (requires Aspose.Pdf purchased separately).
[C#]
// Open the document.
Document doc = new Document(MyDir + "Document.doc");
// ... You can merge data/manipulate document content here.
// Save the document in the Aspose.Pdf.Xml format.
string xmlFile = MyDir + "Document.ConvertToPdf Out.xml";
doc.Save(xmlFile, SaveFormat.AsposePdf);
// Read the document into Aspose.Pdf.
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
pdf.BindXML(xmlFile, null);
// Instruct to delete temporary image files.
pdf.IsImagesInXmlDeleteNeeded = true;
// This is optional, but can speed up Aspose.Pdf if you convert multiple files in one session.
pdf.IsTruetypeFontMapCached = true;
pdf.TruetypeFontMapPath = Path.GetTempPath();
// Produce the PDF file.
pdf.Save(MyDir + "Document.ConvertToPdfViaAsposePdf Out.pdf");
[Visual Basic]
' Open the document.
Dim doc As Document = New Document(MyDir & "Document.doc")
' ... You can merge data/manipulate document content here.
' Save the document in the Aspose.Pdf.Xml format.
Dim xmlFile As String = MyDir & "Document.ConvertToPdf Out.xml"
doc.Save(xmlFile, SaveFormat.AsposePdf)
' Read the document into Aspose.Pdf.
Dim pdf As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()
pdf.BindXML(xmlFile, Nothing)
' Instruct to delete temporary image files.
pdf.IsImagesInXmlDeleteNeeded = True
' This is optional, but can speed up Aspose.Pdf if you convert multiple files in one session.
pdf.IsTruetypeFontMapCached = True
pdf.TruetypeFontMapPath = Path.GetTempPath()
' Produce the PDF file.
pdf.Save(MyDir & "Document.ConvertToPdfViaAsposePdf Out.pdf")
Specifying Save Options
Document has a SaveOptions property that allows specifying some options that control how the document is saved.
Example SaveWithOptions
Shows how to set save options before saving a document.
[C#]
Document doc = new Document(MyDir + "Document.doc");
// Set an option to export form fields as plain text, not as HTML input elements.
doc.SaveOptions.HtmlExportTextInputFormFieldAsText = true;
doc.Save(MyDir + "Document.SaveWithOptions Out.html");
[Visual Basic]
Dim doc As Document = New Document(MyDir & "Document.doc")
' Set an option to export form fields as plain text, not as HTML input elements.
doc.SaveOptions.HtmlExportTextInputFormFieldAsText = True
doc.Save(MyDir & "Document.SaveWithOptions Out.html")
[Java]
Document doc = new Document(getMyDir() + "Document.doc");
// Set an option to export form fields as plain text, not as HTML input elements.
doc.getSaveOptions().setHtmlExportTextInputFormFieldAsText(true);
doc.save(getMyDir() + "Document.SaveWithOptions Out.html");