123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- using System;
- using System.Collections.Generic;
- using ComPDFKit_Conversion.Converter;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using ComDocumentAIKit;
- using ComPDFKit_Conversion.Options;
- using Microsoft.Office.Interop.Word;
- using PDF_Master.Helper;
- using PDF_Master.CustomControl;
- using System.Net.Mime;
- using System.Threading;
- using System.Windows.Threading;
- using ImTools;
- using PDF_Master.Properties;
- namespace PDF_Master.Model.Dialog.ConverterDialogs
- {
- public class ConvertOptions
- {
- public COCRLanguage OCRLanguage = COCRLanguage.COCRLanguageEnglish;
- public bool IsAllowOCR = false;
- public bool IsContainOCRBgImage = true;
- public bool IsContainAnnotations = true;
- public bool IsContainImages = true;
- public ContentOptions ContentOpts = ContentOptions.AllContent;
- public WorksheetOptions WorksheetOpts = WorksheetOptions.ForEachPage;
- public PageAndNavigationPaneOptions PageAndNavigationPaneOpts = PageAndNavigationPaneOptions.SinglePageNavigationByBookmarks;
- public ImgType ImageType = ImgType.PNG;
- public uint ImageDpi = 150;
- public bool IsMergeCsv = false;
- public LayoutOptions LayoutOpts = LayoutOptions.RetainPageLayout;
- }
- public class ConverterDialogsModel
- {
- /// <summary>
- /// 文档地址;
- /// </summary>
- public string InputPath = "";
- /// <summary>
- /// 输出地址;
- /// </summary>
- public string OutputPath = "";
- /// <summary>
- /// 进度;
- /// </summary>
- public OnProgress GetProgress = null;
- /// <summary>
- /// 页码 页码范围;
- /// </summary>
- public List<int> PageIndexLists = new List<int> { };
- /// <summary>
- /// 页面信息
- /// </summary>
- public string PageRange = "";
- /// <summary>
- /// 文档密码;
- /// </summary>
- public string Pawssword = "";
- /// <summary>
- /// 文档密码;
- /// </summary>
- public bool IsCustomFileName = true;
- /// <summary>
- /// 文档密码;
- /// </summary>
- public string ConvertType = "";
- /// <summary>
- /// OCR,与部分功能;
- /// </summary>
- public ConvertOptions Options = new ConvertOptions();
- /// <summary>
- /// 取消进度条
- /// </summary>
- private async void TaskMethod()
- {
- await System.Threading.Tasks.Task.Run(() =>
- {
- //当进度条消失时,结束进程(由于取消时OnOpened没有完全结束,等待OnOpened结束时就会结束)
- for (int i = 1; App.mainWindowViewModel.IsProcessVisible != System.Windows.Visibility.Collapsed; i++)
- {
- try
- {
- Thread.Sleep(500 + i * 500);
- if (i < App.mainWindowViewModel.MaxValue)
- App.mainWindowViewModel.Value = i;
- }
- catch { }
- }
- });
- }
- private void ConverterClear()
- {
- //取消转档
- ConverterHelper.Clear(ConvertType);
- //取消转档的进度条,防止转档还没结束,导致取消后程序崩溃,添加一个取消进度条
- App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Collapsed;
- App.mainWindowViewModel.ProgressTitle = "Canceling";
- //判断取消进度条消失逻辑
- TaskMethod();
- //App.mainWindowViewModel.ProgressTitle = "";
- //App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
- }
- private void SetProgress(int pageIndex)
- {
- App.mainWindowViewModel.Value = pageIndex;
- if (App.mainWindowViewModel.Value == App.mainWindowViewModel.MaxValue) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
- }
- /// <summary>
- /// 转档限制转页面前10页
- /// </summary>
- /// <param name="converterDialogsModel"></param>
- private void limitationConvert(ConverterDialogsModel converterDialogsModel)
- {
- if (Settings.Default.UserDate.subscribestatus!=1)
- {
- //当前文档前10页
- //int limitationindex = 0;
- ////此链表从0开始,查询大于或等于10的索引值
- //for (int i = 0; converterDialogsModel.PageIndexLists.Count > i; i++)
- //{
- // if (converterDialogsModel.PageIndexLists[i] >= 10)
- // {
- // limitationindex = i;
- // break;
- // }
- //}
- ////安全检查判断返回的大于或等于10的索引值是否正常
- //if (converterDialogsModel.PageIndexLists[limitationindex] >= 10)
- //{
- // //删除大于10或等于10之后的Item
- // converterDialogsModel.PageIndexLists.RemoveRange(limitationindex, converterDialogsModel.PageIndexLists.Count - limitationindex);
- //}
- //链表10页
- if (converterDialogsModel.PageIndexLists.Count >= 11)
- {
- converterDialogsModel.PageIndexLists.RemoveRange(10, converterDialogsModel.PageIndexLists.Count-10);
- }
- }
- }
- public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
- {
- if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel || e.Result == Prism.Services.Dialogs.ButtonResult.None) { return; }
- bool result = false;
- string convertType = "";
- OnProgress GetProgress = null;
- GetProgress += SetProgress;
- App.mainWindowViewModel.ProcessCloseAction = ConverterClear;
- convertType = e.Parameters.GetValue<string>(ParameterNames.ConverterType).ToString();
- ConvertType = convertType;
- App.mainWindowViewModel.ProgressTitle = "Converting";
- App.mainWindowViewModel.Value = 0;
- App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
- switch (convertType)
- {
- case "CSV":
- ConverterCSVDialogModel ConverterCSVModel = e.Parameters.GetValue<ConverterCSVDialogModel>(ParameterNames.ConverterTypeModel);
- if (ConverterCSVModel != null)
- {
- //先对链表做处理,在获取链表个数
- limitationConvert(ConverterCSVModel);
- App.mainWindowViewModel.MaxValue = ConverterCSVModel.PageIndexLists.Count();
- for (int i = 0; ConverterCSVModel.PageIndexLists.Count > i; i++)
- {
- ConverterCSVModel.PageIndexLists[i] = ConverterCSVModel.PageIndexLists[i] + 1;
- }
- result = await ConverterHelper.CSVConvert(ConverterCSVModel.InputPath, ConverterCSVModel.OutputPath, GetProgress, ConverterCSVModel.PageIndexLists, ConverterCSVModel.Pawssword, ConverterCSVModel.csvOptions, ConverterCSVModel.IsCustomFileName);
- }
- else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
- break;
- case "Excel":
- ConverterExcelDialogModel ConverterExcelModel = e.Parameters.GetValue<ConverterExcelDialogModel>(ParameterNames.ConverterTypeModel);
- if (ConverterExcelModel != null)
- {
- limitationConvert(ConverterExcelModel);
- App.mainWindowViewModel.MaxValue = ConverterExcelModel.PageIndexLists.Count();
- for (int i = 0; ConverterExcelModel.PageIndexLists.Count > i; i++)
- {
- ConverterExcelModel.PageIndexLists[i] = ConverterExcelModel.PageIndexLists[i] + 1;
- }
- result = await ConverterHelper.ExcelConvert(ConverterExcelModel.InputPath, ConverterExcelModel.OutputPath, GetProgress, ConverterExcelModel.PageIndexLists, ConverterExcelModel.Pawssword, ConverterExcelModel.excelOptions);
- }
- else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
- break;
- case "HTML":
- ConverterHTMLDialogModel ConverterHTMLModel = e.Parameters.GetValue<ConverterHTMLDialogModel>(ParameterNames.ConverterTypeModel);
- if (ConverterHTMLModel != null)
- {
- limitationConvert(ConverterHTMLModel);
- App.mainWindowViewModel.MaxValue = ConverterHTMLModel.PageIndexLists.Count();
- for (int i = 0; ConverterHTMLModel.PageIndexLists.Count > i; i++)
- {
- ConverterHTMLModel.PageIndexLists[i] = ConverterHTMLModel.PageIndexLists[i] + 1;
- }
- result = await ConverterHelper.HTMLConvert(ConverterHTMLModel.InputPath, ConverterHTMLModel.OutputPath, GetProgress, ConverterHTMLModel.PageIndexLists, ConverterHTMLModel.Pawssword, ConverterHTMLModel.htmlOptions);
- }
- else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
- break;
- case "Img":
- ConverterImgDialogModel ConverterImgModel = e.Parameters.GetValue<ConverterImgDialogModel>(ParameterNames.ConverterTypeModel);
- if (ConverterImgModel != null)
- {
- limitationConvert(ConverterImgModel);
- App.mainWindowViewModel.MaxValue = ConverterImgModel.PageIndexLists.Count();
- for (int i = 0; ConverterImgModel.PageIndexLists.Count > i; i++)
- {
- ConverterImgModel.PageIndexLists[i] = ConverterImgModel.PageIndexLists[i] + 1;
- }
- result = await ConverterHelper.ImgConvert(ConverterImgModel.InputPath, ConverterImgModel.OutputPath, GetProgress, ConverterImgModel.PageIndexLists, ConverterImgModel.Pawssword, ConverterImgModel.imageType, ConverterImgModel.imgOptions);
- }
- else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
- break;
- case "PPT":
- ConverterPPTDialogModel ConverterPPTModel = e.Parameters.GetValue<ConverterPPTDialogModel>(ParameterNames.ConverterTypeModel);
- if (ConverterPPTModel != null)
- {
- limitationConvert(ConverterPPTModel);
- App.mainWindowViewModel.MaxValue = ConverterPPTModel.PageIndexLists.Count();
- for (int i = 0; ConverterPPTModel.PageIndexLists.Count > i; i++)
- {
- ConverterPPTModel.PageIndexLists[i] = ConverterPPTModel.PageIndexLists[i] + 1;
- }
- result = await ConverterHelper.PPTConvert(ConverterPPTModel.InputPath, ConverterPPTModel.OutputPath, GetProgress, ConverterPPTModel.PageIndexLists, ConverterPPTModel.Pawssword, ConverterPPTModel.pPTOptions);
- }
- else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
- break;
- case "RTF":
- //App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Collapsed;
- ConverterRTFDialogModel ConverterRTFModel = e.Parameters.GetValue<ConverterRTFDialogModel>(ParameterNames.ConverterTypeModel);
- if (ConverterRTFModel != null)
- {
- limitationConvert(ConverterRTFModel);
- App.mainWindowViewModel.MaxValue = ConverterRTFModel.PageIndexLists.Count();
- for (int i = 0; ConverterRTFModel.PageIndexLists.Count > i; i++)
- {
- ConverterRTFModel.PageIndexLists[i] = ConverterRTFModel.PageIndexLists[i] + 1;
- }
- result = await ConverterHelper.RTFConvert(ConverterRTFModel.InputPath, ConverterRTFModel.OutputPath, GetProgress, ConverterRTFModel.PageIndexLists, ConverterRTFModel.Pawssword, ConverterRTFModel.rTFOptions);
- }
- else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
- break;
- case "Text":
- ConverterTextDialogModel ConverterTextModel = e.Parameters.GetValue<ConverterTextDialogModel>(ParameterNames.ConverterTypeModel);
- if (ConverterTextModel != null)
- {
- limitationConvert(ConverterTextModel);
- App.mainWindowViewModel.MaxValue = ConverterTextModel.PageIndexLists.Count();
- for (int i = 0; ConverterTextModel.PageIndexLists.Count > i; i++)
- {
- ConverterTextModel.PageIndexLists[i] = ConverterTextModel.PageIndexLists[i] + 1;
- }
- result = await ConverterHelper.TxtConvert(ConverterTextModel.InputPath, ConverterTextModel.OutputPath, GetProgress, ConverterTextModel.PageIndexLists, ConverterTextModel.Pawssword, ConverterTextModel.txtOptions);
- }
- else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
- break;
- case "Word":
- ConverterWordDialogModel ConverterWordModel = e.Parameters.GetValue<ConverterWordDialogModel>(ParameterNames.ConverterTypeModel);
- if (ConverterWordModel != null)
- {
- limitationConvert(ConverterWordModel);
- App.mainWindowViewModel.MaxValue = ConverterWordModel.PageIndexLists.Count();
- for (int i = 0; ConverterWordModel.PageIndexLists.Count > i; i++)
- {
- ConverterWordModel.PageIndexLists[i] = ConverterWordModel.PageIndexLists[i] + 1;
- }
- result = await ConverterHelper.WordConvert(ConverterWordModel.InputPath, ConverterWordModel.OutputPath, GetProgress, ConverterWordModel.PageIndexLists, ConverterWordModel.Pawssword, ConverterWordModel.wordOptions);
- }
- else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
- break;
- default:
- break;
- }
- if (!result)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Convert_FailedWarning"), App.ServiceLoader.GetString("Text_ok"));
- }
- App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Visible;
- App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
- }
- }
- }
|