Hello!
Is it possible to add a comment, footnote to a docx file.
And when yes how?
Thanks
wordMLPackage.getMainDocumentPart().addTargetPart(yourNewPart);
14.10.2010 18:14:34 *INFO * SaveToZipFile: PUT SUCCESS: word/_rels/document.xml.rels (SaveToZipFile.java, line 204)
14.10.2010 18:14:34 *INFO * SaveToZipFile: For Relationship Id=rId1 Source is /word/document.xml, Target is comments.xml (SaveToZipFile.java, line 311)
14.10.2010 18:14:34 *INFO * SaveToZipFile: Getting part /word/comments.xml (SaveToZipFile.java, line 349)
14.10.2010 18:14:34 *DEBUG* PartName: Trying to create part name /word/comments.xml (PartName.java, line 150)
14.10.2010 18:14:34 *DEBUG* PartName: /word/comments.xml part name created. (PartName.java, line 170)
14.10.2010 18:14:34 *ERROR* SaveToZipFile: Part word/comments.xml not found! (SaveToZipFile.java, line 354)
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Comments;
public class Maechr {
public static void main(String[] args) throws Exception {
// Create a package
WordprocessingMLPackage wmlPack = new WordprocessingMLPackage();
// Create main document part
MainDocumentPart wordDocumentPart = new MainDocumentPart();
// Create main document part content
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
org.docx4j.wml.Body body = factory.createBody();
org.docx4j.wml.Document wmlDocumentEl = factory.createDocument();
wmlDocumentEl.setBody(body);
wordDocumentPart.setJaxbElement(wmlDocumentEl);
wmlPack.addTargetPart(wordDocumentPart);
CommentsPart cp = new CommentsPart();
// Part must have minimal contents
Comments comments = factory.createComments();
cp.setJaxbElement(comments);
wordDocumentPart.addTargetPart(cp);
// Now you can add comments to your comments part,
// and comment refs in your main document part
wmlPack.save(new java.io.File(System.getProperty("user.dir")+ "/out-m.docx"));
}
}
<w:p>
<w:commentRangeStart w:id="0"/>
<w:r>
<w:t>hello</w:t>
</w:r>
<w:commentRangeEnd w:id="0"/>
<w:r>
<w:rPr>
<w:rStyle w:val="CommentReference"/>
</w:rPr>
<w:commentReference w:id="0"/>
</w:r>
</w:p>
import java.util.Iterator;
import java.util.List;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.JaxbXmlPart;
import org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.relationships.Relationships;
import org.docx4j.wml.ObjectFactory;
import util.RelationshipName;
public class Comment {
private WordprocessingMLPackage wordMlPackage;
private boolean relSet = false;
public Comment(WordprocessingMLPackage wordMLPack) {
this.wordMlPackage = wordMLPack;
setCommentRel();
}
private void setCommentRel() {
if (!commentRelSet()) {
CommentsPart cp;
try {
cp = new CommentsPart();
// Part must have minimal contents
org.docx4j.wml.ObjectFactory wmlObjectFactory = new ObjectFactory();
wordMlPackage.getMainDocumentPart().addTargetPart(cp);
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private boolean commentRelSet() {
Relationship relShip;
boolean relSet = false;
if (!relSet) {
JaxbXmlPart<Relationships> jxpRelShips = wordMlPackage
.getMainDocumentPart().getOwningRelationshipPart();
Relationships pk = jxpRelShips.getJaxbElement();
List<Relationship> mc = pk.getRelationship();
Iterator<Relationship> it = mc.iterator();
while (it.hasNext() && !relSet) {
relShip = it.next();
if (relShip.getValue().equalsIgnoreCase(
RelationshipName.commentIdentifier)) {
relSet = true;
}
}
}
return relSet;
}
}
CommentsPart cp = new CommentsPart();
// Part must have minimal contents
Comments comments = factory.createComments();
cp.setJaxbElement(comments);
wordDocumentPart.addTargetPart(cp)
<w:comment w:id="1" w:author="autor" w:date="2010-08-26T10:03:00Z" w:initials="AM">
<w:p>
<w:pPr>
<w:pStyle w:val="Kommentartext"/>
<w:rPr>
<w:lang w:val="de-AT"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rStyle w:val="Kommentarzeichen"/>
</w:rPr>
<w:annotationRef/>
</w:r>
<w:r w:rsidR="00AC177F">
<w:rPr>
<w:lang w:val="de-AT"/>
</w:rPr>
<w:t>dfsadfas</w:t>
</w:r>
</w:p>
</w:comment>
Comment c = Context.getWmlObjectFactory().createCommentsComment()
Comments body = documentPart.getCommentsPart().getJaxbElement();
*DEBUG* TraversalUtil: getting children of org.docx4j.wml.Comments (TraversalUtil.java, line 110)
org.docx4j.wml.Comments$Comment ""
public List<Object> getEGBlockLevelElts()
// Assuming String xml contains the XML above
org.docx4j.wml.Comments.Comment comment = XmlUtils.unmarshalString(xml);
public static Object unmarshalString(String str, JAXBContext jc, Class declaredType)
org.docx4j.wml.ObjectFactory
Context.getWmlObjectFactory().createCommentsComment()
wordMlPackage.getMainDocumentPart().getJaxbElement().getBody().getEGBlockLevelElts().add(comment);
c.getEGBlockLevelElts().add(c);
comment.getEGBlockLevelElts().add(c);
CommentsPart cp = new CommentsPart();
// Part must have minimal contents
Comments comments = factory.createComments();
cp.setJaxbElement(comments);
public class CommentRel {
private WordprocessingMLPackage wordMlPackage;
private MainDocumentPart mainPart;
private boolean relSet = false;
private org.docx4j.wml.ObjectFactory wmlObjectFactory;
private CommentsPart cmPart;
public CommentRel(WordprocessingMLPackage wordMLPack) {
this.wordMlPackage = wordMLPack;
wmlObjectFactory = new ObjectFactory();
setCommentRel();
cmPart = wordMlPackage.getMainDocumentPart().getCommentsPart();
mainPart = wordMLPack.getMainDocumentPart();
}
private void setCommentRel() {
if (!commentRelSet()) {
CommentsPart cp;
try {
cp = new CommentsPart();
// Part must have minimal contents
Comments comments = wmlObjectFactory.createComments();
cp.setJaxbElement(comments);
wordMlPackage.getMainDocumentPart().addTargetPart(cp);
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private boolean commentRelSet() {
return !(wordMlPackage.getMainDocumentPart().getCommentsPart() == null);
}
public void addNewComment(String author, String text) {
// XMLGregorianCalendar xmlCal = new XMLGregorianCalendarImpl(); // You'll need to fix this!
// CommentRangeEnd cRangeEnde = wmlObjectFactory.createCommentRangeEnd();
// CommentRangeStart cRangeStart = wmlObjectFactory
// .createCommentRangeStart();
Comments comment = wmlObjectFactory.createComments();
// Comments.Comment myCom = wm
Comment c = Context.getWmlObjectFactory().createCommentsComment();
System.out.println("test");
// comment.setParent(cmPart);
c.setAuthor(author);
// c.setDate(xmlCal);
cmPart.getJaxbElement().getComment().add(c);
System.out.println("test ende");
}
// WordprocessingMLPackage wordML;
public static void main(String args[]) throws IOException {
final String APP_ROOT = "C:\\tmpxml\\CommentTest\\";
// File document = new File(APP_ROOT + "Comment.docx");
File document = new File(APP_ROOT + "Leer3.docx");
WordprocessingMLPackage mlPackage;
MainDocumentPart mDocPart;
try {
mlPackage = new WordprocessingMLPackage().load(new java.io.File(
document.getCanonicalPath()));
mDocPart = mlPackage.getMainDocumentPart();
CommentRel myComment = new CommentRel(mlPackage);
myComment.addNewComment("MC","Text");
// my
// FootnoteRel fnote1 = new FootnoteRel(mlPackage);
//
// fnote1.setFootnote("autor","test");
mlPackage.save(new File(APP_ROOT + "Leer3-OUT.docx"));
} catch (Docx4JException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Users browsing this forum: Bing [Bot] and 119 guests