2

I have a string which is generated as an output of a WYSIWYG editor which is HTML where has base 64 data. The sample is as below.

<p>Report requires month to date and year to date <b style="background-color: rgb(255, 255, 0);">values</b> with separate debit and credit columns</p><p><br></p><p><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgmOiaf+niaT+Efj//2Q==" style="width: 180px;" data-filename="8204_659888434064067_2072530899_a.jpg"><br></p><p>Figure I
</p><p style="box-sizing: border-box; margin: 0px 0px 10px; color: rgb(0, 0, 0); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><img src="data:image/jpeg;base64,/9j/4AAQSkZQMP89aKKKAP/2Q==" data-filename="IMG-20130325-WA0000.jpg" style="box-sizing: border-box; border: 0px; vertical-align: middle; width: 168.469px; height: 137px;"><br style="box-sizing: border-box;"></p><p>Figure 2</p><p><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/iigD//2Q==" style="width: 333px;" data-filename="IMG-20130906-WA0000.jpg"><br></p><p>Figure 3</p>

I have removed the src data of img tag as its too long.

I need this string to be pushed into a word document. Sample code is as below

//project name
Microsoft.Office.Interop.Word.Paragraph heading = document.Content.Paragraphs.Add(ref missing);
                heading.Range.set_Style(ref h1);
                heading.Range.HighlightColorIndex = WdColorIndex.wdGray25;
                heading.Range.Font.Color = WdColor.wdColorOrange;
                heading.Range.Text = project.name + " (" + project.projectId + ") " + " - " + project.projectCode;
                heading.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                heading.Range.InsertParagraphAfter();

                //project details
                Microsoft.Office.Interop.Word.Paragraph heading2 = document.Content.Paragraphs.Add(ref missing);
                heading2.Range.set_Style(ref h2);
                heading2.Range.HighlightColorIndex = WdColorIndex.wdNoHighlight;
                heading2.Range.Font.Color = WdColor.wdColorBlue;
                heading2.Range.Text = "Project Details:" + nextLine;
                heading2.Range.Text = "Director: " + project.director.firstName + " " + project.director.lastName + " (" + project.director.email + ")" + nextLine;
                heading2.Range.Text = "Manager: " + project.manager.firstName + " " + project.manager.lastName + " (" + project.manager.email + ")" + nextLine;
                heading2.Range.Text = "Client: " + project.client.firstName + " " + project.client.lastName + " (" + project.client.email + ")" + nextLine;
                heading2.Range.Text = "Start Date: " + project.startDate + nextLine;
                heading2.Range.Text = "Expected End Date: " + project.expectedDate + nextLine;
                heading2.Range.Text = "Hours: " + project.hours + nextLine;
                heading2.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                heading2.Range.InsertParagraphAfter();
List<Ricefw> ricefList = context.BaseChapter.OfType<Ricefw>().Where(u => u.project.Id == project.Id && u.deleted == false).ToList();
foreach (var task in ricefList)
{
    //Task Code and status
    Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
    //para1.Range.ListFormat.ApplyBulletDefault();
    para1.Range.set_Style(ref h2);
    para1.Range.HighlightColorIndex = WdColorIndex.wdGray25;
    para1.Range.Font.Color = WdColor.wdColorOrange;
    para1.Range.Text = task.taskCode;
    para1.Range.InsertParagraphAfter();

    //other details
    Microsoft.Office.Interop.Word.Paragraph para2 = document.Content.Paragraphs.Add(ref missing);
    para2.Range.HighlightColorIndex = WdColorIndex.wdNoHighlight;
    para2.Range.Font.Color = WdColor.wdColorBlack;
    para2.Range.Text = "Description: " + task.description + nextLine;
    para2.Range.InsertParagraphAfter();


    //task strategy
    Microsoft.Office.Interop.Word.Paragraph para3 = document.Content.Paragraphs.Add(ref missing);
    para3.Range.HighlightColorIndex = WdColorIndex.wdNoHighlight;
    para3.Range.Font.Color = WdColor.wdColorBlack;
    para3.Range.Text = "Task Strategy:" + nextLine;
    if (task.taskStrategy != null && task.taskStrategy.Length > 0)
    {
        para2.Range.Text =task.taskStrategy; //task.taskStrategy has the html with base64 embedded image
        para3.Range.InsertParagraphAfter();
    }
    else
    {
        para3.Range.Text = "No Task Strategy.";
    }
    para3.Range.InsertParagraphAfter();
}

I tried copying to clipboard and pasting as well, where HTML is coming as expected, but image is missing.

Any help is really appreciated.

  • Hope this link can help you http://stackoverflow.com/questions/26567042/how-to-write-an-image-from-byte-into-ms-word-using-c-sharp-windows-application – Chandan Kumar Mar 07 '17 at 05:45
  • Thanks for the link, but the difference here is that, the string that I have has HTML tags like

    , , etc. I need to extract the image in base 64 out and convert to normal image and then place it back in the exact same position.

    – Rohith Poyyeri Mar 07 '17 at 05:51

0 Answers0