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.

TestXWPFHeader.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotNull;
  18. import static org.junit.jupiter.api.Assertions.assertNull;
  19. import java.io.IOException;
  20. import org.apache.poi.xwpf.XWPFTestDataSamples;
  21. import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
  22. import org.junit.jupiter.api.Disabled;
  23. import org.junit.jupiter.api.Test;
  24. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
  25. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
  26. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
  27. final class TestXWPFHeader {
  28. @Test
  29. void testSimpleHeader() throws IOException {
  30. try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerFooter.docx")) {
  31. XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
  32. XWPFHeader header = policy.getDefaultHeader();
  33. XWPFFooter footer = policy.getDefaultFooter();
  34. assertNotNull(header);
  35. assertNotNull(footer);
  36. }
  37. }
  38. @Test
  39. void testImageInHeader() throws IOException {
  40. try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx")) {
  41. XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
  42. XWPFHeader header = policy.getDefaultHeader();
  43. assertNotNull(header.getRelations());
  44. assertEquals(1, header.getRelations().size());
  45. }
  46. }
  47. @Test
  48. void testSetHeader() throws IOException {
  49. try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx")) {
  50. // no header is set (yet)
  51. XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
  52. assertNull(policy.getDefaultHeader());
  53. assertNull(policy.getFirstPageHeader());
  54. assertNull(policy.getDefaultFooter());
  55. assertNull(policy.getFirstPageFooter());
  56. CTP ctP1 = CTP.Factory.newInstance();
  57. CTR ctR1 = ctP1.addNewR();
  58. CTText t = ctR1.addNewT();
  59. String tText = "Paragraph in header";
  60. t.setStringValue(tText);
  61. // Commented MB 23 May 2010
  62. //CTP ctP2 = CTP.Factory.newInstance();
  63. //CTR ctR2 = ctP2.addNewR();
  64. //CTText t2 = ctR2.addNewT();
  65. //t2.setStringValue("Second paragraph.. for footer");
  66. // Create two paragraphs for insertion into the footer.
  67. // Previously only one was inserted MB 23 May 2010
  68. CTP ctP2 = CTP.Factory.newInstance();
  69. CTR ctR2 = ctP2.addNewR();
  70. CTText t2 = ctR2.addNewT();
  71. t2.setStringValue("First paragraph for the footer");
  72. CTP ctP3 = CTP.Factory.newInstance();
  73. CTR ctR3 = ctP3.addNewR();
  74. CTText t3 = ctR3.addNewT();
  75. t3.setStringValue("Second paragraph for the footer");
  76. XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc);
  77. XWPFParagraph[] pars = new XWPFParagraph[1];
  78. pars[0] = p1;
  79. XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc);
  80. XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc);
  81. XWPFParagraph[] pars2 = new XWPFParagraph[2];
  82. pars2[0] = p2;
  83. pars2[1] = p3;
  84. // Set headers
  85. XWPFHeader headerD = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
  86. XWPFHeader headerF = policy.createHeader(XWPFHeaderFooterPolicy.FIRST);
  87. // Set a default footer and capture the returned XWPFFooter object.
  88. XWPFFooter footerD = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars2);
  89. XWPFFooter footerF = policy.createFooter(XWPFHeaderFooterPolicy.FIRST);
  90. // Ensure the headers and footer were set correctly....
  91. assertNotNull(policy.getDefaultHeader());
  92. assertNotNull(policy.getFirstPageHeader());
  93. assertNotNull(policy.getDefaultFooter());
  94. assertNotNull(policy.getFirstPageFooter());
  95. // ....and that the footer object captured above contains two
  96. // paragraphs of text.
  97. assertEquals(2, footerD.getParagraphs().size());
  98. assertEquals(0, footerF.getParagraphs().size());
  99. // Check the header created with the paragraph got them, and the one
  100. // created without got none
  101. assertEquals(1, headerD.getParagraphs().size());
  102. assertEquals(tText, headerD.getParagraphs().get(0).getText());
  103. assertEquals(0, headerF.getParagraphs().size());
  104. // As an additional check, recover the defaults footer and
  105. // make sure that it contains two paragraphs of text and that
  106. // both do hold what is expected.
  107. footerD = policy.getDefaultFooter();
  108. XWPFParagraph[] paras = footerD.getParagraphs().toArray(new XWPFParagraph[0]);
  109. assertEquals(2, paras.length);
  110. assertEquals("First paragraph for the footer", paras[0].getText());
  111. assertEquals("Second paragraph for the footer", paras[1].getText());
  112. // Add some text to the empty header
  113. String fText1 = "New Text!";
  114. String fText2 = "More Text!";
  115. headerF.createParagraph().insertNewRun(0).setText(fText1);
  116. headerF.createParagraph().insertNewRun(0).setText(fText2);
  117. // headerF.getParagraphs().get(0).insertNewRun(0).setText(fText1);
  118. // Check it
  119. assertEquals(tText, headerD.getParagraphs().get(0).getText());
  120. assertEquals(fText1, headerF.getParagraphs().get(0).getText());
  121. assertEquals(fText2, headerF.getParagraphs().get(1).getText());
  122. // Save, re-open, ensure it's all still there
  123. XWPFDocument reopened = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
  124. policy = reopened.getHeaderFooterPolicy();
  125. assertNotNull(policy.getDefaultHeader());
  126. assertNotNull(policy.getFirstPageHeader());
  127. assertNull(policy.getEvenPageHeader());
  128. assertNotNull(policy.getDefaultFooter());
  129. assertNotNull(policy.getFirstPageFooter());
  130. assertNull(policy.getEvenPageFooter());
  131. // Check the new headers still have their text
  132. headerD = policy.getDefaultHeader();
  133. headerF = policy.getFirstPageHeader();
  134. assertEquals(tText, headerD.getParagraphs().get(0).getText());
  135. assertEquals(fText1, headerF.getParagraphs().get(0).getText());
  136. assertEquals(fText2, headerF.getParagraphs().get(1).getText());
  137. // Check the new footers have their new text too
  138. footerD = policy.getDefaultFooter();
  139. paras = footerD.getParagraphs().toArray(new XWPFParagraph[0]);
  140. footerF = policy.getFirstPageFooter();
  141. assertEquals(2, paras.length);
  142. assertEquals("First paragraph for the footer", paras[0].getText());
  143. assertEquals("Second paragraph for the footer", paras[1].getText());
  144. assertEquals(1, footerF.getParagraphs().size());
  145. }
  146. }
  147. @Test
  148. void testSetWatermark() throws IOException {
  149. try (XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx")) {
  150. // No header is set (yet)
  151. XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
  152. assertNull(policy.getDefaultHeader());
  153. assertNull(policy.getFirstPageHeader());
  154. assertNull(policy.getDefaultFooter());
  155. policy.createWatermark("DRAFT");
  156. assertNotNull(policy.getDefaultHeader());
  157. assertNotNull(policy.getFirstPageHeader());
  158. assertNotNull(policy.getEvenPageHeader());
  159. // Re-open, and check
  160. XWPFDocument reopened = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
  161. policy = reopened.getHeaderFooterPolicy();
  162. assertNotNull(policy.getDefaultHeader());
  163. assertNotNull(policy.getFirstPageHeader());
  164. assertNotNull(policy.getEvenPageHeader());
  165. }
  166. }
  167. @Test
  168. void testSetWatermarkOnEmptyDoc() throws IOException {
  169. try (XWPFDocument sampleDoc = new XWPFDocument()) {
  170. // No header is set (yet)
  171. XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
  172. assertNull(policy);
  173. policy = sampleDoc.createHeaderFooterPolicy();
  174. policy.createWatermark("DRAFT");
  175. assertNotNull(policy.getDefaultHeader());
  176. assertNotNull(policy.getFirstPageHeader());
  177. assertNotNull(policy.getEvenPageHeader());
  178. // Re-open, and check
  179. XWPFDocument reopened = XWPFTestDataSamples.writeOutAndReadBack(sampleDoc);
  180. policy = reopened.getHeaderFooterPolicy();
  181. assertNotNull(policy.getDefaultHeader());
  182. assertNotNull(policy.getFirstPageHeader());
  183. assertNotNull(policy.getEvenPageHeader());
  184. }
  185. }
  186. @Disabled
  187. void testAddPictureData() {
  188. // TODO
  189. }
  190. @Disabled
  191. void testGetAllPictures() {
  192. // TODO
  193. }
  194. @Disabled
  195. void testGetAllPackagePictures() {
  196. // TODO
  197. }
  198. @Disabled
  199. void testGetPictureDataById() {
  200. // TODO
  201. }
  202. @Test
  203. void bug60293() throws Exception {
  204. //test handling of non-standard header/footer options
  205. try (XWPFDocument xwpf = XWPFTestDataSamples.openSampleDocument("60293.docx")) {
  206. assertEquals(3, xwpf.getHeaderList().size());
  207. }
  208. }
  209. }