zoukankan      html  css  js  c++  java
  • 批量 多个JPG生产PDF .net C#

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    
    using System.Windows.Forms;
    
    namespace JGP2PDF
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
    
                    OpenFileDialog op = new OpenFileDialog();
                    op.Multiselect = true;
                    op.Filter = "标签 | *.jpg";
                    if (op.ShowDialog() == DialogResult.OK)
                    {
                        var filenames = op.FileNames;
                        ConvertJPG2PDF(filenames, textBox2.Text);
                        MessageBox.Show("导出成功!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    throw ex;
                }
    
            }
    
            private static void ConvertJPG2PDF(string[] jpgfiles, string pdf)
            {
                var document = new Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
                using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    PdfWriter.GetInstance(document, stream);
                    document.Open();
                    foreach (var jpgfile in jpgfiles)
                    {
                        using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            var image = iTextSharp.text.Image.GetInstance(imageStream);
                            if (image.Height > iTextSharp.text.PageSize.A4.Height)
                            {
                                image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
                            }
                            else if (image.Width > iTextSharp.text.PageSize.A4.Width)
                            {
                                image.ScaleToFit(iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
                            }
                            image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
                            document.Add(image);
                        }
                    }
    
                    document.Close();
    
                }
            }
        }
    
    
    }
  • 相关阅读:
    跟王千问学数学之什么是比
    王千问之学习的金字塔
    跟王千问学数学之平方差公式
    跟王千问学数学之三角形
    跟王千问学数学之小数加法、最小公倍数
    跟王千问学数学之小数
    一年级孩子的特点以及如何辅导他们学习
    python中的property属性
    python中的浅拷贝和深拷贝
    http协议、web服务器、并发服务器(下)
  • 原文地址:https://www.cnblogs.com/huchaoheng/p/10170147.html
Copyright © 2011-2022 走看看