首页 > 代码库 > 使用C#在word中插入页眉页脚

使用C#在word中插入页眉页脚

 //插入页脚

[csharp] view plaincopyprint?
  1. public void InsertFooter(string footer)  
  2. {  
  3.     if (ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView ||  
  4.         ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)  
  5.     {  
  6.         ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;  
  7.     }  
  8.   
  9.     ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;  
  10.     this.Application.Selection.HeaderFooter.LinkToPrevious = false;  
  11.     this.Application.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;  
  12.     ActiveWindow.ActivePane.Selection.InsertAfter(footer);  
  13.   
  14.     //跳出页眉页脚设置  
  15.     ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;  
  16.   
  17. }  


 

 

msdn上的方法

[csharp] view plaincopyprint?
    1. foreach (Word.Section wordSection in this.Application.ActiveDocument.Sections)  
    2.            {  
    3.                Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;  
    4.                footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;  
    5.                footerRange.Font.Size = 20;  
    6.                footerRange.Text = "页脚 页脚";  
    7.            }  
    8.   
    9.            foreach (Word.Section section in this.Application.ActiveDocument.Sections)  
    10.            {  
    11.                Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;  
    12.                headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);  
    13.                headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;  
    14.            }