简体   繁体   English

使用OpenXML合并PowerPoint幻灯片

[英]Merging PowerPoint slides using OpenXML

All I want to do is merge some slides into a master presentation using OpenXML SDK, I don't want to use Introp as it's not ideal in a server environment, I've tried many code samples but the merged presentation always shows me repair message, when I compared the corrupted file with the repaired one I found out that the ids aren't generated correctly. 我要做的就是使用OpenXML SDK将一些幻灯片合并到主演示文稿中,我不想使用Introp,因为它在服务器环境中并不理想,我尝试了很多代码示例,但是合并后的演示文稿总是向我显示修复消息,将损坏的文件与已修复的文件进行比较时,我发现ID生成不正确。

Is there any open source library or sample code that actually works. 是否有任何实际可用的开源库或示例代码。 I've heard of Aspose but it's a paid library. 我听说过Aspose,但这是一个付费图书馆。

The Microsoft MSDN article Microsoft MSDN文章

http://msdn.microsoft.com/en-us/library/office/ee361883(v=office.12).aspx http://msdn.microsoft.com/en-us/library/office/ee361883(v=office.12).aspx

gives a step by step instruction on how to merge powerpoint presentations together. 提供有关如何将Powerpoint演示文稿合并在一起的分步说明。

It takes care of ensuring the ID's are unique. 它会确保ID唯一。 The only disadvantage is that it also copies the slide masters for each and every slide, making the final powerpoint file larger than expected. 唯一的缺点是,它还会为每张幻灯片复制幻灯片母版,从而使最终的PowerPoint文件比预期的大。

http://openxmldeveloper.org/上尝试演示文稿构建器代码。

Yes u face issues if id's are not unique, below is working code i am using. 是的,如果id不是唯一的,您会遇到问题,以下是我正在使用的工作代码。 I know its old thread, but in case if some one is looking for answer, 我知道它的旧线程,但是如果有人在寻找答案,

public static void MergeSlides(string presentationFolder, string sourcePresentation, string destPresentation)
{
    int id = 0;

    // Open the destination presentation.
    using (PresentationDocument myDestDeck = PresentationDocument.Open(presentationFolder + destPresentation, true))
    {
        PresentationPart destPresPart = myDestDeck.PresentationPart;

        // If the merged presentation does not have a SlideIdList element yet, add it.
        if (destPresPart.Presentation.SlideIdList == null)
            destPresPart.Presentation.SlideIdList = new SlideIdList();

        // Open the source presentation. This will throw an exception if the source presentation does not exist.
        using (PresentationDocument mySourceDeck = PresentationDocument.Open(presentationFolder + sourcePresentation, false))
        {
            PresentationPart sourcePresPart = mySourceDeck.PresentationPart;

            // Get unique ids for the slide master and slide lists for use later.
            uniqueId = GetMaxSlideMasterId(destPresPart.Presentation.SlideMasterIdList);

            uint maxSlideId = GetMaxSlideId(destPresPart.Presentation.SlideIdList);

            // Copy each slide in the source presentation, in order, to the destination presentation.
            foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList)
            {
                SlidePart sp;
                SlidePart destSp;
                SlideMasterPart destMasterPart;
                string relId;
                SlideMasterId newSlideMasterId;
                SlideId newSlideId;

                // Create a unique relationship id.
                id++;
                sp = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId);

                relId = sourcePresentation.Remove(sourcePresentation.IndexOf('.')) + id;

                // Add the slide part to the destination presentation.
                destSp = destPresPart.AddPart<SlidePart>(sp, relId);

                // The slide master part was added. Make sure the relationship between the main presentation part and
                // the slide master part is in place.
                destMasterPart = destSp.SlideLayoutPart.SlideMasterPart;
                destPresPart.AddPart(destMasterPart);

                // Add the slide master id to the slide master id list.
                uniqueId++;
                newSlideMasterId = new SlideMasterId();
                newSlideMasterId.RelationshipId = destPresPart.GetIdOfPart(destMasterPart);
                newSlideMasterId.Id = uniqueId;

                destPresPart.Presentation.SlideMasterIdList.Append(newSlideMasterId);

                // Add the slide id to the slide id list.
                maxSlideId++;
                newSlideId = new SlideId();
                newSlideId.RelationshipId = relId;
                newSlideId.Id = maxSlideId;

                destPresPart.Presentation.SlideIdList.Append(newSlideId);
            }

            // Make sure that all slide layout ids are unique.
            FixSlideLayoutIds(destPresPart);
        }

        // Save the changes to the destination deck.
        destPresPart.Presentation.Save();
    }
}

   public static void FixSlideLayoutIds(PresentationPart presPart)
    {
        //Need to make sure all slide layouts have unique ids
        foreach (SlideMasterPart slideMasterPart in presPart.SlideMasterParts)
        {
            foreach (SlideLayoutId slideLayoutId in slideMasterPart.SlideMaster.SlideLayoutIdList)
            {
                uniqueId++;
                slideLayoutId.Id = (uint)uniqueId;
            }
            slideMasterPart.SlideMaster.Save();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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