示例#1
0
        /// <summary>Scale the image to an absolute size.</summary>
        /// <remarks>
        /// Scale the image to an absolute size. This method will preserve the
        /// width-height ratio of the image.
        /// </remarks>
        /// <param name="fitWidth">the new maximum width of the image</param>
        /// <param name="fitHeight">the new maximum height of the image</param>
        /// <returns>this element</returns>
        public virtual iText.Layout.Element.Image ScaleToFit(float fitWidth, float fitHeight)
        {
            float horizontalScaling = fitWidth / xObject.GetWidth();
            float verticalScaling   = fitHeight / xObject.GetHeight();

            return(Scale(Math.Min(horizontalScaling, verticalScaling), Math.Min(horizontalScaling, verticalScaling)));
        }
示例#2
0
        public override LayoutResult Layout(LayoutContext layoutContext)
        {
            LayoutArea area      = layoutContext.GetArea().Clone();
            Rectangle  layoutBox = area.GetBBox();

            ApplyMargins(layoutBox, false);
            occupiedArea = new LayoutArea(area.GetPageNumber(), new Rectangle(layoutBox.GetX(), layoutBox.GetY() + layoutBox
                                                                              .GetHeight(), 0, 0));
            width = RetrieveWidth(layoutBox.GetWidth());
            float?     angle   = this.GetPropertyAsFloat(Property.ROTATION_ANGLE);
            PdfXObject xObject = ((Image)(GetModelElement())).GetXObject();

            imageWidth     = xObject.GetWidth();
            imageHeight    = xObject.GetHeight();
            width          = width == null ? imageWidth : width;
            height         = (float)width / imageWidth * imageHeight;
            fixedXPosition = this.GetPropertyAsFloat(Property.X);
            fixedYPosition = this.GetPropertyAsFloat(Property.Y);
            float?          horizontalScaling = this.GetPropertyAsFloat(Property.HORIZONTAL_SCALING, 1f);
            float?          verticalScaling   = this.GetPropertyAsFloat(Property.VERTICAL_SCALING, 1f);
            AffineTransform t = new AffineTransform();

            if (xObject is PdfFormXObject && width != imageWidth)
            {
                horizontalScaling *= width / imageWidth;
                verticalScaling   *= height / imageHeight;
            }
            if (horizontalScaling != 1)
            {
                if (xObject is PdfFormXObject)
                {
                    t.Scale((float)horizontalScaling, 1);
                }
                width *= (float)horizontalScaling;
            }
            if (verticalScaling != 1)
            {
                if (xObject is PdfFormXObject)
                {
                    t.Scale(1, (float)verticalScaling);
                }
                height *= (float)verticalScaling;
            }
            float imageItselfScaledWidth  = (float)width;
            float imageItselfScaledHeight = (float)height;

            // See in adjustPositionAfterRotation why angle = 0 is necessary
            if (null == angle)
            {
                angle = 0f;
            }
            t.Rotate((float)angle);
            float scaleCoef = AdjustPositionAfterRotation((float)angle, layoutBox.GetWidth(), layoutBox.GetHeight());

            imageItselfScaledHeight *= scaleCoef;
            imageItselfScaledWidth  *= scaleCoef;
            if (xObject is PdfFormXObject)
            {
                t.Scale(scaleCoef, scaleCoef);
            }
            GetMatrix(t, imageItselfScaledWidth, imageItselfScaledHeight);
            // indicates whether the placement is forced
            bool isPlacingForced = false;

            if (width > layoutBox.GetWidth() || height > layoutBox.GetHeight())
            {
                if (true.Equals(GetPropertyAsBoolean(Property.FORCED_PLACEMENT)))
                {
                    isPlacingForced = true;
                }
                else
                {
                    return(new LayoutResult(LayoutResult.NOTHING, occupiedArea, null, this, this));
                }
            }
            occupiedArea.GetBBox().MoveDown(height);
            occupiedArea.GetBBox().SetHeight(height);
            occupiedArea.GetBBox().SetWidth((float)width);
            float leftMargin = (float)this.GetPropertyAsFloat(Property.MARGIN_LEFT);
            float topMargin  = (float)this.GetPropertyAsFloat(Property.MARGIN_TOP);

            if (leftMargin != 0 || topMargin != 0)
            {
                TranslateImage(leftMargin, topMargin, t);
                GetMatrix(t, imageItselfScaledWidth, imageItselfScaledHeight);
            }
            ApplyMargins(occupiedArea.GetBBox(), true);
            return(new LayoutResult(LayoutResult.FULL, occupiedArea, null, null, isPlacingForced ? this : null));
        }
        protected internal override void DoDraw(SvgDrawContext context)
        {
            ResourceResolver resourceResolver = context.GetResourceResolver();

            if (resourceResolver == null || this.attributesAndStyles == null)
            {
                return;
            }
            String     uri     = this.attributesAndStyles.Get(SvgConstants.Attributes.XLINK_HREF);
            PdfXObject xObject = resourceResolver.RetrieveImageExtended(uri);

            if (xObject == null)
            {
                return;
            }
            PdfCanvas currentCanvas = context.GetCurrentCanvas();
            float     x             = 0;

            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.X))
            {
                x = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.X));
            }
            float y = 0;

            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.Y))
            {
                y = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.Y));
            }
            float width = 0;

            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.WIDTH))
            {
                width = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.WIDTH));
            }
            float height = 0;

            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.HEIGHT))
            {
                height = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.HEIGHT));
            }
            String preserveAspectRatio = "";

            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.PRESERVE_ASPECT_RATIO))
            {
                preserveAspectRatio = attributesAndStyles.Get(SvgConstants.Attributes.PRESERVE_ASPECT_RATIO);
            }
            preserveAspectRatio = preserveAspectRatio.ToLowerInvariant();
            if (!SvgConstants.Values.NONE.Equals(preserveAspectRatio) && !(width == 0 || height == 0))
            {
                float normalizedWidth;
                float normalizedHeight;
                if (xObject.GetWidth() / width > xObject.GetHeight() / height)
                {
                    normalizedWidth  = width;
                    normalizedHeight = xObject.GetHeight() / xObject.GetWidth() * width;
                }
                else
                {
                    normalizedWidth  = xObject.GetWidth() / xObject.GetHeight() * height;
                    normalizedHeight = height;
                }
                switch (preserveAspectRatio.ToLowerInvariant())
                {
                case SvgConstants.Values.XMIN_YMIN: {
                    break;
                }

                case SvgConstants.Values.XMIN_YMID: {
                    y += Math.Abs(normalizedHeight - height) / 2;
                    break;
                }

                case SvgConstants.Values.XMIN_YMAX: {
                    y += Math.Abs(normalizedHeight - height);
                    break;
                }

                case SvgConstants.Values.XMID_YMIN: {
                    x += Math.Abs(normalizedWidth - width) / 2;
                    break;
                }

                case SvgConstants.Values.XMID_YMAX: {
                    x += Math.Abs(normalizedWidth - width) / 2;
                    y += Math.Abs(normalizedHeight - height);
                    break;
                }

                case SvgConstants.Values.XMAX_YMIN: {
                    x += Math.Abs(normalizedWidth - width);
                    break;
                }

                case SvgConstants.Values.XMAX_YMID: {
                    x += Math.Abs(normalizedWidth - width);
                    y += Math.Abs(normalizedHeight - height) / 2;
                    break;
                }

                case SvgConstants.Values.XMAX_YMAX: {
                    x += Math.Abs(normalizedWidth - width);
                    y += Math.Abs(normalizedHeight - height);
                    break;
                }

                case SvgConstants.Values.DEFAULT_ASPECT_RATIO:
                default: {
                    x += Math.Abs(normalizedWidth - width) / 2;
                    y += Math.Abs(normalizedHeight - height) / 2;
                    break;
                }
                }
                width  = normalizedWidth;
                height = normalizedHeight;
            }
            float v = y + height;

            currentCanvas.AddXObject(xObject, width, 0, 0, -height, x, v);
        }