目录
在 PowerPoint 中管理页眉和页脚的 .NET API
PowerPoint 演示文稿中的页眉和页脚用于显示幻灯片编号、作者、日期等附加信息。
为了在 PowerPoint 演示文稿中使用页眉和页脚,我们将使用Aspose.Slides for .NET。它是一个 .NET 类库,可让您创建和操作 PowerPoint 和 OpenOffice 文档。您可以从下载部分下载 API 的 DLL 。此外,API 可以通过NuGet安装 。
PM> Install-Package Aspose.Slides.NET
使用 C# 在 PowerPoint 中添加页眉和页脚
以下是使用 C# 在 PowerPoint 演示文稿中添加页眉和页脚的步骤。
以下代码示例展示了如何在 PowerPoint 演示文稿中添加页眉和页脚。
// Load Presentation
Presentation pres = new Presentation("headerTest.pptx");
// Set Footer
pres.HeaderFooterManager.SetAllFootersText("My Footer text");
pres.HeaderFooterManager.SetAllFootersVisibility(true);
// Access and Update Header
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";
}
}
}
}
// Save presentation
pres.Save("HeaderFooter.pptx", SaveFormat.Pptx);
Aspose.Slides for .NET 还允许您在讲义和注释幻灯片中设置页眉和页脚。为此,您可以在主笔记幻灯片或单个幻灯片中应用更改。以下部分涵盖了这两种情况。
更改 Notes Master 的页眉和页脚设置
以下代码示例展示了如何使用 C# 更改笔记母版中的页眉和页脚。
using (Presentation presentation = new Presentation("presentation.pptx"))
{
// Change Header and Footer settings for notes master and all notes slides
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
}
// Save presentation
presentation.Save("testresult.pptx",SaveFormat.Pptx);
}
以下代码示例展示了如何使用 C# 更改笔记幻灯片中的页眉和页脚。
// Load presentation
using (Presentation presentation = new Presentation("presentation.pptx"))
{
// Change Header and Footer settings for first notes slide only
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
}
// Save presentation
presentation.Save("testresult.pptx",SaveFormat.Pptx);
}