Document
Document()
Creates a blank Word document.
public Document()
Remarks
A blank document is retrieved from resources, and by default, the resulting document looks more like created by Word2007. This blank document contains a default fonts table, minimal default styles, and latent styles.
OptimizeFor
method can be used to optimize the document contents as well as default Aspose.Words behavior to a particular version of MS Word.
The document paper size is Letter by default. If you want to change page setup, use PageSetup
.
After creation, you can use DocumentBuilder
to add document content easily.
Examples
Shows how to format a run of text using its font property.
Document doc = new Document();
Run run = new Run(doc, "Hello world!");
Aspose.Words.Font font = run.Font;
font.Name = "Courier New";
font.Size = 36;
font.HighlightColor = Color.Yellow;
doc.FirstSection.Body.FirstParagraph.AppendChild(run);
doc.Save(ArtifactsDir + "Font.CreateFormattedRun.docx");
Shows how to create simple document.
Document doc = new Document();
// New Document objects by default come with the minimal set of nodes
// required to begin adding content such as text and shapes: a Section, a Body, and a Paragraph.
doc.AppendChild(new Section(doc))
.AppendChild(new Body(doc))
.AppendChild(new Paragraph(doc))
.AppendChild(new Run(doc, "Hello world!"));
Shows how to create and load documents.
// There are two ways of creating a Document object using Aspose.Words.
// 1 - Create a blank document:
Document doc = new Document();
// New Document objects by default come with the minimal set of nodes
// required to begin adding content such as text and shapes: a Section, a Body, and a Paragraph.
doc.FirstSection.Body.FirstParagraph.AppendChild(new Run(doc, "Hello world!"));
// 2 - Load a document that exists in the local file system:
doc = new Document(MyDir + "Document.docx");
// Loaded documents will have contents that we can access and edit.
Assert.AreEqual("Hello World!", doc.FirstSection.Body.FirstParagraph.GetText().Trim());
// Some operations that need to occur during loading, such as using a password to decrypt a document,
// can be done by passing a LoadOptions object when loading the document.
doc = new Document(MyDir + "Encrypted.docx", new LoadOptions("docPassword"));
Assert.AreEqual("Test encrypted document.", doc.FirstSection.Body.FirstParagraph.GetText().Trim());
See Also
- class Document
- namespace Aspose.Words
- assembly Aspose.Words
Document(string)
Opens an existing document from a file. Automatically detects the file format.
public Document(string fileName)
Parameter | Type | Description |
---|---|---|
fileName | String | File name of the document to open. |
Exceptions
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. |
ArgumentException | The name of the file cannot be null or empty string. |
Examples
Shows how to open a document and convert it to .PDF.
Document doc = new Document(MyDir + "Document.docx");
doc.Save(ArtifactsDir + "Document.ConvertToPdf.pdf");
Shows how to convert a PDF to a .docx.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Hello world!");
doc.Save(ArtifactsDir + "PDF2Word.ConvertPdfToDocx.pdf");
// Load the PDF document that we just saved, and convert it to .docx.
Document pdfDoc = new Document(ArtifactsDir + "PDF2Word.ConvertPdfToDocx.pdf");
pdfDoc.Save(ArtifactsDir + "PDF2Word.ConvertPdfToDocx.docx");
Shows how to load a PDF.
Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Hello world!");
doc.Save(ArtifactsDir + "PDF2Word.LoadPdf.pdf");
// Below are two ways of loading PDF documents using Aspose products.
// 1 - Load as an Aspose.Words document:
Document asposeWordsDoc = new Document(ArtifactsDir + "PDF2Word.LoadPdf.pdf");
Assert.AreEqual("Hello world!", asposeWordsDoc.GetText().Trim());
// 2 - Load as an Aspose.Pdf document:
Aspose.Pdf.Document asposePdfDoc = new Aspose.Pdf.Document(ArtifactsDir + "PDF2Word.LoadPdf.pdf");
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
asposePdfDoc.Pages.Accept(textFragmentAbsorber);
Assert.AreEqual("Hello world!", textFragmentAbsorber.Text.Trim());
See Also
- class Document
- namespace Aspose.Words
- assembly Aspose.Words
Document(string, LoadOptions)
Opens an existing document from a file. Allows to specify additional options such as an encryption password.
public Document(string fileName, LoadOptions loadOptions)
Parameter | Type | Description |
---|---|---|
fileName | String | File name of the document to open. |
loadOptions | LoadOptions | Additional options to use when loading a document. Can be null . |
Exceptions
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. |
ArgumentException | The name of the file cannot be null or empty string. |
Examples
Shows how to load an encrypted Microsoft Word document.
Document doc;
// Aspose.Words throw an exception if we try to open an encrypted document without its password.
Assert.Throws<IncorrectPasswordException>(() => doc = new Document(MyDir + "Encrypted.docx"));
// When loading such a document, the password is passed to the document's constructor using a LoadOptions object.
LoadOptions options = new LoadOptions("docPassword");
// There are two ways of loading an encrypted document with a LoadOptions object.
// 1 - Load the document from the local file system by filename:
doc = new Document(MyDir + "Encrypted.docx", options);
// 2 - Load the document from a stream:
using (Stream stream = File.OpenRead(MyDir + "Encrypted.docx"))
{
doc = new Document(stream, options);
}
Shows how to create and load documents.
// There are two ways of creating a Document object using Aspose.Words.
// 1 - Create a blank document:
Document doc = new Document();
// New Document objects by default come with the minimal set of nodes
// required to begin adding content such as text and shapes: a Section, a Body, and a Paragraph.
doc.FirstSection.Body.FirstParagraph.AppendChild(new Run(doc, "Hello world!"));
// 2 - Load a document that exists in the local file system:
doc = new Document(MyDir + "Document.docx");
// Loaded documents will have contents that we can access and edit.
Assert.AreEqual("Hello World!", doc.FirstSection.Body.FirstParagraph.GetText().Trim());
// Some operations that need to occur during loading, such as using a password to decrypt a document,
// can be done by passing a LoadOptions object when loading the document.
doc = new Document(MyDir + "Encrypted.docx", new LoadOptions("docPassword"));
Assert.AreEqual("Test encrypted document.", doc.FirstSection.Body.FirstParagraph.GetText().Trim());
See Also
- class LoadOptions
- class Document
- namespace Aspose.Words
- assembly Aspose.Words
Document(Stream)
Opens an existing document from a stream. Automatically detects the file format.
public Document(Stream stream)
Parameter | Type | Description |
---|---|---|
stream | Stream | Stream where to load the document from. |
Exceptions
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. |
Remarks
The document must be stored at the beginning of the stream. The stream must support random positioning.
Examples
Shows how to load a document using a stream.
using (Stream stream = File.OpenRead(MyDir + "Document.docx"))
{
Document doc = new Document(stream);
Assert.AreEqual("Hello World!\r\rHello Word!\r\r\rHello World!", doc.GetText().Trim());
}
Shows how to load a document from a URL.
// Create a URL that points to a Microsoft Word document.
const string url = "https://filesamples.com/samples/document/docx/sample3.docx";
// Download the document into a byte array, then load that array into a document using a memory stream.
using (WebClient webClient = new WebClient())
{
byte[] dataBytes = webClient.DownloadData(url);
using (MemoryStream byteStream = new MemoryStream(dataBytes))
{
Document doc = new Document(byteStream);
// At this stage, we can read and edit the document's contents and then save it to the local file system.
Assert.AreEqual("There are eight section headings in this document. At the beginning, \"Sample Document\" is a level 1 heading. " +
"The main section headings, such as \"Headings\" and \"Lists\" are level 2 headings. " +
"The Tables section contains two sub-headings, \"Simple Table\" and \"Complex Table,\" which are both level 3 headings.",
doc.FirstSection.Body.Paragraphs[3].GetText().Trim());
doc.Save(ArtifactsDir + "Document.LoadFromWeb.docx");
}
}
See Also
- class Document
- namespace Aspose.Words
- assembly Aspose.Words
Document(Stream, LoadOptions)
Opens an existing document from a stream. Allows to specify additional options such as an encryption password.
public Document(Stream stream, LoadOptions loadOptions)
Parameter | Type | Description |
---|---|---|
stream | Stream | The stream where to load the document from. |
loadOptions | LoadOptions | Additional options to use when loading a document. Can be null . |
Exceptions
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. |
Remarks
The document must be stored at the beginning of the stream. The stream must support random positioning.
Examples
Shows how to open an HTML document with images from a stream using a base URI.
using (Stream stream = File.OpenRead(MyDir + "Document.html"))
{
// Pass the URI of the base folder while loading it
// so that any images with relative URIs in the HTML document can be found.
LoadOptions loadOptions = new LoadOptions();
loadOptions.BaseUri = ImageDir;
Document doc = new Document(stream, loadOptions);
// Verify that the first shape of the document contains a valid image.
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
Assert.IsTrue(shape.IsImage);
Assert.IsNotNull(shape.ImageData.ImageBytes);
Assert.AreEqual(32.0, ConvertUtil.PointToPixel(shape.Width), 0.01);
Assert.AreEqual(32.0, ConvertUtil.PointToPixel(shape.Height), 0.01);
}
Shows how save a web page as a .docx file.
const string url = "https://products.aspose.com/words/";
using (WebClient client = new WebClient())
{
var bytes = client.DownloadData(url);
using (MemoryStream stream = new MemoryStream(bytes))
{
// The URL is used again as a baseUri to ensure that any relative image paths are retrieved correctly.
LoadOptions options = new LoadOptions(LoadFormat.Html, "", url);
// Load the HTML document from stream and pass the LoadOptions object.
Document doc = new Document(stream, options);
// At this stage, we can read and edit the document's contents and then save it to the local file system.
doc.Save(ArtifactsDir + "Document.InsertHtmlFromWebPage.docx");
}
}
Shows how to load an encrypted Microsoft Word document.
Document doc;
// Aspose.Words throw an exception if we try to open an encrypted document without its password.
Assert.Throws<IncorrectPasswordException>(() => doc = new Document(MyDir + "Encrypted.docx"));
// When loading such a document, the password is passed to the document's constructor using a LoadOptions object.
LoadOptions options = new LoadOptions("docPassword");
// There are two ways of loading an encrypted document with a LoadOptions object.
// 1 - Load the document from the local file system by filename:
doc = new Document(MyDir + "Encrypted.docx", options);
// 2 - Load the document from a stream:
using (Stream stream = File.OpenRead(MyDir + "Encrypted.docx"))
{
doc = new Document(stream, options);
}
See Also
- class LoadOptions
- class Document
- namespace Aspose.Words
- assembly Aspose.Words