簡體   English   中英

使用OpenXML合並PowerPoint幻燈片

[英]Merging PowerPoint slides using OpenXML

我要做的就是使用OpenXML SDK將一些幻燈片合並到主演示文稿中,我不想使用Introp,因為它在服務器環境中並不理想,我嘗試了很多代碼示例,但是合並后的演示文稿總是向我顯示修復消息,將損壞的文件與已修復的文件進行比較時,我發現ID生成不正確。

是否有任何實際可用的開源庫或示例代碼。 我聽說過Aspose,但這是一個付費圖書館。

Microsoft MSDN文章

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

提供有關如何將Powerpoint演示文稿合並在一起的分步說明。

它會確保ID唯一。 唯一的缺點是,它還會為每張幻燈片復制幻燈片母版,從而使最終的PowerPoint文件比預期的大。

http://openxmldeveloper.org/上嘗試演示文稿構建器代碼。

是的,如果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();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM