none
CustomXmlPart and SectionProperties RRS feed

  • Question

  • I have a 2010 Word Document I use as a template to create a new multi-page document using OpenXML 2.0 and C# code.  The template has custom Content Controls in the document header, and these are populated using the OpenXml API’s CustomXmlPart.  This has all worked fine … until the requirements changed. 

    Now the document needs to have several different headers.  I am wondering if the technique using the CustomXmlPart can somehow be applied to headers in a document section, say using the OpenXml SectionProperties class.  I am looking to do something like reading and storing a clone of the document’s header (and therefore custom Content Controls) into a prototype object, then deleting the original from the document, then adding a new SectionProperty whenever the new requirements dictate.  I want to be able to add to the new SecitonProperty object a clone of the header prototype.  Up to this point, I think everything is pretty do-able, but now for the unknown part.  I would like to still use CustomXmlPart techniques I used originally. 

    So, does anyone know if a document can have multiple CustomXmlPart objects, or more specifically, if a document can have SectionProperties and a CustomXmlPart can be added to that object?

    Friday, December 23, 2011 5:17 PM

Answers

  • Hi Ryno,

    You can build a document with distinct header content, including ContentControls in a Word template, or the document built from that template. The newest header must be in a new section. With this ability you can show the chapter title in a long document (e.g. book) in the header and using even-odd page headers you can have the book title on the even page and the chapter title on the odd page.  You can see a description of how to do this through the Word user interface in the following article:

    Insert headers and footers - Word - Office.com
    http://office.microsoft.com/en-us/word-help/insert-headers-and-footers-HP001226486.aspx

    Don’t miss the instruction in the article to ‘break the link to the previous section’ when you need to replace the content of the new section’s header with new content.

    To see the XML that results go ahead and build yourself a simple document with multiple sections, each with a custom header containing ContentControls. Following the recipe in the article cited above you can demonstrate what appears to answer your question, i.e. “So, does anyone know if a document can have multiple CustomXmlPart objects, or more specifically, if a document can have SectionProperties and a CustomXmlPart can be added to that object?”

    Unpack the document to see its XML parts to see the result.

    Please “mark as answer” if this reply helps answer your question.

    Regards,
    Chris Jensen
    Senior Technical Support Lead

    • Edited by cjatmsModerator Tuesday, December 27, 2011 4:54 PM
    • Marked as answer by Ryno Wednesday, January 11, 2012 5:20 PM
    Tuesday, December 27, 2011 4:53 PM
    Moderator

All replies

  • Hi Ryno,

    Thanks for your post.

    Do you mean that you are now changing to work with template which has odd and even headers, or even fist header, and you want to map you data to content controls which located at these headers respectively? Then I think you don't need to add multiple CustomXMLParts, binding data to content controls just like what you have done for template with one header.

    If I have misunderstood anything, would you like to correct me and please be more specific about your question.

    I look forward to hearing of you.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 26, 2011 9:23 AM
    Moderator
  • Calvin

    Thanks for your reply.  I have a need for an arbitrary number of different headers, and the page numbers for these headers are not predictable.  Also, even though I said it is a template Word document, it is actually just a regular Word document (.docx) that I construct.  In code, I make a copy of that document and work with the copy.  That is why I call the original document a template – sorry for the confusing terminology.  (Is there a more appropriate and less confusing name for such a document?)

     

    The Template Document:

    My document consists of one section and that section has a single header and a single table.  The header is comprised of four four lines; two of which are Content Controls.  The content controls, Title1 and AsOfDate have been bound to XML elements using the Word 2007 Content Control Toolkit.  The single table is comprised of three TableRows – the top two are Repeat Header Rows, the third is to be used in the program as a template TableRow to be cloned, populated, and added to the end of the table.

     

    Original Requirements and a Description of the Working Design / Implementation:

    Recall from my original post that the original requirements specified the construction of a header at the top of each page of the report, and the header, once set by program data, would not change.  I had a reportEngine class that performed three steps (i.e. had three methods).  The first was to initialize report elements, the second added header data, and the third added table data to the report.  This third step was to be performed an arbitrary number of times, say up to one hundred.  Each table would typically contain from ten to 200 data rows.

    Initialization consisted of reading the document’s Table and storing it in a local variable, and then deleting the table from the document – this table was then used as a template which would be cloned and added to the document.  The initialization also stored the last row of the table as a TableRow template.  This row was also deleted from the template table and its purpose is to serve as a template for cloning TableRow objects which could be populated and added to a table in the document.

    The second step consisted of passing the reportEngine an XML parent element containing two child elements.  This was used to populate the header using the CustomXmlPart object.

    The third step (or method) consisted of passing a generic list of custom objects to the reportEngine.  The method first added a new Table to the document (a clone of the table template) and then populated and added a TableRow to the table for each element in the generic list.  If the table was not the last one in the report, then a page break was added to the document.

     

    Updated Requirements and Questions on Possible Design / Implementation Alternatives:

    That all worked fine, but now I have an additional requirement specifying that the header text may change after several tables have been added.  The header will still be the same format, four lines and two of the lines will be custom.  However, the second custom line (the second Content Control) of the second header will not have the same text as the second custom line of the first header.  Also, there may be third (and so on) header.  Each of the headers will have a second custom line that will be unique in the report.

    I suspect that however I implement the addition of multiple headers to the document that I will have to be adding SectionProperties for each new header.  I am largely unfamiliar with adding SecitonProperties and Header elements to a document, so any help or advice would be appreciated.  Am I correct in stating that the solution will require multiple SectionProperty elements?

    So, I figure the approach would be (please comment on appropriateness / suggestions):

    Updated Step 1. perform step one as described above.

    Updated Step 2. Perform steps two and three as described above until a new header is required.

    Updated Step 3. Add a SectionProperty that will be associated with the tables already added.

    Updated Step 4. Perform Updated Steps 2 and 3 until the report is finished.

    Does it make sense to still use CustomXmlPart elements for my headers?  (I used them originally because it was so easy and conceptually clean to ‘fill in the missing text’ using them and Content Controls.)  Can a CustomXmlPart be added to a SectionProperty element?  If so, how might this be done?  Can you provide some pseudo code showing this?

    Would it make better sense to just recreate the header text when I create a new SectionProperty?


    • Edited by Ryno Tuesday, December 27, 2011 3:00 PM
    Tuesday, December 27, 2011 2:55 PM
  • Hi Ryno,

    You can build a document with distinct header content, including ContentControls in a Word template, or the document built from that template. The newest header must be in a new section. With this ability you can show the chapter title in a long document (e.g. book) in the header and using even-odd page headers you can have the book title on the even page and the chapter title on the odd page.  You can see a description of how to do this through the Word user interface in the following article:

    Insert headers and footers - Word - Office.com
    http://office.microsoft.com/en-us/word-help/insert-headers-and-footers-HP001226486.aspx

    Don’t miss the instruction in the article to ‘break the link to the previous section’ when you need to replace the content of the new section’s header with new content.

    To see the XML that results go ahead and build yourself a simple document with multiple sections, each with a custom header containing ContentControls. Following the recipe in the article cited above you can demonstrate what appears to answer your question, i.e. “So, does anyone know if a document can have multiple CustomXmlPart objects, or more specifically, if a document can have SectionProperties and a CustomXmlPart can be added to that object?”

    Unpack the document to see its XML parts to see the result.

    Please “mark as answer” if this reply helps answer your question.

    Regards,
    Chris Jensen
    Senior Technical Support Lead

    • Edited by cjatmsModerator Tuesday, December 27, 2011 4:54 PM
    • Marked as answer by Ryno Wednesday, January 11, 2012 5:20 PM
    Tuesday, December 27, 2011 4:53 PM
    Moderator
  • Hi Ryno,

    Your most recent post asks for comments on appropriateness/suggestions listing 4 design/architecture steps and summarizes with a question beginning with the phrase “Does it make sense…?” and further asks for pseudo code.  All told, because of the complexity of your needs your questions fall into the paid support category which requires a more in-depth level of support.  Please visit the below link to see the various paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    Regards,
    Chris Jensen
    Senior Technical Support Lead

    Tuesday, December 27, 2011 5:09 PM
    Moderator