C#学习教程:使用OpenXML SDK 2.0将页眉和页脚添加到现有的空单词文档中分享


使用OpenXML SDK 2.0将页眉和页脚添加到现有的空单词文档中

我正在尝试在空文档中添加页眉和页脚。

当将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(); 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()) { // Delete any existing references to headers. foreach (HeaderReference headerReference in sectProperties.Descendants()) 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中没有创建头关系。

         

并且[Content_Types] .xml中没有更多内容类型标头

              

尽管如此,header.xml是在word /中创建的

 Test1 

我的word / document.xml显示我的标题已添加。

       

所以我的问题是如何正确添加页眉和页脚?

谢谢你的帮助。

我很确定你的代码有什么问题,我怀疑它是你改变引用的方式。

无论如何,我有一个工作的例子,你应该指导你。

我的例子来自我的例子: 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

唯一需要注意的是,它假定文档中已有一些内容,一个字母就可以了。 我不确定是否可以避免这种情况,我尝试在Productivity Tool中打开一个空文档,它遇到了同样的错误 – “无法打开文件:存档文件不能为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:UsersJamesDesktopDocument.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(); FooterPart footerPart = mainDocumentPart.AddNewPart(); // 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 sections = mainDocumentPart.Document.Body.Elements(); foreach (var section in sections) { // Delete existing references to headers and footers section.RemoveAllChildren(); section.RemoveAllChildren(); // Create the new header and footer reference node section.PrependChild(new HeaderReference() { Id = headerPartId }); section.PrependChild(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; } } } 

这段代码对我有用。 考虑到:

  1. header的关系Id在我的例子中是“r97”。 也许你想自己决定一个独特的rId
  2. 对于页脚的relationshipId(“r98”)相同
  3. 页眉和页脚都有一个带有一些文本的硬编码段落。
  4. 我假设一份文件已经存在

      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("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().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("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().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); } 

请享用,

我自己找到解决方案。

事实上,在带有OpenXml的docx中添加页眉或页脚涉及:

文件必须存在。

在添加页眉或页脚之前保存和关闭文档不起作用。

 WordprocessingDocument document = WordprocessingDocument.Open(path, true);  document.Save(); document.Close(); ChangeHeader(document); 

你必须重新打开你的docx来创建一个新的文档实例。

正确的解决方案将是(与James Wood解决方案):

上述就是C#学习教程:使用OpenXML SDK 2.0将页眉和页脚添加到现有的空单词文档中分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

 WordprocessingDocument document = WordprocessingDocument.Open(path, true);  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(); FooterPart footerPart = mainDocumentPart.AddNewPart(); // 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 sections = mainDocumentPart.Document.Body.Elements(); foreach (var section in sections) { // Delete existing references to headers and footers section.RemoveAllChildren(); section.RemoveAllChildren(); // Create the new header and footer reference node section.PrependChild(new HeaderReference() { Id = headerPartId }); section.PrependChild(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; } 

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/959888.html

发表评论

邮箱地址不会被公开。 必填项已用*标注