This is a small project (under development) that allows to create Microsoft Word documents (.docx) using .NET. It was created because working with OpenXML is way too hard for me, and time consuming. I originally created it for using within PowerShell module called PSWriteOffice, but thought it may be useful for others. I used to use DocX library (which I co-authored, before it was taken over by Xceed) to create Word documents, but it only supports .NET Framework, and their newest community license makes the project unusuable.
As I am not really a developer, and I hardly know what I'm doing if you know how to help out - please do.
- If you see bad practice, please open and issue/submit PR.
- If you know how to do something in OpenXML that could help this project - please open an issue/submit PR
- If you see something that could work better - please open and issue/submit PR
- If you see something that I totally made a fool of myself - please open an issue/submit PR
- If you see something that works not the way I think it works - please open an issue/submit PR
I hope you get the drift? If it's bad - open an issue/fix it! I don't know what I'm doing! The main thing is - it has to work with .NET Framework 4.7.2, .NET Standard 2.0 and so on.
This project is under development and as such there's a lot of things that can and will change, especially if some people help out.
Platform | Status | Code Coverage |
---|---|---|
Windows | ||
Linux | ||
MacOs |
Here's a list of features currently supported and those that are planned. It's not a closed list, more of TODO, and I'm sure there's more:
- Word basics
- Create
- Load
- Save (autoopen on save as an option)
- SaveAs (not working correcly in edge cases)
- Word properties
- Reading basic and custom properties
- Setting basic and custom properties
- Sections
- Add Paragraphs
- Add Headers and Footers (Odd/Even/First)
- Remove Headers and Footers (Odd/Even/First)
- Remove Paragraphs
- Remove Sections
- Headers and Footers in document (not including sections)
- Add Default, Odd, Even, First
- Remove Default, Odd, Even, First
- Paragraphs/Text and make it bold, underlined, colored and so on
- Paragraphs and change alignment
- Tables
- Add rows and columns
- Add cells
- Add cell properties
- Remove rows
- Remove cells
- Others
- Merge cells (vertically, horizontally)
- Split cells (vertically)
- Split cells (horizontally)
- Images/Pictures (limited support - jpg only / inline type only)
- Add images from file to Word
- Save image from Word to File
- Other image types
- Other location types
- Hyperlinks
- Bookmarks
- Comments
- Add comments
- Remove comments
- Track comments
- Fields
- Shapes
- Charts
- Lists
- Add lists
- Remove lists
- Table of contents
- Add TOC
- Borders
- Background
- Watermarks
- Add watermark
- Remove watermark
This list of features is for times when you want to quickly fix something rather than playing with full features.
This features are available as part of WordHelpers
class.
- Remove Headers and Footers from a file
This short example show how to create Word Document with just one paragraph with Text and few document properties.
string filePath = @"C:\Support\GitHub\PSWriteOffice\Examples\Documents\BasicDocument.docx";
using (WordDocument document = WordDocument.Create(filePath)) {
document.Title = "This is my title";
document.Creator = "Przemysław Kłys";
document.Keywords = "word, docx, test";
var paragraph = document.AddParagraph("Basic paragraph");
paragraph.ParagraphAlignment = JustificationValues.Center;
paragraph.Color = System.Drawing.Color.Red.ToHexColor();
document.Save(true);
}
This short example shows how to add headers and footers to Word Document.
using (WordDocument document = WordDocument.Create(filePath)) {
document.Sections[0].PageOrientation = PageOrientationValues.Landscape;
document.AddParagraph("Test Section0");
document.AddHeadersAndFooters();
document.DifferentFirstPage = true;
document.DifferentOddAndEvenPages = true;
document.Sections[0].Header.First.AddParagraph().SetText("Test Section 0 - First Header");
document.Sections[0].Header.Default.AddParagraph().SetText("Test Section 0 - Header");
document.Sections[0].Header.Even.AddParagraph().SetText("Test Section 0 - Even");
document.AddPageBreak();
document.AddPageBreak();
document.AddPageBreak();
document.AddPageBreak();
var section1 = document.AddSection();
section1.PageOrientation = PageOrientationValues.Portrait;
section1.AddParagraph("Test Section1");
section1.AddHeadersAndFooters();
section1.Header.Default.AddParagraph().SetText("Test Section 1 - Header");
section1.DifferentFirstPage = true;
section1.Header.First.AddParagraph().SetText("Test Section 1 - First Header");
document.AddPageBreak();
document.AddPageBreak();
document.AddPageBreak();
document.AddPageBreak();
var section2 = document.AddSection();
section2.AddParagraph("Test Section2");
section2.PageOrientation = PageOrientationValues.Landscape;
section2.AddHeadersAndFooters();
section2.Header.Default.AddParagraph().SetText("Test Section 2 - Header");
document.AddParagraph("Test Section2 - Paragraph 1");
var section3 = document.AddSection();
section3.AddParagraph("Test Section3");
section3.AddHeadersAndFooters();
section3.Header.Default.AddParagraph().SetText("Test Section 3 - Header");
Console.WriteLine("Section 0 - Text 0: " + document.Sections[0].Paragraphs[0].Text);
Console.WriteLine("Section 1 - Text 0: " + document.Sections[1].Paragraphs[0].Text);
Console.WriteLine("Section 2 - Text 0: " + document.Sections[2].Paragraphs[0].Text);
Console.WriteLine("Section 2 - Text 1: " + document.Sections[2].Paragraphs[1].Text);
Console.WriteLine("Section 3 - Text 0: " + document.Sections[3].Paragraphs[0].Text);
Console.WriteLine("Section 0 - Text 0: " + document.Sections[0].Header.Default.Paragraphs[0].Text);
Console.WriteLine("Section 1 - Text 0: " + document.Sections[1].Header.Default.Paragraphs[0].Text);
Console.WriteLine("Section 2 - Text 0: " + document.Sections[2].Header.Default.Paragraphs[0].Text);
Console.WriteLine("Section 3 - Text 0: " + document.Sections[3].Header.Default.Paragraphs[0].Text);
document.Save(true);
}
I'm using a lot of different resources to make OfficeIMO useful. Following resources may come useful to understand some concepts if you're going to dive into sources.
- Packages and general (Open XML SDK)
- Word processing (Open XML SDK)
- https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/gg537324(v=office.12)
- Office 2010 Visual How Tos
- Points, inches and Emus: Measuring units in Office Open XML
- English Metric Units and Open XML
- Open XML: add a picture
- How to add section break next page using openxml?
- How to Preserve string with formatting in OpenXML Paragraph, Run, Text?