Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
devrony opened this issue Jun 3, 2019 · 0 comments

Comments

@devrony
Copy link

devrony commented Jun 3, 2019

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();
			}
		}
@onizet onizet mentioned this issue Apr 18, 2024
@onizet onizet closed this as completed Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants