Creating Word Documents by OpenXML

For fast processing of documents on server side along with many other available option we could use free and open source Microsoft's OpenXML SDK. for any type of office documents Excel, Prower Point and Word etc.


Creating Dynamic Tables.

Using Open XML we could create Tables with data from any source like xml, web service, RSS, XLSX,  JSON Objects or Databases.

here we will create a table from simple array of values and then we will set styling.

Creating table is as simple as declaring integers.

Table table=new Table();

 We could set properties for table here.

TableProperties props = new TableProperties(
new TableBorders(
new TopBorder
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12


Attaching to Table 


By using following code we can create table row.

var tr=new TableRow();

and attachement is similar.


we can also create table cell inside table row it is also quite simple.

var tc = new TableCell();
tc.Append(new Paragraph(new Run(new Text(data[i, j]))));


Now We will discus managing comments in OpenXML.

Retrive All comments in word document as just a single statement task.

var commentsPart = docPart.WordprocessingCommentsPart;

But we can filter on the basis of author.

var commentsPart = docPart.WordprocessingCommentsPart.Comments.Elements<DocumentFormat.OpenXml.Wordprocessing.Comment>().ToList().Where(x=>x.Author=="pakistan");

we can delete all comments or comments belonging to a specific Author.


Get Content of comments belonging to a specific author.

public static XDocument GetAllComments(string fileName,string authorName)
XDocument comments = null;

using (var document = WordprocessingDocument.Open(fileName, false))
// Retrieve the document part:
var docPart = document.MainDocumentPart;
if (docPart != null)
// Retrieve the comments part:
var commentsPart = docPart.WordprocessingCommentsPart;//.Comments.Elements<DocumentFormat.OpenXml.Wordprocessing.Comment>().ToList().Where(x=>x.Author==authorName);
if (commentsPart != null)

// Load an XDocument with the comments content:
using (Stream stm = commentsPart.GetStream(FileMode.Open, FileAccess.Read))
comments = XDocument.Load(XmlReader.Create(stm));
return comments;

Now Discussing Pictures

Searching picture Box by tag.

Drawing GetPictureByTag(WordprocessingDocument doc,string pictureTag)


SdtBlock cc = doc.MainDocumentPart.Document.Body.Descendants<SdtBlock>()
.FirstOrDefault(c =>
SdtProperties p = c.Elements<SdtProperties>().FirstOrDefault();
if(p !=null)
// Is it a picture content control?
SdtContentPicture pict =
// Get the alias.
SdtAlias a = p.Elements<SdtAlias>().FirstOrDefault();
if(pict !=null&& a.Val==pictureTag)
if(cc !=null)
Drawing dr = cc.Descendants<Drawing>().FirstOrDefault();
return dr;
return null;


Insert Image to Document

Void InsertImageByRelationshipId(WordProcessingDocument doc, string embedId,FileStream fileStream)


IdPartPair idpp = doc.MainDocumentPart.Parts
.Where(pa => pa.RelationshipId== embed).FirstOrDefault();
if(idpp !=null)
ImagePart ip =(ImagePart)idpp.OpenXmlPart;



comments powered by Disqus