Add a comment in a content control => Exception

Hi,

I’m using your Aspose.Words product on a .NET application and i’m facing an issue when i’m trying to add a comment in a content control. I’ve attached you the document (Word document_preview.zip (14.8 KB)) which causes the issue.

Here is my code :

		Document _document;
	
	public void UpdateDocumentContentControl()
	{
		_document = new Document(inputDocFilePath);
		List<StructuredDocumentTag> sdtList = _document.GetChildNodes(NodeType.StructuredDocumentTag, isDeep: true).Cast<StructuredDocumentTag>().ToList();
        foreach (var sdt in sdtList)
        {
			CreateFieldMarkComment(sdt);
        }
	}

	public void CreateFieldMarkComment(StructuredDocumentTag sdt)
    {
        var text = "NO DATA";
		var commentText = "ReadOnlyContext:None";
		AddTextAndComment(sdt, text, commentText);
    }

	public void AddTextAndComment(StructuredDocumentTag sdt, string text, string commentText)
	{
		// https://docs.aspose.com/display/wordsnet/Working+with+Comments
		var paragraph = new Paragraph(_document);
		paragraph.AppendChild(new Run(_document, commentText));
		var comment = new Aspose.Words.Comment(_document);
		comment.AppendChild(paragraph); 
		sdt.RemoveAllChildren();
		sdt.AppendChild(comment); // System.ArgumentException => Cannot insert a node of this type at this location.
		sdt.AppendChild(new CommentRangeStart(_document, 654));
		sdt.AppendChild(new Run(_document, text));
		sdt.AppendChild(new CommentRangeEnd(_document, 654));
	}

Thanks for your help,
Regards,

@lswe,

Thanks for your inquiry. Please note that Aspose.Words mimics the behavior of MS Word. The content control in your document is of type PlainText. MS Word does not allow to insert comment in it. Please check the attached image for detail. comment.png (23.6 KB)

Thanks for your answer,

However your reply could be satisfying if your code worked on RichText and not on PlainText…

You will find here the same document but, this time, containing a RichText content control instead of PlainText content control. Please execute my code and you will get exactly the same exception.

Again, thanks for your help,

@lswe,

Thanks for your inquiry. Please use the following modified code to fix the shared issue. We have attached the output document with this post for your kind reference. output document.zip (15.4 KB)

public static void AddTextAndComment(StructuredDocumentTag sdt, string text, string commentText, Document _document)
{
    // https://docs.aspose.com/display/wordsnet/Working+with+Comments
    var paragraph = new Paragraph(_document);
    paragraph.AppendChild(new Run(_document, commentText));
    var comment = new Aspose.Words.Comment(_document);
    comment.AppendChild(paragraph);

    sdt.RemoveAllChildren();

    var sdtparagraph = new Paragraph(_document);
    sdt.AppendChild(sdtparagraph);

    sdtparagraph.AppendChild(new CommentRangeStart(_document, 654));
    sdtparagraph.AppendChild(new Run(_document, text));
    sdtparagraph.AppendChild(new CommentRangeEnd(_document, 654));

    sdtparagraph.AppendChild(comment); 
}

Thanks,

Now it works,

Regards

A post was split to a new topic: Only Content controls are allowed directl