Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

Copy chart from one presentation to another presentation

$
0
0

Sorry for my English :)

I try to copy a chart element from one presentation to another with the same position

using C =DocumentFormat.OpenXml.Drawing.Charts;

namespace OpenXmlApp{publicclassShapeReader{publicstaticvoidCopyChart(string sourPrePath,string destPrePath){
            using (var sourDoc =PresentationDocument.Open(sourPrePath,false)){
                using (var destDoc =PresentationDocument.Open(destPrePath,true)){// Get copy of graphic frame.var sourGraphicFrameCopy = sourDoc.PresentationPart.SlideParts.First().Slide.CommonSlideData.ShapeTree.GetFirstChild<P.GraphicFrame>().CloneNode(true);// Add copy of graphic frame to destinition slide.var destSlidePart = destDoc.PresentationPart.SlideParts.First();var destShapeTree = destSlidePart.Slide.CommonSlideData.ShapeTree;
                    destShapeTree.Append(sourGraphicFrameCopy);// Add chart part to destinition slide.var sourChartPart = sourDoc.PresentationPart.SlideParts.First().ChartParts.First();var addedChartPart = destSlidePart.AddNewPart<ChartPart>();
                    addedChartPart.FeedData(sourChartPart.GetStream());// Link added graphic frame and added chart part.var chartRef = sourGraphicFrameCopy.Descendants<C.ChartReference>().SingleOrDefault();var addedChartPartId = destSlidePart.GetIdOfPart(addedChartPart);
                    chartRef.Id= addedChartPartId;

                    destDoc.Save();}}}

but this code just broke presentation - PowerPoint tries to restore the result presentation.

How correctly copy chart element?


Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>