使用带有 C# 的 open XML sdk 将页脚仅添加到 word 文档的最后一页
Add Footer only to last page of the word document using open XML sdk with C#
我是使用 C# OPEN XML SDK 2.5 的新手。我创建了一个包含 table 的文档,table 中的内容可能超过 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。页眉和页脚可以根据部分而变化。页眉和页脚的类型为:第一页、奇数页(默认)和偶数页(当相应的属性被激活时)。默认情况下,只有奇数页类型,如果没有其他说明,它将 "cascade" 贯穿整个文档。
"last page" 页眉或页脚没有选项。但它可以使用一组嵌套字段代码来伪造,这些代码测试当前页面的页码是否等于文档中的总页数。如果是,则显示页眉或页脚,否则不显示。字段代码如下所示:
{ IF { PAGE } = {NUMPAGESS } "Text to appear" "" }
请注意,字段括号 { } 必须使用 Ctrl+F9 插入,不能以任何其他方式从键盘输入。使用 Alt+F9 在域代码和域结果显示之间切换。
使用此字段代码创建一个小示例文档,然后在 Open XML SDK 中查看它以查看它生成的 XML 和用于创建它的代码。
我是使用 C# OPEN XML SDK 2.5 的新手。我创建了一个包含 table 的文档,table 中的内容可能超过 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。页眉和页脚可以根据部分而变化。页眉和页脚的类型为:第一页、奇数页(默认)和偶数页(当相应的属性被激活时)。默认情况下,只有奇数页类型,如果没有其他说明,它将 "cascade" 贯穿整个文档。
"last page" 页眉或页脚没有选项。但它可以使用一组嵌套字段代码来伪造,这些代码测试当前页面的页码是否等于文档中的总页数。如果是,则显示页眉或页脚,否则不显示。字段代码如下所示:
{ IF { PAGE } = {NUMPAGESS } "Text to appear" "" }
请注意,字段括号 { } 必须使用 Ctrl+F9 插入,不能以任何其他方式从键盘输入。使用 Alt+F9 在域代码和域结果显示之间切换。
使用此字段代码创建一个小示例文档,然后在 Open XML SDK 中查看它以查看它生成的 XML 和用于创建它的代码。