This project has moved. For the latest updates, please go here.

heading tag and font name changes are not applied to open xml document

Mar 28, 2014 at 6:39 AM
Edited Mar 28, 2014 at 6:41 AM
Hi,
Thanks for the wonderful HtmlConverter code. I'm using the version 1.5 in my application to convert html content to open xml and download a document out of it.

Below is the code that I have,
using (var stream = new MemoryStream())
            {
                // Create a Wordprocessing document.
                using (WordprocessingDocument myDoc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true))
                {
                    // Add a new main document part. 
                    // Create DOM tree for simple document. 
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;
                    if (mainPart == null)
                    {
                        mainPart = myDoc.AddMainDocumentPart();
                        new Document(new Body()).Save(mainPart);
                    }

                    HtmlConverter converter = new HtmlConverter(mainPart);
                    Body body = mainPart.Document.Body;

                    // Append elements appropriately after converting the html to openxml format. 
                    var paragraphs = converter.Parse(htmlContent);
                    for (int i = 0; i < paragraphs.Count; i++)
                    {
                        body.Append(paragraphs[i]);
                    }

                    // Save changes to the main document part. 
                    mainPart.Document.Save();

                    myDoc.Close();
                }

                stream.Position = 0;
                stream.CopyTo(Response.OutputStream);
                Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + docName + ".docx");
                Response.Flush();
                Response.End();
            }
Here, htmlContent that I pass into the parse method looks like below
<h1 style=\"text-align:left;\">This is heading 1</h1>
<h2 style=\"text-align:left;\">This is heading 2</h2>
<h3>This is heading 3</h3>
<p><span style=\"font-family:Georgia,serif;\">This text is in Georgia&nbsp;font</span></p>
<p><span style=\"font-family:Arial,Helvetica,sans-serif;\">This text is in Arial font</span></p>
<p><span style=\"font-family:'Times New Roman',Times,serif;\">This text is in Times New Roman font</span></p>
But none of the above heading levels or the font name changed texts are getting displayed in the downloaded Open XML document.

Could you please let us know if this is supported or not? Or am I missing anything?
Coordinator
Apr 1, 2014 at 9:50 AM
Hi,


I've just committed a patch submitted by davide about the font-family attribute that was missing.
For the headings, this is a recurrent question :)
Don't create a new DOCX from scratch but load an existing one. The reason is simple: I don't provide all the theme/style for all the tags.
Finally, since begin of march, I finally decide to embed the default style inside the lib.

So just download the latest source code, recompile and you are done.

Cheers :)
Marked as answer by Baala on 4/1/2014 at 11:48 AM
Apr 1, 2014 at 7:31 PM
Thank you very much for the updated source code. Now I'm able to see the heading styles and different font-name texts properly in my generated document.

Also, it would be great if you wouldn't mind to provide some pointers on the below two minor issues,
  1. Why is that all the heading level texts are getting displayed in Bold font by default. Could this be changed to normal headings?
  2. When text-align property is set to the heading tag through inline-style, I'm not able to see them in the document. I also tried to add the below highlighted code to the ProcessHeading method, but still it is not getting properly aligned.
    Image
Coordinator
Apr 8, 2014 at 11:30 AM
Hi Baala,


For the headings in Bold, this is like that in the default theme of Word 2010. If you want to customize your headings, I can only propose you to open Word, set the theme as you need and then save your document.
Embed that document inside a resx and use it inside your code:
using (MemoryStream generatedDocument = new MemoryStream())
{
     byte[] data = Demo.Properties.Resources.template;
     generatedDocument.Write(data, 0, data.Length);
     generatedDocument.Position = 0L;
     using (WordprocessingDocument package = WordprocessingDocument.Open(generatedDocument, true))
     {
          ....
     }
}
For the inline style, at the time being, no style are supported. I will commit this feature soon but I need to finish other step.
But you can already now support text-align by adding this code in ProcessHeading:
List<OpenXmlElement> styleAttributes = new List<OpenXmlElement>();
htmlStyles.Paragraph.ProcessCommonAttributes(en, styleAttributes);

AlternateProcessHtmlChunks(en, "</h" + level + ">");
Paragraph p = new Paragraph(elements);
p.InsertInProperties(prop =>
                prop.ParagraphStyleId = new ParagraphStyleId() { Val = htmlStyles.GetStyle(clsName, StyleValues.Paragraph) });

htmlStyles.Paragraph.ApplyTags(p);
htmlStyles.Paragraph.EndTag("<h" + level + ">");
Coordinator
Apr 18, 2014 at 11:57 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Dec 10, 2015 at 1:22 PM
Edited Dec 10, 2015 at 1:29 PM
onizet wrote:
Hi,


I've just committed a patch submitted by davide about the font-family attribute that was missing.
For the headings, this is a recurrent question :)
Don't create a new DOCX from scratch but load an existing one. The reason is simple: I don't provide all the theme/style for all the tags.
Finally, since begin of march, I finally decide to embed the default style inside the lib.

So just download the latest source code, recompile and you are done.

Cheers :)
Hi ,
I am facing same problem(heading tag and font name changes are not applied to open XML document).
I checked the patch(Id 15945),Downloaded latest source code,added Converter Utility class explicitly,added GetAsFont method in Converter Utility class as you have asked to do in Patch.
But in Converter Utility class For HtmlFont there is no namespace available (e.g public HtmlFont GetAsFont(String name)).
I want my heading tag and font name changes as it is in word document.

Could you please let me know if this is supported or not? Or am I missing anything?
Coordinator
Dec 11, 2015 at 4:15 PM
Yes there is really a bug :-(

I have to test more and check against TFS to find why I have not handled this case until now.
But for now and if it's urgent, add this line :
htmlStyles.Paragraph.ApplyTags(currentParagraph);
to ProcessSpan() just before if (newParagraph)
Coordinator
Dec 14, 2015 at 11:21 AM
Ok now I have committed the changes.
Thanks for reporting :)