Table of Contents

Class Document

Namespace
FileFormat.Words
Assembly
FileFormat.Words.dll

Represents a document with structured elements.

public class Document : IDisposable
Inheritance
Document
Implements
Inherited Members

Constructors

Document()

Initializes a new instance of the Document class. This constructor creates a new, empty document.

public Document()

Examples

// Create a new, empty document
Document Doc = new Document();
// Initialize a new instance of body the empty document.
Body body = new Body(doc);
// Add paragraph wiht run
Paragraph para = new Paragraph();
para.AddRun(new FF.Run
   {
        Text = "First Run with Times New Roman Blue Color Bold",
        FontFamily = "Times New Roman",
        Color = FF.Colors.Blue,
        Bold = true,
        FontSize = 12
    });
// Append paragraph to the document
body.AppendChild(para);
// Save document to file.
doc.Save("DocumentWithPara.docx");

Remarks

Use this constructor to create a new, blank document that you can populate with content. To work with an existing document, consider using the Document(string) constructor.

Document(Stream)

Initializes a new instance of the Document class by loading content from a Stream.

public Document(Stream stream)

Parameters

stream Stream

The input stream from which to load the document content.

Exceptions

FileFormatException

Thrown if an error occurs while loading the document.

Document(string)

Initializes a new instance of the Document class by loading content from a file specified by its filename.

public Document(string filename)

Parameters

filename string

The path to the file from which to load the document content.

Exceptions

FileFormatException

Thrown if an error occurs while loading the document.

Methods

Dispose()

Dispose off all managed and unmanaged resources.

public void Dispose()

Dispose(bool)

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

GetElementStyles()

Gets the element styles of the document.

public ElementStyles GetElementStyles()

Returns

ElementStyles

GetElements()

Retrieves a list of existing elements from the internal data structure.

public List<IElement> GetElements()

Returns

List<IElement>

A list of IElement objects representing the existing elements.

See Also

InsertAfter(IElement, IElement)

Inserts an element after a specified element in the structure.

public int InsertAfter(IElement newElement, IElement element)

Parameters

newElement IElement

The element to be inserted.

element IElement

The element after which the new element should be inserted.

Returns

int

The unique identifier ElementId if the new element is successfully inserted after the specified element. If the specified element is not found, -1 is returned. If an error occurs during the insertion operation, a FileFormatException is thrown.

Remarks

This method inserts the specified element after the provided element in the structure. The method attempts to locate the specified element by comparing their unique IDs. If the insertion operation is successful, the method returns ElementId of the inserted element. If the specified element is not found, -1 is returned, and no changes are made to the structure. If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state and throws a FileFormatException with detailed error information.

InsertAfter(IElement, int)

Inserts an element after a specified element in the structure by its unique ID.

public int InsertAfter(IElement newElement, int elementId)

Parameters

newElement IElement

The element to be inserted.

elementId int

The unique ID of the element after which the new element should be inserted.

Returns

int

The unique identifier ElementId of the inserted element if the new element is successfully inserted after the specified element. If the specified element is not found, -1 is returned. If an error occurs during the insertion operation, a FileFormatException is thrown.

Remarks

This method inserts the specified element after the element with the provided unique ID in the structure. It is important to ensure that the specified element exists in the structure to determine the insertion point. If the insertion operation is successful, the method returns ElementId of the inserted element. If the specified element is not found, -1 is returned, and no changes are made to the structure. If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state and throws a FileFormatException with detailed error information.

InsertBefore(IElement, IElement)

Inserts an element before the specified reference element.

public int InsertBefore(IElement newElement, IElement element)

Parameters

newElement IElement

The element to be inserted.

element IElement

The reference element before which the new element should be inserted.

Returns

int

The unique identifier ElementId of inserted element if the new element is successfully inserted before the reference element. If the reference element is not found in the structure, -1 is returned. If an error occurs during the insertion operation, a FileFormatException is thrown.

Remarks

This method inserts the specified element before the provided reference element in the structure. The method attempts to locate the reference element and insert the new element before it. If the insertion operation is successful, the method returns ElementId of the inserted element. If the reference element is not found, -1 is returned, and no changes are made to the structure. If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state and throws a FileFormatException with detailed error information.

InsertBefore(IElement, int)

Inserts an element before the element with the specified unique ID.

public int InsertBefore(IElement newElement, int elementId)

Parameters

newElement IElement

The element to be inserted.

elementId int

The unique ID of the element before which the new element should be inserted.

Returns

int

The unique identifier ElementId of the inserted element if the new element is successfully inserted before the specified element. If the specified element with the provided ID is not found, -1 is returned. If an error occurs during the insertion operation, a FileFormatException is thrown.

Remarks

This method inserts the specified element before the element with the provided unique ID in the structure. The method attempts to locate the element with the specified ID and insert the new element before it. If the insertion operation is successful, the method returns ElementId of the inserted element. If the element with the specified ID is not found, -1 is returned, and no changes are made to the structure. If an exception occurs during the insertion operation, the method attempts to restore the structure to its previous state and throws a FileFormatException with detailed error information.

Remove(int)

Removes the specified element with the given ElementID from the document.

public bool Remove(int elementId)

Parameters

elementId int

The unique identifier of the element you want to remove.

Returns

bool

true if the element is successfully removed; otherwise, false if the element is not found. Throws a FileFormatException if an exception occurs during the operation.

Remarks

If the specified element is not found in the document, this method returns false. If the removal of the element fails due to an exception, it rolls back the operation and reverts the document to its original state. The element is removed from the internal document structure and, if applicable, the underlying OOXML document.

RemoveAfter(IElement)

Removes the element that follows a specified element in the structure.

public bool RemoveAfter(IElement element)

Parameters

element IElement

The element whose follower should be removed.

Returns

bool

true if the element following the specified element is successfully removed; otherwise, false. If the specified element is not found or if there is no element following it, false is returned. If an error occurs during the removal operation, a FileFormatException is thrown.

Remarks

This method removes the element that comes after the specified element in the structure. It is essential to ensure that the specified element exists in the structure and has an element following it. If the removal operation is successful, the method returns true. If the element is not found or there is no element following it, false is returned, and no changes are made to the structure. If an exception occurs during the removal operation, the method attempts to restore the structure to its previous state and throws a FileFormatException with detailed error information.

RemoveAfter(int)

Removes the element that follows a specified element in the structure by its unique ID.

public bool RemoveAfter(int elementId)

Parameters

elementId int

The unique ID of the element whose follower should be removed.

Returns

bool

true if the element following the specified element is successfully removed; otherwise, false. If the specified element is not found or if there is no element following it, false is returned. If an error occurs during the removal operation, a FileFormatException is thrown.

Remarks

This method removes the element that comes after the specified element with the provided unique ID in the structure. It is essential to ensure that the specified element exists in the structure and has an element following it. If the removal operation is successful, the method returns true. If the element is not found or there is no element following it, false is returned, and no changes are made to the structure. If an exception occurs during the removal operation, the method attempts to restore the structure to its previous state and throws a FileFormatException with detailed error information.

RemoveBefore(IElement)

Removes the element that precedes the specified element from the collection. If the specified element is not found or is the first element in the collection, no removal occurs.

public bool RemoveBefore(IElement element)

Parameters

element IElement

The element whose predecessor should be removed.

Returns

bool

true if the preceding element is successfully removed; otherwise, false if the element is not found. Throws a FileFormatException if an exception occurs during the operation.

Remarks

The method searches for the element with the specified element identifier and removes the element that immediately precedes it in the collection. If the specified element is not found or is the first element in the collection, no removal occurs, and the method returns -1. In case of success, it returns the ElementId of removed element. If the removal of the preceding element fails due to an exception, it rolls back the operation and reverts the document to its original state.

See Also

RemoveBefore(int)

Removes the element that precedes the specified element with the given ElementID in the document.

public bool RemoveBefore(int elementId)

Parameters

elementId int

The unique identifier of the element after which you want to remove the preceding element.

Returns

bool

true if the preceding element is successfully removed; otherwise, false if the element is not found. Throws a FileFormatException if an exception occurs during the operation.

Remarks

If the specified element is not found in the document, this method returns false. If the removal of the preceding element fails due to an exception, it rolls back the operation and reverts the document to its original state. The preceding element is removed from the internal document structure and, if applicable, the underlying OOXML document.

Save(Stream)

Saves the document to the specified Stream.

public void Save(Stream stream)

Parameters

stream Stream

The stream to which the document will be saved.

Exceptions

FileFormatException

Thrown if an error occurs while saving the document.

Save(string)

Saves the document to a file specified by its filename.

public void Save(string filename)

Parameters

filename string

The path to the file where the document will be saved.

Exceptions

FileFormatException

Thrown if an error occurs while saving the document.

Update(IElement)

Updates an existing element in the structure.

public bool Update(IElement element)

Parameters

element IElement

The updated element to replace the existing one.

Returns

bool

true if the element is successfully updated; otherwise, false. If the element is not found in the structure, false is returned. If an error occurs during the update operation, a FileFormatException is thrown.

Remarks

This method updates an existing element within the structure based on the element's unique identifier. It locates the element in the structure and replaces it with the updated element. If the update operation is successful, the method returns true>. If the element is not found in the structure, false is returned, and no changes are made to the structure. If an exception occurs during the update operation, the method attempts to restore the structure to its previous state and throws a FileFormatException with detailed error information.

Update(int, IElement)

Updates an existing element in the structure.

public bool Update(int elementId, IElement element)

Parameters

elementId int

The unique identifier of the element to be updated.

element IElement

The updated element to replace the existing one.

Returns

bool

true if the element is successfully updated; otherwise, false. If the element with the specified ID is not found in the structure, false is returned. If the elements' IDs do not match, a FileFormatException is thrown. If an error occurs during the update operation, a FileFormatException is thrown.

Remarks

This method updates an existing element within the structure based on the provided element's unique identifier. It verifies that the element to be updated has the same ID as the specified element ID. If the IDs match, the method attempts to locate the element in the structure and replace it with the updated element. If the update operation is successful, the method returns true>. If the element with the specified ID is not found, false is returned, and no changes are made to the structure. If the provided element's ID does not match the specified element ID, a FileFormatException is thrown. If an exception occurs during the update operation, the method attempts to restore the structure to its previous state and throws a FileFormatException with detailed error information.