2017-06-12 13 views
3

Ich versuche, einen Kommentar zu einem MS Word-Dokument von Apache Poi Api hinzuzufügen.
ich Teil der Arbeit durch den Einsatz getan habe:Wie Kommentar von Apache Poi hinzufügen

CTMarkupRange commentStart = paragraph.getCTP().addNewCommentRangeStart(); 
commentStart.setId(BigInteger.ZERO); 
XWPFRun run = paragraph.createRun(); 
run.setText("text"); 

CTMarkupRange commentEnd = paragraph.getCTP().addNewCommentRangeEnd(); 
commentEnd.setId(BigInteger.ZERO); 

CTR ctr = paragraph.getCTP().addNewR(); 
CTMarkup ctMarkup = ctr.addNewCommentReference(); 
ctMarkup.setId(BigInteger.ZERO); 

Aber ich weiß nicht, wie es zu einem echten Kommentar zu verbinden, und ich finde nichts davon in api-Dokument.
Weiß jemand, wie man es löst?

Antwort

2

In einem Office OpenXML Word-Dokument (XWPF) sind Kommentare in einem speziellen CommentsDocument/word/comments.xml im * .docx ZIP-Archiv. Wir brauchen also zunächst Zugang zu diesem Dokument. Aber bis jetzt liest das XWPFdocument nur dieses Paketteil während des Erstellens. Es gibt weder Schreibzugriff noch eine Möglichkeit, dieses Paketteil zu erstellen.

Also müssen wir zunächst eine solche Möglichkeit bieten, das Paketteil /word/comments.xml im * .docx ZIP-Archiv anzulegen und Schreibzugriff darauf zu bekommen. Im folgenden Beispiel erstellt die Methode MyXWPFCommentsDocument createCommentsDocument(XWPFDocument document) den Paketteil /word/comments.xml und die Relationen dazu. Die Klasse MyXWPFCommentsDocument ist eine Wrapper-Klasse für diesen Paketteil mit Schreibzugriff darauf.

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(); 
    } 

} 
} 
+0

Es funktioniert perfekt und entsprechend der Lösung, andere Teil (commentsExtended.xml) des * .docx ZIP-Archivs könnte in der gleichen Weise erstellt werden.Vielen Dank für Ihre Antwort herzlichst –