다음을 통해 공유


프로그래밍 방식으로 문서에 머리글 및 바닥글 추가

SectionHeaders 속성 및 Footers 속성을 사용하여 문서의 머리글 및 바닥글에 텍스트를 추가할 수 있습니다. 문서의 각 섹션에는 다음 세 개의 머리글과 바닥글이 포함됩니다.

문서 수준 사용자 지정

다음 코드 예제를 사용하려면 프로젝트의 ThisDocument 클래스에서 실행합니다.

문서의 바닥글에 텍스트를 추가하려면

  1. 다음 코드 예제에서는 문서의 각 섹션에 대한 기본 바닥글에 삽입할 텍스트의 글꼴을 설정하고 바닥글에 텍스트를 삽입합니다.

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

문서의 머리글에 텍스트를 추가하려면

  1. 다음 코드 예제에서는 문서의 각 머리글에 페이지 번호를 표시할 필드를 추가하고 텍스트가 머리글 오른쪽에 정렬되도록 단락 맞춤을 설정합니다.

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

VSTO 추가 기능

다음 코드 예제를 사용하려면 프로젝트의 ThisAddIn 클래스에서 실행합니다.

문서의 바닥글에 텍스트를 추가하려면

  1. 다음 코드 예제에서는 문서의 각 섹션에 대한 기본 바닥글에 삽입할 텍스트의 글꼴을 설정하고 바닥글에 텍스트를 삽입합니다. 이 코드 예제에서는 활성 문서를 사용합니다.

    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 = "Confidential";
    }
    

문서의 머리글에 텍스트를 추가하려면

  1. 다음 코드 예제에서는 문서의 각 머리글에 페이지 번호를 표시할 필드를 추가하고 텍스트가 머리글 오른쪽에 정렬되도록 단락 맞춤을 설정합니다. 이 코드 예제에서는 활성 문서를 사용합니다.

    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;
    }