筱冰彤 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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。