Note: Code samples have been updated to work with DocX version 1.0.0.6.
I promised that I would release DocX frequently, and with lots of new useful features.
Version 1.0.0.1 which can be downloaded from here, allows a developer to create a document on the fly, add new paragraphs and insert highly formatted text. Just about every font option that is available in Word 2007, is now exposed by DocX. These options include, but are not limited to; font, size, bold, italics, underline, strike through, superscript, subscript, color, highlight color, scale, shadow, outline, emboss, engrave, hidden, spacing, position, kerning.
Figure 1.0 – Word 2007 font options, tab 1
Figure 1.1 – Word 2007 font options, tab 2
Here’s an example of how to generate a docx file, which contains custom formatted text, out of thin air, using docx.
// Create a new document.The above code will generate the following document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Create a formatting called f1
Formatting f1 = new Formatting();
f1.FontFamily = new FontFamily("Agency FB");
f1.Size = 28;
f1.Bold = true;
f1.FontColor = Color.RoyalBlue;
f1.UnderlineStyle = UnderlineStyle.doubleLine;
f1.UnderlineColor = Color.Red;
// Insert a new Paragraph into this document.
Paragraph p = document.InsertParagraph("I've got style!", false, f1);
// Create a formatting called f2
Formatting f2 = new Formatting();
f2.FontFamily = new FontFamily("Colonna MT");
f2.Size = 36.5;
f2.Italic = true;
f2.FontColor = Color.SeaGreen;
// Insert new text at the end of this Paragraph.
p.InsertText("I have a different style.", false, f2);
// Save all changes made to this document.
document.Save();
}// Release this document from memory
The coolest thing about this code, is that, you can generate .docx files in the cloud, without a dependency on Microsoft Word.
Please remember that DocX is not a full blown solution for .docx creation and manipulation, not yet anyway. It is a project, that I am working on, in my spare time. If you would like to request a feature for version 1.0.0.2, please email me or alternatively leave a comment here.
My current aim for version 1.0.0.3, is to add the ability to create and manipulate images, tables and lists.
happy coding,
Cathal
No comments:
Post a Comment