我想使用 Word.Interop 将页码添加到页面范围。我使用下面的代码添加了页码。此代码将第 2 页的页码添加到文档的末尾 (pageNum = 2)。
例如,我想将页码从第 2 页添加到第 8 页。我尝试将“count”属性与 Selection.GoTo() 一起使用,但情况没有改变,我正在等待您的帮助
代码:
private void HeaderOrFooterAddPageNumber(int pageNum, _Application wdApp, _Document doc, Data data, Section section, HeaderFooter headerfooter)
{
CheckHeaderOrFooter(doc, section, headerfooter);
object missing = Type.Missing;
wdApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToFirst, ref missing, pageNum.ToString());
Range rngPageNum = wdApp.Selection.Range;
rngPageNum.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage);
Section currSec = doc.Sections[rngPageNum.Sections[1].Index];
HeaderFooter ftr = currSec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
ftr.LinkToPrevious = false;
ftr.PageNumbers.RestartNumberingAtSection = true;
ftr.PageNumbers.StartingNumber = 1;
if (!CheckNullOrWhiteSpace(data.PageNumbersNumStyle))
{
if (float.Parse(data.PageNumbersNumStyle) == 2) ftr.PageNumbers.NumberStyle = WdPageNumberStyle.wdPageNumberStyleLowercaseRoman;
}
object TotalPages = WdFieldType.wdFieldSectionPages;
object page = WdFieldType.wdFieldPage;
Range rngCurrSecFooter = ftr.Range;
rngCurrSecFooter.Font.Name = CheckNullOrWhiteSpace(data.PageNumbersFont) ? "Times New Roman" : data.PageNumbersFont;
rngCurrSecFooter.Font.Size = CheckNullOrWhiteSpace(data.PageNumbersFontSize) ? float.Parse("12") : float.Parse(data.PageNumbersFontSize);
rngCurrSecFooter.Font.ColorIndex = CheckNullOrWhiteSpace(data.PageNumbersFontColor) ? WdColorIndex.wdBlack : (WdColorIndex)float.Parse(data.PageNumbersFontColor);
rngCurrSecFooter.Paragraphs.Alignment = CheckNullOrWhiteSpace(data.PageNumbersFontAlignment) ? WdParagraphAlignment.wdAlignParagraphCenter : (WdParagraphAlignment)float.Parse(data.PageNumbersFontAlignment);
rngCurrSecFooter.Collapse(Word.WdCollapseDirection.wdCollapseStart);
rngCurrSecFooter.Fields.Add(rngCurrSecFooter, ref page, ref missing, true);
}