hsf-bk
        #region 图片转PDF
        /// <summary>
        /// 图片转pdf
        /// </summary>
        /// <param name="jpgfile">要转换的图片的路径,不需要加后缀和标识的数字</param>
        /// <param name="pdfoutputpath">转成后的文件地址和文件名</param>]
        /// <param name="count">图片的数量</param>
        public static void ImageToPDF(string jpgfile, string pdfoutputpath, int count)
        {

            var document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
            using (var stream = new FileStream(pdfoutputpath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                iTextSharp.text.pdf.PdfWriter.GetInstance(document, stream);
                document.Open();
                for (int i = 1; i <= count; i++)
                {
                    using (var imageStream = new FileStream(jpgfile + i + ".png", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        var image = iTextSharp.text.Image.GetInstance(imageStream);
                        if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
                        {
                            image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
                        }
                        else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
                        {
                            image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
                        }
                        image.Alignment = iTextSharp.text.Element.ALIGN_MIDDLE;
                        document.NewPage();
                        document.Add(image);
                    }
                }
                document.Close();
            }
        }
        #endregion

 

分类:

技术点:

相关文章:

  • 2022-02-22
  • 2021-11-09
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
猜你喜欢
  • 2022-01-24
  • 2021-09-13
  • 2021-08-30
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案