应用c#进行转换的时候,需要iTextSharp函数包,百度网盘链接,点击获取!提取码:vddj
自定义图片转PDF函数:
1 public void PicToPdf(string path1, string path2,string path3) 2 { 3 iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25); 4 try 5 { 6 List<string> files = new List<string>(); 7 foreach (string zz in Directory.GetFileSystemEntries(path1)) 8 { 9 files.Add(zz); 10 } 11 //this.Close();path3 12 string tt1 = path2 + path3; 13 iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(tt1, FileMode.Create, FileAccess.ReadWrite)); 14 document.Open(); 15 iTextSharp.text.Image image; 16 for (int i = 0; i < files.Count; i++) 17 { 18 if (String.IsNullOrEmpty(files[i])) break; 19 image = iTextSharp.text.Image.GetInstance(files[i]); 20 if (image.Height > iTextSharp.text.PageSize.A4.Height - 25) 21 { 22 image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25); 23 } 24 else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25) 25 { 26 image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25); 27 } 28 image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE; 29 document.NewPage(); 30 document.Add(image); 31 } 32 } 33 catch (Exception ex) 34 { 35 MessageBox.Show("转换失败,原因:" + ex.Message); 36 return; 37 } 38 document.Close(); 39 }
其中path1为图片路径path2、path3为自定义的PDF保存路径,自行修改!
自定义EXCE转PDF函数:
1 public void ExcelToPdf(string path1, string path2) 2 { 3 Microsoft.Office.Interop.Excel.Application lobjExcelApp = null; 4 Microsoft.Office.Interop.Excel.Workbooks lobjExcelWorkBooks = null; 5 Microsoft.Office.Interop.Excel.Workbook lobjExcelWorkBook = null; 6 string lstrTemp = string.Empty; 7 object lobjMissing = System.Reflection.Missing.Value; 8 lobjExcelApp = new Microsoft.Office.Interop.Excel.Application(); 9 lobjExcelApp.Visible = false; 10 lobjExcelWorkBooks = lobjExcelApp.Workbooks; 11 lobjExcelWorkBook = lobjExcelWorkBooks.Open(path1, lobjMissing, false, lobjMissing, lobjMissing, lobjMissing, true, lobjMissing, lobjMissing, lobjMissing, lobjMissing, lobjMissing, false, lobjMissing, lobjMissing); 12 //Microsoft.Office.Interop.Excel 12.0.0.0函数 13 lstrTemp = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".xls" + (lobjExcelWorkBook.HasVBProject ? 'm' : 'x'); 14 //lstrTemp = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".xls"; 15 lobjExcelWorkBook.SaveAs(lstrTemp, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel4Workbook, Type.Missing, Type.Missing, Type.Missing, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing); lobjExcelWorkBook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, path2, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard, Type.Missing, false, Type.Missing, Type.Missing, false, Type.Missing); 16 lobjExcelWorkBooks.Close(); 17 lobjExcelApp.Quit(); 18 }
其中path1为EXCEL路径path2为自定义的PDF保存路径,自行修改!