1

I want to make table using OpenXML WordProcessing. I want to format the font inside the cell. This is my code

MainDocumentPart mainDocumentPart = doc.AddMainDocumentPart();
mainDocumentPart.Document = new Document();
Body body = mainDocumentPart.Document.AppendChild(new Body());

RunProperties runHeader = new RunProperties();
RunFonts runFont = new RunFonts();
runFont.Ascii = "Lucida Sans";
runHeader.Append(runFont);                    
runHeader.Append(new Bold());
runHeader.Append(new FontSize() { Val = "16" }); 

//// Create a new table
Table tbl = new Table();

tr = new TableRow();
tc = new TableCell();

Paragraph paraHeader = new Paragraph();
Text heading_text = new Text("Company Name");
runHeader.Append(heading_text);
paraHeader.Append(runHeader);
tc.Append(paraHeader);
tr.Append(tc);
tbl.Append(tr);

body.AppendChild(tbl);

But when I open up on Microsoft Word, I got error. Its said that the file has problem with the contents

l1th1um
  • 715
  • 2
  • 8
  • 19
  • Have you checked this link: http://stackoverflow.com/questions/7956630/how-can-i-change-the-font-open-xml – Bryan Feb 28 '13 at 17:24

2 Answers2

2

You are appending your text to your Run Properties, it needs to be appended to a Run. try:

Text heading_text = new Text("Company Name");

////create the run
Run runHeaderRun = new Run();

////append the run properties and text to the run
runHeaderRun.Append(runHeader);
runHeaderRun.Append(heading_text);

////append the run to the paragraph
paraHeader.Append(runHeaderRun);

tc.Append(paraHeader);
Muad'dib
  • 423
  • 5
  • 14
0
RunProperties rp = new RunProperties();
RunFonts runFont = new RunFonts() { Ascii = "Calibri Light" };
rp.Append(runFont);
rp.Append(new Color() { Val = "#2E74B5" });

body.Append(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new Run(rp, new Text("3. Risk Assessment"))));

It's a really simple to use, try this.