How to Set Text to Header in Presentation Using Aspose.Slides for .NET 18.3?

Hi everyone again;

We need your help.
We use Aspose Slides for .Net version 18.3, and we try to set text to header.

I look this : Presentation Header and Footer|Aspose.Slides Documentation

But visual studio doesn’t find HeaderFooter, it finds HeaderFooterManager. I try to use that but doesn’t exists IsHeaderVisible and SetHeaderText like that link. Just exists IsDateTimeVisible and SetDateTimeText.

How can we manupilate headers of presentation files?

@hvkktr,
Thank you for posting the question.

Unfortunately, you cannot check if a header is visible or set its visibility using the version 18.3. You can only set text to a visible header like this:

if (slide.NotesSlideManager.NotesSlide == null)
    slide.NotesSlideManager.AddNotesSlide();

foreach (var shape in slide.NotesSlideManager.NotesSlide.Shapes)
{
    if (shape.Placeholder != null && 
        shape.Placeholder.Type == PlaceholderType.Header)
    {
        var autoShape = (IAutoShape)shape;
        autoShape.TextFrame.Text = "My text";
    }
}

We recommend that you use the latest version of Aspose.Slides for .NET.

@hvkktr you are using a very old version (more than 5 years) of Aspose.Slides API, with the release of the version 18.4 several changes related to header and Footer were introduced, like the deprecation of the HeaderFooterManagerClass. Our documentation and support target the last available version of the Aspose packages.
Note: The version 18.4 is the first version that support the example in the documentation.

Thank you for your interest.

I have one more question. How can I set font size and type? In Aspose.Cells I I can do like this:

pageSetup.SetHeader(0, "&\"Times New Roman,Bold\"&12Left Header Sample ");

I mean "&“Times New Roman,Bold”&12Left Header Sample " usage on slides header doesn’t work. How can I do that on Slides?

And I need to set DateAndTime. I used SetDateAndTimeText and visibility and worked. But put the date and time to left-down corner but I need to on right-top corner.

@hvkktr,
I am working on the issues you described above and will get back to you as soon as possible.

1 Like

In Aspose.Slides, the Header, Footer, and DateAndTime are shapes, so you can modify these elements as regular shapes. For example, to set the font of the footer, you can use the following code:

var slide = pres.Slides[0];

foreach (var shape in slide.Shapes)
{
    if(shape.Placeholder == null || shape.Placeholder.Type != PlaceholderType.Footer)
    {
        continue;
    }

    ITextFrame tf = ((IAutoShape)shape).TextFrame;
    if (tf != null)
    {
        foreach(var p in tf.Paragraphs)
        {
            foreach(var por in p.Portions)
            {
                por.PortionFormat.LatinFont = new FontData("Times New Roman");
            }
        }
    }
}

And I need to set DateAndTime. I used SetDateAndTimeText and visibility and worked. But put the date and time to left-down corner but I need to on right-top corner.

The same applies to the DateAndTime element, which is positioned by default in the lower left corner of the Slide, but you can change its position by directly accessing to the shape. See the following example:

foreach (var shape in slide.Shapes)
{
    if (shape.Placeholder == null || shape.Placeholder.Type != PlaceholderType.DateAndTime)
    {
        continue;
    }

    shape.X = 0;
    shape.Y = 0;
}
1 Like

Thank you for your answer. I’ll try that.

Hi and thanks again;

I tried your solution and debug it. But I got just 2 Placeholder Types of Shape. Subtitle and CenteredTitle. I couldn’t get Header and DateAndTime types of Placeholder.

What could be a reason?

@hvkktr in order to add the shape to the presentation you need to set the visibility of the elements to true. For example to set the header visibility and add the shape to the presentation you need to use the method:

pres.HeaderFooterManager.SetAllHeadersVisibility(true);

You can also set the text and visibility of footers and date and time shapes for specific slides using the HeaderFooterManager property of each individual slide. Additionally, you can check if the required shape is present in the slide using the HeaderFooterManager property. Note: The headers can only be accessed from the presentation instance.

1 Like