by thiruppathi » Fri May 15, 2015 11:42 pm
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.
- Attachments
-
- xxx.docx
- in this file the word "reading" is bold.
- (10.13 KiB) Downloaded 383 times