[英]Add Footer only to last page of the word document using open XML sdk with C#
我是使用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中的SECTIONS。 页眉和页脚可以根据部分而有所不同。 页眉和页脚的类型为:激活相应属性时的第一页,奇数页(默认)和偶数页。 默认情况下,只有奇数页类型,如果没有其他指令,它将在整个文档中“级联”。
没有“最后一页”页眉或页脚的选项。 但是可以使用一组嵌套的字段代码来伪造它,该代码测试当前页面的页码是否等于文档中的页面总数。 如果是,则显示页眉或页脚,否则不显示。 域代码如下所示:
{ IF { PAGE } = {NUMPAGESS } "Text to appear" "" }
请注意,必须使用Ctrl + F9插入方括号{},否则无法通过键盘以其他任何方式键入。 使用Alt + F9在域代码和域结果显示之间切换。
使用此域代码创建一个小的示例文档,然后在Open XML SDK中查看它,以查看它生成的XML以及用于创建它的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.