zoukankan      html  css  js  c++  java
  • 一个Itextsharp 批量添加图片到pdf 方法

    这里我就直接把我的页面贴进来了
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using iTextSharp;
    using iTextSharp.text.pdf;
    using iTextSharp.text;
    using System.IO;
    using System.Diagnostics;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
       
    
        }
    
    
    
    
    
    
        /// <summary>
        /// pdf添加图片
        /// </summary>
        /// <param name="imglist">图片的list</param>
        public void CreatePdf(string imglist) 
        {
    
            ///分割list
            string [] imgs = imglist.Split(',');
    
            string pdfpath = Server.MapPath("pdf");
    
            string imagepath = Server.MapPath("Image");
    
            ///实例化一个doc 对象
            Document doc = new Document();
    
            try
            {
                ///创建一个pdf 对象
                PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/" + DateTime.Now.ToString("yyyyMMddhhssff") + ".pdf", FileMode.Create));
             
                //打开文件
                doc.Open();
    
    
                ///向文件中添加图片
                doc.Add(iTextSharp.text.Image.GetInstance(imagepath + "/3.gif"));
    
                ///向文件中循环添加图片
                for (int i = 0; i < imgs.Length; i++)
                {
                    doc.Add(iTextSharp.text.Image.GetInstance(imagepath + imgs[i].ToString()));
                }
    
            }
    
            catch (DocumentException dex)
            {
                ////如果文件出现异常输入文件异常
                Response.Write("文件异常:"+dex.Message);
    
            }
    
            catch (IOException ioex)
            {
                ////如果文件读写出现异常输入文件异常
                Response.Write("文件读写异常:" + ioex.Message);
    
            }
    
            catch (Exception ex)
            {
    
                Response.Write(ex.Message);
    
            }
    
            finally
            {
    
                doc.Close();
    
            }
    
    
        }
    
    
    
    
    }
  • 相关阅读:
    spring整合mybatis的事物管理配置
    平庸久了,是会上瘾的
    kafka基本概念
    JMS编程模型
    aop的基本概念
    事务的隔离级别和传播行为
    IntelliJ IDEA 导入Project
    [PowerShell]列出檔案與資料夾的資訊
    [powershell]统计目录大小
    [powershell]有关IP的设置
  • 原文地址:https://www.cnblogs.com/yiliuyang/p/4259419.html
Copyright © 2011-2022 走看看