这里我就直接把我的页面贴进来了
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 = ; 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(); } } }