Add Header and Footer to an existing empty word document with OpenXML SDK 2.0

前端 未结 3 1194
星月不相逢
星月不相逢 2020-12-16 14:35

I\'m trying to add a Header and Footer to an empty word document.

I use this code to add Header part in word/document.xml when change docx to zip.

           


        
3条回答
  •  不知归路
    2020-12-16 15:11

    I'm quite sure what is wrong with your code, I suspect its the way you are changing the references.

    In any case I have a working example which you should hopefully guide you.

    I've based mine from examples here: http://msdn.microsoft.com/en-us/library/office/cc546917.aspx

    I used the Open XML SDK 2.0 Productivity Tool to Generate the Header and Footer parts. I first create a document with my desired layout then open it with the tool, it generates the XML or Code. You can get it here: http://www.microsoft.com/en-us/download/details.aspx?id=5124

    The only caveat to this is that it assumes the document already some content in the body, a single letter will do. I'm not sure if this can be avoided, I tried opening an empty document in the Productivity Tool and it suffers from the same error - "Cannot open the file: Archive file cannot be size 0". In my sample it will fail on WordprocessingDocument.Open.

    Maybe in the case of an empty document you have to create a body first. In any case I suspect the main aim of this question is to add a header and footer so I feel this is a valid answer.

    If you want I can provide the actual cs/project files.

    Hope this helps.

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

提交回复
热议问题