About Me

My Photo
Ireland
Hello, my name is Cathal Coffey. I am best described as a hybrid between a developer and an adventurer. When I am not behind a keyboard coding, I am hiking and climbing the beautiful mountains of my home country Ireland. I am a full time student studying Computer Science & Software Engineering at the National University of Ireland Maynooth. I am finishing the final year of a 4 year degree in September 2009. I am the creator of an open source project on codeplex.com called DocX. At the moment I spend a lot of my free time advancing DocX and I enjoy this very much. My aim is to build a community around DocX and add features based on requests from this community. I really enjoy hearing about how people are using DocX in their work\personal projects. So if you are one of these people, please send me an email. Cathal coffey.cathal@gmail.com

Friday, August 28, 2009

DocX v1.0.0.8 released

DocX version 1.0.0.8 is now available for download from here. So what's new in this version? Why should you download it?

The biggest addition to this version of DocX is a better method of building highly formatted Paragraphs. This new method is a fluent interface and it was proposed by one of DocX's users Morten Bjerre. So what is a fluent interface? This Wikipedia article explains fluent interfaces quite well.

Here is an example of how this new addition to DocX makes building highly formatted Paragraphs easier and more intuitive. Lets say that we want to create a document that looks like this.

Untitled
This can now be accomplished with the below code.

// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraphs.
Paragraph p = document.InsertParagraph();

p.Append(
"I am ").Append("bold").Bold()
.Append(
" and I am ")
.Append(
"italic").Italic().Append(".")
.AppendLine(
"I am ")
.Append(
"Arial Black")
.Font(
new FontFamily("Arial Black"))
.Append(
" and I am not.")
.AppendLine(
"I am ")
.Append(
"BLUE").Color(Color.Blue)
.Append(
" and I am")
.Append(
"Red").Color(Color.Red).Append(".");

// Save this document.
document.Save();
}
// Release this document from memory.


This version of DocX also contains three new features that were added by a user
Joel Folkerts. Joel is the first user to post working and tested code for inclusion in DocX. These new features are listed below.
1. Row.Height property
2.
Cell.Width property
3. Row.MergeCells
Row.Height and Cell.Width are self explanatory but here is an example of using Row.MergeCells. Lets say we have the following Table.
UntitledWe can merge the cells {1-3} and the cells {6-8} on row 1 to get the following table.Untitled3This can now be accomplished in DocX with the following code.
// Load a document.
using (DocX document = DocX.Load(@"Test.docx"))
{
// Grab the first Table.
Table t = document.Tables[0];

// Grab the first Row of this Table.
Row r = t.Rows[1];

// Merge the cells 1-3 into one new cell.
r.MergeCells(1, 3);

// Merge the cells 4-6
r.MergeCells(4, 6);

// Save all changes made to this document.
document.Save();
}
// Release this document from memory.


I have also added a new feature that was requested by a user Mathetes. This new feature allows TextReplace() to take a Formatting object as an extra parameter and it then only returns text that matches both the input string and that format. Another new extra parameter for this function is a new Formatting object to be applied to the inserted text.
// Load a document.
using (DocX document = DocX.Load(@"Test.docx"))
{
// The formatting to match.
Formatting matchFormatting = new Formatting();
matchFormatting.Size = 10;
matchFormatting.Italic =
true;
matchFormatting.FontFamily =
new FontFamily("Times New Roman");

// The new formatting to apply.
Formatting newFormatting = new Formatting();
newFormatting.Size = 22;
newFormatting.UnderlineStyle =
UnderlineStyle.dotted;
newFormatting.Bold =
true;

// Iterate through the paragraphs
foreach (Paragraph p in document.Paragraphs)
{
p.ReplaceText
(
"wrong", "right", false,
RegexOptions.IgnoreCase,
newFormatting, matchFormatting,
MatchFormattingOptions.SubsetMatch
);
}

// Save all changes made to this document.
document.Save();
}
// Release this document from memory.
I want to take this opportunity to say a huge thank you to Morten Bjerre, Joel Folkerts and Mathetes. You have all enhanced DocX greatly with the features that you have added to this version.

If anyone out there reading this wants to suggest a new feature, please send it my way. I will try my best to include it in a future version for DocX.
happy coding,
Cathal

4 comments:

  1. How to Replace Wingdings symbols?
    Formatting WingdingsitemNew = new Formatting();

    WingdingsitemNew.FontFamily = new FontFamily("Wingdings");
    Formatting Wingdingsitemold = new Formatting();
    Wingdingsitemold.FontFamily = new FontFamily("Cambria");

    document.ReplaceText("chkAFO1", "111", false, RegexOptions.IgnoreCase, WingdingsitemNew, Wingdingsitemold, MatchFormattingOptions.ExactMatch);
    document.ReplaceText("chkAFO", "254", false, RegexOptions.IgnoreCase, WingdingsitemNew, Wingdingsitemold, MatchFormattingOptions.ExactMatch);

    ReplyDelete
  2. Would we be able to replace carriage returns? I used ^13 in VBA previously but DocX doesn't recognise this. For example, replace ^13{ADDRESS3} with "" removed the text {ADDRESS3} (on it's own line in Word) entirely.

    ReplyDelete
  3. Hi, its a great dll, But can you publish more advance example like multiple multi page template with mail merge functionality especially <> in same page. It would be great help.

    ReplyDelete