Finally I've reached a solution that fits my purposes. I'll share it with you:
The html code to insert is not very complex: it comes from a SharePoint's full-html-enabled rich text field, but the user is only using Sharepoint's OOTB editor for the field, so no css, etc.
As consequence, I've decided, instead of inserting the html content as an AlternativeFormatImportPart (altChunk), to parse it before and insert it as pure OpenXml.
To perform the conversion, I'm using the html2openxml library as a base. I've extended it overloading the .Parse(...) method as follows:
- If an "a href=..." tag is found , we analyze href value to decide if it's a link to our internal server or not.
- If href points to our server, we replace the "a href" tag with an special serialized class that contains file's url and icon's image url.
- We let the original .Parse method perform the conversion.
- We analyze the IList returned by original's .Parse function to find OpenXml's Text Elements whose content is our special serialized class containing links.
- We replace each OpenXml's Run Element containing these Text elements by a Run element that references the embedded object with the binary content of each file and a shape containing the icon's image.
I'm sharing here also a code stub so you can know how I've extended the functionality, if someone is interested on the full solution, please let me know.
/// <summary>
/// Replaces anchor hrefs to documents on server with embedded OLE objects
/// Start the parse processing
/// </summary>
/// <param name="html"></param>
/// <param name="embeddServerLinksAsObjects"></param>
/// <returns></returns>
public IList<OpenXmlCompositeElement> Parse(string html, bool embeddServerLinksAsObjects)
{
try
{
if (embeddServerLinksAsObjects)
{
html = ReplaceAnchorLinksByOXMLLinks(html, this.serverRoot);
}
IList<OpenXmlCompositeElement> oceList = base.Parse(html);
if (embeddServerLinksAsObjects)
{
oceList = ReplaceOXMLLinksByOLEObjects(oceList, this.mainDocumentPart, this.serverRoot);
}
return oceList;
}
catch (Exception ex)
{
}
return null;
}