Alter the text in a SmartArt shape in PowerPoint

Respondido Alter the text in a SmartArt shape in PowerPoint

  • segunda-feira, 27 de fevereiro de 2012 20:01
     
     

    Hi,

    Can someone please tell me if it's possible to change the text in a SmartArt shape (e.g. Basic Radial) in PowerPoint (using OpenXML + C#)?

    I've seen the various tutorials on changing the text in a textbox and extracting all the text from a pptx but neither of these touch the text in a SmartArt shape.

    I was able to locate the text object in a smartart shape via:

    List<DiagramDataPart> slDp = slidePart.DiagramDataParts.Where(t => t.DataModelRoot.PointList.InnerText.Equals("hello world")).ToList();

    foreach (DiagramDataPart dp in slDp)
       System.Diagnostics.Debug.WriteLine(dp.DataModelRoot.PointList.InnerText);

    But I can't seem to update the PointList so figure there must be some other property/object I shoudl be dealing with.

    I freely admit that I'm 99% clueless when it comes to this stuff so any help is appreciated.

    Thanks!

Todas as Respostas

  • quarta-feira, 29 de fevereiro de 2012 07:16
    Moderador
     
     Respondido Contém Código

    Hi rifinn,

    Thanks for posting in the MSDN Forum.

    It's based on my exprenice that your need iterate the Point to find the text which your want. Please try this snippet:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Presentation;
    using DocumentFormat.OpenXml.Drawing.Diagrams;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = @"***************.pptx";
                try
                {
                    using (PresentationDocument doc = PresentationDocument.Open(path, true))
                    {
                        PresentationPart pp = doc.PresentationPart;
                        SlidePart sp1 = pp.SlideParts.ToList<SlidePart>()[0];
                        foreach (DiagramDataPart ddp in sp1.DiagramDataParts.ToList<DiagramDataPart>())
                        {
                            DataModelRoot dmr = ddp.DataModelRoot;
                            List<PointList> pl = dmr.Descendants<PointList>().ToList();
                            foreach (PointList item in pl)
                            {
                                List<Point> ps = item.Descendants<Point>().ToList();
                                foreach(Point p in ps)
                                {
                                    if (p.InnerText.Equals("Tom"))
                                        Console.WriteLine(p.InnerText);
                                }
                            }
                        }
                    }
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(String.Format("Stack Trace:\n{0}\nError Message:\n{1}", ex.StackTrace, ex.Message));
                }
                Console.WriteLine("Pause");
                Console.ReadKey();
            }
        }
    }

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marcado como Resposta rifinn quarta-feira, 29 de fevereiro de 2012 15:17
    •  
  • quarta-feira, 29 de fevereiro de 2012 14:11
     
     

    Thanks Tom,

    That works but how would I go about changing the text once I find the point it's stored in?

    I've set up a PowerPoint template with some SmartArt shapes on it and was hoping to pump some data from a database into the template, replacing place holder text with data from the db.

    I can get this to work with regular textbox shapes (thanks to http://blogs.msdn.com/b/brian_jones/archive/2008/11/18/creating-a-presentation-report-based-on-data.aspx) but haven't been able to figure out how to update the shape text.

    Thanks,

    Rich

  • quarta-feira, 29 de fevereiro de 2012 15:17
     
     

    Wahoo! I actually got it to work by adding the following to your program Tom:

                                    if (p.InnerText.Equals("hello"))
                                    {
                                        Console.WriteLine(p.InnerText);
                                        Drawing.Paragraph para = p.TextBody.GetFirstChild<Drawing.Paragraph>();
                                        Drawing.Run run1 = para.GetFirstChild<Drawing.Run>();
                                        Drawing.Text text1 = run1.GetFirstChild<Drawing.Text>();
                                        text1.Text = "World";
                                        Console.WriteLine(p.InnerText);

                                    }

    I added code to save the PowerPoint at the end and when I opened it up my "hello" placeholder text was changed to "world"!

    Thanks for your help.

    Rich