使用 OpenXML SDK 2.0 将页眉和页脚添加到现有的空 word 文档
Posted
技术标签:
【中文标题】使用 OpenXML SDK 2.0 将页眉和页脚添加到现有的空 word 文档【英文标题】:Add Header and Footer to an existing empty word document with OpenXML SDK 2.0 【发布时间】:2012-07-25 06:11:02 【问题描述】:我正在尝试将页眉和页脚添加到空的 Word 文档中。
当将 docx 更改为 zip 时,我使用此代码在 word/document.xml 中添加 Header 部分。
ApplyHeader(doc);
public static void ApplyHeader(WordprocessingDocument doc)
// Get the main document part.
MainDocumentPart mainDocPart = doc.MainDocumentPart;
// Delete the existing header parts.
mainDocPart.DeleteParts(mainDocPart.HeaderParts);
// Create a new header part and get its relationship id.
HeaderPart newHeaderPart = mainDocPart.AddNewPart<HeaderPart>();
string rId = mainDocPart.GetIdOfPart(newHeaderPart);
// Call the GeneratePageHeaderPart helper method, passing in
// the header text, to create the header markup and then save
// that markup to the header part.
GeneratePageHeaderPart("Test1").Save(newHeaderPart);
// Loop through all section properties in the document
// which is where header references are defined.
foreach (SectionProperties sectProperties in
mainDocPart.Document.Descendants<SectionProperties>())
// Delete any existing references to headers.
foreach (HeaderReference headerReference in
sectProperties.Descendants<HeaderReference>())
sectProperties.RemoveChild(headerReference);
// Create a new header reference that points to the new
// header part and add it to the section properties.
HeaderReference newHeaderReference =
new HeaderReference() Id = rId, Type = HeaderFooterValues.First ;
sectProperties.Append(newHeaderReference);
// Save the changes to the main document part.
mainDocPart.Document.Save();
private static Header GeneratePageHeaderPart(string HeaderText)
var element =
new Header(
new Paragraph(
new Paragraphproperties(
new ParagraphStyleId() Val = "Header1" ),
new Run(
new Text(HeaderText))
)
);
return element;
此代码有效,但在 word/_rels/document.xml.rels 中没有创建标题关系。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
<Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
</Relationships>
[Content_Types].xml 中不再有内容类型标头
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
<Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/>
<Override PartName="/word/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>
<Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/>
<Override PartName="/word/webSettings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml"/>
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
</Types>
尽管如此,header.xml 是在 word/ 中创建的
<?xml version="1.0" encoding="utf-8"?><w:hdr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:p><w:pPr><w:pStyle w:val="Header1" /></w:pPr><w:r><w:t>Test1</w:t></w:r></w:p></w:hdr>
还有我的 word/document.xml 以显示我的标题已添加。
<w:sectPr w:rsidR="003310CE" w:rsidSect="00D928B6">
<w:pgSz w:w="11906" w:h="16838" />
<w:pgMar w:top="1417" w:right="1417" w:bottom="1417" w:left="1417" w:header="708" w:footer="708" w:gutter="0" />
<w:cols w:space="708" /><w:docGrid w:linePitch="360" />
<w:headerReference w:type="first" r:id="Recfa318e6a7c44ff" />
</w:sectPr>
所以我的问题是如何正确添加页眉和页脚?
感谢您的帮助。
【问题讨论】:
【参考方案1】:我很确定您的代码有什么问题,我怀疑它是您更改引用的方式。
无论如何,我有一个工作示例,希望您能指导您。
我的例子来自这里:http://msdn.microsoft.com/en-us/library/office/cc546917.aspx
我使用 Open XML SDK 2.0 Productivity Tool 来生成页眉和页脚部分。我首先使用我想要的布局创建一个文档,然后使用该工具打开它,它会生成 XML 或代码。你可以在这里得到它:http://www.microsoft.com/en-us/download/details.aspx?id=5124
唯一需要注意的是,它假定文档正文中已经有一些内容,一个字母就可以了。我不确定这是否可以避免,我尝试在生产力工具中打开一个空文档,但它遇到了同样的错误 - “无法打开文件:存档文件的大小不能为 0”。在我的示例中,它将在WordprocessingDocument.Open
上失败。
可能在空文档的情况下,您必须先创建一个正文。无论如何,我怀疑这个问题的主要目的是添加页眉和页脚,所以我觉得这是一个有效的答案。
如果您愿意,我可以提供实际的 cs/项目文件。
希望这会有所帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
namespace HeaderFooterDocX
class Program
static void Main(string[] args)
ChangeHeader(@"C:\Users\James\Desktop\Document.docx");
static void ChangeHeader(String documentPath)
// Replace header in target document with header of source document.
using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true))
// Get the main document part
MainDocumentPart mainDocumentPart = document.MainDocumentPart;
// Delete the existing header and footer parts
mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts);
mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts);
// Create a new header and footer part
HeaderPart headerPart = mainDocumentPart.AddNewPart<HeaderPart>();
FooterPart footerPart = mainDocumentPart.AddNewPart<FooterPart>();
// Get Id of the headerPart and footer parts
string headerPartId = mainDocumentPart.GetIdOfPart(headerPart);
string footerPartId = mainDocumentPart.GetIdOfPart(footerPart);
GenerateHeaderPartContent(headerPart);
GenerateFooterPartContent(footerPart);
// Get SectionProperties and Replace HeaderReference and FooterRefernce with new Id
IEnumerable<SectionProperties> sections = mainDocumentPart.Document.Body.Elements<SectionProperties>();
foreach (var section in sections)
// Delete existing references to headers and footers
section.RemoveAllChildren<HeaderReference>();
section.RemoveAllChildren<FooterReference>();
// Create the new header and footer reference node
section.PrependChild<HeaderReference>(new HeaderReference() Id = headerPartId );
section.PrependChild<FooterReference>(new FooterReference() Id = footerPartId );
static void GenerateHeaderPartContent(HeaderPart part)
Header header1 = new Header() MCAttributes = new MarkupCompatibilityAttributes() Ignorable = "w14 wp14" ;
header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph() RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" ;
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() Val = "Header" ;
paragraphProperties1.Append(paragraphStyleId1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Header";
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
header1.Append(paragraph1);
part.Header = header1;
static void GenerateFooterPartContent(FooterPart part)
Footer footer1 = new Footer() MCAttributes = new MarkupCompatibilityAttributes() Ignorable = "w14 wp14" ;
footer1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
footer1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
footer1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
footer1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
footer1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
footer1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
footer1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
footer1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
footer1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
footer1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
footer1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
footer1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
footer1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
footer1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
footer1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph() RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" ;
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() Val = "Footer" ;
paragraphProperties1.Append(paragraphStyleId1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Footer";
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
footer1.Append(paragraph1);
part.Footer = footer1;
【讨论】:
您好!你有 Excel 电子表格的类似代码吗?或者,如果你能给我搜索的方向?真的找不到任何关于在 C# 中通过 OpenXML 更改页眉/页脚的信息。【参考方案2】:这段代码对我有用。考虑:
-
在我的示例中,标题的关系 ID 是“r97”。也许您想自己决定一个独特的 rId
页脚的 relationshipId ("r98") 相同
页眉和页脚都有一个带有一些文本的硬编码段落。
我假设一个文档已经存在
using (WordprocessingDocument doc = WordprocessingDocument.Open(destination, true))
var mainDocPart = doc.MainDocumentPart;
if (doc == null)
mainDocPart = doc.AddMainDocumentPart();
if (mainDocPart.Document == null)
mainDocPart.Document = new Document();
ApplyHeader(doc);
ApplyFooter(doc);
ApplyHeader 方法:
public static void ApplyHeader(WordprocessingDocument doc)
// Get the main document part.
MainDocumentPart mainDocPart = doc.MainDocumentPart;
HeaderPart headerPart1 = mainDocPart.AddNewPart<HeaderPart>("r97");
Header header1 = new Header();
Paragraph paragraph1 = new Paragraph() ;
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Header stuff";
run1.Append(text1);
paragraph1.Append(run1);
header1.Append(paragraph1);
headerPart1.Header = header1;
SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants<SectionProperties>().FirstOrDefault();
if (sectionProperties1 == null)
sectionProperties1 = new SectionProperties() ;
mainDocPart.Document.Body.Append(sectionProperties1);
HeaderReference headerReference1 = new HeaderReference() Type = HeaderFooterValues.Default, Id = "r97" ;
sectionProperties1.InsertAt(headerReference1,0);
ApplyFooter 方法:
public static void ApplyFooter(WordprocessingDocument doc)
// Get the main document part.
MainDocumentPart mainDocPart = doc.MainDocumentPart;
FooterPart footerPart1 = mainDocPart.AddNewPart<FooterPart>("r98");
Footer footer1 = new Footer();
Paragraph paragraph1 = new Paragraph() ;
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Footer stuff";
run1.Append(text1);
paragraph1.Append(run1);
footer1.Append(paragraph1);
footerPart1.Footer = footer1;
SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants<SectionProperties>().FirstOrDefault();
if (sectionProperties1 == null)
sectionProperties1 = new SectionProperties() ;
mainDocPart.Document.Body.Append(sectionProperties1);
FooterReference footerReference1 = new FooterReference() Type = DocumentFormat.OpenXml.Wordprocessing.HeaderFooterValues.Default, Id = "r98" ;
sectionProperties1.InsertAt(footerReference1, 0);
享受,
【讨论】:
如何在页眉和页脚中插入像“Header Stuff”这样的html字符串? 我喜欢这段代码,因为它比其他答案简单得多,其他答案有一堆不必要的东西。但是您实际上不必分配自己的 relationshipId,您可以从 MainDocumentPart.GetIdOfPart(HeaderPart) 中获取。【参考方案3】:我自己找到解决办法。
事实上,使用 OpenXml 在 docx 中添加页眉或页脚涉及到:
文档必须存在。
在添加页眉或页脚之前保存和关闭文档不起作用。
WordprocessingDocument document = WordprocessingDocument.Open(path, true);
<operations on docx>
document.Save();
document.Close();
ChangeHeader(document);
您必须第二次重新打开您的 docx 以创建文档的新实例。
正确的解决方案将是(使用 James Wood 解决方案):
WordprocessingDocument document = WordprocessingDocument.Open(path, true);
<operations on docx>
document.Save();
document.Close();
ChangeHeader(path);
public static void ChangeHeader(String documentPath)
// Replace header in target document with header of source document.
using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true))
// Get the main document part
MainDocumentPart mainDocumentPart = document.MainDocumentPart;
// Delete the existing header and footer parts
mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts);
mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts);
// Create a new header and footer part
HeaderPart headerPart = mainDocumentPart.AddNewPart<HeaderPart>();
FooterPart footerPart = mainDocumentPart.AddNewPart<FooterPart>();
// Get Id of the headerPart and footer parts
string headerPartId = mainDocumentPart.GetIdOfPart(headerPart);
string footerPartId = mainDocumentPart.GetIdOfPart(footerPart);
GenerateHeaderPartContent(headerPart);
GenerateFooterPartContent(footerPart);
// Get SectionProperties and Replace HeaderReference and FooterRefernce with new Id
IEnumerable<SectionProperties> sections = mainDocumentPart.Document.Body.Elements<SectionProperties>();
foreach (var section in sections)
// Delete existing references to headers and footers
section.RemoveAllChildren<HeaderReference>();
section.RemoveAllChildren<FooterReference>();
// Create the new header and footer reference node
section.PrependChild<HeaderReference>(new HeaderReference() Id = headerPartId );
section.PrependChild<FooterReference>(new FooterReference() Id = footerPartId );
public static void GenerateHeaderPartContent(HeaderPart part)
Header header1 = new Header() MCAttributes = new MarkupCompatibilityAttributes() Ignorable = "w14 wp14" ;
header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph() RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" ;
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() Val = "Header" ;
paragraphProperties1.Append(paragraphStyleId1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Header";
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
header1.Append(paragraph1);
part.Header = header1;
public static void GenerateFooterPartContent(FooterPart part)
Footer footer1 = new Footer() MCAttributes = new MarkupCompatibilityAttributes() Ignorable = "w14 wp14" ;
footer1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
footer1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
footer1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
footer1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
footer1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
footer1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
footer1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
footer1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
footer1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
footer1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
footer1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
footer1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
footer1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
footer1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
footer1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph() RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" ;
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() Val = "Footer" ;
paragraphProperties1.Append(paragraphStyleId1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Footer";
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
footer1.Append(paragraph1);
part.Footer = footer1;
【讨论】:
以上是关于使用 OpenXML SDK 2.0 将页眉和页脚添加到现有的空 word 文档的主要内容,如果未能解决你的问题,请参考以下文章