Use C to implement the method of inserting the header footer in word

  • 2020-08-22 22:38:54
  • OfStack

The operation of Word is a function that many programs have. This paper demonstrates the method of inserting the header and footer in word by using C# for your reference. The specific method is as follows:

1. Insert footer method:


public void InsertFooter(string footer) 
{ 
  if (ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView || 
    ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView) 
  { 
    ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView; 
  } 
 
  ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter; 
  this.Application.Selection.HeaderFooter.LinkToPrevious = false; 
  this.Application.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; 
  ActiveWindow.ActivePane.Selection.InsertAfter(footer); 
 
  // Jump out of the header footer Settings  
  ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; 
 
} 

2. Methods on msdn:


foreach (Word.Section wordSection in this.Application.ActiveDocument.Sections) 
{ 
    Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; 
    footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed; 
    footerRange.Font.Size = 20; 
    footerRange.Text = " The footer   The footer "; 
} 

foreach (Word.Section section in this.Application.ActiveDocument.Sections) 
{ 
    Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; 
    headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage); 
    headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; 
} 

I hope this example will be helpful to your C# programming.


Related articles: