A simple wrapper for DocumentFormat.OpenXml word processing documents (MS Word and similar).
Allows easier creation of OpenXML wordprocessing documents using a fluent API. This is a .Net Standard 2.0 library and depends on the DocumentFormat.OpenXml library. This library was inspired by the ReportDotNet project on Github.
- .NETStandard 2.0
- DocumentFormat.OpenXml (>= 2.9.1)
The library exposes an abstract class WordDocument
that implements fluent methods to assist in creating sections, parargraphs, checkboxes and tables (with rows and cells).
There is a lot of functionality that must still be added, but this library is already useful for creating fairly complex document layouts.
Available on nuget.org
Install-Package Frontforge.OpenDocx.Core
dotnet add package Frontforge.OpenDocx.Core
See the /examples
folder for more complete examples.
internal class BasicSample
: WordDocument
{
#region implementation
public static BasicSample Create()
{
return new BasicSample().BuildDoc();
}
private BasicSample BuildDoc()
{
// create a new section and set section properties
var section = Section()
.PageSize(PageSize.A4)
.PageMargins(PredefinedPageMargins.Narrow);
// add a paragraph
var par = Par("This is a simple paragraph that is bold and center aligned with a " +
"16pt font size and a 6pt spacing before the paragraph.",
HorizontalAlignment.Center)
.SpacingBefore(6)
.Bold()
.FontSize(16);
section.Add(par);
// add a table
var tbl = Table()
.Width(new Unit(100, UnitType.pct)) // 100% width
.TopBorder()
.BottomBorder();
tbl.Add(
Row(
Cell(Par("Cell 0, 0").Bold()).Width(30, UnitType.pct),
Cell(Par("Cell 0, 1"))
),
Row(
Cell(Par("Cell 1, 0").Bold()),
Cell(Par("Cell 1, 1"))
)
);
// add the table to the section
section.Add(tbl);
// add the section to the document
AddSection(section);
return this;
}
#endregion
}
This document can be created and saved to disk, or any other Stream
as follows:
// save to file
using (var fileStream = new FileStream(FileNameBasicDoc1, FileMode.Create))
{
BasicSample.Create().Save(fileStream);
fileStream.Flush();
}
// open the file
var process = new Process
{
StartInfo =
{
UseShellExecute = true,
FileName = FileNameBasicDoc1
}
};
process.Start();
Any and all contributions are welcomed. Please follow this excellent guide. Note: This project uses git-flow.
The basic steps for contributing are:
- Fork the project & clone locally.
- Create an upstream remote and sync your local copy before you branch.
- Branch for each separate piece of work.
- Do the work, write good commit messages, and read the CONTRIBUTING file.
- Push to your origin repository.
- Create a new PR in GitHub.
- Respond to any code review feedback.