머리글 및 바닥글 작업

Aspose.Words를 사용하면 사용자가 문서의 머리글과 바닥글을 사용할 수 있습니다. 머리글은 페이지 상단에 표시되는 텍스트이고, 바닥글은 페이지 하단에 표시되는 텍스트입니다. 일반적으로 이러한 영역은 페이지 번호, 작성 날짜, 회사 정보 등과 같이 문서의 전체 또는 일부 페이지에서 반복되어야 하는 정보를 삽입하는 데 사용됩니다.

DocumentBuilder를 사용하여 머리글 또는 바닥글 만들기

프로그래밍 방식으로 문서 머리글이나 바닥글을 추가하려는 경우 가장 쉬운 방법은 DocumentBuilder 클래스를 사용하는 것입니다.

다음 코드 예제에서는 문서 페이지에 머리글과 바닥글을 추가하는 방법을 보여줍니다

머리글 또는 바닥글 옵션 지정

문서에 머리글이나 바닥글을 추가할 때 일부 고급 속성을 설정할 수 있습니다. Aspose.Words는 사용자에게 머리글 및 바닥글 사용자 정의 프로세스를 더 효과적으로 제어할 수 있는 HeaderFooterType 열거형과 HeaderFooterHeaderFooterCollection 클래스를 제공합니다.

머리글 또는 바닥글 유형 지정

하나의 문서에 대해 세 가지 머리글 유형과 세 가지 바닥글 유형을 지정할 수 있습니다

  1. 첫 페이지의 머리글 및/또는 바닥글
  2. 짝수 페이지의 머리글 및/또는 바닥글
  3. 홀수 페이지의 머리글 및/또는 바닥글

다음 코드 예제에서는 홀수 문서 페이지에 헤더를 추가하는 방법을 보여줍니다

첫 번째 페이지에 다른 머리글 또는 바닥글을 표시할지 여부 지정

위에서 설명한 것처럼 첫 번째 페이지에 다른 머리글이나 바닥글을 설정할 수도 있습니다. 이렇게 하려면 DifferentFirstPageHeaderFooter 플래그를 true로 설정한 다음 HeaderFirst 또는 FooterFirst 값을 지정해야 합니다.

다음 코드 예제에서는 첫 번째 페이지에만 헤더를 설정하는 방법을 보여줍니다

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify that we want different headers and footers for first page.
builder.PageSetup.DifferentFirstPageHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.Write("Header for the first page.");
builder.MoveToHeaderFooter(HeaderFooterType.FooterFirst);
builder.Write("Footer for the first page.");
builder.MoveToSection(0);
builder.Writeln("Page 1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page 2");
doc.Save(ArtifactsDir + "WorkingWithHeadersAndFooters.DifferentFirstPage.docx");

홀수 또는 짝수 페이지에 대해 서로 다른 머리글이나 바닥글을 표시할지 여부 지정

다음으로, 문서의 홀수 페이지와 짝수 페이지에 서로 다른 머리글이나 바닥글을 설정하고 싶을 것입니다. 이렇게 하려면 OddAndEvenPagesHeaderFooter 플래그를 true로 설정한 다음 HeaderPrimaryHeaderEven 또는 FooterPrimaryFooterEven 값을 지정해야 합니다.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify that we want different headers and footers for even and odd pages.
builder.PageSetup.OddAndEvenPagesHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
builder.Write("Header for even pages.");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Header for odd pages.");
builder.MoveToHeaderFooter(HeaderFooterType.FooterEven);
builder.Write("Footer for even pages.");
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Footer for odd pages.");
builder.MoveToSection(0);
builder.Writeln("Page 1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page 2");
doc.Save(ArtifactsDir + "WorkingWithHeadersAndFooters.OddEvenPages.docx");

헤더에 절대 위치 이미지 삽입

머리글이나 바닥글에 이미지를 삽입하려면 HeaderPrimary 헤더 유형이나 FooterPrimary 바닥글 유형과 InsertImage 방식을 사용합니다.

다음 코드 예제에서는 헤더에 이미지를 추가하는 방법을 보여줍니다

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.InsertImage(ImagesDir + "Logo.jpg", RelativeHorizontalPosition.RightMargin, 10,
RelativeVerticalPosition.Page, 10, 50, 50, WrapType.Through);
doc.Save(ArtifactsDir + "WorkingWithHeadersAndFooters.InsertImage.docx");
view raw insert-image.cs hosted with ❤ by GitHub

머리글 또는 바닥글 텍스트의 글꼴 및 단락 속성 설정

Aspose.Words를 사용하면 글꼴 및 단락 속성을 설정하고 HeaderPrimary 머리글 유형 또는 FooterPrimary 바닥글 유형을 사용할 수 있을 뿐만 아니라 문서 본문에 사용하는 글꼴 및 단락 작업을 위한 메서드 및 속성을 사용할 수 있습니다.

다음 코드 예제에서는 헤더의 텍스트를 Arial, 굵은 글꼴, 크기 14 및 가운데 맞춤으로 설정하는 방법을 보여줍니다

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Name = "Arial";
builder.Font.Bold = true;
builder.Font.Size = 14;
builder.Write("Header for page.");
doc.Save(ArtifactsDir + "WorkingWithHeadersAndFooters.FontProps.docx");
view raw font-props.cs hosted with ❤ by GitHub

머리글이나 바닥글에 페이지 번호 삽입

필요한 경우 머리글이나 바닥글에 페이지 번호를 추가할 수 있습니다. 이를 수행하려면 HeaderPrimary 헤더 유형 또는 FooterPrimary 바닥글 유형과 InsertField 메소드를 사용하여 필수 필드를 추가하십시오.

다음 코드 예제에서는 오른쪽 바닥글에 페이지 번호를 추가하는 방법을 보여줍니다

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("Page ");
builder.InsertField("PAGE", "");
builder.Write(" of ");
builder.InsertField("NUMPAGES", "");
doc.Save(ArtifactsDir + "WorkingWithHeadersAndFooters.PageNumbers.docx");
view raw page-numbers.cs hosted with ❤ by GitHub

이전 섹션에 정의된 머리글 또는 바닥글 사용

이전 섹션의 머리글이나 바닥글을 복사해야 하는 경우에도 그렇게 할 수 있습니다.

다음 코드 예제에서는 이전 섹션의 머리글 또는 바닥글을 복사하는 방법을 보여줍니다

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
/// <summary>
/// Clones and copies headers/footers form the previous section to the specified section.
/// </summary>
private void CopyHeadersFootersFromPreviousSection(Section section)
{
Section previousSection = (Section)section.PreviousSibling;
if (previousSection == null)
return;
section.HeadersFooters.Clear();
foreach (HeaderFooter headerFooter in previousSection.HeadersFooters)
section.HeadersFooters.Add(headerFooter.Clone(true));
}

다양한 페이지 방향 및 페이지 크기를 사용할 때 머리글 또는 바닥글 모양 확인

Aspose.Words를 사용하면 다양한 방향과 페이지 크기를 사용할 때 머리글이나 바닥글의 모양을 제공할 수 있습니다.

다음 예에서는 이를 수행하는 방법을 보여줍니다

머리글만 또는 바닥글만 제거하는 방법

문서의 각 섹션에는 최대 3개의 머리글과 최대 3개의 바닥글(첫 번째 페이지, 짝수 페이지, 홀수 페이지)이 있을 수 있습니다. 문서의 모든 머리글이나 바닥글을 제거하려면 모든 섹션을 반복하고 해당 머리글 노드나 바닥글 노드를 각각 제거해야 합니다.

다음 코드 예제에서는 모든 섹션에서 모든 바닥글을 제거하고 머리글은 그대로 유지하는 방법을 보여줍니다. 비슷한 방법으로 헤더만 제거할 수 있습니다

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Header and footer types.docx");
foreach (Section section in doc)
{
// Up to three different footers are possible in a section (for first, even and odd pages)
// we check and delete all of them.
HeaderFooter footer = section.HeadersFooters[HeaderFooterType.FooterFirst];
footer?.Remove();
// Primary footer is the footer used for odd pages.
footer = section.HeadersFooters[HeaderFooterType.FooterPrimary];
footer?.Remove();
footer = section.HeadersFooters[HeaderFooterType.FooterEven];
footer?.Remove();
}
doc.Save(ArtifactsDir + "RemoveContent.RemoveFooters.docx");