Thêm Đầu trang và Chân trang trong PowerPoint C#

Đầu trang và chân trang trong bản trình bày PowerPoint được sử dụng để hiển thị thông tin bổ sung như số trang trình bày, tác giả, ngày tháng, v.v. Trong bài viết này, bạn sẽ học cách thêm và quản lý đầu trang và chân trang trong bản trình bày PowerPoint PPTX / PPT theo cách lập trình bằng C#.

Để làm việc với đầu trang và chân trang trong bản trình bày PowerPoint, chúng tôi sẽ sử dụng Aspose.Slides for .NET. Nó là một thư viện lớp .NET cho phép bạn tạo và thao tác với các tài liệu PowerPoint và OpenOffice. Bạn có thể tải xuống DLL của API từ phần tải xuống. Ngoài ra, API có thể được cài đặt thông qua NuGet.

PM> Install-Package Aspose.Slides.NET

Sau đây là các bước để thêm đầu trang và chân trang trong bản trình bày PowerPoint bằng C#.

Mẫu mã sau đây cho biết cách thêm đầu trang và chân trang trong bản trình bày PowerPoint.

// Tải bản trình bày
Presentation pres = new Presentation("headerTest.pptx");

// Đặt chân trang
pres.HeaderFooterManager.SetAllFootersText("My Footer text");
pres.HeaderFooterManager.SetAllFootersVisibility(true);

// Truy cập và cập nhật tiêu đề
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";
            }
        }
    }
}

// Lưu bản trình bày
pres.Save("HeaderFooter.pptx", SaveFormat.Pptx);

Aspose.Slides for .NET cũng cho phép bạn đặt đầu trang và chân trang trong các trang trình bày tài liệu và ghi chú. Đối với điều này, bạn có thể áp dụng các thay đổi trong trang chiếu ghi chú chính hoặc một trang chiếu riêng lẻ. Các phần sau bao gồm cả hai tình huống.

Mẫu mã sau đây cho thấy cách thay đổi đầu trang và chân trang trong ghi chú chính bằng cách sử dụng C#.

using (Presentation presentation = new Presentation("presentation.pptx"))
{
	// Thay đổi cài đặt Đầu trang và Chân trang cho chính ghi chú và tất cả các trang trình bày ghi chú
	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
	}
  	// Lưu bản trình bày
	presentation.Save("testresult.pptx",SaveFormat.Pptx);
}

Mẫu mã sau đây cho thấy cách thay đổi đầu trang và chân trang trong các trang trình bày ghi chú bằng C#.

// Tải bản trình bày
using (Presentation presentation = new Presentation("presentation.pptx"))
{
	// Thay đổi cài đặt Đầu trang và Chân trang chỉ cho trang chiếu ghi chú đầu tiên
	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
	}
  	// Lưu bản trình bày
	presentation.Save("testresult.pptx",SaveFormat.Pptx);
}

Nhận giấy phép API miễn phí

Nhận giấy phép tạm thời miễn phí để sử dụng Aspose.Slides for .NET mà không có giới hạn đánh giá.

Sự kết luận

Trong bài viết này, bạn đã học cách thêm đầu trang và chân trang trong bản trình bày PowerPoint bằng C#. Hơn nữa, bạn đã thấy cách thay đổi đầu trang và chân trang trong các trang trình bày ghi chú theo lập trình. Bên cạnh đó, bạn có thể khám phá tài liệu để tìm hiểu thêm về Aspose.Slides for .NET. Ngoài ra, bạn có thể đặt câu hỏi của mình qua diễn đàn của chúng tôi.

Xem thêm