仅将页脚添加到 Word 文档的最后一页,使用 Open XML sdk 和 C#

本文关键字:使用 一页 Open XML sdk 最后 添加 文档 Word | 更新日期: 2023-09-27 18:33:28

我是使用 C# 的 OPEN XML SDK 2.5 新手。我创建了一个包含表格的文档,表格中的内容可能超过 500 行。因此,文档可能有多个页面。我已经成功添加了页眉和页脚。但我想要的是我只需要在文档的最后一页添加页脚。如何做到这一点。请找到随附的示例代码。希望我的问题清楚

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @'C:'temp'HeaderFooterDocument.docx';
            CreateTable(fileName);
        }
        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);
            Text text3 = new Text();
            text3.Text = ''rHeader stuff';
            run1.Append(text3);
            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);
        }
        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);
        }
        // Insert a table into a word processing document.
        public static void CreateTable(string fileName)
        {
            // Use the file name and path passed in as an argument 
            // to open an existing Word 2007 document.
            using (WordprocessingDocument wordDocument =
                    WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
            {
                // Add a main document part. 
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
                // Create the document structure and add some text.
                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());
                run.AppendChild(new Text('Create text in body - CreateWordprocessingDocument'));
                ApplyHeader(wordDocument);
                ApplyFooter(wordDocument);
                // Create an empty table.
                Table table = new Table();
                // Create a TableProperties object and specify its border information.
                TableProperties tblProp = new TableProperties(
                    new TableBorders(
                        new TopBorder()
                        {
                            Val =
                            new EnumValue<BorderValues>(BorderValues.None),
                            Size = 24
                        },
                        new BottomBorder()
                        {
                            Val =
                            new EnumValue<BorderValues>(BorderValues.None),
                            Size = 24
                        },
                        new LeftBorder()
                        {
                            Val =
                            new EnumValue<BorderValues>(BorderValues.None),
                            Size = 24
                        },
                        new RightBorder()
                        {
                            Val =
                            new EnumValue<BorderValues>(BorderValues.None),
                            Size = 24
                        },
                        new InsideHorizontalBorder()
                        {
                            Val =
                            new EnumValue<BorderValues>(BorderValues.None),
                            Size = 24
                        },
                        new InsideVerticalBorder()
                        {
                            Val =
                            new EnumValue<BorderValues>(BorderValues.None),
                            Size = 24
                        }
                    )
                );
                // Append the TableProperties object to the empty table.
                table.AppendChild<TableProperties>(tblProp);
                // Create a row.
                TableRow tr = new TableRow();
                // Create a cell.
                TableCell tc1 = new TableCell();
                // Specify the width property of the table cell.
                tc1.Append(new TableCellProperties(
                    new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = '2400' }));
                // Specify the table cell content.
                tc1.Append(new Paragraph(new Run(new Text('some text'))));
                // Append the table cell to the table row.
                tr.Append(tc1);
                // Create a second table cell by copying the OuterXml value of the first table cell.
                TableCell tc2 = new TableCell(tc1.OuterXml);
                // Append the table cell to the table row.
                tr.Append(tc2);
                // Append the table row to the table.
                table.Append(tr);
                for (int rows = 1; rows < 50; rows++)
                {
                    TableRow tr2 = new TableRow();
                    for (int index2 = 0; index2 < 2; index2++)
                    {
                        TableCell newCells = new TableCell();
                        newCells.Append(new Paragraph(new Run(new Text('row' + rows + 'column' + index2))));
                        tr2.Append(newCells);
                    }
                    table.Append(tr2);
                    // Append the table to the document.
                }
                wordDocument.MainDocumentPart.Document.Body.Append(table);
            }
        }
    }
}

仅将页脚添加到 Word 文档的最后一页,使用 Open XML sdk 和 C#

页眉和页脚链接到 Word 中的 SECTION。页眉和页脚可能因部分而异。页眉和页脚的类型是:第一页、奇数页(默认(和激活相应属性时的偶数页。默认情况下,只有奇数页类型,如果没有其他指令,它将在整个文档中"级联"。

没有"最后一页"页眉或页脚的选项。但它可以使用一组嵌套字段代码来伪造,这些代码测试当前页面的页码是否等同于文档中的总页数。如果是,则显示页眉或页脚,否则不显示任何内容。域代码如下所示:

{ IF { PAGE } = {NUMPAGESS } "Text to appear" "" }

请注意,字段括号 { } 必须使用 Ctrl+F9 插入,不能以任何其他方式从键盘键入它们。使用 Alt+F9 在字段代码和字段结果显示之间切换。

使用此域代码创建一个小型示例文档,然后在 Open XML SDK 中查看它以查看它生成的 XML 以及用于创建它的代码。