Free Support Forum - aspose.com

Get comment Range start and end :

Hi ,

I am converting doc into html using Aspose.words.jdk . I need to get the comment range start and range end . Is it possible ? do help me .

I have attached document .In that i need to get text of ’ a test comment ’ .

Thanks in advance .

Hi there,

Thanks for your inquiry.

Please see this article here to find out how to work with comments in your document. This will show you how to extract comment nodes directly. The same technique can be used to extract the CommentRangeStart and CommentRangeEnd nodes as well.

If you have any further queries, please feel free to ask.

Thanks,

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can use code like the following to get “commented text” associated with Comment:

// Open Document.

Document doc = new Document("C:\\Temp\\comment.docx");

// Get all comments from the document.

Node[] comments = doc.getChildNodes(NodeType.COMMENT, true).toArray();

HashMap<Integer,String> commentedTexts = GetCommentedText(doc);

// Loop throught all comments

for(Node commemtNode : comments)

{

Comment comment = (Comment)commemtNode;

System.out.printf("COMMENTED TEXT: %s\nCOMMENT: %s\n\n", commentedTexts.get(comment.getId()), comment.toTxt());

}

===============================================================

private static HashMap<Integer,String> GetCommentedText(Document doc) throws Exception

{

HashMap commentedTexts = new HashMap();

CommentedTextFinder finder = new CommentedTextFinder(commentedTexts);

doc.accept(finder);

return commentedTexts;

}

private static class CommentedTextFinder extends DocumentVisitor

{

public CommentedTextFinder(HashMap<Integer,String> commentedTexts)

{

mCommentedTexts = commentedTexts;

mBuilder = new StringBuilder();

mIsSkipText = true;

}

///

/// Called when a Run node is encountered in the document.

///

public int visitRun(Run run)

{

AppendText(run.getText());

// Let the visitor continue visiting other nodes.

return VisitorAction.CONTINUE;

}

///

/// Called when visiting of a Paragraph node is ended in the document.

///

public int visitParagraphEnd(Paragraph paragraph)

{

// When outputting to plain text we output Cr+Lf characters.

AppendText(ControlChar.CR_LF);

return VisitorAction.CONTINUE;

}

///

/// Called when visiting of a CommentRangeStart node is ended in the document.

///

public int visitCommentRangeStart(CommentRangeStart commentRangeStart)

{

mIsSkipText = false;

return VisitorAction.CONTINUE;

}

///

/// Called when visiting of a CommentRangeStart node is ended in the document.

///

public int visitCommentRangeEnd(CommentRangeEnd commentRangeEnd)

{

mIsSkipText = true;

// Save commented text in map and reset strign builder.

mCommentedTexts.put(commentRangeEnd.getId(), mBuilder.toString());

mBuilder.delete(0, mBuilder.length());

return VisitorAction.CONTINUE;

}

///

/// Adds text to the current output. Honours the enabled/disabled output flag.

///

private void AppendText(String text)

{

if (!mIsSkipText)

mBuilder.append(text);

}

private boolean mIsSkipText;

private final StringBuilder mBuilder;

private HashMap<Integer,String> mCommentedTexts;

}

Hope this helps.

Best regards,

HI ,
Thanks for your prompt answer.

Hi ,
Is it possible to highlight the selected comment as MSWord does ? So that in converted html i should get highlight the selected comment . Is there any ways to do ? Do help me .



Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Sure, you can. You can use almost the same visitor to achieve this:

@Test

public void Test001() throws Exception {

// Open the source document.

Document doc = new Document("C:\\Temp\\comment.docx");

CommentedTextHighlighter highlighter = new CommentedTextHighlighter();

doc.accept(highlighter);

// Save output document

doc.save("C:\\Temp\\out.html");

}

private static class CommentedTextHighlighter extends DocumentVisitor

{

public CommentedTextHighlighter()

{

mBuilder = new StringBuilder();

mIsSkipText = true;

}

///

/// Called when a Run node is encountered in the document.

///

public int visitRun(Run run)

{

Higlight(run.getFont());

// Let the visitor continue visiting other nodes.

return VisitorAction.CONTINUE;

}

///

/// Called when visiting of a Paragraph node is ended in the document.

///

public int visitParagraphEnd(Paragraph paragraph)

{

Higlight(paragraph.getParagraphBreakFont());

return VisitorAction.CONTINUE;

}

///

/// Called when visiting of a CommentRangeStart node is ended in the document.

///

public int visitCommentRangeStart(CommentRangeStart commentRangeStart)

{

mIsSkipText = false;

return VisitorAction.CONTINUE;

}

///

/// Called when visiting of a CommentRangeStart node is ended in the document.

///

public int visitCommentRangeEnd(CommentRangeEnd commentRangeEnd)

{

mIsSkipText = true;

return VisitorAction.CONTINUE;

}

///

/// Highlights text. Honours the enabled/disabled output flag.

///

private void Higlight(Font font)

{

if (!mIsSkipText)

{

try

{

font.setHighlightColor(Color.yellow);

}

catch(Exception ex){}

}

}

private boolean mIsSkipText;

private final StringBuilder mBuilder;

}

Hope this helps.

Best regards,

Hi,
Are there any code for .net? There are not CommentRangeStart and CommentRangeEnd class in Aspose.Word.Net

Hello

Thanks for your request. It seems you are using an old version of Aspose.Words. The latest version of Aspose.Words is 10.2.0; you can download this version from here:

http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/category1188.aspx

Best regards,