Convert HTML to Word & Create Comments (annotations) without Carriage Return or Line Break using C# .NET

Hi,

I’m struggling with an issue to create comments without carriage return or line break.
Actually, I’m using an external library to annotate comments. While exporting my html to word I convert those annotations to Aspose word comments as follow:

var cssStyleRegex = new Regex(@"<annotation.*?>(.*?)<\/annotation>");
var match = cssStyleRegex.Match(contentXHTML);
if (match.Success)
{
    int commentId = 0;
    int endOfIndexPrevmatch = 0;
    int contentLength = contentXHTML.Length;
    foreach (Match m in cssStyleRegex.Matches(contentXHTML))
    {
        XmlDocument doc = new XmlDocument();
        doc.XmlResolver = null;

        string annotationStr = m.Value;
        int indexMatch = m.Index;

        builder.InsertHtml(contentXHTML.Substring(endOfIndexPrevmatch, indexMatch - endOfIndexPrevmatch));

        Aspose.Words.Comment comment = new Aspose.Words.Comment(builder.Document);
        if (annotation.Comments.Count == 0)
        {
            GetTextEditionTracking(builder, node.InnerXml);
            continue;
        }

        comment.Author = annotation.Comments[0].UserName;
        comment.DateTime = annotation.Comments[0].TimeConverted;
        comment.Initial = annotation.Comments[0].UserId;
        comment.SetText(annotation.Comments[0].Text);

        CommentRangeStart start = new commentRangeStart(builder.Document, comment.Id);
        builder.InsertNode(start);

        // Insert some more text.
        //builder.Write(node.InnerText);
        GetTextEditionTracking(builder, node.InnerXml);
        // Insert end of comment range.
        CommentRangeEnd end = new CommentRangeEnd(builder.Document, comment.Id);

        builder.InsertNode(end);

        // Insert comment and comment range start.
        builder.InsertNode(comment);
        ......

My goal is to avoid the carriage return inserted automatically before my comments.
This is an issue for text annotated in the middle of a sentence.

I’m using Aspose Words: 20.9.0.0 for .Net.

Regards

@amtalbi,

Please ZIP and attach the following resources here for testing:

  • Your simplified source file
  • Aspose.Words for .NET 21.4 generated output file showing the undesired behavior
  • Your expected file showing the desired output. You can create this document manually by using MS Word.
  • Please also create a standalone simple Console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you more information.

Program.zip (3.0 KB)
TestAsposeWordComment.zip (127.3 KB)
Hi,
You can find in attached file our source file “Program.cs” and the input XML file “ImportedFile”
and solution file “TestAsposeWordComment.zip”.
We are using Aspose Words : 20…9.0.0.

Regards

@wch,

We are checking this scenario and will get back to you soon.

@wch, @amtalbi,

Please check Program Modified.zip (2.4 KB) which should help to achieve the desired output on your end.

Hi,

Thank you for the program,

But we find an issue when we comment the same text many times.

ie : an exception is occurred when we comment the word “Confidential” two times with different comments

You can find attached a file where you will see an example.
ImportedFile.zip (1.3 KB)

@wch,

We are checking this scenario and will get back to you soon.

@wch,

keyValuePairs.Add(node.InnerXml, comment);

The above line causes the following exception on my end too:

System.ArgumentException
  HResult=0x80070057
  Message=An item with the same key has already been added. Key: Confidential
  Source=<Cannot evaluate the exception source>

I have also produced an intermediate Word document which contains the word Confidential seven times (see intermediate output.docx (8.2 KB)). But, the regex @"<annotation.*?>(.*?)<\/annotation>" returns only two matches form the ImportedFile.xml that you attached in your previous post. Can you please also provide your expected Word document showing the desired output here for our reference? This will help us to determine out of which seven “Confidential” words you want to insert these two comments to.

Hi @awais.hafeez,
I’m using your delivered program to export our xml to word file. But i have issues in some use cases:

  1. When i comment text that appears many time in my document, you can test this use case using the file uploaded : importfile many time word.xml.

  2. When i have some reply to one comment, you can test this use case using the file uploaded : importfile many reply.xml.

Can you please help me to resolve these issues?

use case demo files.zip (2.0 KB)

@wch,

Unfortunately, your queries are not clear enough therefore we request you to please elaborate your inquiries further by providing complete details. Please also ZIP and attach the following resources here for testing:

  • Your simplified source file
  • Aspose.Words for .NET 21.9 generated output file showing the undesired behavior
  • Your expected file showing the desired output. You can create this document manually by using MS Word.
  • Please also create a standalone simple Console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will then start further investigation into your scenarios and provide you more information.

Hi,
You can find attached file the source file “ModifiesProgram.zip” that you provided on 14th of july Program Modified.zip (2.4 KB)

I have issues in some use cases:

  • When i comment text that appears many time in my document, code generate an exception, you can test this use case using the file uploaded : importfile many time word.xml.

  • When i have some reply to one comment, code generate an exception, you can test this use case using the file uploaded : importfile many reply.xml.

We find sample files in file below :
ImportedFile.zip (1.3 KB)

We are using Aspose Words : 20…9.0.0.

Regards

@wch,

We are checking this scenario and will get back to you soon.

Hi,
Any update for this issue?

@wch,

Thanks for being patient. Please spare us some time to investigate this problem; we will get back to you with our findings as soon as possible.

@awais.hafeez - Hi Awais. I am the Chief Product Officer at Ivalua. We really need resolution asap on this issue, which was opened in April 2021. We are now past code freeze and this issue is preventing us from delivering a major feature, very expected by our customer base, as part of our next major release (beta release at the end of November). Please let us know if you need anything to resolve. Many thanks.

@wch, @pbens,

Please check the following source XML, Aspose.Words generated DOCX and C# code files:

@awais.hafeez - Many thanks for your support. @amtalbi took over on our side and has been able to use your input to resolve the issue on our end. I will let @amtalbi close the issue if fix is verified on our end.

@pbens,

It is great that you were able to resolve the problems with these files on your end. In case you have other inquiries or may need any help in future, please let us know by posting a new thread in Aspose.Words’ forum.