2

我想合并多个 PointPoint 卡组,当每个卡组包含 30 张或更少的幻灯片时,合并工作正常,如果任何卡组包含超过 30 张幻灯片,PowerPoint 会发出修复消息。OpenXML SDK 工具验证没有帮助,我正在使用以下代码进行合并,或者任何其他有帮助的库?

public class SlideMerger
{

    #region variables

    uint uniqueId;
    int partId = 0;        
    List<string> mergingParts = new List<string>() { "introPart" ,"productsPart", "priceGridsPart", "closingPart", "contactPart" };

    #endregion

    #region methods

    public void MergeSlides(List<MemoryStream> sourceDecks, MemoryStream destDeck)
    {

        foreach (var sourcePresentation in sourceDecks)
        {
            MergeSlides(sourcePresentation, destDeck);
            partId++;
        } 
    }

    void MergeSlides(MemoryStream sourcePresentation, MemoryStream destPresentation)
    {
        int id = 0;

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

            // If the merged presentation doesn't have a SlideIdList element yet then 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 SourceDeck = PresentationDocument.Open(sourcePresentation, false))
            {
                PresentationPart sourcePresPart = SourceDeck.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 = mergingParts[partId] + id;

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

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

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

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

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

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

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

    void FixSlideLayoutIds(PresentationPart presPart)
    {
        // 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();
        }
    }

    uint GetMaxSlideId(SlideIdList slideIdList)
    {
        // Slide identifiers have a minimum value of greater than or equal to 256
        // and a maximum value of less than 2147483648. 
        uint max = 256;

        if (slideIdList != null)
            // Get the maximum id value from the current set of children.
            foreach (SlideId child in slideIdList.Elements<SlideId>())
            {
                uint id = child.Id;

                if (id > max)
                    max = id;
            }

        return max;
    }

    uint GetMaxSlideMasterId(SlideMasterIdList slideMasterIdList)
    {
        // Slide master identifiers have a minimum value of greater than or equal to 2147483648. 
        uint max = 2147483648;

        if (slideMasterIdList != null)
            // Get the maximum id value from the current set of children.
            foreach (SlideMasterId child in slideMasterIdList.Elements<SlideMasterId>())
            {
                uint id = child.Id;

                if (id > max)
                    max = id;
            }

        return max;
    }

    #endregion
}
4

0 回答 0