Wednesday, December 21, 2011

New version of WordXML document using OpenXML SDK 2.0 with bold, color and font size formatting


I first dabbled with the OpenXML SDK 2.0 in January 2011.

At that time, there was a problem which I could not solve: specifying different font sizes, font colors and bolding effects for different words on the same line.

I managed to achieve different formatting effects for sentences on different lines, but never managed to get it working if the words were on the same line.

Today, I finally figured out how to do it, and scroll down if you are interested to look at the code.

Oh, and do remember to add in the reference for OpenXML SDK and add the imports.

A screenshot of how the generated Word document looks like:

2011-12-21_214518
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
public void GenerateWord(){
            WordprocessingDocument doc = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document);
            MainDocumentPart mainPart = doc.AddMainDocumentPart();
            mainPart.Document = new Document();
            Body body = new Body();

            /**** Create the contents ****/
            DocumentFormat.OpenXml.Wordprocessing.Run run1, run2, run3, run4;
            Paragraph para;

            string text1 = "OpenXML Demo";
            string text2 = "By Dora Chua";
            string text3 = "Code modified from original at : ";
            string text4 = "http://msdn.microsoft.com/en-us/library/bb448854.aspx";
            string text5 = "Alex";


            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties1 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new RunFonts() { Ascii = "Times New Roman" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties2 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new RunFonts() { Ascii = "Arial" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties3 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new RunFonts() { Ascii = "Script" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties4 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new RunFonts() { Ascii = "Courier New" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties5 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new RunFonts() { Ascii = "Courier New" });
            
            DocumentFormat.OpenXml.Wordprocessing.FontSize fs1 = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
            DocumentFormat.OpenXml.Wordprocessing.Color c1 = new DocumentFormat.OpenXml.Wordprocessing.Color();
            DocumentFormat.OpenXml.Wordprocessing.Bold b1 = new DocumentFormat.OpenXml.Wordprocessing.Bold();

            DocumentFormat.OpenXml.Wordprocessing.FontSize fs2 = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
            DocumentFormat.OpenXml.Wordprocessing.Color c2 = new DocumentFormat.OpenXml.Wordprocessing.Color();
            DocumentFormat.OpenXml.Wordprocessing.Bold b2 = new DocumentFormat.OpenXml.Wordprocessing.Bold();

            DocumentFormat.OpenXml.Wordprocessing.FontSize fs3 = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
            DocumentFormat.OpenXml.Wordprocessing.Color c3 = new DocumentFormat.OpenXml.Wordprocessing.Color();
            DocumentFormat.OpenXml.Wordprocessing.Bold b3 = new DocumentFormat.OpenXml.Wordprocessing.Bold();

            DocumentFormat.OpenXml.Wordprocessing.FontSize fs4 = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
            DocumentFormat.OpenXml.Wordprocessing.Color c4 = new DocumentFormat.OpenXml.Wordprocessing.Color();
            DocumentFormat.OpenXml.Wordprocessing.Bold b4 = new DocumentFormat.OpenXml.Wordprocessing.Bold();


            DocumentFormat.OpenXml.Wordprocessing.FontSize fs5 = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
            DocumentFormat.OpenXml.Wordprocessing.Color c5 = new DocumentFormat.OpenXml.Wordprocessing.Color();
            DocumentFormat.OpenXml.Wordprocessing.Bold b5 = new DocumentFormat.OpenXml.Wordprocessing.Bold();


            /*** Format text1   ***/
            run1 = new DocumentFormat.OpenXml.Wordprocessing.Run();
            fs1.Val = "20";
            c1.Val = "green";
            b1.Val = true;
           
            runProperties1.Append(fs1);
            runProperties1.Append(c1);
            runProperties1.Append(b1);

            run1.Append(runProperties1);
            run1.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text1));

            para = new Paragraph(run1);
            body.AppendChild(para);


            /*** Format text2 ***/
            run2 = new DocumentFormat.OpenXml.Wordprocessing.Run();

            fs2.Val = "20";
            c2.Val = "black";
            b2.Val = false;

            runProperties2.Append(fs2);
            runProperties2.Append(c2);
            runProperties2.Append(b2);

            run2.Append(runProperties2);
            run2.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text2));

            para = new Paragraph(run2);
            body.AppendChild(para);

            /*** Format text3 and text4 as one paragraph ***/
            run3 = new DocumentFormat.OpenXml.Wordprocessing.Run();

            fs3.Val = "15";
            c3.Val = "black";
            b3.Val = false;
            runProperties3.Append(fs3);
            runProperties3.Append(c3);
            runProperties3.Append(b3);

            run3.Append(runProperties3);
            run3.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text3));

            fs4.Val = "15";
            c4.Val = "red";
            b4.Val = true;
            runProperties4.Append(fs4);
            runProperties4.Append(c4);
            runProperties4.Append(b4);

            run3.Append(runProperties4);
            run3.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text4));

            para = new Paragraph(run3);
            body.AppendChild(para);


            /*** Format the user-typed text ***/
            fs5.Val = "30";
            c5.Val = "blue";
            b5.Val = false;
            runProperties5.Append(fs5);
            runProperties5.Append(c5);
            runProperties5.Append(b5);

            run4 = new DocumentFormat.OpenXml.Wordprocessing.Run();

            run4.Append(runProperties5);
            run4.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text5));

            para = new Paragraph(run4);
            body.AppendChild(para);


            /*** Append the entire body ****/
            mainPart.Document.Append(body);

            /* Save the results and close */
            mainPart.Document.Save();
            doc.Close();
}//end GenerateWord