0

我正在一个横向文档上编写一个包含 3 块的页脚,左右边距为 0.5":

  1. 单词页码字段 + 的文字“” + 单词总页数字段(左对齐)
  2. 文字 UNCLASSIFIED(使用制表符居中对齐)
  3. 带有文字“Report Created:”和当前日期 + 时间的格式化字符串(使用制表符右对齐)

当我将范围折叠到末尾并向前移动 1 个字符 ( footerRange.MoveEnd(unit, 1);) 时,Word 总页数字段值 ( ref totalPages) 消失(它应该显示1 of 1。相反,它显示“1 of”)。

在此处输入图像描述

footerRange.MoveEnd(unit, 0);当我将范围折叠到末尾并向前移动 0 个footerRange字符(在“1”之前,以便该ref totalPages值出现在页脚的末尾。

在此处输入图像描述

相关代码

` foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections) { //获取页脚范围并添加页脚详细信息。
Microsoft.Office.Interop.Word.HeaderFooter 页脚 = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];

footer.LinkToPrevious = false;
footer.PageNumbers.RestartNumberingAtSection = true;
footer.PageNumbers.StartingNumber = 1;

Microsoft.Office.Interop.Word.Range footerRange = footer.Range;

footerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdAuto;

footerRange.Font.Size = 12;
footerRange.Font.Name = "Courier New";
footerRange.Font.Bold = 1;

object totalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldSectionPages;
object currentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;

footerRange.Fields.Add(footerRange, ref currentPage, ref missing, false);
footerRange.InsertAfter(" of ");
footerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
footerRange.Fields.Add(footerRange, ref totalPages, ref missing, false);

footerRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
object unit = WdUnits.wdCharacter;
footerRange.MoveEnd(unit, 1);  // What do I need to do do add the following after the totalPages field?

footerRange.Paragraphs.TabStops.ClearAll();
footerRange.Paragraphs.TabStops.Add(wordApp.InchesToPoints(0F), Word.WdTabAlignment.wdAlignTabLeft);
footerRange.Paragraphs.TabStops.Add(wordApp.InchesToPoints(5F), Word.WdTabAlignment.wdAlignTabCenter);
footerRange.Paragraphs.TabStops.Add(wordApp.InchesToPoints(10F), Word.WdTabAlignment.wdAlignTabRight);
footerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

footerRange.Text = string.Format("\tUNCLASSIFIED\tReport Created: {0}", DateTime.Now.ToString("G"));

} `

4

0 回答 0