wordpress hit counter
Add Chart on the fly to powerpoint - PresentationML - Formats - OpenXML Developer

Add Chart on the fly to powerpoint

Formats

Discussions about working with different Open XML Formats

Add Chart on the fly to powerpoint

  • rated by 0 users
  • This post has 6 Replies |
  • 1 Follower


  • Hi everybody, I'm new with OpenXml

    How I do to add a chart into slide on the fly?

    I need generate a presentation with charts and data from a Database

    I need your help

    Thanks a lot.

    :)
  • Well adding a chart on the fly in a package using open xml is not easy. I'd suggest to make a template of the chart and modify (update the data) that on the fly.
    Thanks Aakash Jain http://www.aakashjain.com
  • aakash:
    Well adding a chart on the fly in a package using open xml is not easy. I'd suggest to make a template of the chart and modify (update the data) that on the fly.

    Hello, aakash, could you write down an example code of doing it.
    I'm aslo very interesting.
    Tnanks a lot.
  • Hi,

    Please have a look at this

    http://msdn.microsoft.com/en-us/library/cc820055(office.14).aspx

    Regards,
    Ankush

  • Hi,

    Finally I am able to insert a chart into an existing Presentation.

    Here is the sample code for reference:

    Please note that I have extracted chart1.xml,chartdata.xlsx from a presentation having a single chart in it. Basically I am using them as a template and then manipulating them later on.

    ===========

    // Open an existing Presentation

    PresentationDocument oPDoc = PresentationDocument.Open(@"C:\temp\test.pptx", true);

    PresentationPart oPPart = oPDoc.PresentationPart;

    // Get the ReleationshipId of the first Slide

    SlideId slideId = oPPart.Presentation.SlideIdList.GetFirstChild<SlideId>();

    string relId = slideId.RelationshipId;

    // Get the slide part by the relationship ID.

    SlidePart slidePart = (SlidePart)oPPart.GetPartById(relId);

    // Add a new chart part

    ChartPart chPrt = slidePart.AddNewPart<ChartPart>();

    XmlDocument xDoc = new XmlDocument();

    String strFileName = "C:\\temp\\chart1.xml";

    xDoc.Load(strFileName);

    StreamWriter objDocMainWrt = new StreamWriter(chPrt.GetStream(FileMode.Create, FileAccess.Write));

    xDoc.Save(objDocMainWrt);

    // Add the data Sheet for the Chart

    ExtendedPart objEmbPart = chPrt.AddExtendedPart("http://schemas.openxmlformats.org/officeDocument/2006/relationships/package", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".xlsx");

    strFileName = "C:\\temp\\chartData.xlsx";

    FileStream partStream = new FileStream(strFileName, FileMode.Open, FileAccess.Read);

    objEmbPart.FeedData(partStream);

    // Change the Data releation ID used in Chart.xml

    // Elemenet changed : c:externalData

    string relChtDataID = chPrt.GetIdOfPart(objEmbPart);

    XmlNamespaceManager nsManagerDraw = new XmlNamespaceManager(xDoc.NameTable);

    nsManager1.AddNamespace("c", "http://schemas.openxmlformats.org/drawingml/2006/chart");

    XmlNodeList nodeTest = xDoc.SelectNodes("//c:externalData", nsManagerDraw);

    foreach (XmlNode node in nodeTest)

    {

    node.Attributes["r:id"].Value = relChtDataID;

    }

    // Save changes back to Chart.xml

    xDoc.Save(chPrt.GetStream(FileMode.Create, FileAccess.Write));

    // Get the SlidePart Stream

    const string presentationmlNamespace = "http://schemas.openxmlformats.org/presentationml/2006/main";

    NameTable nt = new NameTable();

    XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);

    nsManager.AddNamespace("p", presentationmlNamespace);

    XmlDocument presXML = new XmlDocument(nt);

    presXML.Load(slidePart.GetStream());

     

    // Get the spTree Element in SlidePart

    XmlNode nodeTree = presXML.SelectSingleNode("//p:spTree", nsManager);

    string rid=slidePart.GetIdOfPart(chPrt).ToString();

    // Generate the Graphic Element for the Chart

    XmlNode childNode = GenerateNode(rid);

    //Append the Graphic Element to spTree Element in SlidePart

    nodeTree.AppendChild(presXML.ImportNode(childNode, true));

    Stream o = slidePart.GetStream();

    presXML.Save(o);

    oPDoc.Close();

    ===================

     

    private XmlNode GenerateNode(String rId)

    {

    XmlDocument xwb = new XmlDocument();

    xwb.AppendChild(xwb.CreateXmlDeclaration("1.0", "UTF-8", "yes"));

    XmlNamespaceManager xmlns = new XmlNamespaceManager(xwb.NameTable);

     

    xmlns.AddNamespace("p", "http://schemas.openxmlformats.org/presentationml/2006/main");

    xmlns.AddNamespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

    xmlns.AddNamespace("a","http://schemas.openxmlformats.org/drawingml/2006/main");

    XmlElement eleGraphic = xwb.CreateElement("p:graphicFrame", xmlns.LookupNamespace("p"));

    xwb.AppendChild(eleGraphic);

    XmlElement eleGrpFrm = xwb.CreateElement("p:nvGraphicFramePr", xmlns.LookupNamespace("p"));

    eleGraphic.AppendChild(eleGrpFrm);

    XmlElement elePpr = xwb.CreateElement("p:cNvPr", xmlns.LookupNamespace("p"));

    eleGrpFrm.AppendChild(elePpr);

    XmlAttribute attrRid = xwb.CreateAttribute("id");

    attrRid.Value = "4";

    elePpr.SetAttributeNode(attrRid);

    XmlAttribute attrName = xwb.CreateAttribute("name");

    attrName.Value = "Chart 2";

    elePpr.SetAttributeNode(attrName);

    XmlElement elecNvGraphicFramePr = xwb.CreateElement("p:cNvGraphicFramePr", xmlns.LookupNamespace("p"));

    eleGrpFrm.AppendChild(elecNvGraphicFramePr);

    XmlElement elenvPr = xwb.CreateElement("p:nvPr", xmlns.LookupNamespace("p"));

    eleGrpFrm.AppendChild(elenvPr);

    XmlElement elexfrm = xwb.CreateElement("p:xfrm", xmlns.LookupNamespace("p"));

    eleGraphic.AppendChild(elexfrm);

    XmlElement eleoff = xwb.CreateElement("a:off", xmlns.LookupNamespace("a"));

    elexfrm.AppendChild(eleoff);

    XmlAttribute xvalue=xwb.CreateAttribute("x");

    xvalue.Value = "1524000";

    eleoff.SetAttributeNode(xvalue);

    XmlAttribute yvalue = xwb.CreateAttribute("y");

    yvalue.Value = "1397000";

    eleoff.SetAttributeNode(yvalue);

     

    XmlElement eleext = xwb.CreateElement("a:ext", xmlns.LookupNamespace("a"));

    elexfrm.AppendChild(eleext);

    XmlAttribute cxvalue = xwb.CreateAttribute("cx");

    cxvalue.Value = "6096000";

    eleext.SetAttributeNode(cxvalue);

    XmlAttribute cyvalue = xwb.CreateAttribute("cy");

    cyvalue.Value = "4064000";

    eleext.SetAttributeNode(cyvalue);

    //<c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId2"/></a:graphicData></a:graphic></p:graphicFrame>

    XmlElement elegraphic = xwb.CreateElement("a:graphic", xmlns.LookupNamespace("a"));

    eleGraphic.AppendChild(elegraphic);

    XmlElement elegraphicData = xwb.CreateElement("a:graphicData", xmlns.LookupNamespace("a"));

    elegraphic.AppendChild(elegraphicData);

    XmlAttribute uri = xwb.CreateAttribute("uri");

    uri.Value = "http://schemas.openxmlformats.org/drawingml/2006/chart";

    elegraphicData.SetAttributeNode(uri);

    XmlElement elechart = xwb.CreateElement("c:chart", "http://schemas.openxmlformats.org/drawingml/2006/chart");

    elegraphicData.AppendChild(elechart);

    XmlAttribute id = xwb.CreateAttribute("r:id", xmlns.LookupNamespace("r"));

    id.Value = rId;

    elechart.SetAttributeNode(id);

     

    XmlNode ss1 = xwb.SelectSingleNode("//p:graphicFrame", xmlns);

    return ss1;

    //xwb.Save(@"C:\temp\test1.xml");

    }

    Thanks

    Ankush

  • Wow! This is amazing stuff, thank you Ankush, this is just what I needed. :D
  • It's not working because the folder charts is places in /ppt/slides/ and not in /ppt/, so that's mean you can't add a chartPart in a slidePart, but you can't either add it in the PresentationPart, it's so stupid !!! PPTX sucks a lot more than DOCX
Page 1 of 1 (7 items)