source: trunk/docx4j/src/main/java/org/docx4j/samples/CommentsSample.java @ 1424

Revision 1424, 5.9 KB checked in by jharrop, 15 months ago (diff)

Basic comments example.

Line 
1/*
2 *  Copyright 2007-2008, Plutext Pty Ltd.
3 *   
4 *  This file is part of docx4j.
5
6    docx4j is licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18
19 */
20
21
22package org.docx4j.samples;
23
24import java.io.File;
25import java.math.BigInteger;
26import java.util.Calendar;
27
28import javax.xml.bind.JAXBContext;
29import javax.xml.bind.Marshaller;
30
31import org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator;
32import org.docx4j.jaxb.Context;
33import org.docx4j.jaxb.NamespacePrefixMapperUtils;
34import org.docx4j.model.table.TblFactory;
35import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
36import org.docx4j.openpackaging.parts.PartName;
37import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
38import org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart;
39import org.docx4j.openpackaging.contenttype.CTDefault;
40import org.docx4j.openpackaging.contenttype.ContentType;
41import org.docx4j.openpackaging.contenttype.ContentTypes;
42import org.docx4j.openpackaging.contenttype.ObjectFactory;
43import org.docx4j.openpackaging.exceptions.InvalidFormatException;
44import org.docx4j.openpackaging.io.SaveToZipFile;
45import org.docx4j.relationships.Relationship;
46import org.docx4j.wml.CTAltChunk;
47import org.docx4j.wml.Comments;
48import org.docx4j.wml.P;
49import org.docx4j.wml.Tbl;
50import org.docx4j.wml.Comments.Comment;
51
52/**
53 * Creates a WordprocessingML document from scratch, and adds a comment.
54 *
55 * Note that only w:commentReference is required; this example
56 * doesn't add w:commentRangeStart or w:commentRangeEnd
57 *
58          <w:p>
59            <w:commentRangeStart w:id="0"/>
60            <w:r>
61              <w:t>hello</w:t>
62            </w:r>
63            <w:commentRangeEnd w:id="0"/>
64            <w:r>
65              <w:rPr>
66                <w:rStyle w:val="CommentReference"/>
67              </w:rPr>
68              <w:commentReference w:id="0"/>
69            </w:r>
70          </w:p>
71 * 
72 *
73 * @author Jason Harrop
74 * @version 1.0
75 */
76public class CommentsSample extends AbstractSample {
77
78           static org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
79           
80           public static void main(String[] args) throws Exception {
81               
82               
83                try {
84                        getInputFilePath(args);
85                } catch (IllegalArgumentException e) {
86                inputfilepath = System.getProperty("user.dir") + "/Comments_out.docx";         
87                }
88               
89                boolean save = 
90                        (inputfilepath == null ? false : true);
91               
92                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
93
94                // Create and add a Comments Part
95              CommentsPart cp = new CommentsPart();
96              wordMLPackage.getMainDocumentPart().addTargetPart(cp);
97                     
98                // Part must have minimal contents
99                Comments comments = factory.createComments();
100                cp.setJaxbElement(comments);
101
102                // Add a comment to the comments part
103                java.math.BigInteger commentId = BigInteger.valueOf(0);
104                Comment theComment = createComment(commentId, "fred", null,
105                                "my first comment");
106                comments.getComment().add(theComment);
107               
108                // Add comment reference to document
109                P paraToCommentOn = wordMLPackage.getMainDocumentPart().addParagraphOfText("here is some content");
110                paraToCommentOn.getParagraphContent().add(createRunCommentReference(commentId));
111
112                // ++, for next comment ...
113                commentId = commentId.add(java.math.BigInteger.ONE);
114
115                // Now save it
116                if (save) {
117                        wordMLPackage.save(new java.io.File(inputfilepath) );
118                        System.out.println("Saved " + inputfilepath);
119                } else {
120                        // Create a org.docx4j.wml.Package object
121                        FlatOpcXmlCreator worker = new FlatOpcXmlCreator(wordMLPackage);
122                        org.docx4j.xmlPackage.Package pkg = worker.get();
123               
124                // Now marshall it
125                        JAXBContext jc = Context.jcXmlPackage;
126                        Marshaller marshaller=jc.createMarshaller();
127                       
128                        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
129                        NamespacePrefixMapperUtils.setProperty(marshaller, 
130                                        NamespacePrefixMapperUtils.getPrefixMapper());                 
131                        System.out.println( "\n\n OUTPUT " );
132                        System.out.println( "====== \n\n " );   
133                        marshaller.marshal(pkg, System.out);                           
134                       
135                }
136               
137                System.out.println("Done.");
138                               
139        }
140           
141
142            private static org.docx4j.wml.Comments.Comment createComment(java.math.BigInteger commentId,
143                        String author, Calendar date, String message) {
144
145                        org.docx4j.wml.Comments.Comment comment = factory.createCommentsComment();
146                        comment.setId( commentId );
147                        if (author!=null) {
148                                comment.setAuthor(author);
149                        }
150                        if (date!=null) {
151//                              String dateString = RFC3339_FORMAT.format(date.getTime()) ;     
152//                              comment.setDate(value)
153                                // TODO - at present this is XMLGregorianCalendar
154                        }
155                        org.docx4j.wml.P commentP = factory.createP();
156                        comment.getEGBlockLevelElts().add(commentP);
157                        org.docx4j.wml.R commentR = factory.createR();
158                        commentP.getParagraphContent().add(commentR);
159                        org.docx4j.wml.Text commentText = factory.createText();
160                        commentR.getRunContent().add(commentText);
161                       
162                        commentText.setValue(message);
163               
164                return comment;
165            }
166           
167            private static org.docx4j.wml.R createRunCommentReference(java.math.BigInteger commentId) {
168               
169                        org.docx4j.wml.R run = factory.createR();
170                        org.docx4j.wml.R.CommentReference commentRef = factory.createRCommentReference();
171                        run.getRunContent().add(commentRef);
172                        commentRef.setId( commentId ); 
173                       
174                        return run;
175               
176            }
177
178       
179}
Note: See TracBrowser for help on using the repository browser.