使用OpenXML合并PowerPoint幻灯片

时间:2012-09-07 15:08:30

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

我想要做的就是使用OpenXML SDK将一些幻灯片合并到一个主演示文稿中,我不想使用Introp,因为它在服务器环境中并不理想,我尝试了很多代码示例,但合并后的演示文稿总是显示我修复了消息,当我将损坏的文件与修复后的文件进行比较时,我发现没有正确生成ID。

是否有任何实际可行的开源库或示例代码。我听说过Aspose,但它是一个付费的图书馆。

3 个答案:

答案 0 :(得分:2)

Microsoft MSDN文章

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

提供有关如何将powerpoint演示文稿合并在一起的分步说明。

确保ID是唯一的。唯一的缺点是它还会为每张幻灯片复制幻灯片母版,使最终的powerpoint文件大于预期。

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

如果id不是唯一的,那么你面临问题,下面是我正在使用的工作代码。 我知道它的旧线程,但如果有人正在寻找答案,

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();
        }
    }