2

I am trying to add a html formatted text to a Open xml Text. How can I apply the formatting already present in the text?

I apply the text as following:

TextElement.Text = formattedString;

where FormattedString contains the following:

<p>test<br/>test2<ul><li>item1</li><li>item2<li2></p>

At the moment it simply inserts the text as it is with the tags into the word document. How can I tell Open XML SDK to add the string with the right format?

2
  • A wild guess: XML encode it (converting the five reserved XML characters (>, <, &, and single and double quotes) to the appropriate encoded characters). A better way would be to download the OpenXML Productivity Tool from the Microsoft site, create a document (using Word) containing your test line (above), save it, then open the document in the Productivity Tool and use the "Reflect" option to see what code gets produced. Commented May 9, 2019 at 14:32
  • Is there a way to specify the type of text as "html"? Please see the properties of "TextElement" to find out. Please see if the link below is helpful: ludovicperrichon.com/create-a-word-document-with-openxml-and-c Commented May 9, 2019 at 16:24

1 Answer 1

2

You need to create an AlternativeFormatImportPart which contains your HTML, then you need to add an AltChunk to your document and give it the id of the AlternativeFormatImportPart.

The following code creates a file from scratch with your HTML in it. Note that HTML fragments don't appear to be supported so I've added <html> tags to your HTML snippet.

using (var document = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document))
{
    document.AddMainDocumentPart();
    document.MainDocumentPart.Document = new Document();
    document.MainDocumentPart.Document.Body = new Body();

    //create a memory stream with the HTML required
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<html><p>test<br/>test2<ul><li>item1</li><li>item2<li2></p><html>"));

    //Create an alternative format import part on the MainDocumentPart
    AlternativeFormatImportPart altformatImportPart = document.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html);

    //Add the HTML data into the alternative format import part
    altformatImportPart.FeedData(ms);

    //create a new altChunk and link it to the id of the AlternativeFormatImportPart
    AltChunk altChunk = new AltChunk();
    altChunk.Id = document.MainDocumentPart.GetIdOfPart(altformatImportPart);

    // add the altChunk to the document
    document.MainDocumentPart.Document.Body.Append(altChunk);

    document.Save();
}

This produces the following output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.