其他分享
首页 > 其他分享> > itextsharp给pd添加logo、图片

itextsharp给pd添加logo、图片

作者:互联网

        /// <summary>
        /// 添加图片、logo等 
        /// 引用组件itextsharp
        /// </summary>
        /// <param name="inputfilepath"></param>
        /// <param name="outputfiledir"></param>
        /// <param name="imgpath"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="startNumber"></param>
        /// <param name="endNumber"></param>
        /// <returns></returns>
        private string AddPdfImage(string inputfilepath, string outputfiledir, string imgpath, int x, int y, int startNumber, int endNumber, int percent)
        {

            try
            {
                using (PdfReader pdfReader = new PdfReader(inputfilepath))
                {
                    var pagesNumber = pdfReader.NumberOfPages;
                    if (startNumber < pagesNumber && endNumber > pagesNumber)
                    {
                        endNumber = pagesNumber;
                    }
                    if (startNumber > pagesNumber || endNumber > pagesNumber)
                    {
                        throw new IndexOutOfRangeException("页码超出范围!");
                    }

                    var filename = Path.GetFileNameWithoutExtension(inputfilepath) + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
                    var outputfilepathtemp = Path.Combine(outputfiledir, filename);

                    using (PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepathtemp, FileMode.Create)))
                    {
                        using (Stream inputImageStream = new FileStream(imgpath, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);//获取图片
                            image.ScalePercent(percent);//设置图片缩放比例
                            for (int i = startNumber; i <= endNumber; i++)
                            {
                                iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(i);
                                float width = psize.Width;
                                float height = psize.Height;

                                float absoluteX = width * x / 100 - image.ScaledWidth;
                                float absoluteY = height * y / 100 - image.ScaledHeight;
                                if (absoluteX < 0)
                                {
                                    absoluteX = 0;
                                }
                                if (absoluteY < 0)
                                {
                                    absoluteY = 0;
                                }
                                image.SetAbsolutePosition(absoluteX, height - image.ScaledHeight - absoluteY);//设置图片的绝对位置
                                var pdfContentByte = pdfStamper.GetOverContent(i);//获取内容

                                pdfContentByte.AddImage(image);
                            }
                        }
                    }

                    return outputfilepathtemp;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 

标签:string,int,endNumber,pagesNumber,itextsharp,startNumber,pd,new,logo
来源: https://www.cnblogs.com/njcxwz/p/15637221.html