用openxml替换Presentation中的Pictureplaceholder

时间:2018-12-09 13:30:18

标签: c# powerpoint openxml-sdk

我尝试用openxml sdk替换Powerpoint演示文稿中的Pictureplaceholder。我想我找到了占位符,但是我不知道如何用图像替换它。这是处理从主要部分创建的幻灯片的要求。

应用母版中指定的布局,该布局可以按预期工作:

Slide slide = new Slide(new CommonSlideData(new ShapeTree()));
SlidePart slidePart = presentationPart.AddNewPart<SlidePart>();
slide.Save(slidePart);
SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.First();
SlideLayoutPart slideLayoutPart = slideMasterPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals("InhaltsFolie", StringComparison.OrdinalIgnoreCase));
slidePart.AddPart<SlideLayoutPart>(slideLayoutPart);
slidePart.Slide.CommonSlideData = (CommonSlideData)slideMasterPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals("InhaltsFolie")).SlideLayout.CommonSlideData.Clone();
replaceImages(slidePart, fotodokumentation.Thumbnail);

ReplaceImages-方法搜索占位符,应将其替换。这是代码样本的混合版本,我发现了一种识别占位符的方法。我不知道如何从我发现的样本中将占位符转换为类似Blip-Object的对象。还是有另一种方法将图像应用于占位符?在myBlip对象下面的代码中,myBlip对象始终为null,但占位符在那里:

private void replaceImages(SlidePart slidePart,byte[] image) {
    var placeholder = slidePart.Slide.Descendants<PlaceholderShape>().FirstOrDefault(pl => pl.Type == PlaceholderValues.Picture);
    var myBlip = slidePart.Slide.Descendants<Drawing.Blip>().FirstOrDefault();
    if (myBlip != null) {
        var imagePart = slidePart.AddImagePart(ImagePartType.Jpeg);

        using (var stream = new MemoryStream(image)) {
            imagePart.FeedData(stream);
        }
        var relID = slidePart.GetIdOfPart(imagePart);
        myBlip.Embed = relID;
    }            
}

0 个答案:

没有答案