Пример #1
0
        public void AddToActionList(string filename, string oldEncodingName, string oldLineEndingName, ConvertResult action)
        {
            string actionStr = "Convert";

            if (action != ConvertResult.OK)
            {
                actionStr = action.ToString();
            }
            resultListView.Items.Add(new ListViewItem(new string[] { filename, oldEncodingName, oldLineEndingName, actionStr }));
        }
Пример #2
0
        public void AddToResultList(ConvertCommand cmd, ConvertResult result)
        {
            string encodingName   = "Unchanged";
            string lineEndingName = "Unchanged";

            if (result == ConvertResult.OK)
            {
                encodingName   = Program.GetEncodingName(cmd.newEncoding);
                lineEndingName = Program.GetLineEndingName(cmd.newLineEnding);
            }
            resultListView.Items.Add(new ListViewItem(new string[] {
                cmd.path, encodingName, lineEndingName, result.ToString()
            }));
        }
Пример #3
0
        /// <summary>
        /// PDF,Wrod转换成图片
        /// </summary>
        /// <param name="fileUrl">源文件路径</param>
        /// <param name="imgFormat">图片格式</param>
        /// <param name="imgResolution">图片分辨率</param>
        /// <param name="imgQuality">图片质量</param>
        /// <returns>返回值格式:图片存放目录|页数</returns>
        public string ConvertToImage(string fileUrl, ImageFormat imgFormat, int imgResolution, int imgQuality)
        {
            DateTime      ConvertStartTime = DateTime.Now;
            ConvertResult convertResult    = ConvertResult.HasException;
            int           pageCount        = 0;
            string        docId            = string.Empty;
            string        tempFilePath     = string.Empty;
            string        tempFilePath2    = string.Empty;
            string        tempFilePath3    = string.Empty;
            string        tempImageDirPath = string.Empty;
            string        extension        = Path.GetExtension(fileUrl).ToLower();
            string        logFilePath      = Path.Combine(this._tempPath, "Log_" + ConvertStartTime.ToString("yyyyMMdd")) + ".txt";

            try
            {
                // validate parameter
                ValidateHelper.Begin().NotNullAndEmpty(fileUrl).NotNull(imgFormat)
                .InRange(imgResolution, 1, 512).InRange(imgQuality, 1, 100).CheckFileType(extension);

                docId         = Path.GetFileNameWithoutExtension(fileUrl);
                tempFilePath  = Path.Combine(this._tempPath, Guid.NewGuid().ToString()) + extension;
                tempFilePath2 = Path.Combine(this._tempPath, Guid.NewGuid().ToString()) + extension;

                // if _autoDelOldFile is ture , we need clearup old file by date and ignore exception
                if (this._autoDelOldFile)
                {
                    ClearupDirDelegate clearupDirDelegate = ClearupDir;
                    clearupDirDelegate(this._tempPath, this._saveDirMaxFileCount, this._deleteDateTime);
                }

                // three case for copy file to local temp directory , 1.from Http:// , 2.from other PC , 3.from local
                if (fileUrl.StartsWith("http://") || fileUrl.StartsWith("https://"))
                {
                    DownloadToFile(fileUrl, tempFilePath);
                }
                else if (fileUrl.StartsWith(@"\\"))
                {
                    using (WindowsIdentityScope wis = new WindowsIdentityScope(this._serverIP, this._serverLogOnName, this._serverLogOnPassword))
                    {
                        File.Copy(fileUrl, tempFilePath);
                    }
                }
                else
                {
                    File.Copy(fileUrl, tempFilePath);
                }

                // if the file is ziped ,we need decompress it
                if (ZipFile.IsFileZiped(tempFilePath))
                {
                    ZipFile.DecompressFirstFile(tempFilePath, tempFilePath2);
                }
                else
                {
                    tempFilePath2 = tempFilePath;
                }

                string tempImageDirName         = docId + "&" + File.GetLastWriteTime(tempFilePath2).ToFileTime().ToString();
                string tempRelativeImageDirPath = PathIncludeSlash(Path.Combine(this._relativeTempPath, tempImageDirName));
                tempImageDirPath = PathIncludeSlash(Path.Combine(this._tempPath, tempImageDirName));

                if (Directory.Exists(tempImageDirPath))
                {
                    DirectoryInfo tempImageDirInfo = new DirectoryInfo(tempImageDirPath);
                    if (tempImageDirInfo.GetFiles().Length > 0)
                    {
                        convertResult = ConvertResult.ReturnCashe;
                        return(tempRelativeImageDirPath + "|" + tempImageDirInfo.GetFiles().Length);
                    }
                }

                // convert file to image
                if (extension == ".docx" || extension == ".doc")
                {
                    string tempPdfFileName = Guid.NewGuid().ToString() + ".pdf";
                    if (this.ConvertWord2PDF(tempFilePath2, this._tempPath, tempPdfFileName))
                    {
                        tempFilePath3 = Path.Combine(this._tempPath, tempPdfFileName);
                        pageCount     = this.ConvertPDF2Image(tempFilePath3, tempImageDirPath, string.Empty, 0, 0, imgFormat, imgResolution, imgQuality);
                    }
                    else
                    {
                        pageCount = 0;
                    }
                }
                else if (extension == ".pdf")
                {
                    pageCount = this.ConvertPDF2Image(tempFilePath2, tempImageDirPath, string.Empty, 0, 0, imgFormat, imgResolution, imgQuality);
                }

                convertResult = ConvertResult.ConvertSuccess;
                return(tempRelativeImageDirPath + "|" + pageCount);
            }
            catch (Exception ex)
            {
                // we need delete the image file when happen exception, because it maybe not correct
                try
                {
                    if (!string.IsNullOrEmpty(tempImageDirPath) && Directory.Exists(tempImageDirPath))
                    {
                        int count = 0;
                        while (true)
                        {
                            try
                            {
                                Directory.Delete(tempImageDirPath, true);
                            }
                            catch (Exception)
                            {
                                Thread.Sleep(50);
                            }

                            if (!Directory.Exists(tempImageDirPath) || count == 10)
                            {
                                break;
                            }
                            count++;
                        }
                    }
                }
                catch (Exception) { }

                throw ex;
            }
            finally
            {
                // we need delete the temp file when convert finished , if has exception ignore it.
                try
                {
                    // write operate log
                    WriteLog(logFilePath, "公文号:" + docId + ",开始时间:" + ConvertStartTime.ToLongTimeString() + ",结束时间:" +
                             DateTime.Now.ToLongTimeString() + ",耗时:" + (DateTime.Now - ConvertStartTime).Milliseconds + "毫秒" + ",转换结果:" + convertResult.ToString());

                    // delete temp file
                    if (!string.IsNullOrEmpty(tempFilePath) && File.Exists(tempFilePath))
                    {
                        File.Delete(tempFilePath);
                    }
                    if (!string.IsNullOrEmpty(tempFilePath2) && File.Exists(tempFilePath2))
                    {
                        File.Delete(tempFilePath2);
                    }
                    if (!string.IsNullOrEmpty(tempFilePath3) && File.Exists(tempFilePath3))
                    {
                        File.Delete(tempFilePath3);
                    }
                }
                catch (Exception) { }
            }
        }