我们使用下面的代码段将新幻灯片添加到现有幻灯片平台。这段代码将内容正确地添加到了幻灯片中,但是当通过PowerPoint打开时,它表示演示文稿已损坏并提示修复。修复后,演示文稿将打开,但是新添加的幻灯片将显示为空白幻灯片,没有任何内容(当解压缩PPTX文件时,内容在slide.xml文件中)。
使用“ Open XML SDK 2.5生产力工具”分析生成的演示文稿时,会显示以下错误消息,
public SlidePart CopySlidePart(PresentationPart presentationPart, SlidePart slidePart)
{
int i = presentationPart.SlideParts.Count();
//Create a new slide part in the presentation.
SlidePart newSlidePart = presentationPart.AddNewPart<SlidePart>("newSlide" + i);
i++;
//Add the source slide content into the new slide.
newSlidePart.FeedData(slidePart.GetStream(FileMode.Open));
//Make sure the new slide references the proper slide layout.
newSlidePart.AddPart(slidePart.SlideLayoutPart, slidePart.GetIdOfPart(slidePart.SlideLayoutPart));
//Get the list of slide ids.
SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;
//Deternmine where to add the next slide (find max number of slides).
uint maxSlideId = 1;
SlideId prevSlideId = null;
foreach (SlideId slideId in slideIdList.ChildElements)
{
var part = presentationPart.GetPartById(slideId.RelationshipId);
if (part == slidePart)
{
prevSlideId = slideId;
}
if (slideId.Id > maxSlideId)
{
maxSlideId = slideId.Id;
}
}
maxSlideId++;
//Add the new slide at the end of the deck.
SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId);
//Make sure the id and relid are set appropriately.
newSlideId.Id = maxSlideId;
newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart);
newSlidePart.Slide.Save();
presentationPart.Presentation.Save();
return newSlidePart;
}