对不起,我的英语:)
我尝试将图表元素从一个演示文稿复制到具有相同位置的另一个演示文稿
using C = DocumentFormat.OpenXml.Drawing.Charts;
namespace OpenXmlApp
{
public class ShapeReader
{
public static void CopyChart(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();
}
}
}
但是此代码只是破坏了演示文稿-PowerPoint尝试还原结果演示文稿。
如何正确复制图表元素?