C# 多张JPG合成PDF 基于iTextSharp5.5.13.3

C# 多张JPG合成PDF

网上找的多张JPG合成PDF多是iTextSharp的老版本,iTextSharp5.5.13.3是比较新的版本,现在最新的全新版本是itext7。老版本用iTextSharp,System.Drawing.Bitmap和会有内存溢出的问题。

引用
using iTextSharp.text;
using iTextSharp.text.pdf;

 private void button_TurnImagesToPdf_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Multiselect = true;//等于true表示可以选择多个文件
            openFile.DefaultExt = "*.jpg";
            openFile.Filter = "图片|*.jpg";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                sw.Start();
                int jpg_i = 0;
                string filePathOnly = Path.GetDirectoryName(openFile.FileName);
                string folderName = Path.GetFileName(filePathOnly);
                string pdfPath = filePathOnly + "\\" + folderName + ".pdf";
                using (Document document = new Document())
                {
                    using (FileStream PdfStream = new FileStream(pdfPath, FileMode.Create, FileAccess.ReadWrite))
                    {
                        using (iTextSharp.text.pdf.PdfWriter write = PdfWriter.GetInstance(document, PdfStream))
                        {
                            document.SetMargins(0, 0, 0, 0);
                            document.Open();
                            iTextSharp.text.Image jpg=null;
                            foreach (string file in openFile.FileNames)
                            {
                                try
                                {
                                    System.Drawing.Image SImage = System.Drawing.Image.FromFile(file);
                                    int Width = SImage.Width;
                                    int Height = SImage.Height;
                                    SImage.Dispose();
                                    jpg = iTextSharp.text.Image.GetInstance(file);
                                    document.SetPageSize(new iTextSharp.text.Rectangle(Width, Height));
                                    document.NewPage();
                                    document.Add(jpg);
                                    jpg_i++;
                                }
                                catch
                                { }
                            }
                            write.Flush();
                            document.Close();
                        }
                    }
                }
                sw.Stop();
                double double_sec = sw.ElapsedMilliseconds / 1000.0;
                MessageBox.Show("合并" + jpg_i.ToString() + "个,且用时" +
                      double_sec.ToString() + "秒");
            }
            GC.Collect();
        }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值