关于插入新的引用文件的思考

在POIXMLDocumentPart类中,存在可以获取文档引用信息的方法,并且通过api建立引用文件好像也是可以实现的,而有关这个功能的的实现就需要依靠下面这个方法:

  1. private void addRelation(PackageRelationship pr, POIXMLDocumentPart part) {
  2. this.relations.put(pr.getId(), new POIXMLDocumentPart.RelationPart(pr, part));
  3. part.incrementRelationCounter();
  4. }

我们先查看一下输入文档原引用信息:

  1. POIXMLDocumentPart poixmlDocumentPart = xwpfDocument.getPart();
  2. List<POIXMLDocumentPart.RelationPart> relationParts = poixmlDocumentPart.getRelationParts();
  3. for(POIXMLDocumentPart.RelationPart relationPart : relationParts){
  4. System.out.println("引用id:"+relationPart.getRelationship().getId());
  5. System.out.println("引用类型:"+relationPart.getRelationship().getRelationshipType());
  6. System.out.println("引用文件指向地址:"+relationPart.getRelationship().getTargetURI().getPath());
  7. System.out.println("被引用地址:"+relationPart.getRelationship().getSourceURI().getPath());
  8. System.out.println(relationPart.getDocumentPart());
  9. System.out.println();
  10. }

打印,控制台显示结果摘录如下:

  1. 开始展示引用文件信息:
  2. 引用idrId1
  3. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml
  4. 引用文件指向地址:/customXml/item1.xml
  5. 被引用地址:/word/document.xml
  6. Name: /customXml/item1.xml - Content Type: application/xml
  7. 引用idrId10
  8. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  9. 引用文件指向地址:/word/footer2.xml
  10. 被引用地址:/word/document.xml
  11. Name: /word/footer2.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  12. 引用idrId11
  13. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  14. 引用文件指向地址:/word/header2.xml
  15. 被引用地址:/word/document.xml
  16. Name: /word/header2.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  17. 引用idrId12
  18. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  19. 引用文件指向地址:/word/footer3.xml
  20. 被引用地址:/word/document.xml
  21. Name: /word/footer3.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  22. 引用idrId13
  23. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  24. 引用文件指向地址:/word/header3.xml
  25. 被引用地址:/word/document.xml
  26. Name: /word/header3.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  27. 引用idrId14
  28. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  29. 引用文件指向地址:/word/footer4.xml
  30. 被引用地址:/word/document.xml
  31. Name: /word/footer4.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  32. 引用idrId15
  33. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/image
  34. 引用文件指向地址:/word/media/image1.png
  35. 被引用地址:/word/document.xml
  36. Name: /word/media/image1.png - Content Type: image/png
  37. 引用idrId16
  38. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  39. 引用文件指向地址:/word/header4.xml
  40. 被引用地址:/word/document.xml
  41. Name: /word/header4.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  42. 引用idrId17
  43. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  44. 引用文件指向地址:/word/footer5.xml
  45. 被引用地址:/word/document.xml
  46. Name: /word/footer5.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  47. 引用idrId18
  48. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  49. 引用文件指向地址:/word/header5.xml
  50. 被引用地址:/word/document.xml
  51. Name: /word/header5.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  52. 引用idrId19
  53. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  54. 引用文件指向地址:/word/header6.xml
  55. 被引用地址:/word/document.xml
  56. Name: /word/header6.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  57. 引用idrId2
  58. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering
  59. 引用文件指向地址:/word/numbering.xml
  60. 被引用地址:/word/document.xml
  61. Name: /word/numbering.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml
  62. 引用idrId20
  63. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  64. 引用文件指向地址:/word/footer6.xml
  65. 被引用地址:/word/document.xml
  66. Name: /word/footer6.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  67. 引用idrId21
  68. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable
  69. 引用文件指向地址:/word/fontTable.xml
  70. 被引用地址:/word/document.xml
  71. Name: /word/fontTable.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml
  72. 引用idrId22
  73. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme
  74. 引用文件指向地址:/word/theme/theme1.xml
  75. 被引用地址:/word/document.xml
  76. Name: /word/theme/theme1.xml - Content Type: application/vnd.openxmlformats-officedocument.theme+xml
  77. 引用idrId3
  78. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles
  79. 引用文件指向地址:/word/styles.xml
  80. 被引用地址:/word/document.xml
  81. Name: /word/styles.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml
  82. 引用idrId4
  83. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings
  84. 引用文件指向地址:/word/settings.xml
  85. 被引用地址:/word/document.xml
  86. Name: /word/settings.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml
  87. 引用idrId5
  88. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings
  89. 引用文件指向地址:/word/webSettings.xml
  90. 被引用地址:/word/document.xml
  91. Name: /word/webSettings.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml
  92. 引用idrId6
  93. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes
  94. 引用文件指向地址:/word/footnotes.xml
  95. 被引用地址:/word/document.xml
  96. Name: /word/footnotes.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml
  97. 引用idrId7
  98. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes
  99. 引用文件指向地址:/word/endnotes.xml
  100. 被引用地址:/word/document.xml
  101. Name: /word/endnotes.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml
  102. 引用idrId8
  103. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  104. 引用文件指向地址:/word/header1.xml
  105. 被引用地址:/word/document.xml
  106. Name: /word/header1.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  107. 引用idrId9
  108. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  109. 引用文件指向地址:/word/footer1.xml
  110. 被引用地址:/word/document.xml
  111. Name: /word/footer1.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  112. 开始展示引用文件信息:
  113. 引用idrId1
  114. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml
  115. 引用文件指向地址:/customXml/item1.xml
  116. 被引用地址:/word/document.xml
  117. Name: /customXml/item1.xml - Content Type: application/xml
  118. 引用idrId10
  119. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  120. 引用文件指向地址:/word/footer2.xml
  121. 被引用地址:/word/document.xml
  122. Name: /word/footer2.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  123. 引用idrId11
  124. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  125. 引用文件指向地址:/word/header2.xml
  126. 被引用地址:/word/document.xml
  127. Name: /word/header2.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  128. 引用idrId12
  129. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  130. 引用文件指向地址:/word/footer3.xml
  131. 被引用地址:/word/document.xml
  132. Name: /word/footer3.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  133. 引用idrId13
  134. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  135. 引用文件指向地址:/word/header3.xml
  136. 被引用地址:/word/document.xml
  137. Name: /word/header3.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  138. 引用idrId14
  139. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  140. 引用文件指向地址:/word/footer4.xml
  141. 被引用地址:/word/document.xml
  142. Name: /word/footer4.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  143. 引用idrId15
  144. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/image
  145. 引用文件指向地址:/word/media/image1.png
  146. 被引用地址:/word/document.xml
  147. Name: /word/media/image1.png - Content Type: image/png
  148. 引用idrId16
  149. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  150. 引用文件指向地址:/word/header4.xml
  151. 被引用地址:/word/document.xml
  152. Name: /word/header4.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  153. 引用idrId17
  154. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  155. 引用文件指向地址:/word/footer5.xml
  156. 被引用地址:/word/document.xml
  157. Name: /word/footer5.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  158. 引用idrId18
  159. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  160. 引用文件指向地址:/word/header5.xml
  161. 被引用地址:/word/document.xml
  162. Name: /word/header5.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  163. 引用idrId19
  164. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  165. 引用文件指向地址:/word/header6.xml
  166. 被引用地址:/word/document.xml
  167. Name: /word/header6.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  168. 引用idrId2
  169. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering
  170. 引用文件指向地址:/word/numbering.xml
  171. 被引用地址:/word/document.xml
  172. Name: /word/numbering.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml
  173. 引用idrId20
  174. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  175. 引用文件指向地址:/word/footer6.xml
  176. 被引用地址:/word/document.xml
  177. Name: /word/footer6.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
  178. 引用idrId21
  179. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable
  180. 引用文件指向地址:/word/fontTable.xml
  181. 被引用地址:/word/document.xml
  182. Name: /word/fontTable.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml
  183. 引用idrId22
  184. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme
  185. 引用文件指向地址:/word/theme/theme1.xml
  186. 被引用地址:/word/document.xml
  187. Name: /word/theme/theme1.xml - Content Type: application/vnd.openxmlformats-officedocument.theme+xml
  188. 引用idrId3
  189. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles
  190. 引用文件指向地址:/word/styles.xml
  191. 被引用地址:/word/document.xml
  192. Name: /word/styles.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml
  193. 引用idrId4
  194. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings
  195. 引用文件指向地址:/word/settings.xml
  196. 被引用地址:/word/document.xml
  197. Name: /word/settings.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml
  198. 引用idrId5
  199. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings
  200. 引用文件指向地址:/word/webSettings.xml
  201. 被引用地址:/word/document.xml
  202. Name: /word/webSettings.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml
  203. 引用idrId6
  204. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes
  205. 引用文件指向地址:/word/footnotes.xml
  206. 被引用地址:/word/document.xml
  207. Name: /word/footnotes.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml
  208. 引用idrId7
  209. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes
  210. 引用文件指向地址:/word/endnotes.xml
  211. 被引用地址:/word/document.xml
  212. Name: /word/endnotes.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml
  213. 引用idrId8
  214. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header
  215. 引用文件指向地址:/word/header1.xml
  216. 被引用地址:/word/document.xml
  217. Name: /word/header1.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml
  218. 引用idrId9
  219. 引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer
  220. 引用文件指向地址:/word/footer1.xml
  221. 被引用地址:/word/document.xml
  222. Name: /word/footer1.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml

因此,如果我们要构建一个页眉页脚引用文件,那么首先得补齐这些信息

另外在查看PackageRelationship类的源码时发现,引用文件的路径应该使用相对路径,使用java.net.URI类进行包装,例如此类的静态代码块:

  1. static {
  2. try {
  3. containerRelationshipPart = new URI("/_rels/.rels");
  4. } catch (URISyntaxException var1) {
  5. }

其类PackageRelationship属性如下:

  1. public final class PackageRelationship {
  2. private static URI containerRelationshipPart;
  3. public static final String ID_ATTRIBUTE_NAME = "Id";
  4. public static final String RELATIONSHIPS_TAG_NAME = "Relationships";
  5. public static final String RELATIONSHIP_TAG_NAME = "Relationship";
  6. public static final String TARGET_ATTRIBUTE_NAME = "Target";
  7. public static final String TARGET_MODE_ATTRIBUTE_NAME = "TargetMode";
  8. public static final String TYPE_ATTRIBUTE_NAME = "Type";
  9. private String id;
  10. private OPCPackage container;
  11. private String relationshipType;
  12. private PackagePart source;
  13. private TargetMode targetMode;
  14. private URI targetUri;

构造方法如下:

  1. public PackageRelationship(OPCPackage pkg, PackagePart sourcePart, URI targetUri, TargetMode targetMode, String relationshipType, String id) {
  2. if (pkg == null) {
  3. throw new IllegalArgumentException("pkg");
  4. } else if (targetUri == null) {
  5. throw new IllegalArgumentException("targetUri");
  6. } else if (relationshipType == null) {
  7. throw new IllegalArgumentException("relationshipType");
  8. } else if (id == null) {
  9. throw new IllegalArgumentException("id");
  10. } else {
  11. this.container = pkg;
  12. this.source = sourcePart;
  13. this.targetUri = targetUri;
  14. this.targetMode = targetMode;
  15. this.relationshipType = relationshipType;
  16. this.id = id;
  17. }
  18. }

在获取当前这个引用对象的元数据包时,可以结合属性

  1. private OPCPackage container;

调用方法

  1. public OPCPackage getPackage() {
  2. return this.container;
  3. }

另外一个未知属性sourcePart在当前类中并没有被使用上,因此暂时不知道是什么

在这里遭遇一个令人窒息的逻辑闭环:
在rel.rel文件中放入新的引用信息 -> 需要提供引用对象 -> 创建引用对象需要该引用文件在此文件中的相对地址->但是要插入的文件根本就还没有放进去???-> 不能在rel文件中插入新的引用信息

所以,在将引用文件插入到rel.rel文件之前,应该先把这个引用文件添加进文档压缩包里面去

清除整个引用文件

  1. xwpfDocument.getPart().getPackagePart().clearRelationships();

即删除了document.xml.rels文件,但是被引用的诸如页眉页脚信息依旧存在
偶然发现poi底层使用了大量的集合数据结构

如何构建一个页眉页脚文件

再一次偶然中,我发现了一种新的类型:

  1. HdrDocument hdrDocument = null;
  2. try {
  3. hdrDocument = HdrDocument.Factory.parse(new File("E:\\header1.xml"));
  4. } catch (XmlException e) {
  5. e.printStackTrace();
  6. } catch (IOException e) {
  7. e.printStackTrace();
  8. }

HdrDocument类和页眉页脚文件:

  1. <?xml version="1.0" encoding="utf-8" standalone="yes"?>
  2. <w:hdr xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  3. <w:p>
  4. <w:pPr>
  5. <w:jc w:val="right" />
  6. </w:pPr>
  7. <w:r>
  8. <w:rPr>
  9. <w:b />
  10. <w:color w:val="0000FF" />
  11. <w:sz w:val="30" />
  12. </w:rPr>
  13. <w:t xml:space="preserve">偶数页页眉</w:t>
  14. </w:r>
  15. </w:p>
  16. </w:hdr>

第一个标签的前缀是吻合的,那么可以推断出,这个就是我们想找的页眉页脚文件的对象。

但是接下来一个问题是:找到了可以存放的对象,但是怎么把这个对象加载进目前的文档中呢

如何利用已知uri路径从引用文件中提取到对应的文件

此处从PackageRelationship先入手,其中有一个静态区块:

  1. static {
  2. try {
  3. containerRelationshipPart = new URI("/_rels/.rels");
  4. } catch (URISyntaxException var1) {
  5. }
  6. }

在poi中有一个类:XWPFRelation

  1. public final class XWPFRelation extends POIXMLRelation {
  2. private static final Map<String, XWPFRelation> _table = new HashMap();
  3. public static final XWPFRelation DOCUMENT = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "/word/document.xml");
  4. public static final XWPFRelation TEMPLATE = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "/word/document.xml");
  5. public static final XWPFRelation MACRO_DOCUMENT = new XWPFRelation("application/vnd.ms-word.document.macroEnabled.main+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "/word/document.xml");
  6. public static final XWPFRelation MACRO_TEMPLATE_DOCUMENT = new XWPFRelation("application/vnd.ms-word.template.macroEnabledTemplate.main+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument", "/word/document.xml");
  7. public static final XWPFRelation GLOSSARY_DOCUMENT = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.document.glossary+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument", "/word/glossary/document.xml");
  8. public static final XWPFRelation NUMBERING = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering", "/word/numbering.xml", XWPFNumbering::new, XWPFNumbering::new);
  9. public static final XWPFRelation FONT_TABLE = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable", "/word/fontTable.xml");
  10. public static final XWPFRelation SETTINGS = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings", "/word/settings.xml", XWPFSettings::new, XWPFSettings::new);
  11. public static final XWPFRelation STYLES = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "/word/styles.xml", XWPFStyles::new, XWPFStyles::new);
  12. public static final XWPFRelation WEB_SETTINGS = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings", "/word/webSettings.xml");
  13. public static final XWPFRelation HEADER = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header", "/word/header#.xml", XWPFHeader::new, XWPFHeader::new);
  14. public static final XWPFRelation FOOTER = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer", "/word/footer#.xml", XWPFFooter::new, XWPFFooter::new);
  15. public static final XWPFRelation THEME = new XWPFRelation("application/vnd.openxmlformats-officedocument.theme+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme", "/word/theme/theme#.xml");
  16. public static final XWPFRelation WORKBOOK = new XWPFRelation("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/package", "/word/embeddings/Microsoft_Excel_Worksheet#.xlsx", XSSFWorkbook::new, XSSFWorkbook::new);
  17. public static final XWPFRelation CHART = new XWPFRelation("application/vnd.openxmlformats-officedocument.drawingml.chart+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart", "/word/charts/chart#.xml", XWPFChart::new, XWPFChart::new);
  18. public static final XWPFRelation HYPERLINK = new XWPFRelation((String)null, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", (String)null);
  19. public static final XWPFRelation COMMENT = new XWPFRelation((String)null, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments", (String)null);
  20. public static final XWPFRelation FOOTNOTE = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes", "/word/footnotes.xml", XWPFFootnotes::new, XWPFFootnotes::new);
  21. public static final XWPFRelation ENDNOTE = new XWPFRelation("application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes", "/word/endnotes.xml", XWPFEndnotes::new, XWPFEndnotes::new);
  22. public static final XWPFRelation IMAGE_EMF = new XWPFRelation("image/x-emf", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.emf", XWPFPictureData::new, XWPFPictureData::new);
  23. public static final XWPFRelation IMAGE_WMF = new XWPFRelation("image/x-wmf", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.wmf", XWPFPictureData::new, XWPFPictureData::new);
  24. public static final XWPFRelation IMAGE_PICT = new XWPFRelation("image/pict", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.pict", XWPFPictureData::new, XWPFPictureData::new);
  25. public static final XWPFRelation IMAGE_JPEG = new XWPFRelation("image/jpeg", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.jpeg", XWPFPictureData::new, XWPFPictureData::new);
  26. public static final XWPFRelation IMAGE_PNG = new XWPFRelation("image/png", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.png", XWPFPictureData::new, XWPFPictureData::new);
  27. public static final XWPFRelation IMAGE_DIB = new XWPFRelation("image/dib", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.dib", XWPFPictureData::new, XWPFPictureData::new);
  28. public static final XWPFRelation IMAGE_GIF = new XWPFRelation("image/gif", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.gif", XWPFPictureData::new, XWPFPictureData::new);
  29. public static final XWPFRelation IMAGE_TIFF = new XWPFRelation("image/tiff", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.tiff", XWPFPictureData::new, XWPFPictureData::new);
  30. public static final XWPFRelation IMAGE_EPS = new XWPFRelation("image/x-eps", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.eps", XWPFPictureData::new, XWPFPictureData::new);
  31. public static final XWPFRelation IMAGE_BMP = new XWPFRelation("image/x-ms-bmp", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.bmp", XWPFPictureData::new, XWPFPictureData::new);
  32. public static final XWPFRelation IMAGE_WPG = new XWPFRelation("image/x-wpg", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "/word/media/image#.wpg", XWPFPictureData::new, XWPFPictureData::new);
  33. public static final XWPFRelation IMAGES = new XWPFRelation((String)null, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", (String)null, XWPFPictureData::new, XWPFPictureData::new);

使用下面这段代码可以获取到指定引用信息中的内容

  1. List<POIXMLDocumentPart.RelationPart> relationParts = xwpfDocument.getRelationParts();
  2. for(POIXMLDocumentPart.RelationPart relationPart : relationParts){
  3. if(relationPart.getRelationship().getRelationshipType().equals(XWPFRelation.FOOTER.getRelation())){
  4. System.out.println("找到了一个页脚");
  5. System.out.println("引用文件指向地址:"+relationPart.getRelationship().getTargetURI().getPath());
  6. // String target = relationPart.getRelationship().getTargetURI().getPath();
  7. // 利用得到的地址创建一个URI
  8. /* try {
  9. URI pFooterPath = new URI(relationPart.getRelationship().getTargetURI().getPath());
  10. } catch (URISyntaxException e) {
  11. e.printStackTrace();
  12. }*/
  13. XWPFFooter footer = relationPart.getDocumentPart();
  14. System.out.println(footer.getParagraphs().get(0).getText());
  15. }
  16. }

此处写法参考POI中的XWPFDoucment对象中的文档构建方法

  1. protected void onDocumentRead() throws IOException {
  2. try {
  3. DocumentDocument doc = Factory.parse(this.getPackagePart().getInputStream(), POIXMLTypeLoader.DEFAULT_XML_OPTIONS);
  4. this.ctDocument = doc.getDocument();
  5. this.initFootnotes();
  6. XmlCursor docCursor = this.ctDocument.newCursor();
  7. docCursor.selectPath("./*");
  8. while(true) {
  9. XmlObject o;
  10. do {
  11. if (!docCursor.toNextSelection()) {
  12. docCursor.dispose();
  13. if (doc.getDocument().getBody().getSectPr() != null) {
  14. this.headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
  15. }
  16. Iterator var13 = this.getRelationParts().iterator();
  17. while(true) {
  18. while(var13.hasNext()) {
  19. RelationPart rp = (RelationPart)var13.next();
  20. POIXMLDocumentPart p = rp.getDocumentPart();
  21. String relation = rp.getRelationship().getRelationshipType();
  22. if (relation.equals(XWPFRelation.STYLES.getRelation())) {
  23. this.styles = (XWPFStyles)p;
  24. this.styles.onDocumentRead();
  25. } else if (relation.equals(XWPFRelation.NUMBERING.getRelation())) {
  26. this.numbering = (XWPFNumbering)p;
  27. this.numbering.onDocumentRead();
  28. } else if (relation.equals(XWPFRelation.FOOTER.getRelation())) {
  29. XWPFFooter footer = (XWPFFooter)p;
  30. this.footers.add(footer);
  31. footer.onDocumentRead();
  32. } else if (relation.equals(XWPFRelation.HEADER.getRelation())) {
  33. XWPFHeader header = (XWPFHeader)p;
  34. this.headers.add(header);
  35. header.onDocumentRead();
  36. } else if (relation.equals(XWPFRelation.COMMENT.getRelation())) {
  37. CommentsDocument cmntdoc = org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument.Factory.parse(p.getPackagePart().getInputStream(), POIXMLTypeLoader.DEFAULT_XML_OPTIONS);
  38. CTComment[] var23 = cmntdoc.getComments().getCommentArray();
  39. int var9 = var23.length;
  40. for(int var10 = 0; var10 < var9; ++var10) {
  41. CTComment ctcomment = var23[var10];
  42. this.comments.add(new XWPFComment(ctcomment, this));
  43. }
  44. } else if (relation.equals(XWPFRelation.SETTINGS.getRelation())) {
  45. this.settings = (XWPFSettings)p;
  46. this.settings.onDocumentRead();
  47. } else if (relation.equals(XWPFRelation.IMAGES.getRelation())) {
  48. XWPFPictureData picData = (XWPFPictureData)p;
  49. picData.onDocumentRead();
  50. this.registerPackagePictureData(picData);
  51. this.pictures.add(picData);
  52. } else if (relation.equals(XWPFRelation.CHART.getRelation())) {
  53. XWPFChart chartData = (XWPFChart)p;
  54. this.charts.add(chartData);
  55. } else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) {
  56. Iterator var7 = p.getRelations().iterator();
  57. while(var7.hasNext()) {
  58. POIXMLDocumentPart gp = (POIXMLDocumentPart)var7.next();
  59. POIXMLDocumentPart._invokeOnDocumentRead(gp);
  60. }
  61. }
  62. }
  63. this.initHyperlinks();
  64. return;
  65. }
  66. }
  67. o = docCursor.getObject();
  68. } while(!(o instanceof CTBody));
  69. XmlCursor bodyCursor = o.newCursor();
  70. bodyCursor.selectPath("./*");
  71. while(bodyCursor.toNextSelection()) {
  72. XmlObject bodyObj = bodyCursor.getObject();
  73. if (bodyObj instanceof CTP) {
  74. XWPFParagraph p = new XWPFParagraph((CTP)bodyObj, this);
  75. this.bodyElements.add(p);
  76. this.paragraphs.add(p);
  77. } else if (bodyObj instanceof CTTbl) {
  78. XWPFTable t = new XWPFTable((CTTbl)bodyObj, this);
  79. this.bodyElements.add(t);
  80. this.tables.add(t);
  81. } else if (bodyObj instanceof CTSdtBlock) {
  82. XWPFSDT c = new XWPFSDT((CTSdtBlock)bodyObj, this);
  83. this.bodyElements.add(c);
  84. this.contentControls.add(c);
  85. }
  86. }
  87. bodyCursor.dispose();
  88. }
  89. } catch (XmlException var12) {
  90. throw new POIXMLException(var12);
  91. }
  92. }


参考资料

http://xmlbeans.apache.org/docs/2.0.0/reference/org/apache/xmlbeans/XmlCursor.html XmlBean javadoc