/// <summary>
        /// Adds a PdfScriptObject to the page that draws an image previously added to the DocumentWriter,
        /// stretched to the given width and height.
        /// </summary>
        public static PdfObjectRef DrawImageRef(this PdfPageWriter page, double x, double y, PdfObjectRef imageRef, double width, double leftRotationDegrees = 0.0)
        {
            var obj = new PdfScriptObject();

            obj.DrawImageByName(x, y, width, page.DocumentWriter.GetImageHeight(imageRef, width), page.DocumentWriter.GetNameOfXObject(imageRef), leftRotationDegrees);
            return(page.WriteObject(obj));
        }
        /// <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));
        }