htmlConverter : converting a rich text content control.

Dec 16, 2010 at 4:03 PM

Hi Eric,

The htmlConverter works very well, but I have to transform the content of a rich content, and I just can't figure out how to just transform the control content.

My idea : get the content using

                var sdt = doc.MainDocumentPart
                            .Where(e => ((string)e.Elements(w + "sdtPr")
                                  .Elements(w + "alias")
                                  .Attributes(w + "val")
                                  .FirstOrDefault()).ToLower() == "rapport")

then lauchn the transform

            XElement xhtml = (XElement)ConvertToHtmlTransform(wordDoc, htmlConverterSettings, sdt, null);

but when I run the code I get this exception

System.InvalidCastException was unhandled
  Message="Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.Object]' to type 'System.Xml.Linq.XElement'."
       at OpenXmlPowerTools.HtmlConverter.ContentConvertToHtml(WordprocessingDocument wordDoc, XElement contentElement) in C:\work\dev\OpenXmlPowerTools\OpenXmlPowerTools\HtmlConverter.cs:line 844
       at Program.Main(String[] args) in C:\work\dev\OpenXmlPowerTools\HtmlConverterExampleSimple\HtmlConverterExampleSimple.cs:line 83
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

just after being through the code I added to ConvertToHtmlTransform methode

                // Transform contents of content control.
                if (element.Name == W.sdtContent)
                    return element.Elements().Select(e => ConvertToHtmlTransform(wordDoc,
                        settings, e, imageHandler));

This is suppose to skp the <sdtContent> tag and recurse on the descendant in the element.

do you have any explanation.


Jean Marie

Jan 27, 2011 at 11:08 AM

answering to myself..

The code is good, but I was remving a tag in the document and caused the error. I also modified the transformation par to handle the sdtContent tag and tranform bold and italic.


Oct 24, 2011 at 9:32 AM

You could submit a patch or just paste the changes here, maybe the project maintainers could implement those?

Oct 24, 2011 at 9:40 AM

The issue around handing the content control is that there are many possible ways to handle it, given the wide range of uses of content controls.  I considered a variety of ways to use them, but decided that for the time being, the code should not handle content controls.

The issues around bold and italic are a bit complicated.  It is easy enough to handle bold and italic for formatting that is applied at the run level.  However, for formatting that is applied via a character style, then you have the issue of inherited styles.  This is further complicated because bold and italic are 'toggle' properties.  And even further complicated in tables, given the richness of table styles.  Table styles also use inheritance.  Toggling also needs to be implemented for table styles.  So, because I felt it was worse to have a very partial implementation, I decided to not transform bold and italic.

Someday, if and when schedules and budgets permit, I would like to enhance the HtmlConverter, but it is not going to be in the near future.