コード例 #1
0
ファイル: PptxManager.cs プロジェクト: harlov-va/st_publish
        public static void DeleteSlide(PresentationDocument presentationDocument, int slideIndex, int slidesCount)
        {
            slideIndex--;
            if (presentationDocument == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }



            if (slideIndex < 0 || slideIndex >= slidesCount)
            {
                throw new ArgumentOutOfRangeException("slideIndex");
            }

            PresentationPart presentationPart = presentationDocument.PresentationPart;

            Presentation presentation = presentationPart.Presentation;

            SlideIdList slideIdList = presentation.SlideIdList;

            SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId;

            string slideRelId = slideId.RelationshipId;

            slideIdList.RemoveChild(slideId);

            if (presentation.CustomShowList != null)
            {
                foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>())
                {
                    if (customShow.SlideList != null)
                    {
                        LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>();
                        foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements())
                        {
                            if (slideListEntry.Id != null && slideListEntry.Id == slideRelId)
                            {
                                slideListEntries.AddLast(slideListEntry);
                            }
                        }

                        foreach (SlideListEntry slideListEntry in slideListEntries)
                        {
                            customShow.SlideList.RemoveChild(slideListEntry);
                        }
                    }
                }
            }

            presentation.Save();

            SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;

            presentationPart.DeletePart(slidePart);
        }
コード例 #2
0
        /// <summary>
        /// replace the current theme with a user specified theme
        /// </summary>
        /// <param name="document">document file lcoation</param>
        /// <param name="themeFile">theme xml file location</param>
        /// <param name="app">which app is the document</param>
        public static void ReplaceTheme(string document, string themeFile, string app)
        {
            if (app == "Word")
            {
                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
                {
                    MainDocumentPart mainPart = wordDoc.MainDocumentPart;

                    // Delete the old document part.
                    mainPart.DeletePart(mainPart.ThemePart);

                    // Add a new document part and then add content.
                    ThemePart themePart = mainPart.AddNewPart <ThemePart>();

                    using (StreamReader streamReader = new StreamReader(themeFile))
                        using (StreamWriter streamWriter = new StreamWriter(themePart.GetStream(FileMode.Create)))
                        {
                            streamWriter.Write(streamReader.ReadToEnd());
                        }
                }
            }
            else if (app == "PowerPoint")
            {
                using (PresentationDocument presDoc = PresentationDocument.Open(document, true))
                {
                    PresentationPart mainPart = presDoc.PresentationPart;
                    mainPart.DeletePart(mainPart.ThemePart);
                    ThemePart themePart = mainPart.AddNewPart <ThemePart>();

                    using (StreamReader streamReader = new StreamReader(themeFile))
                        using (StreamWriter streamWriter = new StreamWriter(themePart.GetStream(FileMode.Create)))
                        {
                            streamWriter.Write(streamReader.ReadToEnd());
                        }
                }
            }
            else
            {
                using (SpreadsheetDocument excelDoc = SpreadsheetDocument.Open(document, true))
                {
                    WorkbookPart mainPart = excelDoc.WorkbookPart;
                    mainPart.DeletePart(mainPart.ThemePart);
                    ThemePart themePart = mainPart.AddNewPart <ThemePart>();

                    using (StreamReader streamReader = new StreamReader(themeFile))
                        using (StreamWriter streamWriter = new StreamWriter(themePart.GetStream(FileMode.Create)))
                        {
                            streamWriter.Write(streamReader.ReadToEnd());
                        }
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Delete the specified slide from the presentation by slide index
        /// </summary>
        /// <param name="presentationPart"></param>
        /// <param name="slideIndex"></param>
        private static void DeleteSlide(PresentationPart presentationPart, int slideIndex)
        {
            // Get the presentation from the presentation part.
            Presentation presentation = presentationPart.Presentation;

            // Get the list of slide IDs in the presentation.
            SlideIdList slideIdList = presentation.SlideIdList;

            // Get the slide ID of the specified slide
            SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId;

            // Get the relationship ID of the slide.
            string slideRelId = slideId.RelationshipId;

            // Remove the slide from the slide list.
            slideIdList.RemoveChild(slideId);

            //
            // Remove references to the slide from all custom shows.
            if (presentation.CustomShowList != null)
            {
                // Iterate through the list of custom shows.
                foreach (CustomShow customShow in presentation.CustomShowList.Elements <CustomShow>())
                {
                    if (customShow.SlideList != null)
                    {
                        // Declare a link list of slide list entries.
                        LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>();
                        foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements())
                        {
                            // Find the slide reference to remove from the custom show.
                            if (slideListEntry.Id != null && slideListEntry.Id == slideRelId)
                            {
                                slideListEntries.AddLast(slideListEntry);
                            }
                        }

                        // Remove all references to the slide from the custom show.
                        foreach (SlideListEntry slideListEntry in slideListEntries)
                        {
                            customShow.SlideList.RemoveChild(slideListEntry);
                        }
                    }
                }
            }
            // Get the slide part for the specified slide.
            SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;

            // Remove the slide part.
            presentationPart.DeletePart(slidePart);
        }
コード例 #4
0
        private void RemoveFromDom(int number)
        {
            PresentationPart presentationPart = _sdkPre.PresentationPart;

            DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation;
            // gets the list of slide identifiers in the presentation
            SlideIdList slideIdList = presentation.SlideIdList;
            // gets the slide identifier of the specified slide
            SlideId slideId = (SlideId)slideIdList.ChildElements[number - 1];
            // gets the relationship identifier of the slide
            string slideRelId = slideId.RelationshipId;

            // removes the slide from the slide list
            slideIdList.RemoveChild(slideId);

            // remove references to the slide from all custom shows
            if (presentation.CustomShowList != null)
            {
                // iterates through the list of custom shows
                foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>())
                {
                    if (customShow.SlideList == null)
                    {
                        continue;
                    }

                    // declares a link list of slide list entries
                    var slideListEntries = new LinkedList <SlideListEntry>();
                    foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements())
                    {
                        // finds the slide reference to remove from the custom show
                        if (slideListEntry.Id != null && slideListEntry.Id == slideRelId)
                        {
                            slideListEntries.AddLast(slideListEntry);
                        }
                    }
                    // removes all references to the slide from the custom show
                    foreach (SlideListEntry slideListEntry in slideListEntries)
                    {
                        customShow.SlideList.RemoveChild(slideListEntry);
                    }
                }
            }

            // gets the slide part for the specified slide
            SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;

            // removes the slide part
            presentationPart.DeletePart(slidePart);
        }
コード例 #5
0
        void DeleteTemplateSlide(PresentationPart presentationPart, SlidePart slideTemplate)
        {
            //Get the list of slide ids
            SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;

            //Delete the template slide reference
            foreach (SlideId slideId in slideIdList.ChildElements)
            {
                if (slideId.RelationshipId.Value.Equals("rId2"))
                {
                    slideIdList.RemoveChild(slideId);
                }
            }
            //Delete the template slide
            presentationPart.DeletePart(slideTemplate);
        }
コード例 #6
0
        internal static void ApplyTableStyles(PresentationPart presentationPart, Drawing.TableStyleList tableStyleList)
        {
            string tableStylesId = null;

            if (presentationPart.TableStylesPart != null)
            {
                tableStylesId = presentationPart.GetIdOfPart(presentationPart.TableStylesPart);
                presentationPart.DeletePart(tableStylesId);
            }
            else
            {
                tableStylesId = presentationPart.GetNextRelationshipId();
            }

            TableStylesPart tableStylesPart = presentationPart.AddNewPart <TableStylesPart>(tableStylesId);

            tableStyleList.Save(tableStylesPart);
        }
コード例 #7
0
        internal static void ApplyTheme(PresentationPart presentationPart, Drawing.Theme theme)
        {
            string themeId = null;

            if (presentationPart.ThemePart != null)
            {
                themeId = presentationPart.GetIdOfPart(presentationPart.ThemePart);
                presentationPart.DeletePart(themeId);
            }
            else
            {
                themeId = presentationPart.GetNextRelationshipId();
            }

            ThemePart themePart = presentationPart.AddNewPart <ThemePart>(themeId);

            theme.Save(themePart);
        }
コード例 #8
0
        // Apply a new theme to the presentation.
        public static void ApplyThemeToPresentation(PresentationDocument presentationDocument, PresentationDocument themeDocument)
        {
            if (presentationDocument == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }
            if (themeDocument == null)
            {
                throw new ArgumentNullException("themeDocument");
            }

            // Get the presentation part of the presentation document.
            PresentationPart presentationPart = presentationDocument.PresentationPart;

            // Get the existing slide master part.
            SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.ElementAt(0);
            string          relationshipId  = presentationPart.GetIdOfPart(slideMasterPart);

            // Get the new slide master part.
            SlideMasterPart newSlideMasterPart = themeDocument.PresentationPart.SlideMasterParts.ElementAt(0);

            // Remove the existing theme part.
            presentationPart.DeletePart(presentationPart.ThemePart);

            // Remove the old slide master part.
            presentationPart.DeletePart(slideMasterPart);

            // Import the new slide master part, and reuse the old relationship ID.
            newSlideMasterPart = presentationPart.AddPart(newSlideMasterPart, relationshipId);

            // Change to the new theme part.
            presentationPart.AddPart(newSlideMasterPart.ThemePart);

            Dictionary <string, SlideLayoutPart> newSlideLayouts = new Dictionary <string, SlideLayoutPart>();

            foreach (var slideLayoutPart in newSlideMasterPart.SlideLayoutParts)
            {
                newSlideLayouts.Add(GetSlideLayoutType(slideLayoutPart), slideLayoutPart);
            }

            string          layoutType    = null;
            SlideLayoutPart newLayoutPart = null;

            // Insert the code for the layout for this example.
            string defaultLayoutType = "Title and Content";

            // Remove the slide layout relationship on all slides.
            foreach (var slidePart in presentationPart.SlideParts)
            {
                layoutType = null;

                if (slidePart.SlideLayoutPart != null)
                {
                    // Determine the slide layout type for each slide.
                    layoutType = GetSlideLayoutType(slidePart.SlideLayoutPart);

                    // Delete the old layout part.
                    slidePart.DeletePart(slidePart.SlideLayoutPart);
                }

                if (layoutType != null && newSlideLayouts.TryGetValue(layoutType, out newLayoutPart))
                {
                    // Apply the new layout part.
                    slidePart.AddPart(newLayoutPart);
                }
                else
                {
                    newLayoutPart = newSlideLayouts[defaultLayoutType];

                    // Apply the new default layout part.
                    slidePart.AddPart(newLayoutPart);
                }
            }
        }
コード例 #9
0
        // Delete the specified slide from the presentation.
        public static void DeleteSlide(PresentationDocument presentationDocument, int slideIndex)
        {
            if (presentationDocument == null)
            {
                throw new ArgumentNullException("presentationDocument");
            }

            // Use the CountSlides sample to get the number of slides in the presentation.
            int slidesCount = CountSlides(presentationDocument);

            if (slideIndex < 0 || slideIndex >= slidesCount)
            {
                throw new ArgumentOutOfRangeException("slideIndex");
            }

            // Get the presentation part from the presentation document.
            PresentationPart presentationPart = presentationDocument.PresentationPart;

            // Get the presentation from the presentation part.
            Presentation presentation = presentationPart.Presentation;

            // Get the list of slide IDs in the presentation.
            SlideIdList slideIdList = presentation.SlideIdList;

            // Get the slide ID of the specified slide
            SlideId slideId = slideIdList.ChildElements[slideIndex] as SlideId;

            // Get the relationship ID of the slide.
            string slideRelId = slideId.RelationshipId;

            // Remove the slide from the slide list.
            slideIdList.RemoveChild(slideId);

            //
            // Remove references to the slide from all custom shows.
            if (presentation.CustomShowList != null)
            {
                // Iterate through the list of custom shows.
                foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>())
                {
                    if (customShow.SlideList != null)
                    {
                        // Declare a link list of slide list entries.
                        LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>();
                        foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements())
                        {
                            // Find the slide reference to remove from the custom show.
                            if (slideListEntry.Id != null && slideListEntry.Id == slideRelId)
                            {
                                slideListEntries.AddLast(slideListEntry);
                            }
                        }

                        // Remove all references to the slide from the custom show.
                        foreach (SlideListEntry slideListEntry in slideListEntries)
                        {
                            customShow.SlideList.RemoveChild(slideListEntry);
                        }
                    }
                }
            }

            // Save the modified presentation.
            presentation.Save();

            // Get the slide part for the specified slide.
            SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;

            // Remove the slide part.
            presentationPart.DeletePart(slidePart);
        }
コード例 #10
0
        private static void AddImage(string file, List <ImagePath> image)
        {
            using (var presentationDocument = PresentationDocument.Open(file, true))
            {
                var                slideCount       = presentationDocument.PresentationPart.SlideParts.Count();
                SlideIdList        slideIdList      = presentationDocument.PresentationPart.Presentation.SlideIdList;
                Presentation       presentation     = presentationDocument.PresentationPart.Presentation;
                PresentationPart   presentationPart = presentationDocument.PresentationPart;
                OpenXmlElementList slideIds         = presentationPart.Presentation.SlideIdList.ChildElements;

                int cnt = 0;             // 画像の添付数

                int       j         = 0; // 画像添付スライド位置
                string    relId     = (slideIds[j] as SlideId).RelationshipId;
                SlidePart slidePart = (SlidePart)presentationPart.GetPartById(relId);

                foreach (ImagePath imgPath in image)
                {
                    ImagePart part = slidePart
                                     .AddImagePart(ImagePartType.Png);

                    using (var stream = File.OpenRead(imgPath.Path))
                    {
                        part.FeedData(stream);
                    }
                    var tree = slidePart
                               .Slide
                               .Descendants <DocumentFormat.OpenXml.Presentation.ShapeTree>()
                               .First();
                    var picture = new DocumentFormat.OpenXml.Presentation.Picture();


                    picture.NonVisualPictureProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties();
                    picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties
                    {
                        Name = "My Shape",
                        Id   = (UInt32)tree.ChildElements.Count - 1
                    });

                    var nonVisualPictureDrawingProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties();
                    nonVisualPictureDrawingProperties.Append(new Drawing2.PictureLocks()
                    {
                        NoChangeAspect = true
                    });
                    picture.NonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
                    picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties());

                    var blipFill = new DocumentFormat.OpenXml.Presentation.BlipFill();
                    var blip1    = new Drawing2.Blip()
                    {
                        Embed = slidePart.GetIdOfPart(part)
                    };
                    var blipExtensionList1 = new Drawing2.BlipExtensionList();
                    var blipExtension1     = new Drawing2.BlipExtension()
                    {
                        Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"
                    };
                    var useLocalDpi1 = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi()
                    {
                        Val = false
                    };
                    useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
                    blipExtension1.Append(useLocalDpi1);
                    blipExtensionList1.Append(blipExtension1);
                    blip1.Append(blipExtensionList1);
                    var stretch = new Drawing2.Stretch();
                    stretch.Append(new Drawing2.FillRectangle());
                    blipFill.Append(blip1);
                    blipFill.Append(stretch);
                    picture.Append(blipFill);

                    picture.ShapeProperties             = new DocumentFormat.OpenXml.Presentation.ShapeProperties();
                    picture.ShapeProperties.Transform2D = new Drawing2.Transform2D();

                    int rotation = 0;
                    switch (imgPath.Rot)
                    {
                    case RotateFlipType.RotateNoneFlipNone:
                        rotation = 0;
                        picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset
                        {
                            X = POSITION0[cnt % 6].X,
                            Y = POSITION0[cnt % 6].Y,
                        });
                        break;

                    case RotateFlipType.Rotate180FlipNone:     // 時計回りに180度回転しているので、180度回転して戻す
                        rotation = 60000 * 180;
                        picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset
                        {
                            X = POSITION0[cnt % 6].X,
                            Y = POSITION0[cnt % 6].Y,
                        });
                        break;

                    case RotateFlipType.Rotate90FlipNone:     // 時計回りに270度回転しているので、90度回転して戻す
                        rotation = 60000 * 90;
                        picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset
                        {
                            X = POSITION90[cnt % 6].X,
                            Y = POSITION90[cnt % 6].Y,
                        });
                        break;

                    case RotateFlipType.Rotate270FlipNone:     // 時計回りに90度回転しているので、270度回転して戻す
                        rotation = 60000 * 270;
                        picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset
                        {
                            X = POSITION90[cnt % 6].X,
                            Y = POSITION90[cnt % 6].Y,
                        });
                        break;

                    default:
                        rotation = 0;
                        picture.ShapeProperties.Transform2D.Append(new Drawing2.Offset
                        {
                            X = POSITION0[cnt % 6].X,
                            Y = POSITION0[cnt % 6].Y,
                        });
                        break;
                    }
                    picture.ShapeProperties.Transform2D.Rotation = rotation;

                    // 縦向き
                    picture.ShapeProperties.Transform2D.Append(new Drawing2.Extents
                    {
                        Cx = imgPath.Size.Width,
                        Cy = imgPath.Size.Height,
                    });

                    picture.ShapeProperties.Append(new Drawing2.PresetGeometry
                    {
                        Preset = Drawing2.ShapeTypeValues.Rectangle
                    });

                    tree.Append(picture);

                    if (cnt % 6 == 5)
                    {
                        if (j < slideCount - 1)
                        {
                            j++;
                            relId     = (slideIds[j] as SlideId).RelationshipId;
                            slidePart = (SlidePart)presentationPart.GetPartById(relId);
                        }
                        else
                        {
                            // 画像ループを抜ける
                            break;
                        }
                    }
                    cnt++;
                }

                // スライドを削除する
                for (int i = slideCount - 1; i > j; i--)
                {
                    //Console.WriteLine(i);
                    SlideId slideId    = slideIds[i] as SlideId;
                    string  slideRelId = slideId.RelationshipId;
                    slideIdList.RemoveChild(slideId);

                    if (presentation.CustomShowList != null)
                    {
                        // Iterate through the list of custom shows.
                        foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>())
                        {
                            if (customShow.SlideList != null)
                            {
                                // Declare a link list of slide list entries.
                                LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>();
                                foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements())
                                {
                                    // Find the slide reference to remove from the custom show.
                                    if (slideListEntry.Id != null && slideListEntry.Id == slideRelId)
                                    {
                                        slideListEntries.AddLast(slideListEntry);
                                    }
                                }

                                // Remove all references to the slide from the custom show.
                                foreach (SlideListEntry slideListEntry in slideListEntries)
                                {
                                    customShow.SlideList.RemoveChild(slideListEntry);
                                }
                            }
                        }
                    }
                    presentation.Save();

                    SlidePart slidePart2 = presentationPart.GetPartById(slideRelId) as SlidePart;

                    presentationPart.DeletePart(slidePart2);
                }
            }
        }
コード例 #11
0
    public static void DeleteSlide(string presentationFile)
    {
        if (presentationFile == null)
        {
            throw new ArgumentNullException("presentationDocument");
        }

        using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true))
        {
            // Get the presentation part from the presentation document.
            PresentationPart presentationPart = presentationDocument.PresentationPart;

            // Get the presentation from the presentation part.
            DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation;

            // Get the list of slide IDs in the presentation.
            SlideIdList slideIdList = presentation.SlideIdList;

            int slideIdx = -1;
            foreach (SlideId _slideId in presentation.SlideIdList)
            {
                slideIdx++;

                // Get the slide ID of the specified slide
                SlideId slideId = slideIdList.ChildElements[slideIdx] as SlideId;

                // Get the relationship ID of the slide.
                string slideRelId = slideId.RelationshipId;

                // Get the slide part for the specified slide.
                SlidePart slidePart = presentationPart.GetPartById(slideRelId) as SlidePart;

                if (slidePart.Slide.Show != null)
                {
                    if (slidePart.Slide.Show.HasValue != null)
                    {
                        // Remove the slide from the slide list.
                        slideIdList.RemoveChild(slideId);

                        // Remove references to the slide from all custom shows.
                        if (presentation.CustomShowList != null)
                        {
                            // Iterate through the list of custom shows.
                            foreach (var customShow in presentation.CustomShowList.Elements <CustomShow>())
                            {
                                if (customShow.SlideList != null)
                                {
                                    // Declare a link list of slide list entries.
                                    LinkedList <SlideListEntry> slideListEntries = new LinkedList <SlideListEntry>();
                                    foreach (SlideListEntry slideListEntry in customShow.SlideList.Elements())
                                    {
                                        // Find the slide reference to remove from the custom show.
                                        if (slideListEntry.Id != null && slideListEntry.Id == slideRelId)
                                        {
                                            slideListEntries.AddLast(slideListEntry);
                                        }
                                    }

                                    // Remove all references to the slide from the custom show.
                                    foreach (SlideListEntry slideListEntry in slideListEntries)
                                    {
                                        customShow.SlideList.RemoveChild(slideListEntry);
                                    }
                                }
                            }
                        }

                        // Save the modified presentation.
                        presentation.Save();

                        // Remove the slide part.
                        presentationPart.DeletePart(slidePart);
                        break;
                    }
                }
            }
        }
    }