Retain comments in Word document

We have a repetitive creation process of a Word document. Now I’m looking for a way to retain the comments. e.g. V1 of the document is created, comments are added to this V1, now the V2 of the document should be created, we extract the comments of V1, recreate the document and now we want to re-add the former comments.
(I found a way to retrieve all comments but I couldn’t find the associated paragraphs.)
Any suggestions how to implement that?
thanks,
thomas

@tpalmie

In your case, we suggest you please bookmark the paragraph in first document and get the comment of bookmarked paragraph and use it according to your requirement.

If you still face problem, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Thanks Tahir
What I did was using this code to store the Comments objects before deleting the document:
// Collect all comments in the document
NodeCollection comments = summaryDoc.GetChildNodes(NodeType.Comment, true);
foreach (Aspose.Words.Comment comment in comments)
{
previousComments.Add(comment);
}
and this code which inserts the comments into the newly assembled document:
foreach (Aspose.Words.Comment comment in previousComments)
{
Aspose.Words.Paragraph paraWithComment = comment.ParentParagraph;
if (paraWithComment != null)
{
int pos = paraWithComment.Range.Text.IndexOf(“”);
if (pos > 0)
{
// find the text of the previous comment
strParentParagraphText = paraWithComment.Range.Text.Substring(0, pos);
}
else
{
strParentParagraphText = paraWithComment.Range.Text;
}
}

            NodeCollection paragraphs = summaryDoc.GetChildNodes(NodeType.Paragraph, true);
            // Look through all comments and gather information about them.
            foreach (Aspose.Words.Paragraph para in paragraphs)
            {
                if (para.Range.Text.Length > strParentParagraphText.Length &&
                    strParentParagraphText == para.Range.Text.Substring(0, strParentParagraphText.Length))
                {
                    // unfortunately this approach only works for comments which have been added to text
                    // this currently does not work for comments which have been added to images
                    Aspose.Words.Comment commentNew = new Aspose.Words.Comment(summaryDoc, comment.Author, comment.Initial, comment.DateTime);
                    para.AppendChild(commentNew);
                    commentNew.Paragraphs.Add(new Aspose.Words.Paragraph(summaryDoc));
                    commentNew.FirstParagraph.Runs.Add(new Run(summaryDoc, comment.GetText()));
                    break;
                }
            }
        }

This works fine for comments which have been added to text. However, it does not work for comments which have been added to images.
Any ideas?
thanks,
thomas

@tpalmie

Please share requested resources in my previous post for testing. We will investigate the issue and provide you more information on it.

Thanks Tahir. I’m attaching a sample app where I’m reading the comments from the file summary.docx, delete that document, copy the the template.docx file into summary.docx and now I’m trying to re-add the previous comments.SetContentControlTest.zip (6.0 MB)

@tpalmie

Thanks for sharing the code example. We are working over your query and will get back to you soon.

Hi Tahir.
Do you have an update on this case?
best regards,
thomas

@tpalmie

Please accept my apology for late response.

You are inserting comment correctly for paragraphs. Regarding the image comment, the same image does not exist in template.docx.

In your case, we suggest you please use Shape.AlternativeText property to set the alternative text of shape in both documents. Once you have this property set with some value, you can get the Shape node using this property and add comment to other document for image. Hope this helps you.