加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

c#操作word设置页眉页脚,页脚同时包括文字和页码

(2013-09-06 10:19:56)

一、页眉

通过插入分节符下一页,将文档分为几节。

object oSectionBreak=WdBreakType.wdSectionBreakNextPage;

WordApp.Selection.Range.InsertBreak(ref oSectionBreak);

给不同的节附不同的页眉

本文将文档分为6节,1、3、5节没有页眉,2、4、6节页眉内容各不相同。

WordDoc.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
Range allRange = WordDoc.Range(ref m_oMissing, ref m_oMissing);
foreach (Section section in allRange.Sections)
{

//各节均不链接前一节
    section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;

//页眉居中
    section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

//设置页眉的字体
    section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Name = "宋体";
    section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Size = 10.5f;
    section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Bold = 0;
 }
//sections[]的编号从1开始

//设置1、3、5节页眉没有横线

allRange.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;

allRange.Sections[3].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;
allRange.Sections[5].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;
//设置2、4、6节页眉内容且有横线
allRange.Sections[2].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
allRange.Sections[2].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text ="第2节"; 
allRange.Sections[4].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
allRange.Sections[4].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "第4节";
allRange.Sections[6].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
allRange.Sections[6].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "第6节";

//从页眉页脚设计模式返回主文档
WordDoc.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;

二、页脚

1、3、5节没有页脚,2、4、6节有页脚,且页脚包括文字和页码,文字居中,页码右对齐

 WordDoc.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;
 Range allRange = WordDoc.Range(ref m_oMissing, ref m_oMissing);

 object oNumberAlignment = WdPageNumberAlignment.wdAlignPageNumberRight;
 object oFirstPage=true;
 foreach(Section section in allRange.Sections)
{
   //设置页脚的文字格式
    section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;

//页脚文字居中
    section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

//页脚文字字体
    section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Name = "宋体";
    section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Size = 10.5f;
    section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Bold = 0;
 }

//设置2、4、6节页脚文字,和页码

//注意前后顺序,要先写入文字,再写入页码,若反过来则页码被文字覆盖掉

allRange.Sections[2].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "第2节"; 
allRange.Sections[2].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oNumberAlignment, ref oFirstPage);


allRange.Sections[4].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "第4节";
allRange.Sections[4].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oNumberAlignment, ref oFirstPage);


allRange.Sections[6].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "第6节";
allRange.Sections[6].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oNumberAlignment, ref oFirstPage); 


WordDoc.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有