Page 1 of 1

insert comment for a word

PostPosted: Mon Apr 20, 2015 5:53 pm
by thiruppathi
hi,

i have to insert a comment for particular words in docx file . for example my docx file is

this is my sampel text.
this are my testing text.


i have a sample file with spelling error(first paragraph "sampel") and grammar error(second paragraph "are").
i want to insert a comment for this two words.

my comment spelling error : change sampel to sample
my comment grammer error : change are to is

please advice with sample code.

regards
thiruppathi

Re: insert comment for a word

PostPosted: Wed Apr 22, 2015 10:04 pm
by thiruppathi
hi,

i have to insert the comment for particular text in a paragraph.

can any one please help me to finish?


Regards
Thiruppathi

Re: insert comment for a word

PostPosted: Thu Apr 23, 2015 2:15 pm
by jason
See https://github.com/plutext/docx4j/blob/ ... ample.java

Combine that with one of the techniques (documented in Getting Started) for locating the text of interest.

Re: insert comment for a word

PostPosted: Wed Apr 29, 2015 7:54 pm
by thiruppathi
hi,

can you please give example code for how to work with CommentRangeStart and CommentRangeEnd to insert the comment for particular text in a paragraph?

Regards
thiruppathi

Re: insert comment for a word

PostPosted: Mon May 04, 2015 5:34 pm
by thiruppathi
hi,

can you please provide the example code for CommentRangeStart and CommentRangeEnd?

Regards
thiruppathi

Re: insert comment for a word

PostPosted: Mon May 04, 2015 8:05 pm
by jason
If you use the docx4j helper add in, or upload to the docx4j webapp, you'll see its just:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
    // Create object for commentRangeStart
    CommentRangeStart commentrangestart = wmlObjectFactory.createCommentRangeStart();
        commentrangestart.setId( BigInteger.valueOf( 0) );  // substitute your comment id

    // Create object for commentRangeEnd
    CommentRangeEnd commentrangeend = wmlObjectFactory.createCommentRangeEnd();
        commentrangeend.setId( BigInteger.valueOf( 0) );  // substitute your comment id

    // Add these to the relevant object, for example
    p.getContent().add( commentrangestart);
    p.getContent().add( commentrangeend);

 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4

Re: insert comment for a word

PostPosted: Tue May 05, 2015 10:08 pm
by thiruppathi
hi,

it's useful for me.but i don't know how to set this for particular text.
please advice with detailed example.

regards
thiruppathi

Re: insert comment for a word

PostPosted: Wed May 06, 2015 4:22 am
by thiruppathi
public static void findAndReplace(WordprocessingMLPackage doc, String toFind, String replacer){
List<Object> paragraphs = getAllElementFromObject(doc.getMainDocumentPart(), P.class);
for(Object par : paragraphs){
P p = (P) par;
List<Object> texts = getAllElementFromObject(p, Text.class);
for(Object text : texts){
Text t = (Text)text;
if(t.getValue().equals(toFind)){

//here I want to insert the comment for this text
}
}
}

I tried with the above code...
Please advice...how to insert the comment for particular text...

Re: insert comment for a word

PostPosted: Thu May 07, 2015 8:29 pm
by thiruppathi
public static org.docx4j.wml.Comments.Comment createComment(java.math.BigInteger commentId,
String author, Calendar date, String message) {

org.docx4j.wml.Comments.Comment comment = factory.createCommentsComment();
comment.setId( commentId );

org.docx4j.wml.P commentP = factory.createP();
comment.getEGBlockLevelElts().add(commentP);

org.docx4j.wml.CommentRangeStart commentRstart = factory.createCommentRangeStart();
commentRstart.setId(commentId );
commentP .getContent().add(commentRstart);

org.docx4j.wml.CommentRangeEnd commentRend = factory.createCommentRangeEnd();
commentRend.setId(commentId );
commentP .getContent().add(commentRend);

org.docx4j.wml.R commentR = factory.createR();
commentP.getContent().add(commentR);

org.docx4j.wml.Text commentText = factory.createText();
commentR.getContent().add(commentText);

commentText.setValue(message);

return comment;
}

public static org.docx4j.wml.R createRunCommentReference(java.math.BigInteger commentId) {

org.docx4j.wml.R run = factory.createR();
org.docx4j.wml.R.CommentReference commentRef = factory.createRCommentReference();
run.getContent().add(commentRef);
commentRef.setId( commentId );

return run;

}

Please advice i tried to insert the comment for partciular text using by CommentRangeStart and commentRangeEnd.but it's not working.

Am i missing do anything?]

Re: insert comment for a word

PostPosted: Thu May 07, 2015 9:08 pm
by jason
This should be trivially easy.

Nevertheless, I've updated the sample to show you how:- https://github.com/plutext/docx4j/commi ... ab477a1d92

Re: insert comment for a word

PostPosted: Fri May 15, 2015 11:42 pm
by thiruppathi
hi,


public static void main(String args[]) throws Docx4JException, IOException
{
String inputfilepath = "D:\\thirupathi\\thiru.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
Commentinsert(wordMLPackage,documentPart);
}
public static void Commentinsert(WordprocessingMLPackage doc, MainDocumentPart documentPart) throws IOException, Docx4JException{
List<Object> paragraphs = getAllElementFromObject(doc.getMainDocumentPart(), P.class);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
// Create and add a Comments Part
CommentsPart cp = new CommentsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(cp);

Comments comments = factory.createComments();
cp.setJaxbElement(comments);

java.math.BigInteger commentId = BigInteger.valueOf(0);

P para = new P();

wordMLPackage.getMainDocumentPart().getContent().add(para);

for(Object par : paragraphs)
{
P p = (P) par;
int size=p.getContent().size();

for(int i=0;i<size;i++)
{
Object obj=p.getContent().get(i);


if(obj.equals("reading"))
{
Comment theComment = createComment(commentId, "thiru", null,"testing");
comments.getComment().add(theComment);

CommentRangeStart commentrangestart = factory.createCommentRangeStart();
commentrangestart.setId( commentId );

para.getContent().add(commentrangestart);

org.docx4j.wml.Text t1 = factory.createText();
t1.setValue(p.getContent().get(i).toString());
//t1.setSpace("preserve");

org.docx4j.wml.R run1 = factory.createR();
run1.getContent().add(t1);

para.getContent().add(run1);

CommentRangeEnd commentrangeend = factory.createCommentRangeEnd();
commentrangeend.setId( commentId ); // substitute your comment id

para.getContent().add(commentrangeend);

para.getContent().add(createRunCommentReference(commentId));
wordMLPackage.save(new java.io.File("D:/comment.docx") );

}
else{
org.docx4j.wml.Text t = factory.createText();
t.setValue(p.getContent().get(i).toString());
t.setSpace("preserve");

org.docx4j.wml.R run = factory.createR();
run.getContent().add(t);

para.getContent().add(run);
wordMLPackage.save(new java.io.File("D:/comment.docx") );
}
}

herewith i have attached the sample file for your reference.i want to insert the comment for the word "reading" that is bold format.
last conversation, i split the content and set node run.

the value is not set t1.setValue(p.getContent().get(i).toString());

and also facing font issue cannot capture "reading" as bold.


please advice.