public static PdfImagePlacement Rotated(this PdfImagePlacement me, PdfImageRotation rotation)
        {
            for (var rotations = 0; rotations < (int)rotation; rotations++)
            {
                switch (me)
                {
                case PdfImagePlacement.LeftOrTop:
                    me = PdfImagePlacement.RightOrTop;
                    break;

                case PdfImagePlacement.RightOrTop:
                    me = PdfImagePlacement.RightOrBottom;
                    break;

                case PdfImagePlacement.RightOrBottom:
                    me = PdfImagePlacement.LeftOrBottom;
                    break;

                case PdfImagePlacement.LeftOrBottom:
                    me = PdfImagePlacement.LeftOrTop;
                    break;
                }
            }

            return(me);
        }
        /// <summary>
        /// Adds a PdfScriptObject to the page that draws an image,
        /// scaled to the given width and height.
        /// </summary>
        public static PdfObjectRef DrawImage(this PdfPageWriter page, double x, double y, Image image, double width, double height, PdfImagePlacement imagePlacement, PdfImageRotation rotation = PdfImageRotation.None)
        {
            var imageRef = page.DocumentWriter.AddImage(image);

            return(page.DrawImageRef(x, y, imageRef, width, height, imagePlacement, rotation));
        }
        /// <summary>
        /// Adds a PdfScriptObject to the page that draws an image previously added to the DocumentWriter,
        /// scaled to the given width and height.
        /// </summary>
        public static PdfObjectRef DrawImageRef(this PdfPageWriter page, double x, double y, PdfObjectRef imageRef, double width, double height, PdfImagePlacement imagePlacement, PdfImageRotation rotation = PdfImageRotation.None)
        {
            var width2  = width;
            var height2 = height;
            var xdelta  = 0.0;
            var ydelta  = 0.0;

            // Fix for rotations:
            if (rotation == PdfImageRotation.Left || rotation == PdfImageRotation.Right)
            {
                Swap(ref width2, ref height2);
            }
            imagePlacement = imagePlacement.Rotated(rotation);

            if (imagePlacement != PdfImagePlacement.Stretch)
            {
                // Calculate box aspect ratio and image aspect ratio:
                var boxar = height2 / width2;
                var imgar = page.DocumentWriter.GetImageHeight(imageRef, 1.0);

                if (imgar < boxar)
                {
                    var imgh = height2 / boxar * imgar;
                    switch (imagePlacement)
                    {
                    case PdfImagePlacement.LeftOrTop:
                    case PdfImagePlacement.RightOrTop:
                        ydelta = (height2 - imgh);
                        break;

                    case PdfImagePlacement.Center:
                        ydelta = (height2 - imgh) / 2.0;
                        break;
                    }
                    height2 = imgh;
                }
                else
                {
                    var imgw = width2 * boxar / imgar;
                    switch (imagePlacement)
                    {
                    case PdfImagePlacement.RightOrBottom:
                        xdelta = (width2 - imgw);
                        break;

                    case PdfImagePlacement.RightOrTop:
                        xdelta = (width2 - imgw);
                        break;

                    case PdfImagePlacement.Center:
                        xdelta = (width2 - imgw) / 2.0;
                        break;
                    }
                    width2 = imgw;
                }
            }

            // Correct for rotations:
            switch (rotation)
            {
            case PdfImageRotation.None: break;

            case PdfImageRotation.Left:
                Swap(ref xdelta, ref ydelta);
                xdelta = width - xdelta;
                break;

            case PdfImageRotation.Right:
                Swap(ref xdelta, ref ydelta);
                ydelta = height - ydelta;
                break;

            case PdfImageRotation.UpsideDown:
                xdelta = width - xdelta;
                ydelta = height - ydelta;
                break;
            }

            var obj = new PdfScriptObject();

            obj.DrawImageByName(x + xdelta, y + ydelta, width2, height2, page.DocumentWriter.GetNameOfXObject(imageRef), ((int)rotation) * 90.0);
            return(page.WriteObject(obj));
        }