使用带有C#的开放XML sdk仅将页脚添加到word文档的最后一页

时间:2016-02-05 18:16:39

标签: c# sdk openxml

我是使用C#的OPEN XML SDK 2.5的新手。我创建了一个带有表格的doc,表格中的内容可能超过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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

页眉和页脚与Word中的SECTIONS相关联。页眉和页脚可以根据部分而有所不同。页眉和页脚的类型是:第一页,奇数页(默认)和激活相应属性时的偶数页。默认情况下,只有奇数页面类型,它会&#34;级联&#34;如果没有其他指示,则在整个文件中。

&#34;最后一页&#34;没有选项。页眉或页脚。但它可以使用一组嵌套字段代码伪造,该代码测试当前页面的页码是否等于文档中的总页数。如果是,则显示页眉或页脚,否则不显示。字段代码如下所示:

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

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

使用此字段代码创建一个小样本文档,然后在Open XML SDK中查看它以查看它生成的XML以及用于创建它的代码。