Create Empty Word Document
IronWord: Creating Word and DOCX Documents Made Simple
IronWord makes creating a new Word and DOCX document very simple. Simply instantiate a new WordDocument
object. Additionally, any document structure object, such as a paragraph, table, and section, can be used to instantiate a new Word document. Finally, use the SaveAs
method to export the document.
Here is a sample code that demonstrates how this can be achieved in C#:
// Import necessary namespaces
using IronWord;
// Instantiate a new WordDocument object
WordDocument document = new WordDocument();
// Create a new section in the document
DocumentSection section = document.AddSection();
// Add a new paragraph to the section
DocumentParagraph paragraph = section.AddParagraph();
paragraph.AppendText("Hello, World! This is a new paragraph in our Word document.");
// Add a table to the document
DocumentTable table = section.AddTable(3, 3); // 3x3 table
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
// Write some text into table cells
table.Rows[row].Cells[col].AddParagraph().AppendText($"Cell {row+1}-{col+1}");
}
}
// Save the document to a file
document.SaveAs("NewDocument.docx");
// Import necessary namespaces
using IronWord;
// Instantiate a new WordDocument object
WordDocument document = new WordDocument();
// Create a new section in the document
DocumentSection section = document.AddSection();
// Add a new paragraph to the section
DocumentParagraph paragraph = section.AddParagraph();
paragraph.AppendText("Hello, World! This is a new paragraph in our Word document.");
// Add a table to the document
DocumentTable table = section.AddTable(3, 3); // 3x3 table
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
// Write some text into table cells
table.Rows[row].Cells[col].AddParagraph().AppendText($"Cell {row+1}-{col+1}");
}
}
// Save the document to a file
document.SaveAs("NewDocument.docx");
' Import necessary namespaces
Imports IronWord
' Instantiate a new WordDocument object
Private document As New WordDocument()
' Create a new section in the document
Private section As DocumentSection = document.AddSection()
' Add a new paragraph to the section
Private paragraph As DocumentParagraph = section.AddParagraph()
paragraph.AppendText("Hello, World! This is a new paragraph in our Word document.")
' Add a table to the document
Dim table As DocumentTable = section.AddTable(3, 3) ' 3x3 table
For row As Integer = 0 To 2
For col As Integer = 0 To 2
' Write some text into table cells
table.Rows(row).Cells(col).AddParagraph().AppendText($"Cell {row+1}-{col+1}")
Next col
Next row
' Save the document to a file
document.SaveAs("NewDocument.docx")
Explanation:
- Namespace Import: The code begins by importing the necessary namespaces required to use the IronWord library.
- Document Creation: A new
WordDocument
object is instantiated. This object will serve as the starting point for the creation of our Word document. - Section Addition: A new section is added to the document using the
AddSection
method. Sections are used to divide the document into separate parts that can have different formatting. - Paragraph Addition: Within the newly added section, a paragraph is created using the
AddParagraph
method of theDocumentSection
class. The paragraph object is then used to append text to the section. - Table Creation: A 3x3 table is added to the section using the
AddTable
method. The table is populated with text in each cell using nested loops to iterate through the rows and columns. - Document Saving: Finally, the document is saved to a .docx file using the
SaveAs
method, which takes the desired file name as an argument.