Пример #1
0
        /// <summary>
        /// 获取指定宽高压缩后的百分比
        /// </summary>
        /// <param name="width">目标宽</param>
        /// <param name="height">目标高</param>
        /// <param name="containerRect">原始对象</param>
        /// <returns>目标与原始对象百分比</returns>
        public static float GetPercentage(float width, float height, CanvasRectangle containerRect)
        {
            float percentage = 0;

            if (height > width)
            {
                percentage = containerRect.RectHeight / height;

                if (width * percentage > containerRect.RectWidth)
                {
                    percentage = containerRect.RectWidth / width;
                }
            }
            else
            {
                percentage = containerRect.RectWidth / width;

                if (height * percentage > containerRect.RectHeight)
                {
                    percentage = containerRect.RectHeight / height;
                }
            }

            return(percentage);
        }
Пример #2
0
        /// <summary>
        /// 指定pdf模板文件添加图片
        /// </summary>
        /// <param name="tempFilePath"></param>
        /// <param name="createdPdfPath"></param>
        /// <param name="pdfImages"></param>
        public void PutImages(string tempFilePath, string createdPdfPath, List <PdfImage> pdfImages)
        {
            PdfReader  pdfReader  = null;
            PdfStamper pdfStamper = null;

            try
            {
                pdfReader  = new PdfReader(tempFilePath);
                pdfStamper = new PdfStamper(pdfReader, new FileStream(createdPdfPath, FileMode.OpenOrCreate));

                var pdfContentByte = pdfStamper.GetOverContent(1);//获取PDF指定页面内容

                foreach (var pdfImage in pdfImages)
                {
                    Uri   uri = null;
                    Image img = null;

                    var imageUrl = pdfImage.ImageUrl;

                    //如果使用网络路径则先将图片转化位绝对路径
                    if (imageUrl.StartsWith("http"))
                    {
                        //var absolutePath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(".."), imageUrl);
                        var absolutePath = System.Web.HttpContext.Current.Server.MapPath("..") + imageUrl;
                        uri = new Uri(absolutePath);
                        img = Image.GetInstance(uri);
                    }
                    else
                    {
                        //如果直接使用图片文件则直接创建iTextSharp的Image对象
                        if (pdfImage.ImgBytes != null)
                        {
                            img = Image.GetInstance(new MemoryStream(pdfImage.ImgBytes));
                        }
                    }

                    if (img != null)
                    {
                        if (pdfImage.ScaleParent)
                        {
                            var containerRect = pdfImage.ContainerRect;

                            float percentage = 0.0f;
                            percentage = CanvasRectangle.GetPercentage(img.Width, img.Height, containerRect);
                            img.ScalePercent(percentage * 100);

                            pdfImage.AbsoluteX = (containerRect.RectWidth - img.Width * percentage) / 2 + containerRect.StartX;
                            pdfImage.AbsoluteY = (containerRect.RectHeight - img.Height * percentage) / 2 + containerRect.StartY;
                        }
                        else
                        {
                            img.ScaleToFit(pdfImage.FitWidth, pdfImage.FitHeight);
                        }

                        img.SetAbsolutePosition(pdfImage.AbsoluteX, pdfImage.AbsoluteY);
                        pdfContentByte.AddImage(img);
                    }
                }

                pdfStamper.FormFlattening = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (pdfStamper != null)
                {
                    pdfStamper.Close();
                }

                if (pdfReader != null)
                {
                    pdfReader.Close();
                }

                pdfStamper = null;
                pdfReader  = null;
            }
        }