1

I have a rich text box which contains html formatted text as well as we can insert a copied images. I tried with AlternativeFormatImportPart and AltChunk method. It's generating the document but getting the below error. Please let me know what am I missing here.

enter image description here enter image description here

  
MemoryStream ms;// = new MemoryStream(new UTF8Encoding(true).GetPreamble().Concat(Encoding.UTF8.GetBytes(h)).ToArray());
                ms = new MemoryStream(HtmlToWord(fileContent));
                //MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(h));
                // Create alternative format import part.
                AlternativeFormatImportPart chunk =
                   mainDocPart.AddAlternativeFormatImportPart(
                      "application/xhtml+xml", altChunkId);
                chunk.FeedData(ms);
                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;

public static byte[] HtmlToWord(String html)
        {
            const string filename = "test.docx";
            if (File.Exists(filename)) File.Delete(filename);
            var doc = new Document();

            using (MemoryStream generatedDocument = new MemoryStream())
            {
                using (WordprocessingDocument package = WordprocessingDocument.Create(
                generatedDocument, WordprocessingDocumentType.Document))
                {
                    MainDocumentPart mainPart = package.MainDocumentPart;

                    if (mainPart == null)
                    {
                        mainPart = package.AddMainDocumentPart();
                        new Document(new Body()).Save(mainPart);
                    }


                    HtmlConverter converter = new HtmlConverter(mainPart);
                    converter.ExcludeLinkAnchor = true;
                    converter.RefreshStyles();
                    converter.ImageProcessing = ImageProcessing.AutomaticDownload;
                    //converter.BaseImageUrl = new Uri(domainNameURL + "Images/");
                    converter.ConsiderDivAsParagraph = false;

                    Body body = mainPart.Document.Body;
                    var paragraphs = converter.Parse(html);
                    for (int i = 0; i < paragraphs.Count; i++)
                    {
                        body.Append(paragraphs[i]);
                    }
                    mainPart.Document.Save();
                }
                return generatedDocument.ToArray();
            }
        }
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Happy Coder
  • 31
  • 1
  • 3
  • 1
    Possible duplicate of [Add HTML String to OpenXML (\*.docx) Document](https://stackoverflow.com/questions/18089921/add-html-string-to-openxml-docx-document) – Cindy Meister Dec 27 '18 at 17:03
  • I think you'll find that this has already been asked and answered: https://stackoverflow.com/questions/18089921/add-html-string-to-openxml-docx-document Specifically, for your problem, compare the order of the commands in the first code snippet of the question. You need to create the `altChunk.Id` before it can be assigned to the new part. – Cindy Meister Dec 27 '18 at 17:05
  • Note: Please do *not* provide error messages as images. Enter them as plain text. There are a number of reasons for this site rule: 1) Images aren't always clear to read, 2) text in images cannot be copied and pasted (to search the message, for example). – Cindy Meister Dec 27 '18 at 17:06

1 Answers1

0

There are some issues in AlternativeFormatImportPart with MemoryStream, document is not getting formatted well. So followed an alternate approach, using HtmlToWord method saved the html content into word and read the file content using FileStream and feed the AlternativeFormatImportPart.

string docFileName;
HtmlToWord(fileContent, out docFileName);
FileStream fileStream = File.Open(docFileName, FileMode.Open);                
// Create alternative format import part.
AlternativeFormatImportPart chunk =mainDocPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
chunk.FeedData(fileStream);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
Happy Coder
  • 31
  • 1
  • 3