Dodaj nagłówek i stopkę w programie PowerPoint C#

Nagłówek i stopka w prezentacjach programu PowerPoint służą do wyświetlania dodatkowych informacji, takich jak numer slajdu, autor, data itp. Z tego artykułu dowiesz się, jak programowo dodawać nagłówki i stopki w prezentacjach PowerPoint PPTX/PPT przy użyciu języka C# i zarządzać nimi.

Aby pracować z nagłówkiem i stopką w prezentacjach PowerPoint, użyjemy Aspose.Slides for .NET. Jest to biblioteka klas .NET, która umożliwia tworzenie i manipulowanie dokumentami PowerPoint i OpenOffice. Bibliotekę DLL API można pobrać z sekcji pliki do pobrania. Ponadto interfejs API można zainstalować za pomocą NuGet.

PM> Install-Package Aspose.Slides.NET

Poniżej przedstawiono kroki dodawania nagłówka i stopki w prezentacji programu PowerPoint przy użyciu języka C#.

Poniższy przykładowy kod pokazuje, jak dodać nagłówek i stopkę w prezentacji programu PowerPoint.

// Załaduj prezentację
Presentation pres = new Presentation("headerTest.pptx");

// Ustaw stopkę
pres.HeaderFooterManager.SetAllFootersText("My Footer text");
pres.HeaderFooterManager.SetAllFootersVisibility(true);

// Uzyskaj dostęp i zaktualizuj nagłówek
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";
            }
        }
    }
}

// Zapisz prezentację
pres.Save("HeaderFooter.pptx", SaveFormat.Pptx);

Aspose.Slides for .NET pozwala również ustawić nagłówek i stopkę w materiałach informacyjnych i slajdach notatek. W tym celu możesz zastosować zmiany na slajdzie z notatkami wzorcowymi lub na pojedynczym slajdzie. W poniższych sekcjach omówiono oba scenariusze.

Poniższy przykładowy kod pokazuje, jak zmienić nagłówek i stopkę we wzorcu notatek przy użyciu języka C#.

using (Presentation presentation = new Presentation("presentation.pptx"))
{
	// Zmień ustawienia nagłówka i stopki dla wzorca notatek i wszystkich slajdów notatek
	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
	}
  	// Zapisz prezentację
	presentation.Save("testresult.pptx",SaveFormat.Pptx);
}

Poniższy przykładowy kod pokazuje, jak zmienić nagłówek i stopkę na slajdach notatek przy użyciu języka C#.

// Załaduj prezentację
using (Presentation presentation = new Presentation("presentation.pptx"))
{
	// Zmień ustawienia nagłówka i stopki tylko dla pierwszego slajdu z notatkami
	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
	}
  	// Zapisz prezentację
	presentation.Save("testresult.pptx",SaveFormat.Pptx);
}

Uzyskaj bezpłatną licencję API

Uzyskaj bezpłatną tymczasową licencję na korzystanie z Aspose.Slides dla platformy .NET bez ograniczeń ewaluacyjnych.

Wniosek

W tym artykule nauczyłeś się dodawać nagłówek i stopkę w prezentacjach programu PowerPoint przy użyciu języka C#. Ponadto widziałeś, jak programowo zmienić nagłówek i stopkę w notatkach. Poza tym możesz zapoznać się z dokumentacją, aby dowiedzieć się więcej o Aspose.Slides for .NET. Możesz także zadawać pytania za pośrednictwem naszego forum.

Zobacz też