Esempio n. 1
0
        /// <summary>
        /// Adds logo from model to current pdf document.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="logo">Logo model.</param>
        /// <returns>
        /// Returns current pdf document.
        /// </returns>
        public static Document AddLogo(this Document document, LogoModel logo)
        {
            SentinelHelper.ArgumentNull(document);

            if (logo == null)
            {
                return(document);
            }

            if (logo.IsDefault)
            {
                return(document);
            }

            if (logo.Show == YesNo.No)
            {
                return(document);
            }

            System.Drawing.Image imageLogo;
            var found = logo.Image.TryGetImage(out imageLogo);

            if (!found)
            {
                return(document);
            }

            var root = logo.Parent.Parent;

            var imagePath     = root.Resources.Images[0].Path;
            var imageFileName = Path.GetFileNameWithoutExtension(root.ParseRelativeFilePath(imagePath));

            var modifiedImageFileNamePathBuilder = new StringBuilder();

            modifiedImageFileNamePathBuilder.Append(FileHelper.TinExportTempDirectory);
            modifiedImageFileNamePathBuilder.Append(imageFileName);
            modifiedImageFileNamePathBuilder.Append(".png");

            imageLogo.Save(modifiedImageFileNamePathBuilder.ToString(), ImageFormat.Png);

            var logoWidth = logo.LogoSize.Width == -1
                                ? imageLogo.Width
                                : logo.LogoSize.Width;

            var logoHeight = logo.LogoSize.Height == -1
                                 ? imageLogo.Height
                                 : logo.LogoSize.Height;

            var modifiedLogo = Image.GetInstance(modifiedImageFileNamePathBuilder.ToString());

            modifiedLogo.ScaleAbsolute(logoWidth, logoHeight);

            var logoPosition = logo.Location;
            var positionType = logoPosition.LocationType;

            if (positionType.Equals(KnownElementLocation.ByAlignment))
            {
                document.SetVerticalLocationFrom(logoPosition);

                var x         = 0.0f;
                var y         = document.PageSize.Height - logoHeight - document.TopMargin;
                var alignMode = ((AlignmentModel)logoPosition.Mode).Horizontal;
                switch (alignMode)
                {
                case KnownHorizontalAlignment.Center:
                    x = document.PageSize.Left + document.LeftMargin + ((document.PageSize.Width - document.LeftMargin - document.RightMargin - logoWidth) / 2);
                    break;

                case KnownHorizontalAlignment.Left:
                    x = document.PageSize.Left + document.LeftMargin;
                    break;

                case KnownHorizontalAlignment.Right:
                    x = document.PageSize.Right - document.RightMargin - logoWidth;
                    break;
                }

                modifiedLogo.SetAbsolutePosition(x, y);
            }

            document.Add(modifiedLogo);

            return(document);
        }