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

Thursday, March 19, 2009

DocX version 1.0.0.1 released

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.

imageFigure 1.0 – Word 2007 font options, tab 1

image
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.
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 above code will generate the following document.

imageFigure 1.2 – Out of thin air

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

Thursday, March 12, 2009

Compiling DocX from source code

I received a few emails outlining problems that people have encountered while trying to compile DocX from source. This post explains how to solve those problems.

Unable to read the project file “DocX.csproj”

Anyone who downloaded change set 17108, this was the build uploaded (Mon at 6:58 PM) would have experienced the following error on opening DocX.csproj in Visual Studio.

Unable to read the projectFigure 1.0 – Unable to read the project file “DocX.csproj”

There are two ways to overcome this problem,

  1. You can simply download change set 15663, this is the build uploaded (Thursday at 11:08 AM).

    or
  2. Open DocX.csproj in a text editor (notepad.exe) and remove the following two lines,
    1. <DeepSeaObfuscate>false</DeepSeaObfuscate>
    2. <Import Project="$(MSBuildExtensionsPath)\DeepSea Obfuscator\DeepSea.Obfuscator.targets" />

Missing reference DocumentFormat.OpenXml

If Visual Studio’s reference window cannot find DocumentFormat.OpenXml then you need to download and install the Open XML Format SDK 2.0.

imageFigure 1.1 – Missing reference DocumentFormat.OpenXml

Test projects missing reference DocX

If Visual Studio’s reference window for the projects CustomPropertyTextApp and StringReplaceTestApp cannot find DocX then you need to update the reference.

imageFigure 1.2 – Test projects missing reference DocX

First you need to build the DocX, you can do this by right clicking on the project and selecting build.

imageFigure 1.3 – Build DocX

Right click on the references for both projects and select “Add Reference…”

imageFigure 1.4 – Add reference…

Select the Projects tab and choose DocX.

imageFigure 1.5 – Select the projects tab

That’s it your done, now the entire solution will compile.