Friday, November 30, 2012

[C#] DocX .NET introduction - Headers, footers and some body

DocX is a fantastic tool to create Microsoft Word-eligible documents, without actually having Word installed. This is because .docx objects are a zip-file, containing other objects of XML. You can see this for yourself by opening a .docx file with WinRar. The XML heritage creates great standalone functionality, which for example makes the code deployable on a webserver (without paying high prices for a Word license).

And to be honest, the original Word API (Look for Microsoft Word Object Library in COM in Visual Studio, if you have Word installed) is terrible. An absolute disaster to code and read. DocX will save you. 
The DocX library is it's extremely easy and straight-forward coding, making anyone able to understand and use it just after a few minutes of introduction.

Visual Studio 2010
.NET 4.0

Do not make the mistake of pressing the fat, purple and inviting "Download" button - at the date of writing this article it links to an old version  (v., while the current, community-updated version is v. and contains essential and new methods such as proper support for merging cells in a table. The author seems to have taken a year-long hiatus, which is sad to see.

I have taken the liberty to host both the compiled (and updated to v. DLL for DocX, and a stripped-down project for you to compile.


And here is the link to the original source

But now, let's see some code. We are going to make a document with headers, footers, and some body-text.

Example code to get you started:
Single code:

Reference DocX in your project

using Novacode;


String fileName = "MyDocXDocument.docx";
DocX document = DocX.Create(fileName);

The "document" object is now our textfile which can be used to append pictures, tables and text.


Adding text is simple:

document.InsertParagraph("some text");

Paragraphs are also objects:

Paragraph p1 = document.InsertParagraph();
p1.Append("I'm appended to a paragraph object.");     

The Paragraph object has many methods. For example, you can use p1.InsertPicturep1.InsertPageBreakAfterSelfp1.InsertTable, and many more things.

In essense, paragraphs are an "anchor" which many things can be done to manipulate the page at the position it resides. Every paragraph is a line on the document, as if you would press Enter on a text you're writing. Thus the code must go in a sequential manner as you insert paragraphs in your document in a flowing manner: up to down.


The "document" object has of course properties linked to how the document looks overall, such as

document.MarginBottom = 1f;
document.PageWidth = 800f;

You can even use " ctrl+f " functionality with document.ReplaceText

Headers and Footers

Headers and footers functions almost the same, with the difference of headers residing in the top of the document on every page, and footers in the bottom of every page.

The document object has a property called DifferentFirstPage which makes you able to use a unique header on the first page of your document, utilising the document.Headers.First object, making it not repeat on other pages. There is also the document.Headers.Even and document.Headers.Odd which manipulates pages with even and odd numbers respectively. However, there is no functionality to specifiy a header/footer on a specific page, probably due to limitations in the fundamental docx markup.

Self-explanatory code:

document.DifferentFirstPage = true;

Header firstHeader = document.Headers.first;
Header evenHeader = document.Headers.even;
Header oddHeader = document.Headers.odd;
firstHeader.InsertParagraph("I'm a paragraph at the first header, at page 0.");
evenHeader.InsertParagraph("I'm a paragraph at even headers, page 2, 4, 6 etc.");
oddHeader.InsertParagraph("I'm a paragraph at odd headers, page 1, 3, 5 etc.");

Please check the authors site for more and advanced examples

There is also a fairly active forum

No comments:

Post a Comment