日期:2014-05-20  浏览次数:20436 次

[分享]文件转pdf
最近有个项目需要把文件(word、excel、txt、image等)转换为pdf文件,搜了一下资料,有很多实现方式,如通过调用虚拟打印机来转换,利用第三方组件(itextsharp、sharppdf等)实现。现两个都用到了,html转换为pdf文件时,我是先把html截取为图片然后转换为pdf,若各位有好的建议,可以一起探讨。
Pdf文件转Excel等文件各位有啥好的实现方法?
先贴上一些源码,下载地址:http://download.csdn.net/source/2646952
C# code

   /// <summary>
        /// excel转换为pdf
        /// </summary>
        /// <param name="file_inputname"></param>
        public void PrintSheetToPDF(string fileInputName)
        {
            try
            {
                GetPrinterName(ref PrinterName, ref obj_printer_settings, ref oType);

                PrintDocument prtdoc = new PrintDocument();

                currentPrinterName = prtdoc.PrinterSettings.PrinterName;   //获取默认的打印机名 

                //更改默认打印机
                SetDefaultPrinter(PrinterName.Trim());
                //通知更改
                SendNotifyMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows");

                if (fileInputName == "")
                {
                    return;
                }

                filePath = fileSavePath + "\\" + GetFileName(fileInputName) + ".pdf";

                oType.InvokeMember("Init", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, null);

                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "output", filePath });

                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "showsettings", "never" });

                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "ShowPDF", "no" });

                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "ShowProgress", "no" });

                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "ShowProgressFinished", "no" });

                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "SuppressErrors", "yes" });

                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "ConfirmOverwrite", "no" });

                oType.InvokeMember("WriteSettings", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { true });

                if (File.Exists(filePath) == true)
                {
                    File.Delete(filePath);
                }

                object objMissing = System.Type.Missing;

                object objValue = true;

                Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application();

                Microsoft.Office.Interop.Excel.Workbook objWorkbook = objExcel.Workbooks.Open(fileSavePath+"\\"+fileInputName, objMissing, objValue, objMissing, objMissing,objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);

                objExcel.Visible = false;

                for (int i = 1; i < objWorkbook.Worksheets.Count; i++)
                {
                    _Worksheet active_workbook = (_Worksheet)objExcel.Worksheets.get_Item(i);

                    active_workbook.PageSetup.Zoom = false;

                    active_workbook.PageSetup.FitToPagesWide = 1;

                    active_workbook.PageSetup.FitToPagesTall = 10;
                }

                objExcel.ActiveWorkbook.PrintOut(objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);

                for (int f_intStep = 1; f_intStep < 20000; f_intStep++)
                {
                    System.Threading.Thread.Sleep(100);

                    if (File.Exists(filePath) == true)
                    {
                        break;
                    }
                }

                objWorkbook.Close(false, fileInputName, false);

                objExcel.Quit();

                objWorkbook = null;

                objExcel = null;

                int k = 0;

                IntPtr t = new IntPtr(objExcel.Hwnd);

                GetWindowThreadProcessId(t, out   k);

                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);

                p.Kill();

                obj_printer_settings = null;

                //更改默认打印机
                SetDefaultPrinter(currentPrinterName.Trim());
                //通知更改
                SendNotifyMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows");
            }
            catch (Exception e)
            {
                obj_printer_settings = null;
                //throw e;
            }
        }