Add Text and logo in slides- Power Point

No, it is not a bug, please tell me what you exactly want to do, I can help you achieve it.

I would like to do some things like:

masterSld.HeaderFooter.IsFooterVisible = true;
masterSld.HeaderFooter.IsDateTimeVisible = true;
masterSld.HeaderFooter.IsPageNumberVisible = true;
masterSld.HeaderFooter.FooterText = "First Slide Footer";

But the HeaderFooter is always null.

I am using Aspose.Slides for .NET version 2.8.4.0

Please advice.

You will have to make use of Metacharacters to set HeaderFooter properties of the master slide.

Please see the following code and its output attached by me.

C#

-------------------------------------------------------------------

Presentation pres = new Presentation();

//Make the page number and footer visible visible

Slide sld = pres.GetSlideByPosition(1);

sld.HeaderFooter.IsPageNumberVisible = true;

sld.HeaderFooter.IsFooterVisible = true;

//Add another slide

sld = pres.AddDoubleBodySlide();

sld.HeaderFooter.IsPageNumberVisible = true;

sld.HeaderFooter.IsFooterVisible = true;

//Set the page number and footer on master slide

Slide mstr = pres.GetSlideById(sld.MasterId);

foreach (Shape shp in mstr.Shapes)

{

if (shp.TextFrame != null)

{

TextFrame tf = shp.TextFrame;

if (tf.MetaCharacters != null)

{

MetaCharacterType metaType = tf.MetaCharacters[0];

switch (metaType)

{

case MetaCharacterType.Footer:

tf.Paragraphs[0].Portions[0].Text = " Footer Text";

break;

case MetaCharacterType.SlideNumer:

tf.Paragraphs[0].Portions[0].Text = " of 10";

break;

default:

break;

}

}

}

}

pres.Write("c:\\outHeaderFooter.ppt");

Here is the same code in JAVA as above.

JAVA

Presentation pres = new Presentation();
Slide sld = pres.getSlideByPosition(1);

//Make the page number and footer visible
sld.getHeaderFooter().setFooterVisible(true);
sld.getHeaderFooter().setPageNumberVisible(true);

//Add another slide
sld = pres.addEmptySlide();
sld.getHeaderFooter().setFooterVisible(true);
sld.getHeaderFooter().setPageNumberVisible(true);

//Set the page number and footer text on master slide
Slide mstr = pres.getSlideById(sld.getMasterId());
int shpsCount = mstr.getShapes().size();

for (int i = 0; i < shpsCount; i++) {
    com.aspose.slides.Shape shp = mstr.getShapes().get(i);
    TextFrame tf = shp.getTextFrame();

    if (tf != null) {
        MetaCharacters metaChars = tf.getMetaCharacters();
        if (metaChars != null) {
            int metaType = metaChars.get(0);
            if (metaType == MetaCharacterType.FOOTER) {
                Paragraph para = tf.getParagraphs().get(0);
                Portion port = para.getPortions().get(0);
                port.setText(" Footer Text");
            }//if
            else if (metaType == MetaCharacterType.SLIDE_NUMBER) {
                Paragraph para = tf.getParagraphs().get(0);
                Portion port = para.getPortions().get(0);
                port.setText(" of 10");
            }//else if
        }//if metacharacters are not null
    }//if text frame is not null
}//for

pres.write(new FileOutputStream("c:\\outHeaderFooter.ppt"));

Hi msfaiz,

The above code for java works fine ,I modified the above code to header and set the header text , but it is not visible , is there any possiblilty to create header in PPT.

Regards

Janakiraman

Dear Janakiraman,

You should add a textframe on master slide at the top, which will work as a header text on all normal slides.

Hi,

I have added the TextFrame using below code

Shape shp1 = slide.getShapes().addRectangle(

2500, 400, 2000, 370);

shp1.getLineFormat().setShowLines(false);

TextFrame tf1 = shp1.addTextFrame("");

tf1.setText( " AAAAAAAAAAA ");

Portion port1 = tf1.getParagraphs().get(0).getPortions().get(0);

but the the thing is i want to set the header as you did for Footer and setting Slide number using

MetaCharacters.

How to set the Header using MetaCharacterType.HEADER

Regards

Janakiraman

Dear Janakiraman,

Header does not appear on Normal view. It appears in Notes Page view. See the image below and area marked by red color.

To make it appear in normal view, you need to add a textframe on master slide.

To open notes page view, follow View – > Notes Page

Hi msfaiz,

Thanks for your reply.

Is it possible to create Header and Footer (as you said using TextFrame )in PPTX file using .Net or Java.

Regards

Janakiraman

Dear Janakiraman,

Adding textframe inside PPTX is under development, so it is not possible right now.

Hi Msfaiz

This code is work fine .but i need something extra… I need Slide number format in (Page 1 of 2)
I tried to insert new Portions to Paragraphs[0] but did not get the out put I need.
Any suggestion?.

thanks
Gayan



msfaiz:

You will have to make use of Metacharacters to set HeaderFooter properties of the master slide.

Please see the following code and its output attached by me.

C#

-------------------------------------------------------------------

Presentation pres = new Presentation();

//Make the page number and footer visible visible

Slide sld = pres.GetSlideByPosition(1);

sld.HeaderFooter.IsPageNumberVisible = true;

sld.HeaderFooter.IsFooterVisible = true;

//Add another slide

sld = pres.AddDoubleBodySlide();

sld.HeaderFooter.IsPageNumberVisible = true;

sld.HeaderFooter.IsFooterVisible = true;

//Set the page number and footer on master slide

Slide mstr = pres.GetSlideById(sld.MasterId);

foreach (Shape shp in mstr.Shapes)

{

if (shp.TextFrame != null)

{

TextFrame tf = shp.TextFrame;

if (tf.MetaCharacters != null)

{

MetaCharacterType metaType = tf.MetaCharacters[0];

switch (metaType)

{

case MetaCharacterType.Footer:

tf.Paragraphs[0].Portions[0].Text = " Footer Text";

break;

case MetaCharacterType.SlideNumer:

tf.Paragraphs[0].Portions[0].Text = " of 10";

break;

default:

break;

}

}

}

}

pres.Write("c:\\outHeaderFooter.ppt");

Hello,

It’s better to use template presentation with already added “Page x of y” frame instead of creating this text from scratch. Also you can add these frames as static text. Most probably you already know the page numbers of slides when you generate a presentation.

One additional comment. MS PowerPoint always inserts ‘*’ character at the metacharacter position so it’s better to do the same with generated textframes.

Hi, adding “1 of n” code works fine for templates created in 2003 but for 2007 presentations it doesnt display it why is that ?

Hi Aneef,

Thanks for considering Aspose.Slides.

Your query has also been responded in another similar thread. Please follow this link for further details.

We are sorry for your nuisance,

I’m trying to use your example code to replace text in datetime fields, but it appears to only append text to the datetime field. My need is to replace the datetime information with custom text in an arbitrary PowerPoint presentation.

Hi Wade,

I have tried to understand the issue shared by you and regret to share that I am unable to completely understand the issue shared. Please share the source presentation along with code snippet to elaborate the issue completely. I really appreciate your cooperation for that.

Thanks and Regards,

The attached ppt has a date/time field in the footer. I need to be able to replace this date/time with arbitrary text. When I follow your example code, my text is appended to the date/time, rather than replacing it:



if (presentation is Presentation)

{

Presentation pres = presentation as Presentation;



Slide slide = null;

int numSlides = pres.Slides.LastSlidePosition;



for (int slideNum = 1; slideNum
{

slide = pres.GetSlideByPosition(slideNum);

//slide = pres.MainMaster as Slide;



foreach (Shape shape in slide.Shapes)

{

if (shape.TextFrame != null)

{

TextFrame frame = shape.TextFrame;

if (frame.MetaCharacters != null)

{

MetaCharacterType type = frame.MetaCharacters[0];

switch (type)

{

case MetaCharacterType.DateTime:

frame.Paragraphs[0].Portions[0].Text = “[Autotext]”;

break;

case MetaCharacterType.Footer:

break;

case MetaCharacterType.GenericDateTime:

break;

case MetaCharacterType.Header:

break;

case MetaCharacterType.SlideNumer:

break;

default:

break;

}

}

}

}

}

}

Hi Wade,

I like to share that you may not replace the metacharacters associated with any text frame. However, you can copy the properties of DateTime field and create a new similar shape and add the desired text yourself. For your kind reference, I have updated the code snippet shared by you and hopefully it will help you in resolving the issue.

Presentation pres = new Presentation("D:\\Aspose Data\\YesHeaderFooterNotesAuto.ppt");
Slide slide = null;

int numSlides = pres.Slides.LastSlidePosition;
int x = 0, y = 0, w = 0, h = 0;

for (int slideNum = 1; slideNum < pres.Slides.Count; slideNum++)
{
    slide = pres.GetSlideByPosition(slideNum);
    foreach (Shape shape in slide.Shapes)
    {
        if (shape.TextFrame != null)
        {
            TextFrame frame = shape.TextFrame;
            if (frame.MetaCharacters != null)
            {
                MetaCharacterType type = frame.MetaCharacters[0];
                switch (type)
                {
                    case MetaCharacterType.DateTime:
                        slide.HeaderFooter.DateTimeVisible = false;
                        slide.HeaderFooter.IsDateTimeVisible = false;

                        x = shape.X;
                        y = shape.Y;
                        w = shape.Width;
                        h = shape.Height;
                        break;

                    case MetaCharacterType.Footer:
                        break;

                    case MetaCharacterType.GenericDateTime:
                        break;

                    case MetaCharacterType.Header:
                        break;

                    case MetaCharacterType.SlideNumer:
                        break;

                    default:
                        break;
                }
            }
        }
    }

    if (x >= 0 && y >= 0)
    {
        Shape temshape = slide.Shapes.AddRectangle(x, y, w, h);
        temshape.AddTextFrame("Hello World");
    }
}

pres.Write("D:\\Aspose Data\\TestDate.ppt");

Thanks and Regards,

This is an acceptable workaround. Thanks. Do you know of any way I can do the same thing in a pptx file?

Hi Wade,

I regret to share that MS PowerPoint 2007 does not add real headers and footers, rather it adds normal rectangle shapes as headers and footers. In the current Aspose.Slides API, ShapeEx.AlternativeText property can be used to identify a shape provided the creator of the presentation adds unique "Alternative Text" to shapes. You can add a shape and add the slide index in the text of that rectangle to show slide number. Other option is to use a template PPTX slide, that has been created in PowerPoint and has macro enabled slide number.

Thanks and Regards,