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.
// 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.We can merge the cells {1-3} and the cells {6-8} on row 1 to get the following table.This 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
How to Replace Wingdings symbols?
ReplyDeleteFormatting 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);
Hi. I created a bullet list with DocX and try to change the font size of the bullet (the font is "Symbol").
DeleteI don't find a way. Thanks!
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.
ReplyDeleteHi, 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*"<< next record >>" got hidden
ReplyDeleteI was trying to load an existing microsoft word doc using docx, search all occurences of a word and highlight it. While I could find ways to highlight a paragraph or highlight a new text to be appended, I couldnt find a way to search a text and highlight it.
ReplyDeleteSo, i used an indirect method using replace. Something like this
if (doc.Paragraphs[i] is Paragraph)
{
Paragraph sen = doc.Paragraphs[i] as Paragraph;
Formatting form = new Formatting();
form.Highlight = Highlight.yellow;
form.Bold = true;
sen.ReplaceText(searchText, searchText, false, System.Text.RegularExpressions.RegexOptions.IgnoreCase,
form, null, MatchFormattingOptions.ExactMatch);
}
This works but I wanted to check if there is a better way
Your code snippet saved my time. :)
DeleteThank you for bringing more information to this topic for me. I’m truly grateful and really impressed.
ReplyDeletexml data conversion