Skip to content

DIffplex highlighting of different text not working #150

@nabi-zain

Description

@nabi-zain

The following code as per ChatGPT fails to correctly highlight differences on word document that was initially uploaded.

private void ApplyDiffToDocument(string filePath, DiffPaneModel diff)
{
    using var doc = WordprocessingDocument.Open(filePath, true);
    var body = doc.MainDocumentPart.Document.Body;
    body.RemoveAllChildren<Paragraph>();

    foreach (var line in diff.Lines)
    {
        var para = new Paragraph();
        var run = new Run(new Text(line.Text));
        var props = new RunProperties();
        Highlight highlight = null;

        switch (line.Type)
        {
            case ChangeType.Inserted:
                highlight = new Highlight() { Val = HighlightColorValues.Green }; break;
            case ChangeType.Deleted:
                highlight = new Highlight() { Val = HighlightColorValues.Yellow }; break;
            case ChangeType.Modified:
                highlight = new Highlight() { Val = HighlightColorValues.LightGray }; break;
        }

        if (highlight != null)
            props.Append(highlight);

        run.RunProperties = props;
        para.Append(run);
        body.Append(para);
    }

    doc.MainDocumentPart.Document.Save();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions