从服务器下载文件,图片转PDF,Word转PDF,将PDF合并


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


     ///
<summary> /// 图片转成pdf /// </summary> public static void ConvertJPG2PDF(string jpgfile, string pdf, out string ErrorMsg) { ErrorMsg = string.Empty; try { iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0f, 0f, 0f, 20f); using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None)) { PdfWriter.GetInstance(document, stream); document.Open(); using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var image = iTextSharp.text.Image.GetInstance(imageStream); image.ScaleAbsoluteWidth(iTextSharp.text.PageSize.A4.Width + 300); image.ScaleAbsoluteHeight(iTextSharp.text.PageSize.A4.Height - 300); image.RotationDegrees = 270f; image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE; document.Add(image); } document.Close(); } } catch (Exception ex) { Log.WriteLog(ex); ErrorMsg = ex.Message; } } /// <summary> /// word文档转pdf /// </summary> public static string WordToPdf(string sourcePath, out string ErrorMsg) { ErrorMsg = string.Empty; Microsoft.Office.Interop.Word.Application application = null; Microsoft.Office.Interop.Word.Document document = null; string PDFPath = string.Empty; if (!File.Exists(sourcePath)) return string.Empty; try { application = new Microsoft.Office.Interop.Word.Application(); application.Visible = false; document = application.Documents.Open(sourcePath); PDFPath = sourcePath.Replace(".doc", ".pdf");//pdf存放位置 //if (!File.Exists(PDFPath))//存在PDF,不需要继续转换 document.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;//设置纸张样式为A4纸 document.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//排列方式为垂直方向 //document.PageSetup.TopMargin = 0f; //document.PageSetup.BottomMargin = 0f; //document.PageSetup.LeftMargin = 0f; //document.PageSetup.RightMargin = 0f; document.ExportAsFixedFormat(PDFPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF); } catch (Exception ex) { ErrorMsg = ex.Message; Log.WriteLog(ex); PDFPath = string.Empty; } finally { try { if (document != null) document.Close(); if (application != null) application.Quit(); } catch (Exception ex) { Log.WriteLog(ex); } } return PDFPath; } /// <summary> /// 合成pdf文件 /// </summary> /// <param name="fileList">文件名list</param> /// <param name="outMergeFile">输出路径</param> public static void mergePDFFiles(List<string> fileList, string outMergeFile, out string ErrorMsg) { ErrorMsg = string.Empty; try { PdfReader reader; //Rectangle rec = new Rectangle(0, 0); Document document = new Document(); document.SetMargins(0f, 0f, 0f, 0f); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create)); document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage newPage; for (int i = 0; i < fileList.Count; i++) { reader = new PdfReader(fileList[i]); int iPageNum = reader.NumberOfPages; for (int j = 1; j <= iPageNum; j++) { document.NewPage(); newPage = writer.GetImportedPage(reader, j); cb.AddTemplate(newPage, 0, 0); } } document.Close(); } catch (Exception ex) { ErrorMsg = ex.Message; Log.WriteLog(ex); } } /// <summary> /// 获取文件服务器路径下载到本地 /// </summary> /// <param name="_url"></param> /// <param name="ErrorMsg"></param> /// <returns></returns> public static string Http_GetFile(string _url,string docPath, out string ErrorMsg) { string FileName = ""; ErrorMsg = ""; try { string url = _url; string path = docPath;//路径 if (Directory.Exists(path) == false)//判断文件夹是否存在 { Directory.CreateDirectory(path); } string fn = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + url.Split('.')[url.Split('.').Length - 1]; if (!File.Exists(path + FileName))//判断文件是否存在 { File.Create(path + FileName).Close(); } FileName = path + FileName; System.Net.WebClient web = new System.Net.WebClient(); web.Credentials = System.Net.CredentialCache.DefaultCredentials; web.DownloadFile(url, path + FileName); } catch (Exception ex) { Log.WriteLog(ex); ErrorMsg = ex.Message; } return FileName; }