• 应用C#实现图片转PDF与Excel转PDF


    应用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保存路径,自行修改!

  • 相关阅读:
    解决:transform-decorators-legacy 报错
    leetcode刷题笔记 232题 用栈实现队列
    leetcode刷题笔记 231题 2的幂
    leetcode刷题笔记 230题 二叉搜索树中第K小的元素
    leetcode刷题笔记 229题 求众数II
    leetcode刷题笔记 228题 汇总区间
    leetcode刷题笔记 227题 基本计算器II
    leetcode刷题笔记 225题 用队列实现栈
    leetcode刷题笔记 224题 基本计算器
    leetcode刷题笔记 223题 矩形面积
  • 原文地址:https://www.cnblogs.com/yxd000/p/15165289.html
Copyright © 2020-2023  润新知