DanilGholtsman
DanilGholtsman

Reputation: 2374

Inserting HTML into Word via Word.Interop cause different paragraph headers font size

In one of my program I need to insert some simple htmls into word document at some range, so I use function like that to use for every range I want

 public static void AddHtmlTextAtRange(this Range range, string html)
        {
            var tempFilename = string.Format("{0}{1}{2}", Path.GetTempPath(), Guid.NewGuid().ToString(), ".html");
            var formattedHtml = TextToHtmlPreProcessing(html);
            File.WriteAllText(tempFilename, string.Format("<html> {0} </html>", formattedHtml));

            range.InsertFile(tempFilename, ConfirmConversions:false);

            File.Delete(tempFilename);
        }

The problem is that in some cases when I add html file at range like that, there are some paragraph header size problems occures.

Seems like it depends of what foes first int file. If it's header - then number font is bigger.

File example:

<html>
    <h2 style=''><span>Document Purpose</span></h2>Some data info<span></span>
    <h2 style=''><span>Scope & Requirements Traceability</span></h2>
    Some data too
    <table>
        <tr>
            <td style='border-collapse:collapse;border:1px solid black;font-weight: bold; background:#BFBFBF'>Ref</td>
            <td style='border-collapse:collapse;border:1px solid black;font-weight: bold; background:#BFBFBF'>Requirement</td>
            <td style='border-collapse:collapse;border:1px solid black;font-weight: bold; background:#BFBFBF'>Req Met (Y/N)</td>
            <td style='border-collapse:collapse;border:1px solid black;font-weight: bold; background:#BFBFBF'>Action taken</td>
        </tr>
        <tr>
            <td style='border-collapse:collapse;border:1px solid black;'>&nbsp;</td>
            <td style='border-collapse:collapse;border:1px solid black;'>&nbsp;</td>
            <td style='border-collapse:collapse;border:1px solid black;'>&nbsp;</td>
            <td style='border-collapse:collapse;border:1px solid black;'>&nbsp;</td>
        </tr>
        <tr>
            <td style='border-collapse:collapse;border:1px solid black;'>&nbsp;</td>
            <td style='border-collapse:collapse;border:1px solid black;'>&nbsp;</td>
            <td style='border-collapse:collapse;border:1px solid black;'>&nbsp;</td>
            <td style='border-collapse:collapse;border:1px solid black;'>&nbsp;</td>
        </tr>
    </table>
    <h2 style=''><span>Background</span></h2>
    Some info
    <h2 style=''><span>Risks, Assumptions, Issues and Dependencies</span></h2>
    Risks info
</html>

Output example: enter image description here

Otherwise, if first goes text and after goes header - everything is looks fine.

(Nevermind the text it absolutley random from the top of my head :) )

File example:

<html>
    I dont know
    <h2 style=''><span>Test1</span></h2>
    I dont know
    <h2 style=''><span>Test2</span>
    </h2>
    I dont know too
    <h2 style=''><span>Test3</span>
    </h2>
    Some datainfo
    <h2 style=''><span>Test4</span>
    </h2>
    Test
</html>

Output example:

enter image description here

So, the problem is, how to get rid of this first header bigger size problem?

There is sort of workaroud if I will put something like that <p>&nbsp;</p> right after beggining but it cause extra new line, which I really dont want to have there.

Maybe there is some opton to turn it off?

Upvotes: 1

Views: 1541

Answers (1)

Jbjstam
Jbjstam

Reputation: 884

This is an example of how you could remove the placeholder "<p>TEMPTEXT</p>"

private static void InsertHtml(Word.Range range, string html)
{
    File.WriteAllText("temp.html", html);

    range.InsertFile(Path.GetFullPath("temp.html"), Type.Missing, false);

    //this is the interesting part
    if (range.Find.Execute("TEMPTEXT")) {
        //range now points to only the text TEMPTEXT

        //We make sure to take the entire paragraph
        var paragraph = range.Paragraphs[1];

        //Then we remove the entire paragraph range
        var paragraphRange = paragraph.Range;
        paragraphRange.Delete();
    }
}

I obtained the range object like this:

var doc = app.ActiveDocument;
var range = doc.StoryRanges[WdStoryType.wdMainTextStory];
range.Collapse(WdCollapseDirection.wdCollapseEnd);

Upvotes: 1

Related Questions