Another questio about textholders/putting text in slides

Hi Shakeel,

What is a slide with a placeholder in it? Is that merely a slide w/ a textbox (and some text put into it?)

My question is related to the program below. I am trying to create a presentation called "pres", which I will write to the powerpoint slide "helloworld.ppt". The first slide of pres is the first slide of the presentation pres1, the second slide is the first slide of presentation pres2, the this is the first slide of the presentation pres3.

The remaining slides (i+2=4,...,15) of pres are as follows. Take a copy of the (single-slide) presentation "TemplateMRCAgain.ppt", alter copy of that slide, and then pop it at the end. Specifically, replace the text "Top three categories of customer penetration for Houehold type..." in the textbox of "TemplateMRCAgain.ppt" with something else, that depends on i.

The program compiles fine, but when I try to run, I get the error message "NullReferenceException was unhandled". Any way to fix this?

Thanks!

Mike

namespace ConsoleApplication1

{

class AIPOutput

{

static void Main(string[] args)

{

Slide[] slideDeck = new Slide[14];

string[] Ourtitle = new String[14];

Portion[] por = new Portion[14];

int i = 0;

Ourtitle[0] = "Processing Summary";

Ourtitle[1] = "Region";

Ourtitle[2] = "Age Range";

Ourtitle[3] = "Income";

Ourtitle[4] = "Gender";

Ourtitle[5] = "Marital Status";

Ourtitle[6] = "Household Type";

Ourtitle[7] = "Number of Children";

Ourtitle[8] = "Occupation";

Ourtitle[9] = "Dwelling Type";

Ourtitle[10] = "Length of Residence";

Ourtitle[11] = "Home Ownership";

Ourtitle[12] = "eConsumer Decile";

Ourtitle[13] = "Small Office/Home Office (SOHO)";

Presentation pres1 = new Presentation("C:\\Documents and Settings\\mcapalbo\\Desktop\\AIP\\CompletedAIPs\\Title.ppt");

Presentation pres = pres1;

Presentation pres2 = new Presentation("C:\\Documents and Settings\\mcapalbo\\Desktop\\AIP\\CompletedAIPs\\Waterfall.ppt");

Presentation pres3 = new Presentation("C:\\Documents and Settings\\mcapalbo\\Desktop\\AIP\\CompletedAIPs\\RegionSlide.ppt");

Presentation pres4 = new Presentation("C:\\Documents and Settings\\mcapalbo\\Desktop\\AIP\\CompletedAIPs\\TemplateMRCAgain.ppt");

Slide slide = pres2.GetSlideByPosition(1);

System.Collections.SortedList slist = new System.Collections.SortedList();

pres2.CloneSlide(slide, pres.Slides.LastSlidePosition + 1, pres, slist);

Slide slide3 = pres3.GetSlideByPosition(1);

System.Collections.SortedList slist3 = new System.Collections.SortedList();

pres3.CloneSlide(slide3, pres.Slides.LastSlidePosition + 1, pres, slist3);

for (i = 2; i < 14; i++)

{

/* Make slide title */

Slide slide4 = pres4.GetSlideByPosition(1);

Aspose.Slides.Rectangle rectTitle = slide4.Shapes.AddRectangle(240, 60, 4500, 200);

rectTitle.LineFormat.ShowLines = false;

rectTitle.AddTextFrame("Demographic Profile: " + Ourtitle[i]);

TextFrame titleMadeForSlide = rectTitle.TextFrame;

titleMadeForSlide.Paragraphs[0].Portions[0].FontBold = true;

/* Make slide textbox */

TextHolder th = (TextHolder)slide4.Placeholders[1];

if (th.Text.Equals("Top three categories of customer penetration for Household Type are Adult Male and Adult Female Present With Children, Adult Male Present With Children, and Adult Male & Adult Female Present"))

{

th.Paragraphs[0].Portions[0].Text = "The top category for customer penetration for " + Ourtitle[i] + " is";

}

th.Paragraphs[0].Portions[0].FontBold = true;

/* Put slide in presentation */

System.Collections.SortedList slist4 = new System.Collections.SortedList();

pres4.CloneSlide(slide4, pres.Slides.LastSlidePosition +1, pres, slist4);

}

pres.Write("C:\\Documents and Settings\\mcapalbo\\desktop\\helloworld.ppt");

}

}

The "NullReferenceException was unhandled" warning was in reference to the.

if (th.Text.Equals("Top three categories of customer penetration for Household Type are Adult Male and Adult Female Present With Children, Adult Male Present With Children, and Adult Male & Adult Female Present")){...}

line.

Also, could I ask you to respond here, as I cant seem to be able to get into my private messages...

One last thing...changing the "1" in the line

Textholder th = (TextHolder)slide4.Placeholders[1];

to 0 or 2 didnt help.

Dear AsAd,

FYI: TextHolders are actually Placeholders that have text in them; they are shipped with MS-PowerPoint as a Slide Layout. You cannot create them yourself. The example of TextHolder is the Placeholder, which says “Click to add title” or “Click to add subtitle” etc

TextFrames are like Text Boxes in MS-PowerPoint. Like you can create as many text boxes as you want on a slide, you can create as many textframes using Aspose.Slides.

Now, when we know the difference between them, we should know how to differ between them in Aspose.Slides.

You can differ between TextHolder and TextFrame by watching the Shape.IsTextHolder property.

For example to extract the text out of shape you will write a code like this

if (shp.IsTextHolder)
{
    TextHolder th = shp.Placeholder as TextHolder;
    Console.WriteLine(th.Text);
    th.Text = th.Text + " changed";
}
else
{
    if (shp.TextFrame != null)
    {
        TextFrame tf = shp.TextFrame;
        Console.WriteLine(tf.Text);
        tf.Text = tf.Text + " changed";
    }
}

If you want to read/write all the text on some slide, you will iterate through all the shapes on that slide and read/write the text from them using above code. Complete code for this is shown below.

Besides above method, you can also read the TextHolders from Slide.Placeholders collection. However, this collection has 8 hardcoded placeholders and the first, Slide.Placeholders[0] correspond to Title Placeholder/Textholder, the second is for Body /Subtitle Placeholder/TextHolder and so on…

For example, if your slide has a title placeholder you can read/write its text using this code.

TextHolder th = sld.Placeholders[0] as TextHolder;
Console.WriteLine(th.Text);
th.Text = th.Text + " changed";

This is the code, I have also attached the sample source presentation along with code and also output presentation generated by me. Hopefully, it clarifies you everything.

Presentation srcPres = new Presentation(@"c:\srcPlaceholder-Textholder-TextFrame.ppt");
Slide sld = srcPres.GetSlideByPosition(1);

foreach (Shape shp in sld.Shapes)
{
    if (shp.IsTextHolder)
    {
        TextHolder th = shp.Placeholder as TextHolder;
        Console.WriteLine(th.Text);
        th.Text = th.Text + " changed";
    }
    else
    {
        if (shp.TextFrame != null)
        {
            TextFrame tf = shp.TextFrame;
            Console.WriteLine(tf.Text);
            tf.Text = tf.Text + " changed";
        }
    }
}

srcPres.Write(@"c:\out.ppt");

Thanks very much!!

Mike

Could you give an example when using powerpoints that were saved with Office 2007 and later?

It looks like I figured it out myself.

I posted my code below, if anyone wants to refer to it.
Also, you can modify the “Type” criteria to search for particular types of text boxes.

Thanks!

//Instantiate a PresentationEx class that represents the PPTX file
PresentationEx pres = new PresentationEx(tbPPTpath.Text);
int iSlideCount = pres.Slides.Count;
for (int ind = 0; ind < iSlideCount; ind++)
{
//Access each slide
SlideEx sld = pres.Slides[ind];

foreach (ShapeEx shp in sld.Shapes)
{
string sTitleText = null;
string sTitleType = null;
try
{
sTitleText = ((AutoShapeEx)shp).TextFrame.Text.ToString();
sTitleType = ((AutoShapeEx)shp).Name.ToString();
}
catch (Exception ex)
{ }

if (!String.IsNullOrEmpty(sTitleText))
{

if (shp.IsTextHolder && sTitleType.Contains(“Title”))
{
lbTitles.Items.Add("Slide " + (ind + 1).ToString() + ": " + sTitleText);
}
else if (shp.Placeholder != null && sTitleType.Contains(“Title”))
{

lbTitles.Items.Add(((AutoShapeEx)shp).TextFrame.Text);
}
}
}

Hi John,

It's really great to listen that you have managed to figure out the requirements your self. Please share if I may provide you with any further help.

Many Thanks,