使用OpenXml SDK向Word文档中添加页、段落、页眉和页脚

1. 创建word文档作为模版

2. 以下代码实现需求

using DocumentFormat.OpenXml.Packaging;
using System.IO;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OpenXmlAddParagraphsToNewPage
{
    class Program
    {
        static void Main(string[] args)
        {
            if (File.Exists("copy.docx"))
            {
                File.Delete("copy.docx");
            }

            File.Copy("InsertNewPageAndParagraphs.docx", "copy.docx");
            using (WordprocessingDocument doc = WordprocessingDocument.Open("copy.docx", true))
            {
                var body = doc.MainDocumentPart.Document.Body;

                Paragraph newPara = new Paragraph(new Run
                     (new Break() { Type = BreakValues.Page },
                     new Text("text on the new page")));

                body.Append(newPara);

                AddHeader(doc);
                AddFooter(doc);
                doc.MainDocumentPart.Document.Save();
            }
        }

        private static void AddFooter(WordprocessingDocument doc)
        {
            // Declare a string for the header text.
            string newFooterText =
              "New footer via Open XML Format SDK 2.0 classes";

            // Get the main document part.
            MainDocumentPart mainDocPart = doc.MainDocumentPart;

            // Delete the existing footer parts.
            mainDocPart.DeleteParts(mainDocPart.FooterParts);

            // Create a new footer part and get its relationship id.
            FooterPart newFooterPart = mainDocPart.AddNewPart<FooterPart>();
            string rId = mainDocPart.GetIdOfPart(newFooterPart);

            // Call the GeneratePageFooterPart helper method, passing in
            // the footer text, to create the footer markup and then save
            // that markup to the footer part.
            GeneratePageFooterPart(newFooterText).Save(newFooterPart);

            // Loop through all section properties in the document
            // which is where footer references are defined.
            foreach (SectionProperties sectProperties in
              mainDocPart.Document.Descendants<SectionProperties>())
            {
                //  Delete any existing references to footers.
                foreach (FooterReference footerReference in
                  sectProperties.Descendants<FooterReference>())
                    sectProperties.RemoveChild(footerReference);

                //  Create a new footer reference that points to the new
                // footer part and add it to the section properties.
                FooterReference newFooterReference =
                  new FooterReference() { Id = rId, Type = HeaderFooterValues.Default };
                sectProperties.Append(newFooterReference);
            }
            
            //  Save the changes to the main document part.
            mainDocPart.Document.Save();
        }

        private static void AddHeader(WordprocessingDocument doc)
        {
            // Declare a string for the header text.
            string newHeaderText =
              "New header via Open XML Format SDK 2.0 classes";


            // 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(newHeaderText).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.Default };
                sectProperties.Append(newHeaderReference);
            }

            //  Save the changes to the main document part.
            //mainDocPart.Document.Save();

        }

        // Creates an header instance and adds its children.
        private static Header GeneratePageHeaderPart(string HeaderText)
        {
            // set the position to be the center
            PositionalTab pTab = new PositionalTab()
            {
                Alignment = AbsolutePositionTabAlignmentValues.Center,
                RelativeTo = AbsolutePositionTabPositioningBaseValues.Margin,
                Leader = AbsolutePositionTabLeaderCharValues.None
            };

            var element =
              new Header(
                new Paragraph(
                  new ParagraphProperties(
                    new ParagraphStyleId() { Val = "Header" }),
                  new Run(pTab,
                    new Text(HeaderText))
                )
              );

            return element;
        }

        // Creates an Footer instance and adds its children.
        private static Footer GeneratePageFooterPart(string FooterText)
        {
            PositionalTab pTab = new PositionalTab()
            {
                Alignment = AbsolutePositionTabAlignmentValues.Center,
                RelativeTo = AbsolutePositionTabPositioningBaseValues.Margin,
                Leader = AbsolutePositionTabLeaderCharValues.None
            };

            var elment =
                new Footer(
                    new Paragraph(
                        new ParagraphProperties(
                            new ParagraphStyleId() { Val = "Footer" }),
                        new Run(pTab,
                            new Text(FooterText))
                                )
                            );
            return elment;
        }
    }
}
3. 运行效果





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值