Document Constructor (Stream, LoadOptions) |
Namespace: Aspose.Words
Exception | Condition |
---|---|
UnsupportedFileFormatException | The document format is not recognized or not supported. |
FileCorruptedException | The document appears to be corrupted and cannot be loaded. |
Exception | There is a problem with the document and it should be reported to Aspose.Words developers. |
IOException | There is an input/output exception. |
IncorrectPasswordException | The document is encrypted and requires a password to open, but you supplied an incorrect password. |
ArgumentNullException | The stream cannot be null. |
NotSupportedException | The stream does not support reading or seeking. |
ObjectDisposedException | The stream is a disposed object. |
The document must be stored at the beginning of the stream. The stream must support random positioning.
Document doc = new Document(); string fileName = MyDir + "Document.html"; // Open the stream using (Stream stream = File.OpenRead(fileName)) { // Open the document. Note the Document constructor detects HTML format automatically // Pass the URI of the base folder so any images with relative URIs in the HTML document can be found LoadOptions loadOptions = new LoadOptions(); loadOptions.BaseUri = ImageDir; doc = new Document(stream, loadOptions); } // Save in the DOC format doc.Save(ArtifactsDir + "Document.OpenFromStreamWithBaseUri.doc");
Document doc; // Trying to open a password-encrypted document the normal way will cause an exception to be thrown Assert.Throws<IncorrectPasswordException>(() => { doc = new Document(MyDir + "Encrypted.docx"); }); // To open it and access its contents, we need to open it using the correct password // The password is delivered via a LoadOptions object, after being passed to it's constructor LoadOptions options = new LoadOptions("docPassword"); // We can now open the document either by filename or stream doc = new Document(MyDir + "Encrypted.docx", options); using (Stream stream = File.OpenRead(MyDir + "Encrypted.docx")) { doc = new Document(stream, options); }
// The url of the page to load const string url = "http://www.aspose.com/"; // Create a WebClient object to easily extract the HTML from the page WebClient client = new WebClient(); string pageSource = client.DownloadString(url); client.Dispose(); // Get the HTML as bytes for loading into a stream Encoding encoding = client.Encoding; byte[] pageBytes = encoding.GetBytes(pageSource); // Load the HTML into a stream. using (MemoryStream stream = new MemoryStream(pageBytes)) { // The baseUri property should be set to ensure any relative img paths are retrieved correctly LoadOptions options = new LoadOptions(Aspose.Words.LoadFormat.Html, "", url); // Load the HTML document from stream and pass the LoadOptions object Document doc = new Document(stream, options); // Save the document to disk // The extension of the filename can be changed to save the document into other formats. e.g PDF, DOCX, ODT, RTF. doc.Save(ArtifactsDir + "Document.InsertHtmlFromWebPage.doc"); }