示例#1
0
        /// <summary>
        /// Saves shape in storage
        /// Returns a key to find the shape by,
        /// or null if the shape cannot be copied
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="formats">Required for msoPlaceholder</param>
        /// <returns>identifier of copied shape</returns>
        public string CopyShape(Shape shape, Format[] formats)
        {
            #pragma warning disable 618
            PowerPointPresentation pres  = PowerPointPresentation.Current;
            PowerPointSlide        slide = PowerPointCurrentPresentationInfo.CurrentSlide;
            Shape copiedShape            = null;
            if (shape.Type == MsoShapeType.msoPlaceholder)
            {
                ClipboardUtil.RestoreClipboardAfterAction(() =>
                {
                    copiedShape = ShapeUtil.CopyMsoPlaceHolder(formats, shape, GetTemplateShapes());
                    return(ClipboardUtil.ClipboardRestoreSuccess);
                }, pres, slide);
            }
            else
            {
                try
                {
                    ClipboardUtil.RestoreClipboardAfterAction(() =>
                    {
                        copiedShape = Slides[0].Shapes.SafeCopyPlaceholder(shape);
                        return(ClipboardUtil.ClipboardRestoreSuccess);
                    }, pres, slide);
                }
                catch
                {
                    copiedShape = null;
                }
            }
            #pragma warning restore 618

            if (copiedShape == null)
            {
                return(null);
            }

            string shapeKey = nextKey.ToString();
            nextKey++;
            copiedShape.Name = shapeKey;

            #pragma warning disable 618
            if (Globals.ThisAddIn.IsApplicationVersion2013())
            {
                // sync glow, 2013 gives the wrong Glow.Fill color after copying the shape
                SyncFormats(shape, copiedShape, _glowFormats);
                // sync shape fill, 2013 gives the wrong fill color after copying the shape
                SyncFormats(shape, copiedShape, _fillFormats);

                // backup artistic effects for 2013
                // ForceSave() will make artistic effect permanent on the shapes for 2013 and no longer retrievable
                List <MsoPictureEffectType> extractedEffects = ArtisticEffectFormat.GetArtisticEffects(copiedShape);
                _backupArtisticEffects.Add(shapeKey, extractedEffects);
            }
            #pragma warning restore 618

            ForceSave();
            return(shapeKey);
        }
        /// <summary>
        /// Saves shape in storage
        /// Returns a key to find the shape by,
        /// or null if the shape cannot be copied
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="formats">Required for msoPlaceholder</param>
        /// <returns>identifier of copied shape</returns>
        public string CopyShape(Shape shape, Format[] formats)
        {
            Shape copiedShape = null;

            if (shape.Type == MsoShapeType.msoPlaceholder)
            {
                copiedShape = ShapeUtil.CopyMsoPlaceHolder(formats, shape, GetTemplateShapes());
            }
            else
            {
                try
                {
                    shape.Copy();
                    copiedShape = Slides[0].Shapes.Paste()[1];
                }
                catch
                {
                    copiedShape = null;
                }
            }

            if (copiedShape == null)
            {
                return(null);
            }

            string shapeKey = nextKey.ToString();

            nextKey++;
            copiedShape.Name = shapeKey;

            #pragma warning disable 618
            if (Globals.ThisAddIn.IsApplicationVersion2013())
            {
                // sync glow, 2013 gives the wrong Glow.Fill color after copying the shape
                SyncFormats(shape, copiedShape, _glowFormats);
                // sync shape fill, 2013 gives the wrong fill color after copying the shape
                SyncFormats(shape, copiedShape, _fillFormats);

                // backup artistic effects for 2013
                // ForceSave() will make artistic effect permanent on the shapes for 2013 and no longer retrievable
                List <MsoPictureEffectType> extractedEffects = ArtisticEffectFormat.GetArtisticEffects(copiedShape);
                _backupArtisticEffects.Add(shapeKey, extractedEffects);
            }
            #pragma warning restore 618

            ForceSave();
            return(shapeKey);
        }
        public Shape GetShape(string shapeKey)
        {
            Shapes shapes = Slides[0].Shapes;

            for (int i = 1; i <= shapes.Count; i++)
            {
                if (shapes[i].Name.Equals(shapeKey))
                {
                    Shape shape = shapes[i];
                    if (_backupArtisticEffects.ContainsKey(shapeKey))
                    {
                        // apply artistic effect from backup only when shape is retrieved, to reduce loading time of CopyShape
                        List <MsoPictureEffectType> extractedEffects = _backupArtisticEffects[shapeKey];
                        ArtisticEffectFormat.ClearArtisticEffects(shape);
                        ArtisticEffectFormat.ApplyArtisticEffects(shape, extractedEffects);
                    }
                    return(shape);
                }
            }
            return(null);
        }