/// <summary> /// PPT文档转图片 /// </summary> /// <param name="pptFileName"></param> /// <param name="outPutFilePath"></param> public void PPTToImg(string pptFileName, string outPutFilePath) { Presentation ppt = new Presentation(pptFileName); Stream st = new MemoryStream(); ppt.Save(st, SaveFormat.Pdf); Aspose.Pdf.Document document = new Aspose.Pdf.Document(st); var device = new Aspose.Pdf.Devices.JpegDevice(); //默认质量为100,设置质量的好坏与处理速度不成正比,甚至是设置的质量越低反而花的时间越长,怀疑处理过程是先生成高质量的再压缩 device = new Aspose.Pdf.Devices.JpegDevice(100); //遍历每一页转为jpg for (var i = 1; i <= document.Pages.Count; i++) { string savepath = outPutFilePath + "\\" + string.Format("{0}.jpg", i); FileStream fs = new FileStream(savepath, FileMode.OpenOrCreate); try { device.Process(document.Pages[i], fs); fs.Close(); } catch (Exception ex) { fs.Close(); LogControl.LogInfo(lastCheckId.ToString() + "PPT文档转图片执行失败:" + ex.Message.ToString()); } } }
string savePath = System.Configuration.ConfigurationSettings.AppSettings.Get("savePath"); //图片保存的路劲 public void DoJob() { LogControl.LogInfo("执行服务任务:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); try { lastCheckId = GetLastId(); //最后一次执行的id var filePath = string.Empty; //源文档路劲 var saveFilePath = string.Empty; //文件保存的路劲 var searchPath = string.Empty; //文件搜索路劲 var sqlGetData = string.Format(@"select AttachmentId,SavePath,FileExt from Attachment where TargetType in(1,2,3) and FileExt in('doc','docx','pdf','ppt','pptx') and AttachmentId>{0} order by AttachmentId", lastCheckId); var data = SqlHelper.ExecuteDataSet(System.Data.CommandType.Text, sqlGetData, null); if (data != null && data.Tables.Count > 0) { var dataTable = data.Tables[0]; if (dataTable != null && dataTable.Rows.Count > 0) { for (var i = 0; i < dataTable.Rows.Count; i++) { sourcePath = System.Configuration.ConfigurationSettings.AppSettings.Get("sourcePath"); //源文件路劲 tempPath = System.Configuration.ConfigurationSettings.AppSettings.Get("tempPath"); //临时文件路劲 savePath = System.Configuration.ConfigurationSettings.AppSettings.Get("savePath"); //图片保存的路劲 lastCheckId = Convert.ToInt32(dataTable.Rows[i]["AttachmentId"]); var pathArr = dataTable.Rows[i]["SavePath"].ToString().Split('/'); searchPath = sourcePath + "\\" + pathArr[0] + "\\" + pathArr[1] + "\\OfficeFileToPDF"; savePath = sourcePath + "\\" + pathArr[0] + "\\" + pathArr[1] + "\\FileToImg"; var ext = dataTable.Rows[i]["FileExt"].ToString().ToUpper(); if (savePath.Contains("FileToImg") && Directory.Exists(savePath)) { Directory.Delete(savePath, true); } Directory.CreateDirectory(savePath); if (ext == "PDF" || ext == "PPT" || ext == "PPTX") { ext = "PDF"; GetPdfImgFromClipboard(searchPath, ext); //PdfToImg(searchPath, ext); } else { WordToImg(searchPath, ext); } ChangeLastId(lastCheckId.ToString()); } } } DoErroeData(); } catch (Exception ex) { LogControl.LogInfo("执行服务异常:" + ex.Message.ToString()); } LogControl.LogInfo("**********************************执行服务任结束务:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "******************************"); }
public void test() { try { GetCaptureImage(); } catch (Exception ex) { LogControl.LogInfo("发生异常:" + ex.Message.ToString()); } }
/// <summary> /// 执行作业 /// </summary> public void doJob(object sender, System.Timers.ElapsedEventArgs e) { if (isDoJob) { LogControl.LogInfo("****************************************服务开始执行任务" + DateTime.Now.ToString("yyyyMMddHHmmss") + "**************************************"); ServerDemoJob job = new ServerDemoJob(); isDoJob = false; job.DoJob(); //job.test(); isDoJob = true; } }
/// <summary> /// pdf转img /// </summary> public void PdfToImg(string searchPath, string fileExt) { if (Directory.Exists(searchPath)) { try { DirectoryInfo dir = new DirectoryInfo(searchPath); FileInfo[] dirInfo = dir.GetFiles(); string fileName = string.Empty; if (dirInfo != null && dirInfo.Length > 0) { foreach (var info in dirInfo) { if (info.Extension.ToUpper() == "." + fileExt.ToUpper()) { sourceFilePath = info.FullName; fileName = info.Name; CheckPath(tempPath); tempFilePath = tempPath + "\\" + info.Name; if (File.Exists(tempFilePath)) { File.Delete(tempFilePath); } File.Copy(sourceFilePath, tempFilePath); break; } } PdfLibNetToImg pdfHandle = new PdfLibNetToImg(); var result = pdfHandle.PdfToImg(lastCheckId, sourcePath, tempFilePath, savePath, ImageFormat.Png, 200, 30); //var result = pdfHandle.ConvertPDF2ImageMew(tempFilePath, savePath, fileName.Split('.')[0], ImageFormat.Png, FileToImgService.PdfHandle.Definition.Five, lastCheckId, sourcePath); if (!result) { RecodeErroeData(lastCheckId); } else { File.Delete(tempFilePath); } } } catch (Exception ex) { LogControl.LogInfo(lastCheckId.ToString() + "执行失败:" + ex.Message.ToString()); RecodeErroeData(lastCheckId); } } else { LogControl.LogInfo(lastCheckId.ToString() + "执行失败找不到路劲:" + searchPath); RecodeErroeData(lastCheckId); } }
/// <summary> /// word文档操作 /// </summary> /// <param name="searchPath"></param> /// <param name="fileExt"></param> /// <param name="fileSavePath"></param> public void WordToImg(string searchPath, string fileExt) { try { if (Directory.Exists(searchPath)) { DirectoryInfo dir = new DirectoryInfo(searchPath); FileInfo[] dirInfo = dir.GetFiles(); if (dirInfo != null && dirInfo.Length > 0) { foreach (var info in dirInfo) { if (info.Extension.ToUpper() == "." + fileExt.ToUpper()) { sourceFilePath = info.FullName; CheckPath(tempPath); tempFilePath = tempPath + "\\" + info.Name; if (File.Exists(tempFilePath)) { File.Delete(tempFilePath); } File.Copy(sourceFilePath, tempFilePath); break; } } Word2ImageConverter wordHandle = new Word2ImageConverter(); string message = string.Empty;; var result = wordHandle.ConvertToImageNew(sourcePath, tempFilePath, savePath, lastCheckId, out message); if (!result) { //执行失败不删除临时文件 可以单独处理 LogControl.LogInfo(lastCheckId.ToString() + "执行失败:" + message); RecodeErroeData(lastCheckId); } else { File.Delete(tempFilePath);//删除临时文件 } } } else { LogControl.LogInfo(lastCheckId.ToString() + "执行失败:找不到路劲:" + searchPath); RecodeErroeData(lastCheckId); } } catch (Exception ex) { LogControl.LogInfo(lastCheckId.ToString() + "执行失败:" + ex.Message.ToString()); RecodeErroeData(lastCheckId); } }
/// <summary> /// 执行报错记录的数据 /// </summary> public void DoErroeData() { LogControl.LogInfo("开始处理异常数据:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); var filePath = string.Empty; //源文档路劲 var saveFilePath = string.Empty; //文件保存的路劲 var searchPath = string.Empty; //文件搜索路劲 var sql = @"select a.Id,a.AttachmentId,b.SavePath,b.FileExt from PreviewFileErrorTable as a left join Attachment as b on b.AttachmentId=a.attachmentid"; var data = SqlHelper.ExecuteDataSet(System.Data.CommandType.Text, sql, null); if (data != null && data.Tables.Count > 0) { var dataTable = data.Tables[0]; } try { if (data != null && data.Tables.Count > 0) { var dataTable = data.Tables[0]; if (dataTable != null && dataTable.Rows.Count > 0) { for (var i = 0; i < dataTable.Rows.Count; i++) { sourcePath = System.Configuration.ConfigurationSettings.AppSettings.Get("sourcePath"); //源文件路劲 tempPath = System.Configuration.ConfigurationSettings.AppSettings.Get("tempPath"); //临时文件路劲 savePath = System.Configuration.ConfigurationSettings.AppSettings.Get("savePath"); //图片保存的路劲 lastCheckId = Convert.ToInt32(dataTable.Rows[i]["AttachmentId"]); var pathArr = dataTable.Rows[i]["SavePath"].ToString().Split('/'); searchPath = sourcePath + "\\" + pathArr[0] + "\\" + pathArr[1] + "\\OfficeFileToPDF"; savePath = sourcePath + "\\" + pathArr[0] + "\\" + pathArr[1] + "\\FileToImg"; var ext = dataTable.Rows[i]["FileExt"].ToString().ToUpper(); if (ext == "PDF") { PdfToImg(searchPath, ext); } else { WordToImg(searchPath, ext); } DeleteErrorData(Convert.ToInt32(dataTable.Rows[i]["Id"])); } } } } catch (Exception ex) { LogControl.LogInfo("处理异常数据报错:" + ex.Message.ToString()); } LogControl.LogInfo("处理异常数据结束:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); }
/// <summary> /// 启动服务执行方法 /// </summary> /// <param name="args"></param> protected override void OnStart(string[] args) { try { LogControl.LogInfo("服务开启" + DateTime.Now.ToString("yyyyMMddHHmmss")); //开启 objPollTimer.Start(); System.Diagnostics.EventLog.WriteEntry(ServiceConfig.ServiceName, ServiceConfig.ServiceName + " 在" + DateTime.Now.ToString() + " 时启动"); } catch (Exception e) { System.Diagnostics.EventLog.WriteEntry(ServiceConfig.ServiceName, ServiceConfig.ServiceName + " 在" + DateTime.Now.ToString() + " 时启动失败,详细信息:" + e.Message, EventLogEntryType.Error); OnStop(); } }
private static void GetCaptureImage() { IDataObject iData = Clipboard.GetDataObject(); Image img = null; if (iData != null) { if (iData.GetDataPresent(DataFormats.Bitmap)) { img = (Image)iData.GetData(DataFormats.Bitmap); } else if (iData.GetDataPresent(DataFormats.Dib)) { img = (Image)iData.GetData(DataFormats.Dib); } } LogControl.LogInfo(img == null ? "Null" : "img"); }
private void GetImgFromClipboard() { try { string pdfInputPath = tempFilePath; string imageOutputPath = savePath; //string pdfInputPath = @"D:\pdfImgs\test.pdf"; //string imageOutputPath = @"D:\pdfImgs\"; int startPageNum = 1; ImageFormat imageFormat = ImageFormat.Jpeg; int resolution = 1; Acrobat.CAcroPDDoc pdfDoc = null; Acrobat.CAcroPDPage pdfPage = null; Acrobat.CAcroRect pdfRect = null; Acrobat.CAcroPoint pdfPoint = null; // Create the document (Can only create the AcroExch.PDDoc object using late-binding) // Note using VisualBasic helper functions, have to add reference to DLL pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", ""); // validate parameter if (!pdfDoc.Open(pdfInputPath)) { throw new FileNotFoundException(); } if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } int endPageNum = pdfDoc.GetNumPages(); if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; } if (resolution <= 0) { resolution = 1; } // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1); pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize(); pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", ""); int imgWidth = (int)((double)pdfPoint.x * resolution); int imgHeight = (int)((double)pdfPoint.y * resolution); pdfRect.Left = 0; pdfRect.right = (short)imgWidth; pdfRect.Top = 0; pdfRect.bottom = (short)imgHeight; // Render to clipboard, scaled by 100 percent (ie. original size) // Even though we want a smaller image, better for us to scale in .NET // than Acrobat as it would greek out small text bool b = pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution)); IDataObject clipboardData = Clipboard.GetDataObject(); if (clipboardData.GetDataPresent(DataFormats.Bitmap)) { Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap); string imtName = i.ToString() + ".jpg"; pdfBitmap.Save(imageOutputPath + "\\" + imtName, imageFormat); pdfBitmap.Dispose(); } } if (endPageNum > 0) { StringBuilder sb = new StringBuilder(); sb.Append(string.Format("delete from PreviewFile where AttachmentId={0};", lastCheckId)); for (int num = 1; num <= endPageNum; num++) { string filename = imageOutputPath + "\\" + num.ToString() + ".jpg"; sb.Append(string.Format("insert into PreviewFile(AttachmentId,SavePath,CreateTime)values({0},'{1}','{2}');", lastCheckId, filename.Replace(sourcePath, "").Replace("\\", "/"), DateTime.Now)); Thread.Sleep(1000); } var result = SqlHelper.ExecteNonQueryText(sb.ToString(), null); if (result > 0) { } else { RecodeErroeData(lastCheckId); } } pdfDoc.Close(); Marshal.ReleaseComObject(pdfPage); Marshal.ReleaseComObject(pdfRect); Marshal.ReleaseComObject(pdfDoc); Marshal.ReleaseComObject(pdfPoint); } catch (Exception ex) { LogControl.LogInfo("从剪切板获取PDF图片异常lastCheckId:" + lastCheckId + " " + ex.ToString()); RecodeErroeData(lastCheckId); } }
public void GetPdfImgFromClipboard(string searchPath, string fileExt) { if (Directory.Exists(searchPath)) { try { DirectoryInfo dir = new DirectoryInfo(searchPath); FileInfo[] dirInfo = dir.GetFiles(); string fileName = string.Empty; if (dirInfo != null && dirInfo.Length > 0) { foreach (var info in dirInfo) { #region 制pdf文件到临时目录 if (info.Extension.ToUpper() == "." + fileExt.ToUpper()) { sourceFilePath = info.FullName; fileName = info.Name; CheckPath(tempPath); tempFilePath = tempPath + "\\" + info.Name; if (File.Exists(tempFilePath)) { File.Delete(tempFilePath); } File.Copy(sourceFilePath, tempFilePath); break; } #endregion } Thread cbThread = new Thread(new ThreadStart(GetImgFromClipboard)); cbThread.TrySetApartmentState(ApartmentState.STA); cbThread.Start(); cbThread.Join(); File.Delete(tempFilePath); //PdfLibNetToImg pdfHandle = new PdfLibNetToImg(); //var result = pdfHandle.PdfToImg(lastCheckId, sourcePath, tempFilePath, savePath, ImageFormat.Png, 200, 30); //var result = pdfHandle.ConvertPDF2ImageMew(tempFilePath, savePath, fileName.Split('.')[0], ImageFormat.Png, FileToImgService.PdfHandle.Definition.Five, lastCheckId, sourcePath); //if (!result) //{ // RecodeErroeData(lastCheckId); //} //else //{ // File.Delete(tempFilePath); //} } } catch (Exception ex) { LogControl.LogInfo(lastCheckId.ToString() + "执行失败:" + ex.Message.ToString()); RecodeErroeData(lastCheckId); } } else { LogControl.LogInfo(lastCheckId.ToString() + "执行失败找不到路劲:" + searchPath); RecodeErroeData(lastCheckId); } }