Adding comments to document leads to errors while saving

Hello, everybody,

I have a big problem adding comments to documents (AsposeWords4Java 3.0; .doc-files from Word 2003).

Since the software I write works as a preflight tool for further processing it is essential to add comments to documents in order to mark parts of the document which are not marked up as they should be.

I encounter the error “IllegalStateException (Comments are only allowed in the main text of the document.)” quite regularly. I could work around this problem fine, if I could somehow validate the positioning of a comment.

So far I have figured out the following 'no-no’s:

  • comments are forbidden in header, footer, comment
  • comment sometimes! ? !forbidden in tables

Strangely I could add comments to shapes sometimes, but cannot reproduce when it is allowed and when it is not.

Is there another way than saving after each added comment to make sure the document stays valid?

Is there a way to mark a run as commented inside a commented paragraph?

Kind regards,

Martin Hillert

Hi Martin,

Thanks for your request.

  1. Comments are only allowed inside the main text of the document. If you have added a comment to a paragraph that is inside a footer or textbox, it is invalid and an exception will be thrown.
  2. No, unfortunately, currently there is no way to mark range with comment. I linked your request to the appropriate issue. You will be notified as soon as this feature is supported.

Best regards.

Hi, Alexey,

Thanks for the prompt reply.

But your answer seems not to include all cases:

Today I encountered another misplaced comment. The document structure was as follows:

Document Section
Body
Table Row
Cell
Paragraph

Adding the comment to the paragraph threw an exception saving the document. Neither was the paragraph outside the main body of the document nor inside a textbox.

Adding a comment with word to the selfsame paragraph lead to no problems at all.

Please check if this is intended behaviour. If so, please complete the list, where no comments are allowed.

Kind regards,

Martin

Hi

Thank you for additional information. Could you please create sample code that will allow me to replicate the problem on my side? I will check the issue and provide you more information.
Best regards.

Hi, Alexey,

I work on an application written in RubyOnRails, using the RubyJavaBridge(Rjb) class to access Aspose.words. Most of the code below should be well understandable since the Java methods are accessed directly. @doc represents the document node of the word document.

# Adds an comment to a AncestorOrSelf of Type PARAGRAPH
def add_comment(node, comment_string, format = '')
# no comments in footnotes, header/footer or comments
if ((node.getAncestor(NodeType.HEADER_FOOTER)) or(node.getAncestor(NodeType.COMMENT)) or(node.getAncestor(NodeType.FOOTNOTE)))
return nil
else
    unless node.getNodeType == NodeType.PARAGRAPH
cnode = nil
while node = node.getParentNode
cnode = node
if node.getNodeType == NodeType.PARAGRAPH
end
if cnode.nil?
return nil
end
node = cnode 
end
builder = Builder.new(@doc)
builder.moveTo(node)
comment = Comment.new(@doc)
builder.getCurrentParagraph().appendChild(comment)
comment.setInitial(‘LTX’)
comment.setAuthor('Le - TeX formatting errors')
comment.getParagraphs().add(Paragraph.new(@doc))
run = Run.new(@doc, comment_string)
if format == 'bold'
run.getFont.setBold(true)
end
comment.getFirstParagraph().getRuns().add(run)
return node
end
end

Adding NodeType.TABLE to the exclusion list in line 4 removed the error but suppressed the comment as well…
Kind regards,

Martin

Hi

Thank you for additional information. Yes, your code is understandable. However, it is incomplete a bit. Use code like the following to check if Paragraph is child of main story:

/**
* Retorns true if paragraph is located in the main story.
*/
private static boolean isChildOfMainStory(Paragraph par)
{
    // Return true if story of current node is main story (Body)
    return par.getAncestor(NodeType.SHAPE) == null &&
        par.getAncestor(NodeType.HEADER_FOOTER) == null &&
        par.getAncestor(NodeType.COMMENT) == null &&
        par.getAncestor(NodeType.FOOTNOTE) == null;
}

Hope this helps.
Best regards.

Hi, Alexey,

this seems to have done the trick. Thanks so much.

See the ruby-code below

#returns true if node is a proper position to add a comment to
def is_paragraph_in_main_body?(node)
return false unless node.getNodeType == NodeType.PARAGRAPH
return false
if node.getAncestor(NodeType.SHAPE)
return false
if node.getAncestor(NodeType.COMMENT)
return false
if node.getAncestor(NodeType.FOOTNOTE)
return false
if node.getAncestor(NodeType.HEADER_FOOTER)
return true
end

# Adds an comment to a AncestorOrSelf of Type PARAGRAPH if possible
def add_comment(node, comment_string, format = '')\ # find proper place
for comment
until node.nil ? or is_paragraph_in_main_body ? (node)
node = node.getParentNode()
end
return nil if node.nil?
builder = Builder.new(@doc)
builder.moveTo(node)
comment = Comment.new(@doc)
builder.getCurrentParagraph().appendChild(comment)
comment.setInitial(‘LTX’)
comment.setAuthor('Le - TeX formatting errors')
comment.getParagraphs().add(Paragraph.new(@doc))
run = Run.new(@doc, comment_string)
if format == 'bold'
run.getFont.setBold(true)
end
comment.getFirstParagraph().getRuns().add(run)
return node
end

Kind regards,

Martin

We are happy to notify you that the latest version of Aspose.Words supports commented ranges. You can download the latest version fo Aspose.Words from here..
Here is a simple code example, which allows insertion commented range into the document:

// Create an empty document and DocumentBuilder object.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a Comment.
Comment comment = new Comment(doc);
// Insert some text into the comment.
Paragraph commentParagraph = new Paragraph(doc);
commentParagraph.AppendChild(new Run(doc, "This is comment!!!"));
comment.AppendChild(commentParagraph);
// Create CommentRangeStart and CommentRangeEnd.
int commentId = 0;
CommentRangeStart start = new CommentRangeStart(doc, commentId);
CommentRangeEnd end = new CommentRangeEnd(doc, commentId);
// Insert some text into the document.
builder.Write("This is text before comment ");
// Insert comment and comment range start.
builder.InsertNode(comment);
builder.InsertNode(start);
// Insert some more text.
builder.Write("This is commented text ");
// Insert end of comment range.
builder.InsertNode(end);
// And finaly insert some more text.
builder.Write("This is text aftr comment");
// Save output document.
doc.Save(@"Test001\out.doc");

Best regards,