在复制具有视频的PPT时使用Open xml会抛出异常

时间:2016-05-16 14:22:42

标签: c# openxml openxml-sdk

我有一张带有嵌入视频的单张幻灯片的PPT。我正在另一个PPT复制幻灯片的内容。

static void Main(string[] args)
{
    using (var destDoc = PresentationDocument.Open(@"C:\PPT With Embedded Video.pptx", true))
    {
        var destPres = destDoc.PresentationPart;
        using (var sourceDoc = PresentationDocument.Open(@"C:\PPT With Video.pptx", true))
        {


            var firstSlide = destPres.SlideParts.First();


            sourceDoc.PresentationPart.AddPart<SlidePart>(firstSlide);
        }
    }
}

AddPart抛出以下异常。

Collection was modified; enumeration operation may not execute.  
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
   at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddSubPartFromOtherPackage(OpenXmlPart part, IDictionary`2 partDictionary, IDictionary`2 dataPartsDictionary, Boolean keepIdAndUri, String rId)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddSubPartFromOtherPackage(OpenXmlPart part, Boolean keepIdAndUri, String rId)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddSubPart(OpenXmlPart part, String rId)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddPartFrom(OpenXmlPart subPart, String rId)
   at DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddPart[T](T part)
   at ForTestConsoleApp.Program.Main(String[] args) in c:\Users\manjayd\Documents\Visual Studio 2013\Projects\ForTestConsoleApp\ForTestConsoleApp\Program.cs:line 31
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

0 个答案:

没有答案