筱冰彤 2016-08-22 06:32 采纳率: 0%
浏览 2390

C# 怎么给word文档每一页添加不同的页脚信息,如“第m页,共n页”?

利用下面的代码添加,会有一些问题。

            //获取页数
            int pageNum = doc.ComputeStatistics(WdStatistic.wdStatisticPages, ref missing);
            //添加最后一页页脚
            doc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
            doc.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            string text = string.Format("试卷第{0}页,总{1}页", pageNum, pageNum);
            if (pageNum % 2 != 0)
            {
                text += "\r\n侵权必究";
            }
            doc.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Text = text;
            doc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
            //添加前n-1页页脚
            object what = WdGoToItem.wdGoToPage;
            object type = WdBreakType.wdSectionBreakContinuous;
            for (int i = 1; i < pageNum; i++)
            {
                object name = i + 1;
                doc.ActiveWindow.ActivePane.Selection.GoTo(ref what, ref missing, ref missing, ref name);
                doc.ActiveWindow.ActivePane.Selection.InsertBreak(ref type);
                doc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
                text = string.Format("试卷第{0}页,总{1}页", i, pageNum);
                if (i % 2 != 0)
                {
                    text += "\r\n侵权必究";
                }
                doc.ActiveWindow.ActivePane.Selection.HeaderFooter.LinkToPrevious = false;
                doc.ActiveWindow.ActivePane.Selection.HeaderFooter.Range.Text = text;
                doc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
            }

这样在文档本身没有任何分隔符(包括连续和分页符)的情况下是可以完成的,但是一旦文档本身在某个地方存在分隔符,那么自那个分隔符开始,插入的内容就乱了。跪求大神。。。

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-08-22 15:11
    关注

    在word中录制一个宏,在C#里照着抄生成的代码。添加页码可以双击页眉,插入域,选择页码和页数。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog