與標題和页脚一起工作

Aspose.Words 讓使用者可以在文件中處理標題和页脚。 標頭是放在頁面頂部的文字,尾腳是放到頁面的底部的文字。 這些區域通常用於插入在文件中所有或某些頁面都應該重複的資訊,例如頁碼、建立日期、公司資訊等。

透過 DocumentBuilder 创建标头或页脚

如果您想要以程式方式加上文件頭或腳列,最簡單的方法是使用 DocumentBuilder 類別來完成它。

接下來的程式碼範例示範了如何在文件中加入標頭和尾欄:

指定標頭或页脚選項

當你在文件中加入標題或页脚時,你可以設定一些進階的屬性。Aspose.Words 提供給用戶 HeaderFooterHeaderFooterCollection 類別,以及 HeaderFooterType 列舉,這讓使用者可以在標題和页腳自訂時有更多的控制權。

指定標題或页脚類型

您可以為一篇文章指定三個不同的標題類型和三個不同的腳注類型:

  1. 第一頁的標題和/或页脚
  2. 每偶數頁的標題和/或页脚 3。 奇數頁的標頭和/或页脚

接下來的程式碼範例說明如何在奇數頁面添加標頭:

指定是否要為第一頁顯示不同的標頭或页脚

如上所述,您也可以為第一頁設定不同的標頭或尾列。 要做到這一點,你需要將旗標 DifferentFirstPageHeaderFooter 設定為 true,然後指定 HeaderFirstFooterFirst 值。

接下來的程式碼範例示範如何只為第一頁設定標頭:

// 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 讓您在使用不同的orientations和頁面尺寸時,可以設定頭欄或尾欄的外觀。

以下示例顯示如何做到這點:

如何移除僅標頭或僅腳線

文件中的每個節段可以有最多三個標題和最多三個页腳(用於第一頁、偶數頁和奇數頁)。 如果你想從文件中移除此所有標題或所有腳注,你需要迴圈通過所有部分並移除每個對應的標題節點或腳注節點。

以下範例代碼示範了如何從所有節點中移除腳底線,但保留標題。 您透過類似的方法可以移除頭條:

// 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");