Free Support Forum - aspose.com

[.Net] Modifing Handouts header

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

To update the existing content I guess you can use the same code and ich just overrides the existing content. That´s what IU would assume but I neither have tried right now or am the Aspose support so they may can explain you further questions :roll_eyes: .

We use the following code to convert to PDF:

                    var options = new PdfOptions
                    {
                        DrawSlidesFrame = true,
                        ShowHiddenSlides = false,
                        JpegQuality = 100,
                    };
                    options.NotesCommentsLayouting.CommentsPosition = CommentsPositions.None;
                    options.NotesCommentsLayouting.NotesPosition = NotesPositions.BottomFull;
                    Presentation.Save(dstFile, SaveFormat.Pdf, options);

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

Below is the code i use and attaching the source and converted pdfSource.zip (130.7 KB)

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());

            foreach (var notes in presentation.Slides.Select(slide => slide.NotesSlideManager.NotesSlide))
            {
                if(notes != null)
                notes.HeaderFooterManager.SetDateTimeText(lastSavedDateTime.ToString());                    
            }        

            foreach (ISlide slide in presentation.Slides)
            {
                if ((slide.Hidden))
                {
                    slide.Hidden = false;
                }




                IShapeCollection sc = slide.Shapes;
            int count = sc.Count;
            bool fieldremoveflag = false;

                for (int i = 0; i<count; i++)
                {
                    
                    IShape shape;
                    if (fieldremoveflag)
                    {
                        count = count - 1;
                        i = i - 1;
                        shape = sc[i];
                        fieldremoveflag = false;
                    }
                    else
                    {
                        shape = sc[i];
                    }

                    if (shape.Placeholder != null
                        && !(shape is Aspose.Slides.PictureFrame)
                        && !(shape is Aspose.Slides.SmartArt.SmartArt))
                    {
                        IPlaceholder pc = ((AutoShape)shape).Placeholder;

                           if (((AutoShape) shape).TextFrame != null)
                            {
                                foreach (IParagraph para in ((AutoShape) shape).TextFrame.Paragraphs)
                                {
                                    foreach (IPortion port in para.Portions)
                                    {
                                        if (port.Field != null)
                                        {

                                        if (port.Field.Type.InternalString.StartsWith("datetime"))
                                          //  if (port.Field.Type.InternalString.Contains("datetime"))
                                            {
                                            //((AutoShape)shape).ShapeLock.SelectLocked = false;
                                            //  sc.Remove(shape);

                                           // port.Text = lastSavedDateTime.ToString();
                                            fieldremoveflag = true;

                                            }
                                        }

                                    }
                                }
                            }
                       // }
                    }
                }
            }



          

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

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


        }

@pradeepdone,

I suggest you to please visit following thread link for your convenience.