Since the Document class represents a Microsoft Word document that should be processed by Aspose.Words, just use it to get a new document object. Document has several overloaded constructors allowing you to create a blank document or to load it from a file or stream.
Creating a New Document
Call the Document constructor without parameters to create a new blank document:
Example DocumentCtor
Shows how to create a blank document. Note the blank document contains one section and one paragraph.
[C#]
Document doc = new Document();
[Visual Basic]
Dim doc As Document = New Document()
[Java]
Document doc = new Document();
The document paper size is Letter by default.
If you want to generate a document programmatically, the most reasonable step after creation is to use DocumentBuilder to add document contents.
Example DocumentBuilderAndSave
Shows how to create build a document using a document builder.
[C#]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello World!");
doc.Save(MyDir + "DocumentBuilderAndSave Out.docx");
[Visual Basic]
Dim doc As Document = New Document()
Dim builder As DocumentBuilder = New DocumentBuilder(doc)
builder.Writeln("Hello World!")
doc.Save(MyDir & "DocumentBuilderAndSave Out.docx")
[Java]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello World!");
doc.save(getMyDir() + "DocumentBuilderAndSave Out.docx");
Opening from a File
Pass a file name as a string to the Document constructor to open an existing document from a file:
Example OpenFromFile
Opens a document from a file.
[C#]
// Open a document. The file is opened read only and only for the duration of the constructor.
Document doc = new Document(MyDir + "Document.doc");
[Visual Basic]
' Open a document. The file is opened read only and only for the duration of the constructor.
Dim doc As Document = New Document(MyDir & "Document.doc")
[Java]
// Open a document. The file is opened read only and only for the duration of the constructor.
Document doc = new Document(getMyDir() + "Document.doc");
Opening from a Stream
Simply pass a stream object that contains a document to the Document constructor:
Example OpenFromStream
Opens a document from a stream.
[C#]
// Open the stream. Read only access is enough for Aspose.Words to load a document.
Stream stream = File.OpenRead(MyDir + "Document.doc");
// Load the entire document into memory.
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
Assert.AreEqual("Hello World!\x000c", doc.GetText());
[Visual Basic]
' Open the stream. Read only access is enough for Aspose.Words to load a document.
Dim stream As Stream = File.OpenRead(MyDir & "Document.doc")
' Load the entire document into memory.
Dim doc As Document = 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
Assert.AreEqual("Hello World!" & Constants.vbFormFeed, doc.GetText())
[Java]
// Open the stream. Read only access is enough for Aspose.Words to load a document.
InputStream stream = new FileInputStream(getMyDir() + "Document.doc");
// Load the entire document into memory.
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
Assert.assertEquals("Hello World!\u000c",doc.getText());
Opening Encrypted Documents
You can open Word documents encrypted with a password. To do that, use a special constructor overload, which accepts the password string.
Example OpenEncrypted
Loads a Microsoft Word document encrypted with a password.
[C#]
Document doc = new Document(MyDir + "Document.LoadEncrypted.doc", Aspose.Words.LoadFormat.Doc, "qwerty");
[Visual Basic]
Dim doc As Document = New Document(MyDir & "Document.LoadEncrypted.doc", Aspose.Words.LoadFormat.Doc, "qwerty")
[Java]
Document doc = new Document(getMyDir() + "Document.LoadEncrypted.doc", LoadFormat.DOC, "qwerty");