将IMAGE转为PDF后上传

     using iTextSharp.text;
     using iTextSharp.text.pdf;

     ///
<summary> /// 将IMAGE转为PDF后上传 /// </summary> /// <param name="img"></param> /// <param name="strUpLoadPdfPath"></param> /// <param name="strNewFileName"></param> /// <param name="imgFormat"></param> public void uploadImageToPdf(System.Drawing.Image img, string strUpLoadPdfPath, ref string strNewFileName, System.Drawing.Imaging.ImageFormat imgFormat = null) { try { if (imgFormat == null) { imgFormat = System.Drawing.Imaging.ImageFormat.Jpeg; } //PDF存档路径 if (strUpLoadPdfPath.LastIndexOf(@"\") == strUpLoadPdfPath.Length - 1) { strUpLoadPdfPath = strUpLoadPdfPath.Substring(0, strUpLoadPdfPath.Length - 1); } if (strNewFileName.Trim() == "") { strNewFileName = DateTime.Now.ToString("yyyyMMddHHmm"); } strNewFileName = strUpLoadPdfPath + @"\" + strNewFileName; if (!strNewFileName.ToLower().Contains(".pdf")) { strNewFileName += ".pdf"; } if (!Directory.Exists(strUpLoadPdfPath)) { Directory.CreateDirectory(strUpLoadPdfPath); } //定义pdf大小,设置上下左右边距 iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A3, 15, 15, 20, 20); //设置保存路径 PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(strNewFileName, FileMode.Create)); //生成pdf路径,创建文件流 doc.Open(); //将图片写入PDF中 iTextSharp.text.Image imgSave = iTextSharp.text.Image.GetInstance(img, imgFormat); imgSave.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE; imgSave.ScaleAbsolute(750f, 1150f); doc.Add(imgSave); //保存PDF文件 writer.Flush(); writer.CloseStream = true; doc.Close(); } catch (Exception ex) { throw ex; } }

猜你喜欢

转载自www.cnblogs.com/6B23/p/12205662.html