在Office OpenXML Word文档(
XWPF)中,注释是
Commentsdocument
/word/comments.xml*
.docx
ZIP存档中的特殊内容。因此,首先我们需要访问该文档。但是直到现在,它们
XWPFdocument只会在创建时读取该软件包的一部分。没有写访问权限,也没有创建该程序包部件的可能性。
因此,我们首先必须提供这样一种可能性,以便
/word/comments.xml在* .docx ZIP归档文件中创建程序包部件并对其进行写访问。
在下面的示例中,该方法
MyXWPFCommentsdocument createCommentsdocument(XWPFdocumentdocument)创建包装零件
/word/comments.xml及其相关关系。该类
MyXWPFCommentsdocument是对该包装部件具有写访问权的包装器类。
import java.io.*;import org.apache.poi.*;import org.apache.poi.openxml4j.opc.*;import org.apache.xmlbeans.*;import org.apache.poi.xwpf.usermodel.*;import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;import javax.xml.namespace.QName;import java.math.BigInteger;import java.util.GregorianCalendar;import java.util.Locale;public class CreateWordWithComments {//a method for creating the Commentsdocument /word/comments.xml in the *.docx ZIP archive private static MyXWPFCommentsdocument createCommentsdocument(XWPFdocument document) throws Exception { OPCPackage oPCPackage = document.getPackage(); PackagePartName partName = PackagingURIHelper.createPartName("/word/comments.xml"); PackagePart part = oPCPackage.createPart(partName, "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"); MyXWPFCommentsdocument myXWPFCommentsdocument = new MyXWPFCommentsdocument(part); String rId = "rId" + (document.getRelationParts().size()+1); document.addRelation(rId, XWPFRelation.COMMENT, myXWPFCommentsdocument); return myXWPFCommentsdocument; } public static void main(String[] args) throws Exception { XWPFdocument document = new XWPFdocument(); MyXWPFCommentsdocument myXWPFCommentsdocument = createCommentsdocument(document); CTComments comments = myXWPFCommentsdocument.getComments(); CTComment ctComment; XWPFParagraph paragraph; XWPFRun run;//first comment BigInteger cId = BigInteger.ZERO; ctComment = comments.addNewComment(); ctComment.setAuthor("Axel Ríchter"); ctComment.setInitials("AR"); ctComment.setDate(new GregorianCalendar(Locale.US)); ctComment.addNewP().addNewR().addNewT().setStringValue("The first comment."); ctComment.setId(cId); paragraph = document.createParagraph(); paragraph.getCTP().addNewCommentRangeStart().setId(cId); run = paragraph.createRun(); run.setText("Paragraph with the first comment."); paragraph.getCTP().addNewCommentRangeEnd().setId(cId); paragraph.getCTP().addNewR().addNewCommentReference().setId(cId);//paragraph without comment paragraph = document.createParagraph(); run = paragraph.createRun(); run.setText("Paragraph without comment.");//second comment cId = cId.add(BigInteger.ONE); ctComment = comments.addNewComment(); ctComment.setAuthor("Axel Ríchter"); ctComment.setInitials("AR"); ctComment.setDate(new GregorianCalendar(Locale.US)); ctComment.addNewP().addNewR().addNewT().setStringValue("The second comment."); ctComment.setId(cId); paragraph = document.createParagraph(); paragraph.getCTP().addNewCommentRangeStart().setId(cId); run = paragraph.createRun(); run.setText("Paragraph with the second comment."); paragraph.getCTP().addNewCommentRangeEnd().setId(cId); paragraph.getCTP().addNewR().addNewCommentReference().setId(cId);//write document document.write(new FileOutputStream("CreateWordWithComments.docx")); document.close(); }//a wrapper class for the Commentsdocument /word/comments.xml in the *.docx ZIP archive private static class MyXWPFCommentsdocument extends POIXMLdocumentPart { private CTComments comments; private MyXWPFCommentsdocument(PackagePart part) throws Exception { super(part); comments = Commentsdocument.Factory.newInstance().addNewComments(); } private CTComments getComments() { return comments; } @Override protected void commit() throws IOException { XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); xmlOptions.setSaveSyntheticdocumentElement(new QName(CTComments.type.getName().getNamespaceURI(), "comments")); PackagePart part = getPackagePart(); OutputStream out = part.getOutputStream(); comments.save(out, xmlOptions); out.close(); } }}这适用于
apache poi 3.17。由于
apache poi 4.0.0该
ooxml部分被分开。因此必须有:
...import org.apache.poi.ooxml.*;...import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;...



