private static List <SlidePart> CreateImageSlideParts(PresentationPart presentationPart, List <SvgDocument> svgDocs)
        {
            int           id = 256;
            string        relId;
            SlideId       newSlideId;
            SlideLayoutId newSlideLayoutId;
            uint          uniqueId   = GetMaxUniqueId(presentationPart);
            uint          maxSlideId = GetMaxSlideId(presentationPart.Presentation.SlideIdList);
            // get first slide master part: template
            SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.First();

            List <SlidePart> slideParts = new List <SlidePart>();

            for (int i = 0; i < svgDocs.Count; i++)
            {
                id++;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (System.Drawing.Bitmap image = svgDocs[i].Draw())
                    {
                        image.Save(ms, ImageFormat.Bmp);
                        ms.Seek(0, SeekOrigin.Begin);
                        relId = "rId" + id;
                        // add new slide part
                        SlidePart slidePart = presentationPart.AddNewPart <SlidePart>(relId);

                        // add image part to slide part
                        ImagePart imagePart = slidePart.AddImagePart(ImagePartType.Bmp, relId);
                        imagePart.FeedData(ms);
                        // add image slide
                        CreateImageSlide(relId).Save(slidePart);

                        // add slide layout part to slide part
                        SlideLayoutPart slideLayoutPart = slidePart.AddNewPart <SlideLayoutPart>();
                        CreateSlideLayoutPart().Save(slideLayoutPart);
                        slideMasterPart.AddPart(slideLayoutPart);
                        slideLayoutPart.AddPart(slideMasterPart);

                        uniqueId++;
                        newSlideLayoutId = new SlideLayoutId();
                        newSlideLayoutId.RelationshipId = slideMasterPart.GetIdOfPart(slideLayoutPart);
                        newSlideLayoutId.Id             = uniqueId;
                        slideMasterPart.SlideMaster.SlideLayoutIdList.Append(newSlideLayoutId);

                        // add slide part to presentaion slide list
                        maxSlideId++;
                        newSlideId = new SlideId();
                        newSlideId.RelationshipId = relId;
                        newSlideId.Id             = maxSlideId;
                        presentationPart.Presentation.SlideIdList.Append(newSlideId);
                    }
                }
            }
            slideMasterPart.SlideMaster.Save();
            return(slideParts);
        }
Ejemplo n.º 2
0
    static void CreateSlides(List <string> imageFileNames, string newPresentation)
    {
        int           id = 0;
        string        relId;
        SlideId       newSlideId;
        SlideLayoutId newSlideLayoutId;

        string imageFileNameNoPath;

        long imageWidthEMU  = 0;
        long imageHeightEMU = 0;

        // Open the new presentation.
        using (PresentationDocument newDeck = PresentationDocument.Open(newPresentation, true))
        {
            // Get the presentation part of the new deck.
            PresentationPart presentationPart = newDeck.PresentationPart;

            // Reuse the slide master. Otherwise, create a new slide master part and a new theme part.
            var slideMasterPart = presentationPart.SlideMasterParts.First();

            // If the new presentation doesn't have a SlideIdList element yet then add it.
            if (presentationPart.Presentation.SlideIdList == null)
            {
                presentationPart.Presentation.SlideIdList = new SlideIdList();
            }

            // If the slide master doesn't have a SlideLayoutIdList element yet then add it.
            if (slideMasterPart.SlideMaster.SlideLayoutIdList == null)
            {
                slideMasterPart.SlideMaster.SlideLayoutIdList = new SlideLayoutIdList();
            }

            // Get a unique id for both the slide master id and slide layout id lists.
            uint uniqueId = GetMaxUniqueId(presentationPart);

            // Get a unique id for the slide id list.
            uint maxSlideId = GetMaxSlideId(presentationPart.Presentation.SlideIdList);

            // Loop through each file in the image folder creating slides in the new presentation.
            foreach (string imageFileNameWithPath in imageFileNames)
            {
                imageFileNameNoPath = Path.GetFileNameWithoutExtension(imageFileNameWithPath);

                // Create a unique relationship id based on the name of the image file.
                id++;
                relId = imageFileNameNoPath.Replace(" ", "") + id;

                // Get the bytes and size of the image.
                byte[] imageBytes = GetImageData(imageFileNameWithPath, ref imageWidthEMU, ref imageHeightEMU);

                // Create the new slide part.
                var slidePart = presentationPart.AddNewPart <SlidePart>(relId);
                GenerateSlidePart(relId, imageFileNameNoPath, imageFileNameNoPath, imageWidthEMU, imageHeightEMU).Save(slidePart);

                var imagePart = slidePart.AddImagePart(ImagePartType.Jpeg, relId);
                GenerateImagePart(imagePart, imageBytes);

                //slidePart.AddPart(slideLayoutPart);
                var slideLayoutPart = slidePart.AddNewPart <SlideLayoutPart>();
                GenerateSlideLayoutPart().Save(slideLayoutPart);

                slideMasterPart.AddPart(slideLayoutPart);
                slideLayoutPart.AddPart(slideMasterPart);

                // Add new slide layout into the list in slideMasterPart
                uniqueId++;
                newSlideLayoutId = new SlideLayoutId();
                newSlideLayoutId.RelationshipId = slideMasterPart.GetIdOfPart(slideLayoutPart);
                newSlideLayoutId.Id             = uniqueId;
                slideMasterPart.SlideMaster.SlideLayoutIdList.Append(newSlideLayoutId);

                // Add slide to slide list.
                maxSlideId++;
                newSlideId = new SlideId();
                newSlideId.RelationshipId = relId;
                newSlideId.Id             = maxSlideId;
                presentationPart.Presentation.SlideIdList.Append(newSlideId);
            }

            slideMasterPart.SlideMaster.Save();

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

            // Save the changes to the new deck.
            presentationPart.Presentation.Save();
        }
    }