An open-source set of extension methods for DocumentFormat.OpenXml
which simplifies creating and modifying Word documents (such as .docx).
You can use these methods with your existing DocumentFormat.OpenXml
code without having to change anything.
If you think something very important is missing in this library, please create an Issue for it. I'm willing to implement anything that is crucial. Contributions via Pull Requests are always welcome! :)
If you have any questions, feel free to open a Discussion.
Please remember that I may not always be able to push updates because of lack of time. If you wish to support me, you can buy me a coffee:
Thank you! ❤️
Example document creation with 1 formatted paragraph:
string fileName = "TEST.docx";
using (var document = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
Body body = document.CreateBody()
.PageWidth(21, MeasuringUnits.Centimeters) // A4 width
.PageHeight(29.7, MeasuringUnits.Centimeters); // A4 height
body.AppendChild(new Paragraph(
new Run().Text("This is a single, centered paragraph.")
.FontSize(16)
.FontColor("Red") // #FF0000 will also work
.FontFace("Times New Roman"))
.Justification(JustificationValues.Center));
document.Save();
}
- Super quick and easy Builder-like pattern:
var run = new Run().Text("This is a simple Run.")
.FontSize(24)
.FontColor("Red")
.Bold()
.Italic();
- The ability to create Paragraphs with multiple Runs and multiple formattings:
Run[] runs = new[]
{
new Run().Text("This is a").FontSize(16),
new Run().Text(" single paragraph ").FontFace("Comic Sans MS"),
new Run().Text("with 3 Runs.").HighlightColor(HighlightColorValues.Cyan)
};
body.AppendChild(new Paragraph(runs))
Example with 5 runs:
- The ability to create tables quickly:
body.AppendChild(QTable.Create(new[]
{
new[] { "1", "John", "Doe" },
new[] { "2", "Jane", "Doe" },
new[] { "3", "John", "Smith" },
new[] { "4", "Jane", "Smith" }
}));
Example:
- The ability quickly merge cells:
// Merge cells [0,0] and [1,0] (vertically)
table.Rows(0)?.Cells(0)?.VerticalMerge(MergedCellValues.Restart);
table.Rows(1)?.Cells(0)?.VerticalMerge(MergedCellValues.Continue);
// Merge cells [2,0] and [3,0] (vertically)
table.Rows(2)?.Cells(0)?.VerticalMerge(MergedCellValues.Restart);
table.Rows(3)?.Cells(0)?.VerticalMerge(MergedCellValues.Continue);
Example:
- The ability to quickly create formatted rows and cells:
table1.AppendChild(QTableRow.Create(
new[] { "ID", "First Name", "Last Name" },
rowFormatting: new TableRowFormatting { IsHeader = true },
runFormatting: new RunFormatting { Bold = true }));
- The ability to easily customize existing table cells:
foreach (TableCell? cell in table2.GetColumnOfCells(3).Skip(1)) // 4th column of cells, skip header cell
{
if (cell?.Paragraphs(0)?.GetText() == "Yes")
cell?.FillColor("LightGreen");
else
cell?.FillColor("LightPink");
}
Example:
- The ability to create inlined images:
body.AppendChild(new Paragraph(
new Run(QDrawing.FromImage(body, "Icon.png", ImagePartType.Png, 32, 32)),
new Run().Text(" This image is inlined with the text.").VerticalPosition(8)
).Justification(JustificationValues.Center));
Example:
- The ability to create anchored images:
body.AppendChild(new Paragraph(
new Run(QDrawing.FromImage(body, "Icon.png", ImagePartType.Png, 128, 128)
.ToAnchoredDrawing()
.AbsoluteVerticalPosition(0, ImageMeasuringUnits.Pixels, DW.VerticalRelativePositionValues.Paragraph)
.SquareWrapping(0, 0, 0.5, 0, ImageMeasuringUnits.Centimeters)),
new Run().Text("This\nimage\nis\naligned\nbefore\nthe text.\n")
));
Example:
With images you can also:
- Set the transparency
- Set the rotation
- Set cropping
- Set a border
- Set text wrapping
- Set the position
- many more...
- You can also use additional
Paragraph
inherited objects, such as:EmptyLine
PageBreak
HorizontalLine