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.

XWPFFooter.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import javax.xml.namespace.QName;
  21. import org.apache.poi.ooxml.POIXMLDocumentPart;
  22. import org.apache.poi.ooxml.POIXMLException;
  23. import org.apache.poi.openxml4j.opc.PackagePart;
  24. import org.apache.xmlbeans.XmlCursor;
  25. import org.apache.xmlbeans.XmlObject;
  26. import org.apache.xmlbeans.XmlOptions;
  27. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtr;
  28. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering;
  29. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
  30. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
  31. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
  32. import org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument;
  33. /**
  34. * Sketch of XWPF footer class
  35. */
  36. public class XWPFFooter extends XWPFHeaderFooter {
  37. public XWPFFooter() {
  38. super();
  39. }
  40. public XWPFFooter(XWPFDocument doc, CTHdrFtr hdrFtr) throws IOException {
  41. super(doc, hdrFtr);
  42. XmlCursor cursor = headerFooter.newCursor();
  43. cursor.selectPath("./*");
  44. while (cursor.toNextSelection()) {
  45. XmlObject o = cursor.getObject();
  46. if (o instanceof CTP) {
  47. XWPFParagraph p = new XWPFParagraph((CTP) o, this);
  48. paragraphs.add(p);
  49. bodyElements.add(p);
  50. }
  51. if (o instanceof CTTbl) {
  52. XWPFTable t = new XWPFTable((CTTbl) o, this);
  53. tables.add(t);
  54. bodyElements.add(t);
  55. }
  56. }
  57. cursor.dispose();
  58. }
  59. /**
  60. * @since POI 3.14-Beta1
  61. */
  62. public XWPFFooter(POIXMLDocumentPart parent, PackagePart part) throws IOException {
  63. super(parent, part);
  64. }
  65. /**
  66. * save and commit footer
  67. */
  68. @Override
  69. protected void commit() throws IOException {
  70. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  71. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "ftr"));
  72. PackagePart part = getPackagePart();
  73. OutputStream out = part.getOutputStream();
  74. super._getHdrFtr().save(out, xmlOptions);
  75. out.close();
  76. }
  77. @Override
  78. protected void onDocumentRead() throws IOException {
  79. super.onDocumentRead();
  80. FtrDocument ftrDocument;
  81. try (InputStream is = getPackagePart().getInputStream()) {
  82. ftrDocument = FtrDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
  83. headerFooter = ftrDocument.getFtr();
  84. // parse the document with cursor and add
  85. // the XmlObject to its lists
  86. XmlCursor cursor = headerFooter.newCursor();
  87. cursor.selectPath("./*");
  88. while (cursor.toNextSelection()) {
  89. XmlObject o = cursor.getObject();
  90. if (o instanceof CTP) {
  91. XWPFParagraph p = new XWPFParagraph((CTP) o, this);
  92. paragraphs.add(p);
  93. bodyElements.add(p);
  94. }
  95. if (o instanceof CTTbl) {
  96. XWPFTable t = new XWPFTable((CTTbl) o, this);
  97. tables.add(t);
  98. bodyElements.add(t);
  99. }
  100. if (o instanceof CTSdtBlock) {
  101. XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
  102. bodyElements.add(c);
  103. }
  104. }
  105. cursor.dispose();
  106. } catch (Exception e) {
  107. throw new POIXMLException(e);
  108. }
  109. }
  110. /**
  111. * get the PartType of the body
  112. *
  113. * @see org.apache.poi.xwpf.usermodel.IBody#getPartType()
  114. */
  115. @Override
  116. public BodyType getPartType() {
  117. return BodyType.FOOTER;
  118. }
  119. }