如何在WordprocessingDocument中获取页眉/页脚流

时间:2021-09-09 07:18:28

I need to know how I get the stream of the headers and footers of a WordprocessingDocument. I get the MainDocument by the MainDocumentPart of a WordprocessingDocument. Do you know how I get the header? I tried to use the method GetPartsOfType but it returned null.

我需要知道如何获取WordprocessingDocument的页眉和页脚的流。我通过WordprocessingDocument的MainDocumentPart获取MainDocument。你知道我怎么得到标题吗?我试图使用方法GetPartsOfType但它返回null。

Greets

Sebastian

1 个解决方案

#1


0  

Obtaining the header and the footer is a little bit more complex, than getting a document part.

获取页眉和页脚比获取文档部分要复杂一些。

As it is described here (a quite good sorce of information), there can be many headers and footers within the document and even within a section.

正如这里所描述的(非常好的信息),文档中甚至在一个部分中可以有许多页眉和页脚。

So what you have to do is to open that main document part, identify the w:headerReference elemnt inside the section (w:sectPr) element. This headerReference (or footer of course) contains information about the header kind (first page, odd, even pages, or general), and also a reference id (e.g. "rId6"). Based on the reference id, you can look up the header/footer references (GetRelationship()) and with that you can find the header/footer part.

所以你要做的就是打开那个主文档部分,在section(w:sectPr)元素中标识w:headerReference元素。此headerReference(或页脚)当然包含有关标题类型(第一页,奇数页,偶数页或常规页)的信息,以及引用标识符(例如“rId6”)。根据引用ID,您可以查找页眉/页脚引用(GetRelationship()),然后您可以找到页眉/页脚部分。

Note: interestingly the relationship type of the headers/footers was "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" instead of "http://schemas.openxmlformats.org/wordprocessingml/2006/header" which is in the specification, but since you anyway should lookup the header/footer by reference id, and not reference type, this is not a problem actually.

注意:有趣的是页眉/页脚的关系类型是“http://schemas.openxmlformats.org/officeDocument/2006/relationships/header”而不是“http://schemas.openxmlformats.org/wordprocessingml/2006/header”这是在规范中,但由于你无论如何应该通过引用id而不是引用类型查找页眉/页脚,这实际上不是问题。

#1


0  

Obtaining the header and the footer is a little bit more complex, than getting a document part.

获取页眉和页脚比获取文档部分要复杂一些。

As it is described here (a quite good sorce of information), there can be many headers and footers within the document and even within a section.

正如这里所描述的(非常好的信息),文档中甚至在一个部分中可以有许多页眉和页脚。

So what you have to do is to open that main document part, identify the w:headerReference elemnt inside the section (w:sectPr) element. This headerReference (or footer of course) contains information about the header kind (first page, odd, even pages, or general), and also a reference id (e.g. "rId6"). Based on the reference id, you can look up the header/footer references (GetRelationship()) and with that you can find the header/footer part.

所以你要做的就是打开那个主文档部分,在section(w:sectPr)元素中标识w:headerReference元素。此headerReference(或页脚)当然包含有关标题类型(第一页,奇数页,偶数页或常规页)的信息,以及引用标识符(例如“rId6”)。根据引用ID,您可以查找页眉/页脚引用(GetRelationship()),然后您可以找到页眉/页脚部分。

Note: interestingly the relationship type of the headers/footers was "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" instead of "http://schemas.openxmlformats.org/wordprocessingml/2006/header" which is in the specification, but since you anyway should lookup the header/footer by reference id, and not reference type, this is not a problem actually.

注意:有趣的是页眉/页脚的关系类型是“http://schemas.openxmlformats.org/officeDocument/2006/relationships/header”而不是“http://schemas.openxmlformats.org/wordprocessingml/2006/header”这是在规范中,但由于你无论如何应该通过引用id而不是引用类型查找页眉/页脚,这实际上不是问题。