
PowerPoint 演示文稿中的頁眉和頁腳用於顯示幻燈片編號、作者、日期等附加信息。在本文中,您將學習如何使用 C# 以編程方式在 PowerPoint PPTX/PPT 演示文稿中添加和管理頁眉和頁腳。
在 PowerPoint 中管理頁眉和頁腳的 .NET API
為了在 PowerPoint 演示文稿中使用頁眉和頁腳,我們將使用 Aspose.Slides for .NET。它是一個 .NET 類庫,可讓您創建和操作 PowerPoint 和 OpenOffice 文檔。您可以從 下載 部分下載 API 的 DLL。此外,API 可以通過 NuGet 安裝。
PM> Install-Package Aspose.Slides.NET
使用 C# 在 PowerPoint 中添加頁眉和頁腳
以下是使用 C# 在 PowerPoint 演示文稿中添加頁眉和頁腳的步驟。
- 首先,使用 Presentation 類創建一個新的演示文稿或加載現有的演示文稿。
- 然後,使用 Presentation.HeaderFooterManager.SetAllFootersText(string) 方法設置頁腳。
- 使用 Presentation.MasterNotesSlideManager.MasterNotesSlide 屬性訪問 IMasterNotesSlide 對像中的主註釋幻燈片。
- 循環遍歷 IMasterNotesSlide.Shapes 集合中的每個形狀。
- 如果 IShape.Placeholder.Type 是 PlaceholderType.Header,則使用 ((IAutoShape)shape).TextFrame.Text 屬性設置頁眉文本。
- 最後,使用 Presentation.Save(string, SaveFormat) 方法保存演示文稿。
以下代碼示例顯示如何在 PowerPoint 演示文稿中添加頁眉和頁腳。
// 負載演示
Presentation pres = new Presentation("headerTest.pptx");
// 設置頁腳
pres.HeaderFooterManager.SetAllFootersText("My Footer text");
pres.HeaderFooterManager.SetAllFootersVisibility(true);
// 訪問和更新標題
IMasterNotesSlide masterNotesSlide = pres.MasterNotesSlideManager.MasterNotesSlide;
if (null != masterNotesSlide)
{
foreach (IShape shape in masterNotesSlide.Shapes)
{
if (shape.Placeholder != null)
{
if (shape.Placeholder.Type == PlaceholderType.Header)
{
((IAutoShape)shape).TextFrame.Text = "HI there new header";
}
}
}
}
// 保存演示文稿
pres.Save("HeaderFooter.pptx", SaveFormat.Pptx);
使用 C# 管理講義和註釋幻燈片中的頁眉和頁腳
Aspose.Slides for .NET 還允許您在講義和註釋幻燈片中設置頁眉和頁腳。為此,您可以在主筆記幻燈片或單個幻燈片中應用更改。以下部分涵蓋了這兩種情況。
更改 Notes Master 的頁眉和頁腳設置
- 首先,使用 Presentation 類創建一個新的演示文稿或加載現有的演示文稿。
- 然後,使用 Presentation.MasterNotesSlideManager.MasterNotesSlide 屬性訪問 IMasterNotesSlide 對像中的主註釋幻燈片。
- 從 IMasterNotesSlide.HeaderFooterManager 屬性中獲取 IMasterNotesSlideHeaderFooterManager 的引用。
- 使用 IMasterNotesSlideHeaderFooterManager 對象更新頁眉頁腳。
- 最後,使用 Presentation.Save(string, SaveFormat) 方法保存演示文稿。
下面的代碼示例顯示瞭如何使用 C# 在 notes master 中更改頁眉和頁腳。
using (Presentation presentation = new Presentation("presentation.pptx"))
{
// 更改筆記母版和所有筆記幻燈片的頁眉和頁腳設置
IMasterNotesSlide masterNotesSlide = presentation.MasterNotesSlideManager.MasterNotesSlide;
if (masterNotesSlide != null)
{
IMasterNotesSlideHeaderFooterManager headerFooterManager = masterNotesSlide.HeaderFooterManager;
headerFooterManager.SetHeaderAndChildHeadersVisibility(true); // make the master notes slide and all child Footer placeholders visible
headerFooterManager.SetFooterAndChildFootersVisibility(true); // make the master notes slide and all child Header placeholders visible
headerFooterManager.SetSlideNumberAndChildSlideNumbersVisibility(true); // make the master notes slide and all child SlideNumber placeholders visible
headerFooterManager.SetDateTimeAndChildDateTimesVisibility(true); // make the master notes slide and all child Date and time placeholders visible
headerFooterManager.SetHeaderAndChildHeadersText("Header text"); // set text to master notes slide and all child Header placeholders
headerFooterManager.SetFooterAndChildFootersText("Footer text"); // set text to master notes slide and all child Footer placeholders
headerFooterManager.SetDateTimeAndChildDateTimesText("Date and time text"); // set text to master notes slide and all child Date and time placeholders
}
// 保存演示文稿
presentation.Save("testresult.pptx",SaveFormat.Pptx);
}
更改註釋幻燈片的頁眉和頁腳設置
- 首先,使用 Presentation 類創建一個新的演示文稿或加載現有的演示文稿。
- 然後,使用 Presentation.Slides[index].NotesSlideManager.NotesSlide 屬性訪問所需幻燈片的 INotesSlide 對象。
- 從 INotesSlide.HeaderFooterManager 屬性獲取 INotesSlideHeaderFooterManager 的引用。
- 使用 INotesSlideHeaderFooterManager 對象更新頁眉頁腳。
- 最後,使用 Presentation.Save(string, SaveFormat) 方法保存演示文稿。
以下代碼示例顯示如何使用 C# 更改備註幻燈片中的頁眉和頁腳。
// 負載演示
using (Presentation presentation = new Presentation("presentation.pptx"))
{
// 僅更改第一張筆記幻燈片的頁眉和頁腳設置
INotesSlide notesSlide = presentation.Slides[0].NotesSlideManager.NotesSlide;
if (notesSlide != null)
{
INotesSlideHeaderFooterManager headerFooterManager = notesSlide.HeaderFooterManager;
if (!headerFooterManager.IsHeaderVisible)
headerFooterManager.SetHeaderVisibility(true); // make this notes slide Header placeholder visible
if (!headerFooterManager.IsFooterVisible)
headerFooterManager.SetFooterVisibility(true); // make this notes slide Footer placeholder visible
if (!headerFooterManager.IsSlideNumberVisible)
headerFooterManager.SetSlideNumberVisibility(true); // make this notes slide SlideNumber placeholder visible
if (!headerFooterManager.IsDateTimeVisible)
headerFooterManager.SetDateTimeVisibility(true); // make this notes slide Date-time placeholder visible
headerFooterManager.SetHeaderText("New header text"); // set text to notes slide Header placeholder
headerFooterManager.SetFooterText("New footer text"); // set text to notes slide Footer placeholder
headerFooterManager.SetDateTimeText("New date and time text"); // set text to notes slide Date-time placeholder
}
// 保存演示文稿
presentation.Save("testresult.pptx",SaveFormat.Pptx);
}
獲取免費的 API 許可證
獲得免費的臨時許可,以在沒有評估限制的情況下使用 Aspose.Slides for .NET。
結論
在本文中,您了解瞭如何使用 C# 在 PowerPoint 演示文稿中添加頁眉和頁腳。此外,您還看到瞭如何以編程方式更改筆記幻燈片中的頁眉和頁腳。此外,您可以瀏覽 文檔 以了解有關 Aspose.Slides for .NET 的更多信息。此外,您可以通過我們的 論壇 提問。