You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXWPFComments.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xwpf.usermodel;
  16. import org.apache.poi.xwpf.XWPFTestDataSamples;
  17. import org.junit.jupiter.api.Test;
  18. import java.io.IOException;
  19. import java.math.BigInteger;
  20. import java.util.List;
  21. import static org.junit.jupiter.api.Assertions.*;
  22. class TestXWPFComments {
  23. @Test
  24. void testAddCommentsToDoc() throws IOException {
  25. BigInteger cId = BigInteger.ZERO;
  26. try (XWPFDocument docOut = new XWPFDocument()) {
  27. assertNull(docOut.getDocComments());
  28. // create comments
  29. XWPFComments comments = docOut.createComments();
  30. assertNotNull(comments);
  31. assertSame(comments, docOut.createComments());
  32. // create comment
  33. XWPFComment comment = comments.createComment(cId);
  34. comment.setAuthor("Author");
  35. comment.createParagraph().createRun().setText("comment paragraph");
  36. // apply comment to run text
  37. XWPFParagraph paragraph = docOut.createParagraph();
  38. paragraph.getCTP().addNewCommentRangeStart().setId(cId);
  39. paragraph.getCTP().addNewR().addNewT().setStringValue("HelloWorld");
  40. paragraph.getCTP().addNewCommentRangeEnd().setId(cId);
  41. paragraph.getCTP().addNewR().addNewCommentReference().setId(cId);
  42. // check
  43. XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(docOut);
  44. assertNotNull(docIn.getDocComments());
  45. assertEquals(1, docIn.getComments().length);
  46. comment = docIn.getCommentByID("0");
  47. assertNotNull(comment);
  48. assertEquals("Author", comment.getAuthor());
  49. }
  50. }
  51. @Test
  52. void testReadComments() throws IOException {
  53. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("testComment.docx")) {
  54. XWPFComments docComments = doc.getDocComments();
  55. assertNotNull(docComments);
  56. XWPFComment[] comments = doc.getComments();
  57. assertEquals(1, comments.length);
  58. List<XWPFPictureData> allPictures = docComments.getAllPictures();
  59. assertEquals(1, allPictures.size());
  60. }
  61. }
  62. }