Showing posts with label Generate Word document. Show all posts
Showing posts with label Generate Word document. Show all posts

Thursday, August 2, 2012

Applying superscript and subscript to WordXML


I wrote an entry sometime back in Jan 2011 to show how you can easily generate a Word document using the OpenXML SDK with font size, font color and bold formatting.
Yesterday, I received a comment from Hema, who asked me how to add subscript and superscript formatting.
To tell the truth, I had not dabbled in those before.  But I decided since I have already come so far, I can just spend a little time to see if I can apply subscript and superscript effects to my previous code too.
I am glad I tried, because I realised its not too difficult.  The only problem is that there’s little documentation out there, so I managed to arrive at the solution purely through my own ‘experiment’ and I am not sure if my method is the most efficient!
I am assuming that you are familiar with the OpenXML SDK and procedures such as how to create Run and RunProperties based on my previous post.  What you need to add superscript or subscript is to use the “VerticalTextAlignment” class to specify if you wish to have a “Superscript” or “Subscript” or the default “Baseline” alignment.
Scroll below for the full code to generate a Word document with one single sentence with output like this:
2012-08-02_112934
 public void GenerateSentence(string filename)
        {
            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;
            Paragraph para;

            string text1 = "The answer for ";
            string text2 = "10";            
            string text3 = "2";
            string text4 = " is 100";


            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties1 = 
               new DocumentFormat.OpenXml.Wordprocessing.RunProperties
               (new RunFonts() { Ascii = "Script" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties2 = 
               new DocumentFormat.OpenXml.Wordprocessing.RunProperties
               (new RunFonts() { Ascii = "Courier New" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties3 = 
               new DocumentFormat.OpenXml.Wordprocessing.RunProperties
               (new RunFonts() { Ascii = "Arial" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties4 = 
               new DocumentFormat.OpenXml.Wordprocessing.RunProperties
               (new RunFonts() { Ascii = "Times New Roman" });            

            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.VerticalTextAlignment vta1 = 
               new DocumentFormat.OpenXml.Wordprocessing.VerticalTextAlignment();

            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.VerticalTextAlignment vta2 = 
               new DocumentFormat.OpenXml.Wordprocessing.VerticalTextAlignment();

            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.VerticalTextAlignment vta3 = 
               new DocumentFormat.OpenXml.Wordprocessing.VerticalTextAlignment();


            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.VerticalTextAlignment vta4 = 
               new DocumentFormat.OpenXml.Wordprocessing.VerticalTextAlignment();
            
        


            /*** Format text1, text2, text3 and text4  as one paragraph ***/
            run1 = new DocumentFormat.OpenXml.Wordprocessing.Run();

            fs1.Val = "40";
            c1.Val = "black";
            b1.Val = false;
            vta1.Val = VerticalPositionValues.Baseline;
            runProperties1.Append(fs1);
            runProperties1.Append(c1);
            runProperties1.Append(b1);
            runProperties1.Append(vta1);
            run1.Append(runProperties1);
            run1.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text1));

            fs2.Val = "40";
            c2.Val = "black";
            b2.Val = false;
            vta2.Val = VerticalPositionValues.Baseline;
            runProperties2.Append(fs2);
            runProperties2.Append(c2);
            runProperties2.Append(b2);
            runProperties2.Append(vta2);
            run1.Append(runProperties2);
            run1.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text2));

            fs3.Val = "40";
            c3.Val = "black";
            b3.Val = false;
            vta3.Val = VerticalPositionValues.Superscript;
            runProperties3.Append(fs3);
            runProperties3.Append(c3);
            runProperties3.Append(b3);
            runProperties3.Append(vta3);
            run1.Append(runProperties3);
            run1.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text3));


            fs4.Val = "40";
            c4.Val = "green";
            b4.Val = false;
            vta4.Val = VerticalPositionValues.Baseline;
            runProperties4.Append(fs4);
            runProperties4.Append(c4);
            runProperties4.Append(b4);
            runProperties4.Append(vta4);
            run1.Append(runProperties4);
            run1.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text4));

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

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

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

NOTE: You might notice that when you open up the Word document, the spaces go missing.  I had this problem too, and upon Googling, found out that this is due to a  bug in Microsoft’s OpenXML Office 2010/2007 implementation.  Oh well….

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
        

Monday, January 31, 2011

Create Word 2007 document using OpenXML SDK with font size, font color and bold formatting

 

Step 1
Install OpenXML SDK 2.0. You can download from here:

Step 2
Add these assemblies:

  • OpenXML SDK (browse to the folder where you installed OpenXML SDK)
  • Windows Base

<assemblies><add assembly="DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></assemblies>

Step 3
Import these :
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using System.Xml;
using System.IO;
using System.Text;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Collections;

Step 4
Create a class that looks like this:

public class MyParagraph
{
private string paragraphType;
private string text;

public MyParagraph()
{
}

public MyParagraph(string pt, string t)
{
this.paragraphType = pt;
this.text = t;
}

public string ParagraphType
{
get { return paragraphType; }
set { paragraphType = value; }
}

public string Text
{
get { return text; }
set { text = value; }
}
}
Step 5
Create this method
private void GenerateDocument(string filename, ArrayList myparagraphs)
{

WordprocessingDocument doc =
WordprocessingDocument.Create(filename,
WordprocessingDocumentType.Document);
MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = new Body();

/* Create the contents */
for (int i = 0; i < myparagraphs.Count; i++)
{
DocumentFormat.OpenXml.Wordprocessing.Run run = new DocumentFormat.OpenXml.Wordprocessing.Run();
MyParagraph p = (MyParagraph)myparagraphs[i];
string txt = p.Text;
if (p.ParagraphType.Equals("Header1"))
{
DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties = new DocumentFormat.OpenXml.Wordprocessing.RunProperties();
runProperties.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Bold());
DocumentFormat.OpenXml.Wordprocessing.FontSize fs = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
fs.Val = "50";
DocumentFormat.OpenXml.Wordprocessing.Color c = new DocumentFormat.OpenXml.Wordprocessing.Color();
c.Val = "red";
runProperties.AppendChild(fs);
runProperties.AppendChild(c);
run.AppendChild(runProperties);
run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text(txt));
}
else
{
run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text(txt));
}
Paragraph para = new Paragraph(run);
body.AppendChild(para);
}

mainPart.Document.Append(body);

/* Save the results and close */
mainPart.Document.Save();
doc.Close();
}
 
Step 6
Call the function defined in Step 5 from an ASP.NET document

private void Generate(){
private void Generate()
{
MyParagraph p1 = new MyParagraph("Header1", "Line 1");
MyParagraph p2 = new MyParagraph("Header2", "Line 2");
MyParagraph p3 = new MyParagraph("Header3", "Line 3");
MyParagraph p4 = new MyParagraph("Header4", "Line 4");
ArrayList list = new ArrayList();
list.Add(p1); list.Add(p2); list.Add(p3); list.Add(p4);
GenerateDocument(Server.MapPath("Output\\Document.docx"),list);
}