简体   繁体   中英

how to delete a chart from an slide in powerpoint

I have a pptx file with one slide. There is a chart in the slide. I want to delete that chart from the slide using C# codes. I written a C# program to do it. After running the solution, when I open the converted pptx file; I get this error:

powerpoint removed unreadable content in test2

My C# codes are as following.

string pptxFileNameTemplate = "D:\\test.pptx";
string pptxFileNameGenerate = "D:\\test2.pptx";

File.Copy(pptxFileNameTemplate, pptxFileNameGenerate, true);
using (PresentationDocument presentationDocument = PresentationDocument.Open(pptxFileNameGenerate, true))
{  
    PresentationPart presentationPart = presentationDocument.PresentationPart;
    List<SlidePart> slideParts = new List<SlidePart>();
    presentationPart.GetPartsOfType<SlidePart>(slideParts);
    SlidePart slp = slideParts[0];
    List<ChartPart> chartParts = new List<ChartPart>();
    slp.GetPartsOfType<ChartPart>(chartParts);
    ChartPart cp = chartParts[0];
    string chartPartIdBookMark = slp.GetIdOfPart(cp);
    slp.DeletePart(chartPartIdBookMark);
    slp.Slide.Save();
    presentationDocument.Close();
}

您可以使用application.presentation.slides循环所有幻灯片,并在slide.shapes集合中为每个形状检查shape.type(类型为图表调用shape.delete时)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM