Exemple #1
0
        private static void CreateRotatedFillInBackground(PowerPoint.Shape shape, Bitmap slideImage, double magnifyRatio = 1.0)
        {
            var rotatedShape = new Utils.PPShape(shape, false);
            var topLeftPoint = new PointF(rotatedShape.ActualTopLeft.X * Utils.Graphics.PictureExportingRatio,
                                          rotatedShape.ActualTopLeft.Y * Utils.Graphics.PictureExportingRatio);

            Bitmap rotatedImage = new Bitmap(slideImage.Width, slideImage.Height);

            using (Graphics g = Graphics.FromImage(rotatedImage))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                using (System.Drawing.Drawing2D.Matrix mat = new System.Drawing.Drawing2D.Matrix())
                {
                    mat.Translate(-topLeftPoint.X, -topLeftPoint.Y);
                    mat.RotateAt(-shape.Rotation, topLeftPoint);

                    g.Transform = mat;
                    g.DrawImage(slideImage, new Rectangle(0, 0, slideImage.Width, slideImage.Height));
                }
            }

            var magnifiedImage = KiCut(rotatedImage, 0, 0, shape.Width * Utils.Graphics.PictureExportingRatio,
                                       shape.Height * Utils.Graphics.PictureExportingRatio, magnifyRatio);

            magnifiedImage.Save(FillInBackgroundPicture, ImageFormat.Png);
        }
        /// <summary>
        /// Adjust the visual width of the specified shapes to the resize factors of first
        /// selected shape's visual width.
        /// </summary>
        /// <param name="selectedShapes"></param>
        public void AdjustVisualWidthProportionally(PowerPoint.ShapeRange selectedShapes)
        {
            try
            {
                var referenceWidth = GetReferenceWidth(selectedShapes);

                if (referenceWidth <= 0 || AdjustProportionallyProportionList?.Count != selectedShapes.Count) return;

                for (int i = 1; i < AdjustProportionallyProportionList.Count; i++)
                {
                    var newWidth = referenceWidth*
                                   (AdjustProportionallyProportionList[i] / AdjustProportionallyProportionList[0]);
                    var shape = new PPShape(selectedShapes[i + 1]);
                    var anchorPoint = GetVisualAnchorPoint(shape);

                    shape.AbsoluteWidth = newWidth;
                    AlignVisualShape(shape, anchorPoint);
                }
                AdjustProportionallyProportionList = null;
            }
            catch (Exception e)
            {
                Logger.LogException(e, "AdjustVisualWidthProportionally");
            }
        }
 private PointF GetActualAnchorPoint(PPShape shape)
 {
     switch (AnchorPointType)
     {
         case AnchorPoint.TopLeft:
             return shape.ActualTopLeft;
         case AnchorPoint.TopCenter:
             return shape.ActualTopCenter;
         case AnchorPoint.TopRight:
             return shape.ActualTopRight;
         case AnchorPoint.MiddleLeft:
             return shape.ActualMiddleLeft;
         case AnchorPoint.Center:
             return shape.ActualCenter;
         case AnchorPoint.MiddleRight:
             return shape.ActualMiddleRight;
         case AnchorPoint.BottomLeft:
             return shape.ActualBottomLeft;
         case AnchorPoint.BottomCenter:
             return shape.ActualBottomCenter;
         case AnchorPoint.BottomRight:
             return shape.ActualBottomRight;
     }
     return shape.ActualTopLeft;
 }
        /// <summary>
        /// Resize the visual height of selected shapes to match their visual width.
        /// </summary>
        /// <param name="selectedShapes"></param>
        private void MatchVisualWidth(PowerPoint.ShapeRange selectedShapes)
        {
            try
            {
                var isAspectRatio = selectedShapes.LockAspectRatio;
                selectedShapes.LockAspectRatio = MsoTriState.msoFalse;

                for (int i = 1; i <= selectedShapes.Count; i++)
                {
                    var shape = new PPShape(selectedShapes[i]);
                    var anchorPoint = GetVisualAnchorPoint(shape);

                    shape.AbsoluteHeight = shape.AbsoluteWidth;
                    AlignVisualShape(shape, anchorPoint);
                }

                selectedShapes.LockAspectRatio = isAspectRatio;
            }
            catch (Exception e)
            {
                Logger.LogException(e, "MatchVisualWidth");
            }
        }
        /// <summary>
        /// Stretch shapes in the list
        /// </summary>
        /// <param name="stretchShapes">The shapes to stretch</param>
        /// <param name="stretchAction">The function to use to stretch</param>
        /// <param name="defaultReferenceEdge">The function to return the default reference edge</param>
        /// <param name="stretchType">The type of stretch to perform</param>
        private void Stretch(PowerPoint.ShapeRange stretchShapes, GetAppropriateStretchAction stretchAction, 
            GetDefaultReferenceEdge defaultReferenceEdge, StretchType stretchType)
        {
            if (!ValidateSelection(stretchShapes))
            {
                return;
            }

            var referenceShape = GetReferenceShape(stretchShapes, defaultReferenceEdge, stretchType);
            var referenceEdge = defaultReferenceEdge(new PPShape(referenceShape));

            for (var i = 1; i <= stretchShapes.Count; i++)
            {
                if (referenceShape.Equals(stretchShapes[i]))
                {
                    continue;
                }
                var stretchShape = new PPShape(stretchShapes[i]);
                var sa = stretchAction(referenceEdge, stretchShape);
                sa(referenceEdge, stretchShape);
            }
        }
 private static void StretchBottomAction(float referenceEdge, PPShape stretchShape)
 {
     var oldTop = stretchShape.VisualTop;
     stretchShape.AbsoluteHeight += referenceEdge - GetBottom(stretchShape);
     stretchShape.VisualTop = oldTop;
 }
 private static void StretchTopAction(float referenceEdge, PPShape stretchShape)
 {
     stretchShape.AbsoluteHeight += stretchShape.VisualTop - referenceEdge;
     stretchShape.VisualTop = referenceEdge;
 }
 private static void StretchRightAction(float referenceEdge, PPShape stretchShape)
 {
     var oldLeft = stretchShape.VisualLeft;
     stretchShape.AbsoluteWidth += referenceEdge - GetRight(stretchShape);
     stretchShape.VisualLeft = oldLeft;
 }
        /// <summary>
        /// Fit the selected shapes without aspect ratio according to the set dimension type.
        /// </summary>
        /// <param name="selectedShapes"></param>
        /// <param name="dimension"></param>
        /// <param name="slideWidth"></param>
        /// <param name="slideHeight"></param>
        private void FitFreeShapes(PowerPoint.ShapeRange selectedShapes, Dimension dimension, float slideWidth, float slideHeight)
        {
            try
            {
                for (int i = 1; i <= selectedShapes.Count; i++)
                {
                    var shape = new PPShape(selectedShapes[i]);

                    if ((dimension == Dimension.Height) || (dimension == Dimension.HeightAndWidth))
                    {
                        shape.AbsoluteHeight = slideHeight;
                        shape.VisualTop = 0;
                    }

                    if ((dimension == Dimension.Width) || (dimension == Dimension.HeightAndWidth))
                    {
                        shape.AbsoluteWidth = slideWidth;
                        shape.VisualLeft = 0;
                    }

                    if (dimension == Dimension.HeightAndWidth)
                    {
                        shape.VisualTop = 0;
                        shape.VisualLeft = 0;
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogException(e, "FitFreeShapes");
            }
        }
 private static float GetBottom(PPShape aShape)
 {
     return aShape.VisualTop + aShape.AbsoluteHeight;
 }
        /// <summary>
        /// Symmetrize the shapes.
        /// </summary>
        /// <param name="selectedShapes"></param>
        /// <param name="msoFlipCmd"></param>
        /// <param name="adjustPosition"></param>
        private static void Symmetrize(PowerPoint.ShapeRange selectedShapes, MsoFlipCmd msoFlipCmd, Action<PPShape, PPShape> adjustPosition)
        {
            try
            {
                for (int i = 1; i <= selectedShapes.Count; i++)
                {
                    var originalShape = new PPShape(selectedShapes[i]);
                    var newShape = originalShape.Duplicate();

                    newShape.Flip(msoFlipCmd);
                    newShape.Select(MsoTriState.msoFalse);
                    adjustPosition.Invoke(originalShape, newShape);
                }
            }
            catch (Exception e)
            {
                Logger.LogException(e, "Symmetrize");
            }
        }
 private static int TopComparator(PPShape s1, PPShape s2)
 {
     return s1.VisualTop.CompareTo(s2.VisualTop);
 }
 private static int LeftComparator(PPShape s1, PPShape s2)
 {
     return s1.VisualLeft.CompareTo(s2.VisualLeft);
 }
        /// <summary>
        /// Resize the selected shapes according to the set dimension type.
        /// </summary>
        /// <param name="selectedShapes"></param>
        /// <param name="dimension"></param>
        private void ResizeVisualShapes(PowerPoint.ShapeRange selectedShapes, Dimension dimension)
        {
            try
            {
                var referenceHeight = GetReferenceHeight(selectedShapes);
                var referenceWidth = GetReferenceWidth(selectedShapes);

                if (!IsMoreThanOneShape(selectedShapes, Equalize_MinNoOfShapesRequired, true, Equalize_ErrorParameters) 
                    || (referenceHeight < 0) || (referenceWidth < 0))
                {
                    return;
                }

                for (int i = 1; i <= selectedShapes.Count; i++)
                {
                    var shape = new PPShape(selectedShapes[i]);
                    var anchorPoint = GetVisualAnchorPoint(shape);

                    if ((dimension == Dimension.Height) || (dimension == Dimension.HeightAndWidth))
                    {
                        shape.AbsoluteHeight = referenceHeight;
                    }

                    if ((dimension == Dimension.Width) || (dimension == Dimension.HeightAndWidth))
                    {
                        shape.AbsoluteWidth = referenceWidth;
                    }

                    AlignVisualShape(shape, anchorPoint);
                }
            }
            catch (Exception e)
            {
                Logger.LogException(e, "ResizeVisualShapes");
            }
        }
 /// <summary>
 /// Align the shape according to the anchor point given.
 /// </summary>
 /// <param name="shape"></param>
 /// <param name="anchorPoint"></param>
 private void AlignVisualShape(PPShape shape, PointF anchorPoint)
 {
     switch (AnchorPointType)
     {
         case AnchorPoint.TopLeft:
             shape.VisualTopLeft = anchorPoint;
             break;
         case AnchorPoint.TopCenter:
             shape.VisualTopCenter = anchorPoint;
             break;
         case AnchorPoint.TopRight:
             shape.VisualTopRight = anchorPoint;
             break;
         case AnchorPoint.MiddleLeft:
             shape.VisualMiddleLeft = anchorPoint;
             break;
         case AnchorPoint.Center:
             shape.VisualCenter = anchorPoint;
             break;
         case AnchorPoint.MiddleRight:
             shape.VisualMiddleRight = anchorPoint;
             break;
         case AnchorPoint.BottomLeft:
             shape.VisualBottomLeft = anchorPoint;
             break;
         case AnchorPoint.BottomCenter:
             shape.VisualBottomCenter = anchorPoint;
             break;
         case AnchorPoint.BottomRight:
             shape.VisualBottomRight = anchorPoint;
             break;
     }
 }
 /// <summary>
 /// Create a duplicate of the specified Shape object and return a new shape.
 /// </summary>
 /// <returns></returns>
 public PPShape Duplicate()
 {
     var newShape = new PPShape(_shape.Duplicate()[1]) {Name = _shape.Name + "Copy"};
     return newShape;
 }
        private PowerPoint.Shape GetReferenceShape(PowerPoint.ShapeRange shapes, GetDefaultReferenceEdge getReferenceEdge,
            StretchType stretchType)
        {
            var refShapeIndex = 1;

            if (ReferenceType == StretchRefType.Outermost)
            {
                var refPpShape = new PPShape(shapes[1]);
                for (var i = 2; i <= shapes.Count; i++)
                {
                    var tempPpShape = new PPShape(shapes[i]);
                    if (((stretchType == StretchType.Left || stretchType == StretchType.Top) &&
                        getReferenceEdge(refPpShape) > getReferenceEdge(tempPpShape)) ||
                        ((stretchType == StretchType.Right || stretchType == StretchType.Bottom) &&
                        getReferenceEdge(refPpShape) < getReferenceEdge(tempPpShape)))
                    {
                        refPpShape = tempPpShape;
                        refShapeIndex = i;
                    }
                }
            }

            return shapes[refShapeIndex];

        }
        /// <summary>
        /// Adjust the actual height of the specified shapes to the resize factor of first
        /// selected shape's actual height.
        /// </summary>
        /// <param name="selectedShapes"></param>
        public void AdjustActualHeightProportionally(PowerPoint.ShapeRange selectedShapes)
        {
            try
            {
                var referenceHeight = GetReferenceHeight(selectedShapes);

                if (referenceHeight <= 0 || AdjustProportionallyProportionList?.Count != selectedShapes.Count) return;

                for (int i = 1; i < AdjustProportionallyProportionList.Count; i++)
                {
                    var newHeight = referenceHeight*
                                    (AdjustProportionallyProportionList[i] / AdjustProportionallyProportionList[0]);
                    var shape = new PPShape(selectedShapes[i + 1], false);
                    var anchorPoint = GetActualAnchorPoint(shape);

                    shape.ShapeHeight = newHeight;
                    AlignActualShape(shape, anchorPoint);
                }
                AdjustProportionallyProportionList = null;
            }
            catch (Exception e)
            {
                Logger.LogException(e, "AdjustActualHeightProportionally");
            }
        }
 private static float GetRight(PPShape aShape)
 {
     return aShape.VisualLeft + aShape.AbsoluteWidth;
 }
 private static int LeftComparator(PPShape s1, PPShape s2)
 {
     return(s1.VisualLeft.CompareTo(s2.VisualLeft));
 }
 private static Drawing.PointF GetSwapReferencePoint(PPShape shape, SwapReference r)
 {
     switch (r)
     {
         case SwapReference.TopLeft:
             return shape.VisualTopLeft;
         case SwapReference.TopCenter:
             return shape.VisualTopCenter;
         case SwapReference.TopRight:
             return shape.VisualTopRight;
         case SwapReference.MiddleLeft:
             return shape.VisualMiddleLeft;
         case SwapReference.MiddleCenter:
             return shape.VisualCenter;
         case SwapReference.MiddleRight:
             return shape.VisualMiddleRight;
         case SwapReference.BottomLeft:
             return shape.VisualBottomLeft;
         case SwapReference.BottomCenter:
             return shape.VisualBottomCenter;
         case SwapReference.BottomRight:
             return shape.VisualBottomRight;
         default:
             return shape.VisualCenter;
     }
 }
        /// <summary>
        /// Fit the selected shapes with aspect ratio according to the set dimension type.
        /// </summary>
        /// <param name="selectedShapes"></param>
        /// <param name="dimension"></param>
        /// <param name="slideWidth"></param>
        /// <param name="slideHeight"></param>
        private void FitAspectRatioShapes(PowerPoint.ShapeRange selectedShapes, Dimension dimension, float slideWidth, float slideHeight)
        {
            try
            {
                for (int i = 1; i <= selectedShapes.Count; i++)
                {
                    var shape = selectedShapes[i];
                    var anchorPoint = GetVisualAnchorPoint(new PPShape(shape, false));

                    if (dimension == Dimension.Height)
                    {
                        FitToSlide.FitToHeight(shape, slideWidth, slideHeight);

                        var ppShape = new PPShape(shape, false);
                        AlignVisualShape(ppShape, anchorPoint);
                        ppShape.VisualTop = 0;
                    }
                    else if (dimension == Dimension.Width)
                    {
                        FitToSlide.FitToWidth(shape, slideWidth, slideHeight);

                        var ppShape = new PPShape(shape, false);
                        AlignVisualShape(ppShape, anchorPoint);
                        ppShape.VisualLeft = 0;
                    }
                    else if (dimension == Dimension.HeightAndWidth)
                    {
                        FitToSlide.AutoFit(shape, slideWidth, slideHeight);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogException(e, "FitAspectRatioShapes");
            }
        }
        private void SlightAdjustActualShape(PowerPoint.ShapeRange shapes, Action<PPShape> resizeAction)
        {
            try
            {
                foreach (PowerPoint.Shape shape in shapes)
                {
                    var ppShape = new PPShape(shape, false);
                    var anchorPoint = GetActualAnchorPoint(ppShape);

                    resizeAction(ppShape);
                    AlignActualShape(ppShape, anchorPoint);
                }
            }
            catch (Exception e)
            {
                Logger.LogException(e, "SlightAdjustActualShape");
            }
        }
 private static void StretchLeftAction(float referenceEdge, PPShape stretchShape)
 {
     stretchShape.AbsoluteWidth += stretchShape.VisualLeft - referenceEdge;
     stretchShape.VisualLeft = referenceEdge;
 }
 private static int TopComparator(PPShape s1, PPShape s2)
 {
     return(s1.VisualTop.CompareTo(s2.VisualTop));
 }
        /// <summary>
        /// Resize the actual width of selected shapes to match their actual height.
        /// </summary>
        /// <param name="selectedShapes"></param>
        private void MatchActualHeight(PowerPoint.ShapeRange selectedShapes)
        {
            try
            {
                var isAspectRatio = selectedShapes.LockAspectRatio;
                selectedShapes.LockAspectRatio = MsoTriState.msoFalse;

                for (int i = 1; i <= selectedShapes.Count; i++)
                {
                    var shape = new PPShape(selectedShapes[i], false);
                    var anchorPoint = GetActualAnchorPoint(shape);

                    shape.ShapeWidth = shape.ShapeHeight;
                    AlignActualShape(shape, anchorPoint);
                }

                selectedShapes.LockAspectRatio = isAspectRatio;
            }
            catch (Exception e)
            {
                Logger.LogException(e, "MatchActualHeight");
            }
        }