调用: string[] files = { "jyadmin20160700111.jpg", "jyadmin20170720128.jpg", "jyadmin20170730127.jpg" }; string imgs = string.Join(",",files); CreatePdf(imgs);
#region 图片插入PDF方法 /// <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"); string pdfpath = @"G:\MyWeb\Web学习\Windows\LiveProject\LiveProject\Images\"; //文件路 径 string imagepath = @"G:\MyWeb\Web学习\Windows\LiveProject\LiveProject\Images\"; ///实例化一个doc 对象 Document doc = new Document(); try { ///创建一个pdf 对象 PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/" + DateTime.Now.ToString("yyyyMMddhhssff") + ".pdf", FileMode.Create)); //打开文件 doc.Open(); ///向文件中添加单个图片 //iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath + "/jyadmin20160700111.jpg"); //image.ScaleToFit(520, 800); //doc.Add(image); ///向文件中循环添加图片 iTextSharp.text.Image image; for (int i = 0; i < imgs.Length; i++) { image = iTextSharp.text.Image.GetInstance(imagepath + imgs[i].ToString()); image.ScaleToFit(520, 800); doc.NewPage(); doc.Add(image); } } catch (DocumentException dex) { ////如果文件出现异常输入文件异常 Response.Write("文件异常:" + dex.Message); } catch (IOException ioex) { ////如果文件读写出现异常输入文件异常 Response.Write("文件读写异常:" + ioex.Message); } catch (Exception ex) { Response.Write(ex.Message); } finally { doc.Close(); } } #endregion
原文地址:https://www.cnblogs.com/sharing1986687846/p/10649665.html
时间: 2024-11-09 03:58:26