关于插入新的引用文件的思考
在POIXMLDocumentPart类中,存在可以获取文档引用信息的方法,并且通过api建立引用文件好像也是可以实现的,而有关这个功能的的实现就需要依靠下面这个方法:
private void addRelation(PackageRelationship pr, POIXMLDocumentPart part) {this.relations.put(pr.getId(), new POIXMLDocumentPart.RelationPart(pr, part));part.incrementRelationCounter();}
我们先查看一下输入文档原引用信息:
POIXMLDocumentPart poixmlDocumentPart = xwpfDocument.getPart();List<POIXMLDocumentPart.RelationPart> relationParts = poixmlDocumentPart.getRelationParts();for(POIXMLDocumentPart.RelationPart relationPart : relationParts){System.out.println("引用id:"+relationPart.getRelationship().getId());System.out.println("引用类型:"+relationPart.getRelationship().getRelationshipType());System.out.println("引用文件指向地址:"+relationPart.getRelationship().getTargetURI().getPath());System.out.println("被引用地址:"+relationPart.getRelationship().getSourceURI().getPath());System.out.println(relationPart.getDocumentPart());System.out.println();}
打印,控制台显示结果摘录如下:
开始展示引用文件信息:引用id:rId1引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml引用文件指向地址:/customXml/item1.xml被引用地址:/word/document.xmlName: /customXml/item1.xml - Content Type: application/xml引用id:rId10引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer2.xml被引用地址:/word/document.xmlName: /word/footer2.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml引用id:rId11引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header2.xml被引用地址:/word/document.xmlName: /word/header2.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId12引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer3.xml被引用地址:/word/document.xmlName: /word/footer3.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml引用id:rId13引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header3.xml被引用地址:/word/document.xmlName: /word/header3.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId14引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer4.xml被引用地址:/word/document.xmlName: /word/footer4.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml引用id:rId15引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/image引用文件指向地址:/word/media/image1.png被引用地址:/word/document.xmlName: /word/media/image1.png - Content Type: image/png引用id:rId16引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header4.xml被引用地址:/word/document.xmlName: /word/header4.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId17引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer5.xml被引用地址:/word/document.xmlName: /word/footer5.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml引用id:rId18引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header5.xml被引用地址:/word/document.xmlName: /word/header5.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId19引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header6.xml被引用地址:/word/document.xmlName: /word/header6.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId2引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering引用文件指向地址:/word/numbering.xml被引用地址:/word/document.xmlName: /word/numbering.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml引用id:rId20引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer6.xml被引用地址:/word/document.xmlName: /word/footer6.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml引用id:rId21引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable引用文件指向地址:/word/fontTable.xml被引用地址:/word/document.xmlName: /word/fontTable.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml引用id:rId22引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme引用文件指向地址:/word/theme/theme1.xml被引用地址:/word/document.xmlName: /word/theme/theme1.xml - Content Type: application/vnd.openxmlformats-officedocument.theme+xml引用id:rId3引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles引用文件指向地址:/word/styles.xml被引用地址:/word/document.xmlName: /word/styles.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml引用id:rId4引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings引用文件指向地址:/word/settings.xml被引用地址:/word/document.xmlName: /word/settings.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml引用id:rId5引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings引用文件指向地址:/word/webSettings.xml被引用地址:/word/document.xmlName: /word/webSettings.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml引用id:rId6引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes引用文件指向地址:/word/footnotes.xml被引用地址:/word/document.xmlName: /word/footnotes.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml引用id:rId7引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes引用文件指向地址:/word/endnotes.xml被引用地址:/word/document.xmlName: /word/endnotes.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml引用id:rId8引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header1.xml被引用地址:/word/document.xmlName: /word/header1.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId9引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer1.xml被引用地址:/word/document.xmlName: /word/footer1.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml开始展示引用文件信息:引用id:rId1引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml引用文件指向地址:/customXml/item1.xml被引用地址:/word/document.xmlName: /customXml/item1.xml - Content Type: application/xml引用id:rId10引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer2.xml被引用地址:/word/document.xmlName: /word/footer2.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml引用id:rId11引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header2.xml被引用地址:/word/document.xmlName: /word/header2.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId12引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer3.xml被引用地址:/word/document.xmlName: /word/footer3.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml引用id:rId13引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header3.xml被引用地址:/word/document.xmlName: /word/header3.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId14引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer4.xml被引用地址:/word/document.xmlName: /word/footer4.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml引用id:rId15引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/image引用文件指向地址:/word/media/image1.png被引用地址:/word/document.xmlName: /word/media/image1.png - Content Type: image/png引用id:rId16引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header4.xml被引用地址:/word/document.xmlName: /word/header4.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId17引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer5.xml被引用地址:/word/document.xmlName: /word/footer5.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml引用id:rId18引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header5.xml被引用地址:/word/document.xmlName: /word/header5.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId19引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header6.xml被引用地址:/word/document.xmlName: /word/header6.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId2引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering引用文件指向地址:/word/numbering.xml被引用地址:/word/document.xmlName: /word/numbering.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml引用id:rId20引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer6.xml被引用地址:/word/document.xmlName: /word/footer6.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml引用id:rId21引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable引用文件指向地址:/word/fontTable.xml被引用地址:/word/document.xmlName: /word/fontTable.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml引用id:rId22引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme引用文件指向地址:/word/theme/theme1.xml被引用地址:/word/document.xmlName: /word/theme/theme1.xml - Content Type: application/vnd.openxmlformats-officedocument.theme+xml引用id:rId3引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles引用文件指向地址:/word/styles.xml被引用地址:/word/document.xmlName: /word/styles.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml引用id:rId4引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings引用文件指向地址:/word/settings.xml被引用地址:/word/document.xmlName: /word/settings.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml引用id:rId5引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings引用文件指向地址:/word/webSettings.xml被引用地址:/word/document.xmlName: /word/webSettings.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml引用id:rId6引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes引用文件指向地址:/word/footnotes.xml被引用地址:/word/document.xmlName: /word/footnotes.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml引用id:rId7引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes引用文件指向地址:/word/endnotes.xml被引用地址:/word/document.xmlName: /word/endnotes.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml引用id:rId8引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/header引用文件指向地址:/word/header1.xml被引用地址:/word/document.xmlName: /word/header1.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml引用id:rId9引用类型:http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer引用文件指向地址:/word/footer1.xml被引用地址:/word/document.xmlName: /word/footer1.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml
因此,如果我们要构建一个页眉页脚引用文件,那么首先得补齐这些信息
另外在查看PackageRelationship类的源码时发现,引用文件的路径应该使用相对路径,使用java.net.URI类进行包装,例如此类的静态代码块:
static {try {containerRelationshipPart = new URI("/_rels/.rels");} catch (URISyntaxException var1) {}
其类PackageRelationship属性如下:
public final class PackageRelationship {private static URI containerRelationshipPart;public static final String ID_ATTRIBUTE_NAME = "Id";public static final String RELATIONSHIPS_TAG_NAME = "Relationships";public static final String RELATIONSHIP_TAG_NAME = "Relationship";public static final String TARGET_ATTRIBUTE_NAME = "Target";public static final String TARGET_MODE_ATTRIBUTE_NAME = "TargetMode";public static final String TYPE_ATTRIBUTE_NAME = "Type";private String id;private OPCPackage container;private String relationshipType;private PackagePart source;private TargetMode targetMode;private URI targetUri;
构造方法如下:
public PackageRelationship(OPCPackage pkg, PackagePart sourcePart, URI targetUri, TargetMode targetMode, String relationshipType, String id) {if (pkg == null) {throw new IllegalArgumentException("pkg");} else if (targetUri == null) {throw new IllegalArgumentException("targetUri");} else if (relationshipType == null) {throw new IllegalArgumentException("relationshipType");} else if (id == null) {throw new IllegalArgumentException("id");} else {this.container = pkg;this.source = sourcePart;this.targetUri = targetUri;this.targetMode = targetMode;this.relationshipType = relationshipType;this.id = id;}}
在获取当前这个引用对象的元数据包时,可以结合属性
private OPCPackage container;
调用方法
public OPCPackage getPackage() {return this.container;}
另外一个未知属性sourcePart在当前类中并没有被使用上,因此暂时不知道是什么
在这里遭遇一个令人窒息的逻辑闭环:
在rel.rel文件中放入新的引用信息 -> 需要提供引用对象 -> 创建引用对象需要该引用文件在此文件中的相对地址->但是要插入的文件根本就还没有放进去???-> 不能在rel文件中插入新的引用信息
所以,在将引用文件插入到rel.rel文件之前,应该先把这个引用文件添加进文档压缩包里面去
清除整个引用文件
xwpfDocument.getPart().getPackagePart().clearRelationships();
即删除了document.xml.rels文件,但是被引用的诸如页眉页脚信息依旧存在
偶然发现poi底层使用了大量的集合数据结构
如何构建一个页眉页脚文件
再一次偶然中,我发现了一种新的类型:
HdrDocument hdrDocument = null;try {hdrDocument = HdrDocument.Factory.parse(new File("E:\\header1.xml"));} catch (XmlException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
HdrDocument类和页眉页脚文件:
<?xml version="1.0" encoding="utf-8" standalone="yes"?><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"><w:p><w:pPr><w:jc w:val="right" /></w:pPr><w:r><w:rPr><w:b /><w:color w:val="0000FF" /><w:sz w:val="30" /></w:rPr><w:t xml:space="preserve">偶数页页眉</w:t></w:r></w:p></w:hdr>
第一个标签的前缀是吻合的,那么可以推断出,这个就是我们想找的页眉页脚文件的对象。
但是接下来一个问题是:找到了可以存放的对象,但是怎么把这个对象加载进目前的文档中呢
如何利用已知uri路径从引用文件中提取到对应的文件
此处从PackageRelationship先入手,其中有一个静态区块:
static {try {containerRelationshipPart = new URI("/_rels/.rels");} catch (URISyntaxException var1) {}}
在poi中有一个类:XWPFRelation
public final class XWPFRelation extends POIXMLRelation {private static final Map<String, XWPFRelation> _table = new HashMap();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");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");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");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");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");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);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");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);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);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");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);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);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");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);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);public static final XWPFRelation HYPERLINK = new XWPFRelation((String)null, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", (String)null);public static final XWPFRelation COMMENT = new XWPFRelation((String)null, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments", (String)null);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);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);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);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);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);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);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);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);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);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);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);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);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);public static final XWPFRelation IMAGES = new XWPFRelation((String)null, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", (String)null, XWPFPictureData::new, XWPFPictureData::new);
使用下面这段代码可以获取到指定引用信息中的内容
List<POIXMLDocumentPart.RelationPart> relationParts = xwpfDocument.getRelationParts();for(POIXMLDocumentPart.RelationPart relationPart : relationParts){if(relationPart.getRelationship().getRelationshipType().equals(XWPFRelation.FOOTER.getRelation())){System.out.println("找到了一个页脚");System.out.println("引用文件指向地址:"+relationPart.getRelationship().getTargetURI().getPath());// String target = relationPart.getRelationship().getTargetURI().getPath();// 利用得到的地址创建一个URI/* try {URI pFooterPath = new URI(relationPart.getRelationship().getTargetURI().getPath());} catch (URISyntaxException e) {e.printStackTrace();}*/XWPFFooter footer = relationPart.getDocumentPart();System.out.println(footer.getParagraphs().get(0).getText());}}
此处写法参考POI中的XWPFDoucment对象中的文档构建方法
protected void onDocumentRead() throws IOException {try {DocumentDocument doc = Factory.parse(this.getPackagePart().getInputStream(), POIXMLTypeLoader.DEFAULT_XML_OPTIONS);this.ctDocument = doc.getDocument();this.initFootnotes();XmlCursor docCursor = this.ctDocument.newCursor();docCursor.selectPath("./*");while(true) {XmlObject o;do {if (!docCursor.toNextSelection()) {docCursor.dispose();if (doc.getDocument().getBody().getSectPr() != null) {this.headerFooterPolicy = new XWPFHeaderFooterPolicy(this);}Iterator var13 = this.getRelationParts().iterator();while(true) {while(var13.hasNext()) {RelationPart rp = (RelationPart)var13.next();POIXMLDocumentPart p = rp.getDocumentPart();String relation = rp.getRelationship().getRelationshipType();if (relation.equals(XWPFRelation.STYLES.getRelation())) {this.styles = (XWPFStyles)p;this.styles.onDocumentRead();} else if (relation.equals(XWPFRelation.NUMBERING.getRelation())) {this.numbering = (XWPFNumbering)p;this.numbering.onDocumentRead();} else if (relation.equals(XWPFRelation.FOOTER.getRelation())) {XWPFFooter footer = (XWPFFooter)p;this.footers.add(footer);footer.onDocumentRead();} else if (relation.equals(XWPFRelation.HEADER.getRelation())) {XWPFHeader header = (XWPFHeader)p;this.headers.add(header);header.onDocumentRead();} else if (relation.equals(XWPFRelation.COMMENT.getRelation())) {CommentsDocument cmntdoc = org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument.Factory.parse(p.getPackagePart().getInputStream(), POIXMLTypeLoader.DEFAULT_XML_OPTIONS);CTComment[] var23 = cmntdoc.getComments().getCommentArray();int var9 = var23.length;for(int var10 = 0; var10 < var9; ++var10) {CTComment ctcomment = var23[var10];this.comments.add(new XWPFComment(ctcomment, this));}} else if (relation.equals(XWPFRelation.SETTINGS.getRelation())) {this.settings = (XWPFSettings)p;this.settings.onDocumentRead();} else if (relation.equals(XWPFRelation.IMAGES.getRelation())) {XWPFPictureData picData = (XWPFPictureData)p;picData.onDocumentRead();this.registerPackagePictureData(picData);this.pictures.add(picData);} else if (relation.equals(XWPFRelation.CHART.getRelation())) {XWPFChart chartData = (XWPFChart)p;this.charts.add(chartData);} else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) {Iterator var7 = p.getRelations().iterator();while(var7.hasNext()) {POIXMLDocumentPart gp = (POIXMLDocumentPart)var7.next();POIXMLDocumentPart._invokeOnDocumentRead(gp);}}}this.initHyperlinks();return;}}o = docCursor.getObject();} while(!(o instanceof CTBody));XmlCursor bodyCursor = o.newCursor();bodyCursor.selectPath("./*");while(bodyCursor.toNextSelection()) {XmlObject bodyObj = bodyCursor.getObject();if (bodyObj instanceof CTP) {XWPFParagraph p = new XWPFParagraph((CTP)bodyObj, this);this.bodyElements.add(p);this.paragraphs.add(p);} else if (bodyObj instanceof CTTbl) {XWPFTable t = new XWPFTable((CTTbl)bodyObj, this);this.bodyElements.add(t);this.tables.add(t);} else if (bodyObj instanceof CTSdtBlock) {XWPFSDT c = new XWPFSDT((CTSdtBlock)bodyObj, this);this.bodyElements.add(c);this.contentControls.add(c);}}bodyCursor.dispose();}} catch (XmlException var12) {throw new POIXMLException(var12);}}
参考资料
http://xmlbeans.apache.org/docs/2.0.0/reference/org/apache/xmlbeans/XmlCursor.html XmlBean javadoc
