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