onizet / html2openxml

Html2OpenXml is a small .Net library that convert simple or advanced HTML to plain OpenXml components. This program has started in 2009, initially to convert user's comments into templated Word.
MIT License
321 stars 109 forks source link

Number List with nested Bullet List rending nested elements with Numbers instead of Bullets #56

Closed devrony closed 3 months ago

devrony commented 5 years ago

Hi, I have a web application that uses TinyMCE Editor where users can add Rich Text content which is saved as HTML. I'm generating a Word.docx from all content entered by the users from the web form. Only some of the content is HTML/Rich Text. I'm using the Html2OpenXml library to inject the HTML into parts of the Word.docx file. The issue I'm running into is that a basic ordered Number List with nested Bulleted List which only outputs with incremented numbers, not with nested bullets. I've tested with this basic HTML.

The output comes out like this. FYI, the sample I'm showing comes out nested with correct with spacing, just doesn't show bullets.

1.) Number Parent Item 1 1.) Bullet Sub Item 1-1 2.) Bullet Sub Item 1-2 2.) Number Parent Item 2 1.) Bullet Sub Item 2-1 2.) Bullet Sub Item 2-2

<div>
    <ol>
        <li>Number Parent Item 1
            <ul>
                <li>Bullet Sub Item 1-1</li>
                <li>Bullet Sub Item 2-1</li>
            </ul>
        </li>
        <li>Number Parent Item 2
            <ul>
                <li>Bullet Sub Item 2-1</li>
                <li>Bullet Sub Item 2-2</li>
            </ul>
        </li>
    </ol>
</div>

Here is the code I'm using.

                private List<Paragraph> ConvertHtmlToOpenXML(string htmlText)
        {
            // Must return at least 1 paragraph
            if (string.IsNullOrEmpty(htmlText))
            {
                List<Paragraph> paragraphs = new List<Paragraph>();
                paragraphs.Add(new Paragraph());
                return paragraphs;
            }

            // Temporarily create new document for HTML conversion and then retrieve the generated paragraphs and 
            // append to original document.
            using (var tmpGeneratedDocument = new System.IO.MemoryStream())
            {
                var tmpPackage = WordprocessingDocument.Create(tmpGeneratedDocument, WordprocessingDocumentType.Document);

                var tmpMainDocumentPart1 = tmpPackage.MainDocumentPart;
                if (tmpMainDocumentPart1 == null)
                {
                    tmpMainDocumentPart1 = tmpPackage.AddMainDocumentPart();
                    new Document(new Body()).Save(tmpMainDocumentPart1);
                }

                var htmlConverter = new HtmlConverter(tmpMainDocumentPart1);

                // ParseHtml will automatically append to temp document
                htmlConverter.ParseHtml(htmlText);
                tmpMainDocumentPart1.Document.Save();

                tmpPackage.Close();
                tmpGeneratedDocument.Close();

                // Return parsed HTML paragraphs
                return tmpMainDocumentPart1.Document.Body.Descendants<Paragraph>().ToList();
            }
        }