Lets Learn

Opinion Matters

  • Subscribe

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 7 other followers

  • Blog Stats

    • 72,532 hits
  • Ankush

    Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Content Controls- A Complete Summary

Posted by Ankush on October 22, 2010


While generating a document from a database, it is very important that how you have designed the document. If you have a very good template then the insertion of the data becomes very easy. Word provides a very nice feature “Content Controls” to make this task easy. This blog post provides a collection of links which you can use .

So what is a Content Control. Here I what MSDN doc says:

Content controls are bounded and potentially labeled regions in a document that serve as containers for specific types of content. Individual content controls can contain content such as dates, lists, or paragraphs of formatted text. In some cases, content controls might remind you of forms. However, they are much more powerful, flexible, and useful because they enable you to create rich, structured blocks of content. Content controls also build on the custom XML support introduced in Microsoft Office Word 2003. Content controls enable you to author templates that insert well-defined blocks into your documents. Content controls enable you to:

• Specify structured regions in a template. Each structured region has its own unique ID so that you can read from and write to it. Examples of types of structured regions (or content controls) are combo boxes, pictures, text blocks, and calendars.

• Determine the behavior of content controls. Each content control takes up a portion of a document and, as the template author, you can specify what each region does. For example, if you want a region of your template to be a calendar, you insert a calendar content control in that area of the document, which automatically determines what that block of content does. Similarly, if you want a section of a template to display an image, create a picture content control in that area. In this way, you can build a template with predefined block types.

• Restrict the content of content controls. Each content control can be restricted, so that it cannot be deleted or edited. This is useful if, for example, you have copyright information in a template that the user should be able to read but not edit. You can also lock a template’s content so that a user does not accidentally delete portions of it. This makes templates more robust than in previous versions.

• Map the contents of a content control to data in a custom XML part that is stored with the document. For example, if you insert a document parts content control that contains a table of stock prices, you can map the table cells to nodes in an XML file that contain the current stock prices. When the prices change, an add-in can programmatically update the attached XML file, which is bound to each cell, and the new, updated prices automatically appear in the table.

The easiest way to create a content control is through the user interface (although you can also create them programmatically). To create a content control through the user interface (UI), select the text that you want to turn into a content control and then choose the content control type you want from the content controls section of the Developer ribbon. This creates a content control around the selected text.

Content Controls are a powerful features when it comes to feeding the data into document in an ASP.NET app. If you have created a document template and content control’s mapping is set then you can just replace one XML file and Word will pick the new data.

So here is the list.

Goal: Get the data from SQL Server and feed the Word document. The application is a Web application.

Solution: Create a Word template with content controls.  Bound them with an XML file. Use Open XML to manipulate the document and change the content of the XML file. The advantage of using content control is, they can be easily bound  to an XML file.

• Create a new Word document, drag few content controls onto the document surface. Create an XML file and create a node for every content control

Data-driven document generation with Word 2007 and the Office XML File Formats: Part 1
http://blogs.msdn.com/erikaehrli/archive/2006/08/11/word2007DataDocumentGenerationPart1.aspx

• There are 2 ways by which you do the mapping [ Nodes <-> Content Control]. Using Code, which is already  explained in the above article. If you would like to use an application, there is sample application available which can help you.

Word 2007 Content Control Toolkit
http://www.codeplex.com/wikipage?ProjectName=dbeOnce this is done, the sample XML file which you created, will be a part of the  document package, so you just need to change the content of the XML nodes and when you will open the document, it will reflect the dynamic data.

• Now you need to use Open XML, get the data from SQL and replace the XML contents it in the document. Following article  explains this[It has the sample code as well]

Data-driven document generation with Word 2007 and the Office XML File Formats: Part 2
http://blogs.msdn.com/erikaehrli/archive/2006/08/11/word2007DataDocumentGenerationPart2.aspxCreting documents by using Open XML Format SDK 2.0 (Part 3 of 3)
http://msdn.microsoft.com/en-us/library/dd469465.aspx#CreateDocswithOpenXMLSDK_TakingAdvantageofBoundContentControlsIf you would like to do this through Open XML SDK 2.0, then following article explains about this.

Creating Data-Bound Content Controls using the Open XML SDK and LINQ to XML
http://blogs.msdn.com/ericwhite/archive/2008/10/19/creating-data-bound-content-controls-using-the-open-xml-sdk-and-linq-to-xml.aspxTaking Advantage of Bound Content Controls
http://blogs.msdn.com/brian_jones/archive/2009/01/05/taking-advantage-of-bound-content-controls.aspx
So basically once the template is created, you can use Open XML SDK and do something like. Here I simply get the xml, delete the old XML file and add the new one.

========
string templatePath = Server.MapPath(“./Templates/ModelTemplate.docx”);
string custXMLFilePath = Server.MapPath(“./CustomXML/Model.xml”);
using (wdDoc = OpenXML.WordprocessingDocument.Open(templatePath, true))
{
OpenXML.MainDocumentPart mainPart = wdDoc.MainDocumentPart;
mainPart.DeleteParts<OpenXML.CustomXmlPart>(mainPart.CustomXmlParts);
XmlDataDocument custXML = new XmlDataDocument();
custXML.Load(custXMLFilePath);
OpenXML.CustomXmlPart custXMLPart = mainPart.AddNewPart<OpenXML.CustomXmlPart>();
using (Stream s = custXMLPart.GetStream(FileMode.Create))
{
custXML.Save(s);
}
}
=========
Inserting Repeating Data Items into a Word 2007 Table by Using the Open XML API
http://msdn.microsoft.com/en-us/library/cc197932.aspxCreating Valid Open XML Documents by Using the Validation Tools in the Open XML Format SDK
http://msdn.microsoft.com/en-us/library/dd633612.aspx

Let me know what you think about this::

Ankush

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: