Exemple #1
0
        /// <summary>
        /// 文件转换成PDF
        /// </summary>
        /// <param name="fnOri">源文件</param>
        /// <param name="fnPDF">生成的PDF文件</param>
        /// <returns></returns>
        private bool FileConverToPDF(string fnOri, string fnPDF)
        {
            //.JPG,.JPEG,.PNG,.GIF,.BMP,.DOC,.DOCX,.XLS,.XLSX,.TIF,.TIFF,.PDF
            bool   result_flg = false;
            string sExt       = System.IO.Path.GetExtension(fnOri);

            if (sExt.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase) ||
                sExt.Equals(".jpeg", StringComparison.InvariantCultureIgnoreCase) ||
                sExt.Equals(".tif", StringComparison.InvariantCultureIgnoreCase) ||
                sExt.Equals(".tiff", StringComparison.InvariantCultureIgnoreCase) ||
                sExt.Equals(".bmp", StringComparison.InvariantCultureIgnoreCase) ||
                sExt.Equals(".png", StringComparison.InvariantCultureIgnoreCase) ||
                sExt.Equals(".gif", StringComparison.InvariantCultureIgnoreCase) ||
                sExt.Equals(".doc", StringComparison.InvariantCultureIgnoreCase) ||
                sExt.Equals(".docx", StringComparison.InvariantCultureIgnoreCase) ||
                sExt.Equals(".xls", StringComparison.InvariantCultureIgnoreCase) ||
                sExt.Equals(".xlsx", StringComparison.InvariantCultureIgnoreCase))
            {
                MyCommon.DeleteAndCreateEmptyDirectory(tempFolder, false);
                MyCommon.DeleteAndCreateEmptyDirectory(tempFolder, true);
                try
                {
                    using (TransAndSignFile.UserControl1 s = new TransAndSignFile.UserControl1())
                    {
                        if (s.TransFileToPDF(fnOri, tempFolder, "error"))
                        {
                            string file_pdf = Path.Combine(tempFolder, Path.GetFileNameWithoutExtension(fnOri) + ".pdf");
                            if (File.Exists(file_pdf))
                            {
                                File.Copy(file_pdf, fnPDF, true);
                            }
                            result_flg = true;
                        }
                        else
                        {
                            MyCommon.WriteLog("ConvertCell2PDF转换PDF错误:" + s.GetLastError());
                        }
                    }
                }
                catch (Exception ex)
                {
                    MyCommon.WriteLog("ConvertCell2PDF转换PDF错误:" + fnOri + " " + ex.Message);
                }
            }
            return(result_flg);
        }
Exemple #2
0
        public List <ConvertPdfFile> GeneratePDFList(ListView.SelectedListViewItemCollection ListItems, frm2PDFProgressMsg dlg, string strNameBK = "", TreeNodeEx targeNode = null)
        {
            List <ConvertPdfFile> cplist = new List <ConvertPdfFile>();
            int i1 = 0;

            TransAndSignFile.UserControl1 s = null;
            s = new TransAndSignFile.UserControl1();

            foreach (ListViewItem item in ListItems)
            {
                string eFileID = Guid.NewGuid().ToString();
                string fnOri   = item.Name;
                string sExt    = System.IO.Path.GetExtension(fnOri);
                string fnPDF   = Globals.ProjectPath + "PDF\\" + eFileID + ".pdf";
                if ((sExt.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase) ||
                     sExt.Equals(".jpeg", StringComparison.InvariantCultureIgnoreCase) ||
                     sExt.Equals(".tif", StringComparison.InvariantCultureIgnoreCase) ||
                     sExt.Equals(".tiff", StringComparison.InvariantCultureIgnoreCase) ||
                     sExt.Equals(".bmp", StringComparison.InvariantCultureIgnoreCase) ||
                     sExt.Equals(".png", StringComparison.InvariantCultureIgnoreCase) ||
                     sExt.Equals(".gif", StringComparison.InvariantCultureIgnoreCase) ||
                     sExt.Equals(".doc", StringComparison.InvariantCultureIgnoreCase) ||
                     sExt.Equals(".docx", StringComparison.InvariantCultureIgnoreCase) ||
                     sExt.Equals(".xls", StringComparison.InvariantCultureIgnoreCase) ||
                     sExt.Equals(".xlsx", StringComparison.InvariantCultureIgnoreCase)) && fnOri != "")
                {
                    MyCommon.DeleteAndCreateEmptyDirectory(tempFolder, false);
                    MyCommon.DeleteAndCreateEmptyDirectory(tempFolder, true);
                    try
                    {
                        i1++;
                        if (dlg.label2.Text != "")
                        {
                            dlg.label2.Text        = "正在导入:" + i1.ToString() + "/" + ListItems.Count.ToString();
                            dlg.progressBar1.Value = i1;
                            Application.DoEvents();
                        }
                        targeNode.Text = "导入中(" + i1 + "/" + ListItems.Count + ") " + strNameBK;
                        if (s.TransFileToPDF(fnOri, tempFolder, "error"))
                        {
                            string file_pdf = Path.Combine(tempFolder, Path.GetFileNameWithoutExtension(fnOri) + ".pdf");
                            if (File.Exists(file_pdf))
                            {
                                File.Copy(file_pdf, fnPDF);
                            }
                            ConvertPdfFile cp = new ConvertPdfFile();
                            cp.SourceFilePath = fnOri;
                            cp.PDFFilePath    = fnPDF;
                            cplist.Add(cp);
                        }
                        else
                        {
                            MyCommon.WriteLog("转换PDF错误:" + s.GetLastError());
                        }
                    }
                    catch (Exception ex)
                    {
                        MyCommon.WriteLog("转换PDF错误:" + fnOri + " " + ex.Message);
                    }
                }
            }
            if (dlg != null)
            {
                dlg.Close();
            }
            if (s != null)
            {
                s = null;
            }
            return(cplist);
        }