0

How do I add/append HTML content in an existing .docx file, using OpenXML in asp.net C#?

In an existing word file, I want to append the html content part. For example:

In this example, I want to place "This is a Heading" inside a H1 tag.

Here its my code

protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            using (WordprocessingDocument doc = WordprocessingDocument.Open(@"C:\Users\admin\Downloads\WordGenerator\WordGenerator\FTANJS.docx", true))
            {
                string altChunkId = "myId";
                MainDocumentPart mainDocPart = doc.MainDocumentPart;

                var run = new Run(new Text("test"));
                var p = new Paragraph(new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), run);

                var body = mainDocPart.Document.Body;
                body.Append(p);


                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<html><head></head><body><h1>HELLO</h1></body></html>"));

                // Uncomment the following line to create an invalid word document.
                // MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<h1>HELLO</h1>"));

                // Create alternative format import part.
                AlternativeFormatImportPart formatImportPart =
                   mainDocPart.AddAlternativeFormatImportPart(
                      AlternativeFormatImportPartType.Html, altChunkId);
                //ms.Seek(0, SeekOrigin.Begin);

                // Feed HTML data into format import part (chunk).
                formatImportPart.FeedData(ms);
                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;

                mainDocPart.Document.Body.Append(altChunk);
            }
        }
        catch (Exception ex)
        {

            ex.ToString ();
        }


    }
4
  • 2
    Only information are not enough. please share your code with us. Commented Sep 20, 2018 at 10:47
  • Spelling, formatting. Commented Sep 20, 2018 at 13:27
  • 1
    Please add the code you have attempted so far and tell us where you got stuck. Commented Sep 20, 2018 at 13:27
  • code are added now the code is running but no output of this code html tag part are not added in my existing document FTANJS.docx file how to add the html part in word documnet Commented Sep 20, 2018 at 13:33

2 Answers 2

2

Add HTML content as Chunk should work, and you are almost there.

If I understand the question properly, this code should work.

        //insert html content to H1 tag
        using(WordprocessingDocument fDocx = WordprocessingDocument.Open(sDocxFile,true))
        {
            string sChunkID = "myhtmlID";
            AlternativeFormatImportPart oChunk = fDocx.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, sChunkID);
            using(FileStream fs = File.Open(sHtml,FileMode.OpenOrCreate))
            {
                oChunk.FeedData(fs);
            }
            AltChunk oAltChunk = new AltChunk();
            oAltChunk.Id =sChunkID ;

            //insert html to the tag of 'H1' and remove H1.
            Body body = fDocx.MainDocumentPart.Document.Body;
            Paragraph theParagraph = body.Descendants<Paragraph>().Where(p => p.InnerText == "H1").FirstOrDefault();
            theParagraph.InsertAfterSelf<AltChunk>(oAltChunk);
            theParagraph.Remove();

            fDocx.MainDocumentPart.Document.Save();
        }
Sign up to request clarification or add additional context in comments.

Comments

-2

The short answer is "You can't add HTML to a docx file".

Docx is an open format defined here. If you're using the Microsoft version they have a number of extensions.

In any case, the file contains XML, not HTML and you can't simply add HTML to a docx file. There are styles and formatting objects and pointers that all need to be updated.

If you need to modify a docx file and don't want to do a lot of research and a lot of coding, you'll need to find an existing library to work with.

3 Comments

so what i am using add/append html content in existing docx file any link ??
@ASIFSHAIKH I don't have any specific recommendations. You would need to google it and find one you like.
You literally can though see Victor Xie's answer

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.