c#程序里给生成的word添加页码例如第几页共几页

1,我通过程序生成一个word,这个word本身是一个模板,通过标签的方式添加进去内容,然后在把其他文件复制粘贴到这个目标word的后面
2,现在出现的问题是,模板自带的3页word可以实现页码的显示
3,但是后面添加进去的其他页面的页码就不正确了,比如总共是12页,那么除了前面3页之外,其余9页的页码都是“第12页 共12页”

            int pageCount = doc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, oMissing);
            for (int i = 3; i <= pageCount; i++)
            {
                Word.Range range = GetPages(doc, i);
                object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                object oPageBreak = Word.WdBreakType.wdSectionBreakContinuous;//分页符     
                //range.Collapse(ref oCollapseEnd);  
                range.InsertBreak(ref oPageBreak);
                //range.Collapse(ref oCollapseEnd);
            }
            
            int j = 1;
            Word.Range allRange = doc.Range(ref oMissing, ref oMissing);
            foreach (Word.Section section in allRange.Sections)
            {
                //设置页脚的文字格式
                section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;
                //页脚文字居中
                section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                //页脚文字字体
                section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Name = "宋体";
                section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Size = 10.5f;
                section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Bold = 0;
                section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "第" + j.ToString() + "页";
                j++;
            }
阅读 3.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进