c#openxml设置自运行功率点演示

时间:2017-05-27 09:52:36

标签: c# asp.net-mvc powerpoint openxml openxml-sdk

我正在开发一个演示应用程序(Asp.net MVC),我希望在现有的Power Point上应用自运行和过渡时间动画。我在下面写了代码,

using (var templateFile = System.IO.File.Open(Server.MapPath("~/OurCompanyTeam.pptx"), FileMode.Open, FileAccess.Read))
{
    using (var stream = new MemoryStream())
    {
        templateFile.CopyTo(stream); 
        using (var presentationDocument = PresentationDocument.Open(stream, true)) 
        {
            var presentationPart = presentationDocument.PresentationPart;                            
            var presentation = presentationPart.Presentation;
            var slideList = new List<SlidePart>();                            

            foreach (SlideId slideID in presentation.SlideIdList)
            {
                var slide = (SlidePart)presentationPart.GetPartById(slideID.RelationshipId);
                Transition trns = new Transition();
                trns.Duration =  "2.5";
                trns.Speed = TransitionSpeedValues.Fast;
                slide.Slide.Transition = trns;
                slideList.Add(slide);
            }
            presentationPart.Presentation.Save();
        }
        byte[] buffer = stream.ToArray();
        MemoryStream ms = new MemoryStream(buffer);                        
        FileStream file = new FileStream(Server.MapPath("~/output.pptx"), FileMode.Create, FileAccess.Write);
        ms.WriteTo(file);
        file.Close();
    }
}

我如何改进此代码以设置自运行演示文稿?

1 个答案:

答案 0 :(得分:0)

此答案中的代码将使任何演示文稿(.pptx)成为自运行幻灯片(.ppsx)。它执行以下项目:

  1. 将演示文稿置于自助服务终端模式
  2. 将每个幻灯片的AdvanceAfterTime属性设置为2秒
  3. 将文档类型更改为幻灯片放映
  4. .pptx的内容保存到新的.ppsx文件中。
  5. 您可以使用以下代码替换上面的代码。添加_sourceFile字符串局部变量以保存文件的完整路径。上述项目评论中的注释:

    using (var templateFile = System.IO.File.Open(Server.MapPath("~/OurCompanyTeam.pptx"), FileMode.Open, FileAccess.Read))
    {
        using (var stream = new MemoryStream())
                {
                    templateFile.CopyTo(stream);
                    using (var presentationDocument = PresentationDocument.Open(stream, true))
                    {
                        var presentationPart = presentationDocument.PresentationPart;
                        var presentation = presentationPart.Presentation;
                        var slideList = new List<SlidePart>();
    
                        foreach (SlideId slideID in presentation.SlideIdList)
                        {
                            var slidePart = (SlidePart)presentationPart.GetPartById(slideID.RelationshipId);
    
                            AlternateContent alternateContent1 = slidePart.Slide.GetFirstChild<AlternateContent>();
    
                            if (alternateContent1 != null)
                            {
                                slidePart.Slide.RemoveAllChildren<AlternateContent>();
                            }
    
                                var trns1 = new Transition();
                                trns1.Duration = "2000";
                                trns1.AdvanceOnClick = false;
                                trns1.AdvanceAfterTime = "2000"; //ITEM #2
                                trns1.Speed = TransitionSpeedValues.Slow;
    
                                var alternateContent = new AlternateContent();
                                alternateContent.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
    
                                AlternateContentChoice alternateContentChoice = new AlternateContentChoice() { Requires = "p14" };
                                alternateContentChoice.AddNamespaceDeclaration("p14", "http://schemas.microsoft.com/office/powerpoint/2010/main");
    
                                alternateContentChoice.Append(trns1);
                                alternateContent.Append(alternateContentChoice);
                                slidePart.Slide.Append(alternateContent);
                        }
    
                        var presentationPropertiesPart = presentationPart.PresentationPropertiesPart;
                        var presentationProperties = presentationPropertiesPart.PresentationProperties;
                        presentationProperties.RemoveAllChildren<ShowProperties>();
    
                        presentationProperties.Append(NewShowProperties());
    
                        presentationDocument.ChangeDocumentType(PresentationDocumentType.Slideshow);  //ITEM #3
    
                    }
                    byte[] buffer = stream.ToArray();
                    MemoryStream ms = new MemoryStream(buffer);
                    FileStream file = new FileStream(System.IO.Path.GetDirectoryName(_sourceFile) + "/NewSlideShow.ppsx",
                        FileMode.Create, FileAccess.Write);  //ITEM #4
                    ms.WriteTo(file);
                    file.Close();
                }   
    }
    

    然后在课程的底部添加此方法:

        private ShowProperties NewShowProperties()
        {
            var showProperties = new ShowProperties { Loop = true, ShowNarration = true };
            showProperties.Append(new KioskSlideMode());  //ITEM #1
            showProperties.Append(new SlideAll());
            showProperties.Append(new PresenterSlideMode());
            return showProperties;
        }
    

    我已经测试了这个,以便在新创建的PowerPoint 2013和2016演示文稿上工作。双击.ppsx文件,它将启动自运行演示文稿。