/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> private void ConvertPDF2ImageO2S(string pdfInputPath) { // Is NULL if (pdfInputPath == null) { m_nConvertStatus = EErrorType.PTJ_FILENAME_EMPTY; } PDFFile pdfFile = PDFFile.Open(pdfInputPath); try { m_nTotalPage = pdfFile.PageCount; PrintLog("ConvertStatus:" + (int)ConvertStatus.START + " " + 0 + " " + m_nTotalPage); // start to convert each page for (int i = 0; i < m_nTotalPage; i++) { PrintLog("ConvertStatus:" + (int)ConvertStatus.ING + " " + (i + 1).ToString() + " " + m_nTotalPage); string strJPGName = m_strOutDir + "\\" + Path.GetFileNameWithoutExtension(pdfInputPath) + "_" + m_nTotalPage + "_" + (i + 1).ToString() + ".jpg"; Bitmap pageImage = pdfFile.GetPageImage(i, 56 * (int)m_dQuality); pageImage.Save(strJPGName, ImageFormat.Jpeg); pageImage.Dispose(); } } catch (Exception ex) { PrintLog(ex.Message.ToString()); m_nConvertStatus = EErrorType.PTJ_EXCEPTION_FAILED; } finally { pdfFile.Dispose(); } }
/// <summary> /// Sends an error message /// </summary> /// <typeparam name="T">Message type</typeparam> /// <param name="errorType">Error type</param> /// <param name="message">Error message</param> /// <param name="isFatal">Is error fatal</param> public void SendErrorMessage <T>(EErrorType errorType, string message, bool isFatal) where T : IBaseMessageData { SendMessage(new ErrorMessageData(errorType, Naming.GetMessageTypeNameFromMessageDataType <T>(), message)); if (isFatal) { Peer.Disconnect(EDisconnectionReason.Error); } }
public void AddError(EErrorCodes code, EErrorType type, string message) { Error err = new Error() { Code = code, Message = message, Type = type }; AddError(err); }
/// <summary> /// Constructs an error message /// </summary> /// <param name="errorType">Error type</param> /// <param name="errorMessageType">Error message</param> /// <param name="message">Error message</param> public ErrorMessageData(EErrorType errorType, string errorMessageType, string message) : base(Naming.GetMessageTypeNameFromMessageDataType<ErrorMessageData>()) { if (errorType == EErrorType.Invalid) { throw new ArgumentException("Error type can't be invalid.", nameof(errorType)); } if (string.IsNullOrWhiteSpace(errorMessageType)) { throw new ArgumentNullException(nameof(errorMessageType)); } ErrorType = errorType; ErrorMessageType = errorMessageType; Message = message ?? throw new ArgumentNullException(nameof(message)); }
public void Log(EErrorType type, string msg) { string message = string.Format("[{0}] [Trd:{1}]\t[{2}]\t{3}", DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss"), Thread.CurrentThread.ManagedThreadId, type.ToString(), msg); lock (_lock) { _msgBuffer.Add(message); } FlushBuffer(); }
/// <summary> /// 转换PPT到PDF /// </summary> /// <param name="wpsFilename">原始文件</param> /// <param name="pdfFilename">转换后pdf文件</param> private void ConvertPptToPdf() { PrintLog("Start Convert PPT Office file To PDF"); PowerPoint.Application pps = new PowerPoint.Application(); Presentation curPPTFile = null; try { // Is NULL if (m_strOfficeName == null || m_strPDFName == null) { m_nConvertStatus = EErrorType.OTP_FILENAME_EMPTY; } else { // To PDF //忽略警告提示 此处无法设置,原因不清楚! //pps.Visible = PowerPoint.MsoTriState.msoFalse; pps.DisplayAlerts = PowerPoint.PpAlertLevel.ppAlertsNone; curPPTFile = pps.Presentations.Open(m_strOfficeName, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); m_nTotalPage = curPPTFile.Slides.Count; PrintLog("ConvertStatus:" + (int)ConvertStatus.START + " " + 0 + " " + m_nTotalPage); for (int i = 1; i <= m_nTotalPage; i++) { Slide slide = curPPTFile.Slides[i]; //int nWidth = (int)(slide.Background.Width * (int)m_dQuality); //int nHeight = (int)(slide.Background.Height * (int)m_dQuality); string strJpgName = m_strOutDir + "\\" + Path.GetFileNameWithoutExtension(m_strOfficeName) + "_" + m_nTotalPage + "_" + i + ".jpg"; slide.Export(strJpgName, "jpeg" /*, nWidth, nHeight*/); PrintLog("ConvertStatus:" + (int)ConvertStatus.ING + " " + (i).ToString() + " " + m_nTotalPage); } QuitWPP(ref pps, ref curPPTFile); // To JPG //ConvertPDF2ImageO2S(m_strPDFName); } } catch (Exception ex) { PrintLog(ex.Message.ToString()); m_nConvertStatus = EErrorType.OTP_EXCEPTION_FAILED; } finally { QuitWPP(ref pps, ref curPPTFile); } }
/// <summary> /// 转换DOC到PDF /// </summary> /// <param name="wpsFilename">原始文件</param> /// <param name="pdfFilename">转换后pdf文件</param> private void ConvertDocToPdf() { PrintLog("Start Convert Word Office file To PDF"); Word.Application wps = new Word.Application(); Document doc = null; try { // Is NULL if (m_strOfficeName == null || m_strPDFName == null) { m_nConvertStatus = EErrorType.OTP_FILENAME_EMPTY; } else { // To PDF //忽略警告提示 wps.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; wps.Visible = false; doc = wps.Documents.Open(m_strOfficeName, Type.Missing, true); doc.ExportAsFixedFormat(m_strPDFName, WdExportFormat.wdExportFormatPDF /*, false,Word.WdExportOptimizeFor.wdExportOptimizeForPrint, Word.WdExportRange.wdExportAllDocument, 0, 0*/); QuitWPS(ref wps, ref doc); // To JPG ConvertPDF2ImageO2S(m_strPDFName); } } catch (Exception ex) { PrintLog(ex.Message.ToString()); m_nConvertStatus = EErrorType.OTP_EXCEPTION_FAILED; } finally { QuitWPS(ref wps, ref doc); } }
/// <summary> /// 转换EXCEL到PDF /// </summary> /// <param name="wpsFilename">原始文件</param> /// <param name="pdfFilename">转换后pdf文件</param> private void ConvertExcelToPdf() { PrintLog("Start Convert Excel Office file To PDF"); Excel.Application eps = new Excel.Application(); Workbook wkCur = null; try { // Is NULL if (m_strOfficeName == null || m_strPDFName == null) { m_nConvertStatus = EErrorType.OTP_FILENAME_EMPTY; } else { // To PDF //忽略警告提示 eps.DisplayAlerts = false; eps.Visible = false; wkCur = eps.Workbooks.Open(m_strOfficeName, Type.Missing, true); wkCur.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, m_strPDFName); QuitExcel(ref eps, ref wkCur); // To JPG ConvertPDF2ImageO2S(m_strPDFName); } } catch (Exception ex) { PrintLog(ex.Message.ToString()); m_nConvertStatus = EErrorType.OTP_EXCEPTION_FAILED; } finally { QuitExcel(ref eps, ref wkCur); } }
/// <summary> /// Sends an error message /// </summary> /// <typeparam name="T">Message type</typeparam> /// <param name="errorType">Error type</param> /// <param name="message">Error message</param> public void SendErrorMessage <T>(EErrorType errorType, string message) where T : IBaseMessageData => SendErrorMessage <T>(errorType, message, false);
public void Log(EErrorType type, string msg) { }
public void OfficeToJPGEx(string strSrcFile, string strOutDir, Definition dQuality, bool isDelePDF) { try { // 判断文件是否存在,输出文件夹是否存在 if (!System.IO.File.Exists(strSrcFile)) { m_nConvertStatus = EErrorType.WPS_FILE_NOTEXISTS; return; } if (!System.IO.Directory.Exists(strOutDir)) { Directory.CreateDirectory(strOutDir); } // Set Data Member m_strOfficeName = strSrcFile; m_strOutDir = strOutDir.TrimEnd('\\'); m_dQuality = dQuality; m_bDeletePDF = isDelePDF; m_nConvertStatus = EErrorType.WPS_NO_ERROR; // Generate PDF Name m_strPDFName = m_strOutDir + "\\" + Path.GetFileNameWithoutExtension(strSrcFile) + ".pdf"; // Start Convert to JPG string pdfFilename = Path.GetExtension(strSrcFile); if (pdfFilename == ".doc" || pdfFilename == ".docx") { ConvertDocToPdf(); } else if (pdfFilename == ".xls" || pdfFilename == ".xlsx") { ConvertExcelToPdf(); } else if (pdfFilename == ".ppt" || pdfFilename == ".pptx") { ConvertPptToPdf(); } else if (pdfFilename == ".pdf") { GetCopyMutex(); m_bDeletePDF = false; ConvertPDF2Image(m_strOfficeName); } else { m_nConvertStatus = EErrorType.WPS_NOTSUPPORT_TYPE; } if (m_bDeletePDF) { // 删除PDF文件 File.Delete(m_strPDFName); } } catch (Exception ex) { PrintLog(ex.Message.ToString()); m_nConvertStatus = EErrorType.OTP_EXCEPTION_FAILED; } finally { PrintLog("ConvertStatus:" + (int)ConvertStatus.END + " " + (int)m_nConvertStatus + " " + m_nTotalPage); PrintResultError(); } }
/// <summary> /// 将PDF文档转换为图片的方法,你可以像这样调用该方法:ConvertPDF2ImageO2S("F:\\A.pdf", "F:\\", "A", 0, 0, null, 0); /// 因为大多数的参数都有默认值,startPageNum默认值为1,endPageNum默认值为总页数, /// imageFormat默认值为ImageFormat.Jpeg,resolution默认值为1 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">图片的名字,不需要带扩展名</param> /// <param name="startPageNum">从PDF文档的第几页开始转换,默认值为1</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换,默认值为PDF总页数</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="resolution">设置图片的分辨率,数字越大越清晰,默认值为1</param> public void ConvertPDF2Image(string pdfInputPath) { // Is NULL if (pdfInputPath == null) { m_nConvertStatus = EErrorType.PTJ_FILENAME_EMPTY; } if (!System.IO.File.Exists(pdfInputPath)) { m_nConvertStatus = EErrorType.WPS_FILE_NOTEXISTS; } Acrobat.CAcroPDDoc pdfDoc = null; Acrobat.CAcroPDPage pdfPage = null; Acrobat.CAcroRect pdfRect = null; Acrobat.CAcroPoint pdfPoint = null; ewhCopyJpg = new EventWaitHandle(false, 0, "WPSToJPGPDFToJPGUsingArobat"); try { // 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(m_strOutDir)) { Directory.CreateDirectory(m_strOutDir); } m_nTotalPage = pdfDoc.GetNumPages(); PrintLog("ConvertStatus:" + (int)ConvertStatus.START + " " + 0 + " " + m_nTotalPage); // start to convert each page for (int i = 1; i <= m_nTotalPage; i++) { PrintLog("ConvertStatus:" + (int)ConvertStatus.ING + " " + i + " " + m_nTotalPage); pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1); pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize(); pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", ""); // 如果当前分辨率少于1280*720,强制放大 int imgWidth = (int)((double)pdfPoint.x * (int)m_dQuality); int imgHeight = (int)((double)pdfPoint.y * (int)m_dQuality); pdfRect.Left = 0; pdfRect.right = (short)imgWidth; pdfRect.Top = 0; pdfRect.bottom = (short)imgHeight; // 临界区 ewhCopyJpg.WaitOne(); // 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 pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * (int)m_dQuality)); IDataObject clipboardData = Clipboard.GetDataObject(); if (clipboardData.GetDataPresent(DataFormats.Bitmap)) { string strJPGName = m_strOutDir + "\\" + Path.GetFileNameWithoutExtension(pdfInputPath) + "_" + pdfDoc.GetNumPages() + "_" + (i).ToString() + ".jpg"; Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap); pdfBitmap.Save(strJPGName, ImageFormat.Jpeg); pdfBitmap.Dispose(); } Clipboard.Clear(); ewhCopyJpg.Set(); } QuitAcrobat(ref pdfDoc, ref pdfPage, ref pdfRect, ref pdfPoint); } catch (Exception ex) { PrintLog(ex.Message.ToString()); m_nConvertStatus = EErrorType.PTJ_EXCEPTION_FAILED; } finally { QuitAcrobat(ref pdfDoc, ref pdfPage, ref pdfRect, ref pdfPoint); } }
/*----------Functions----------*/ //PUBLIC /// <summary> /// Initialise this drawer with the default values for the required entry /// </summary> /// <param name="type">The type of error that this drawer is responsible for displaying</param> /// <param name="additional">An additional message that can be displayed to support the display error</param> public ErrorParameterDrawer(EErrorType type, string additional = "") { errorType = type; additionalText = additional; }
public Error(EErrorType type, string message) { this.Type = type; this.Message = message; }
public ServiceException(EErrorType errorType, string msg) : base(msg) { ErrorType = errorType; }