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);
}

3 comments:

Mauro Zamaro said...

I did exacly this and i'm unable to open the generated document at Word 2010.
public void Write(WordprocessingDocument mCurrentDocument;, string strText)
{
if (mCurrentDocument != null)
{
Run r = new Run();
Text t = new Text();
t.Text = strText;
t.Space = SpaceProcessingModeValues.Preserve;

r.AppendChild(t);
Paragraph p = new Paragraph();
p.AppendChild(r);

mCurrentDocument.MainDocumentPart.Document.Body.AppendChild(p);
mCurrentDocument.MainDocumentPart.Document.Save();
}
}

I'm using the OpenXLM SDK 2.0. What am i doing wrong?

Hema said...

hi how to add superscript and subscript?

Dora said...

Hi Hema

Thanks for reading my blog.

With reference to your question, I have written a short post (link below) to share with you a snippet of code that I used to create a sentence that has superscript formatting.

Hope that helps! :)

http://deaspdotnet.blogspot.sg/2012/08/applying-superscript-and-subscript-to.html