Free Support Forum - aspose.com

[.Net] Modifing Handouts header

Hi,

I am trying to modify the handout header and footer values of a presentation. I am talking about the options you can set in power point in the second tab of the header / footer configuration about notes and handouts:
image.png (8.4 KB)

I tried the following code to modify the data:

            var masterNoteSlide = pres.MasterNotesSlideManager.MasterNotesSlide;
            foreach (var shape in masterNoteSlide.Shapes)
            {
                if (shape.Placeholder != null)
                {
                    if (shape.Placeholder.Type == PlaceholderType.Header)
                    {
                        ((IAutoShape) shape).AddTextFrame("New header text");
                    }
                }
            }

and later render the power point to pdf in notes mode:

            var po = new PdfOptions();
            pres.Save("file.pdf", SaveFormat.PdfNotes, po);

If I open the created PDF file I still see the old value header handout in the top left corner:
image.png (3.4 KB)

I am pretty sure the code I have written should work, as if I inspect the individual shape in the upper foreach loop and check its TextFrame.Text property, it has the original text defined in my example presentation:
image.png (30.6 KB)

So I assume the text is not updated correctly if I call AddTextFrame method, even it´s description says, it would change the frames text if it already exists.
I am facing the same issue with the DateAndTime and Footer placeholder shapes.

Also another question: What would be the best way to create those shapes if they do not already exist? As if I check the Shapes.AddAutoShape I would have to set position etc. manually.

I attached you my example presentation and the PDF output.
files.zip (246.5 KB)

Kind regards,
Daniel

@Serraniel,

I have worked with the presentation file and sample code shared by you. I have been able to observe the issue specified on my end. An issue with ID SLIDESNET-39527 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be automatically notified once the issue will be fixed.

The issues you have found earlier (filed as SLIDESNET-39527) have been fixed in Aspose.Slides for .NET 18.1. Please try using the latest release version and in case you experience any issue or you have any further query, please feel free to contact.

Hi @Adnan.Ahmad,

I meanwhile have updated to Aspose.Slides 18.1 via Nuget and tried again to modify the header content of the notes slide. This is the full code I am using now:

var pres = new Presentation("example_new_test.pptx");

var masterNoteSlide = pres.MasterNotesSlideManager.MasterNotesSlide;
foreach (var shape in masterNoteSlide.Shapes)
{
   if (shape.Placeholder != null)
   {
   	if (shape.Placeholder.Type == PlaceholderType.Header)
   	{
   		((AutoShape) shape).TextFrame.Text = "New header text";
   	}
   }
}


var po = new PdfOptions
{
   DrawSlidesFrame = false,
   ShowHiddenSlides = false,
};
po.NotesCommentsLayouting.CommentsPosition = CommentsPositions.None;
po.NotesCommentsLayouting.NotesPosition = NotesPositions.BottomTruncated;

pres.Save("file.pdf", SaveFormat.Pdf, po);

Using the attached power point file from my first post and this code, I still get a PDF as output with header handout instead of New header text written in the handout header area.

Maybe I am doing something wrong so I will try to explain again what result I am targeting:
I am using the handout PDF output and want to modify the content of the following four areas in the output:
image.png (15.1 KB)
Those can be set in power point by navigating to the header and footer dialog, switching to the notes and handout tab and using the following configuration:
image.png (43.9 KB)

As I am not sure as my code does the wrong thing (even I expect it to work as in the debugger I for example can see the header handout as value of the Text property of the shape), may you please provide me a working example on how to modify those four areas (I am aware of the fact the page number cannot be changed, but activated) which also activates those areas in the case the checkboxes in power point originally have been unchecked?

Kind regards,
Daniel

@Serraniel,

I have observed your comments. I have also shared a piece of code with you. This will help you to achieve your requirements. Please share feedback with us if there is still an issue.

public static void TestPdfHeader()
{
Presentation pres = new Presentation(path + “example_new_test.pptx”);

        var masterNoteSlide = pres.MasterNotesSlideManager.MasterNotesSlide;
        UpdatePlaceholdersText(masterNoteSlide, PlaceholderType.Header, "New Header Text");

        foreach (ISlide slide in pres.Slides)
        {
            INotesSlide notes = slide.NotesSlideManager.NotesSlide;
            if (notes != null)
                UpdatePlaceholdersText(notes, PlaceholderType.Header, "New Header Text");
        }

        pres.Save(path + "file.pptx", SaveFormat.Pptx);

        pres = new Presentation(path + "file.pptx");
        PdfOptions opts = new PdfOptions();
        opts.NotesCommentsLayouting.NotesPosition = NotesPositions.BottomFull;
        pres.Save(path + "file.pdf", SaveFormat.Pdf, opts);
    }

//

    public static void UpdatePlaceholdersText(IBaseSlide slide, PlaceholderType placeholderType, string text)
    {
        foreach (IShape shape in slide.Shapes)
        {
            if (shape.Placeholder != null)
            {
                if (shape.Placeholder.Type == placeholderType)
                    ((IAutoShape)shape).TextFrame.Text = text;
            }
        }
    }

Hi Adnan,

thanks for your reply. I have tried to adjust the code to also make it work with presentations, who do not have set andy notes slides yet (created a new empty presentation with power point and only added two slides). So the first thing I recognized was, that MasterNotesSlide was null on the attached power point presentation so my first change was to add one by using pres.MasterNotesSlideManager.SetDefaultMasterNotesSlide();. However this corrupts the presentation in some way. The output pdf is empty (except page numbers) and if I save this as a power point (out.pptx), power point cannot open this any more.

static void Main(string[] args)
{
	var pres = new Presentation("example_empty.pptx");

	var masterNoteSlide = pres.MasterNotesSlideManager.MasterNotesSlide ??
						  pres.MasterNotesSlideManager.SetDefaultMasterNotesSlide();
	if (masterNoteSlide != null)
	{
		UpdatePlaceholdersText(masterNoteSlide, PlaceholderType.Header, "New Header Text");
		UpdatePlaceholdersText(masterNoteSlide, PlaceholderType.DateAndTime, "Date Time Text");
		UpdatePlaceholdersText(masterNoteSlide, PlaceholderType.Footer, "Footer Text");
	}

	foreach (ISlide slide in pres.Slides)
	{
		INotesSlide notes = slide.NotesSlideManager.NotesSlide ?? slide.NotesSlideManager.AddNotesSlide();
		UpdatePlaceholdersText(notes, PlaceholderType.Header, "New Header Text");
		UpdatePlaceholdersText(notes, PlaceholderType.DateAndTime, "Date Time Text");
		UpdatePlaceholdersText(notes, PlaceholderType.Footer, "Footer Text");
	}
	
	var po = new PdfOptions
	{
		DrawSlidesFrame = false,
		ShowHiddenSlides = false,
	};
	po.NotesCommentsLayouting.CommentsPosition = CommentsPositions.None;
	po.NotesCommentsLayouting.NotesPosition = NotesPositions.BottomFull;
	
	pres.Save("out.pptx", SaveFormat.Pptx);
	pres.Save("file.pdf", SaveFormat.Pdf, po);
}

public static void UpdatePlaceholdersText(IBaseSlide slide, PlaceholderType placeholderType, string text)
{
	foreach (IShape shape in slide.Shapes)
	{
		if (shape.Placeholder?.Type == placeholderType)
			((IAutoShape) shape).TextFrame.Text = text;
	}
}

Additionally, in the UpldatePlaceholder method you provided me, you are only changing existing shapes. If I set a new master note slide for example, those shapes do not yet exist. I looked a bit around with adding a shape. Is there an easy way to create the required shapes which are equal to the previous mentioned “areas” you can configure in power point header footer dialog? Or do I have to add AutoShapes and try setting it´s correct position myself? Is it possible to set the Placeholder?

Kind regards and thanks for your help,
Daniel

Hi Adnan,

thanks for your reply. I have tried to adjust the code to also make it work with presentations, who do not have set andy notes slides yet (created a new empty presentation with power point and only added two slides). So the first thing I recognized was, that MasterNotesSlide was null on the attached power point presentation so my first change was to add one by using pres.MasterNotesSlideManager.SetDefaultMasterNotesSlide();. However this corrupts the presentation in some way. The output pdf is empty (except page numbers) and if I save this as a power point (out.pptx), power point cannot open this any more.

static void Main(string[] args)
{
	var pres = new Presentation("example_empty.pptx");

	var masterNoteSlide = pres.MasterNotesSlideManager.MasterNotesSlide ??
						  pres.MasterNotesSlideManager.SetDefaultMasterNotesSlide();
	if (masterNoteSlide != null)
	{
		UpdatePlaceholdersText(masterNoteSlide, PlaceholderType.Header, "New Header Text");
		UpdatePlaceholdersText(masterNoteSlide, PlaceholderType.DateAndTime, "Date Time Text");
		UpdatePlaceholdersText(masterNoteSlide, PlaceholderType.Footer, "Footer Text");
	}

	foreach (ISlide slide in pres.Slides)
	{
		INotesSlide notes = slide.NotesSlideManager.NotesSlide ?? slide.NotesSlideManager.AddNotesSlide();
		UpdatePlaceholdersText(notes, PlaceholderType.Header, "New Header Text");
		UpdatePlaceholdersText(notes, PlaceholderType.DateAndTime, "Date Time Text");
		UpdatePlaceholdersText(notes, PlaceholderType.Footer, "Footer Text");
	}
	
	var po = new PdfOptions
	{
		DrawSlidesFrame = false,
		ShowHiddenSlides = false,
	};
	po.NotesCommentsLayouting.CommentsPosition = CommentsPositions.None;
	po.NotesCommentsLayouting.NotesPosition = NotesPositions.BottomFull;
	
	pres.Save("out.pptx", SaveFormat.Pptx);
	pres.Save("file.pdf", SaveFormat.Pdf, po);
}

public static void UpdatePlaceholdersText(IBaseSlide slide, PlaceholderType placeholderType, string text)
{
	foreach (IShape shape in slide.Shapes)
	{
		if (shape.Placeholder?.Type == placeholderType)
			((IAutoShape) shape).TextFrame.Text = text;
	}
}

Additionally, in the UpldatePlaceholder method you provided me, you are only changing existing shapes. If I set a new master note slide for example, those shapes do not yet exist. I looked a bit around with adding a shape. Is there an easy way to create the required shapes which are equal to the previous mentioned “areas” you can configure in power point header footer dialog? Or do I have to add AutoShapes and try setting it´s correct position myself? Is it possible to set the Placeholder?

Kind regards and thanks for your help,
Daniel

examples.zip (56.2 KB)

@Serraniel,

I have observed your comments. If I understand correctly you need a HeaderFooterManager for MasterNotes/Notes. This managers exists only for Slides/Layouts/Masters now. Can you please confirm this.

Yes, that sounds pretty much for what I am looking for as I am trying to set those 4 header and footer fields power point provides in the UI.

@Serraniel,

I have shared your complete requirements in ticket already shared with you. We have reopened issue and i have also discussed issue with our product team. We will share good news with you soon. I request for your patience.

Thanks, I will wait for the reply then :slight_smile:

@Serraniel,

Sure, we will share good news with you soon.

Hi,

I noticed that the issue status has changed to Closed for a while now, so I assume there is any feedback you may give me? I haven´t seen the issue in the release notes.

Kind regards,
Daniel

@Serraniel,

I have observed your comments. I like to inform this issue is going to resolved tentatively in Aspose.Slides for Java 18.4. We will share good news with you soon.

I have tried to use the above code but i am still not able to update the Date Time in handouts header. Can you please check and let me am i doing anything wrong

Version used is 18.6.0

using (Aspose.Slides.Presentation presentation = new Aspose.Slides.Presentation(“D:\Convert\Test.pptx”))
{
// presentation.UpdateDateTimeFields = false;
DateTime lastSavedDateTime = presentation.DocumentProperties.LastSavedTime;
IPresentationHeaderFooterManager presentationHeaderFooter = presentation.HeaderFooterManager;
presentationHeaderFooter.SetAllDateTimesText(lastSavedDateTime.ToString());

            var masterNoteSlide = presentation.MasterNotesSlideManager.MasterNotesSlide;
            if (masterNoteSlide != null)
            {
                UpdatePlaceholdersText(masterNoteSlide, PlaceholderType.Header, "New Header Text");
                UpdatePlaceholdersText(masterNoteSlide, PlaceholderType.DateAndTime, lastSavedDateTime.ToString());                   
            }

            foreach (ISlide slide in presentation.Slides)
            {
                INotesSlide notes = slide.NotesSlideManager.NotesSlide;
                UpdatePlaceholdersText(masterNoteSlide, PlaceholderType.Header, "New Header Text");
                UpdatePlaceholdersText(notes, PlaceholderType.DateAndTime, lastSavedDateTime.ToString());
            }

PdfOptions pdfOptions = new PdfOptions();
pdfOptions.DrawSlidesFrame = false;
pdfOptions.NotesCommentsLayouting.NotesPosition = NotesPositions.BottomFull;
pdfOptions.NotesCommentsLayouting.CommentsPosition = CommentsPositions.Right;

            presentation.Save("D:\\Convert\\Date_ppt.pdf", Aspose.Slides.Export.SaveFormat.Pdf, pdfOptions);                

}

public static void UpdatePlaceholdersText(IBaseSlide slide, PlaceholderType placeholderType, string text)
{
foreach (IShape shape in slide.Shapes)
{
if (shape.Placeholder?.Type == placeholderType)
((IAutoShape)shape).TextFrame.Text = text;
}
}

Attaching the source and converted file.Date_ppt.pdf (109.4 KB)

@pradeepdone,

I have observed the PDF file shared by you. Can you please provide the source presentation and desired output PDF for reference that I may use on my end to investigate it further.

I am attaching the source and the converted pdf in a zip file for evaluation
Source_pdf.zip (130.5 KB)

The desired output i am looking for is update the date time fields(These date time is inserted and opted as update automatically) with last saved date time

Hi @pradeepdone,

we use the following snippet to update the notes “header” and “footer” in all slides. The PresentationHeaderFooterType is a custom enum which just maps the corners of the PDF to the properties of the notes slides.

foreach (var notes in Presentation.Slides.Select(slide => slide.NotesSlideManager.NotesSlide ?? slide.NotesSlideManager.AddNotesSlide()))
{
	switch (headerFooterType)
	{
		case PresentationHeaderFooterType.TopLeft:
			notes.HeaderFooterManager.SetHeaderVisibility(true);
			notes.HeaderFooterManager.SetHeaderText(text);
			break;
		case PresentationHeaderFooterType.TopRight:
			notes.HeaderFooterManager.SetDateTimeVisibility(true);
			notes.HeaderFooterManager.SetDateTimeText(text);
			break;
		case PresentationHeaderFooterType.BottomLeft:
			notes.HeaderFooterManager.SetFooterVisibility(true);
			notes.HeaderFooterManager.SetFooterText(text);
			break;
		case PresentationHeaderFooterType.BottomRight:
			notes.HeaderFooterManager.SetSlideNumberVisibility(true);
			break;
		case PresentationHeaderFooterType.NotesText:
			notes.NotesTextFrame.Text = text;
			break;
		default:
			throw new ArgumentOutOfRangeException(nameof(headerFooterType), headerFooterType, null);
	}
}

Now i am able to update the datetime in the Handouts headers but have couple of more queries in this.

  1. How do we do the same if the content in the slide has date time and need to update it.
  2. After converting to PDF i cant see the date that is inserted in the notes