using ComDocumentAIKit; using ComPDFKit_Conversion.Converter; using ComPDFKit_Conversion.Options; using ComPDFKitViewer.PdfViewer; using ImageMagick; using Microsoft.Office.Interop.Excel; using PDF_Master.CustomControl; using PDF_Master.Helper; using PDF_Master.Model; using PDF_Master.Model.Dialog.ConverterDialogs; using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing; using PDF_Master.Properties; using PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing.HomePageConverter; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Windows; using System.Windows.Forms; using DialogResult = Prism.Services.Dialogs.DialogResult; namespace PDF_Master.ViewModels.Dialog.ConverterDialogs { public class ConverterExcelDialogViewModel : BindableBase, IDialogAware { #region 文案 private string T_title; public string T_Title { get { return T_title; } set { SetProperty(ref T_title, value); } } private string T_yes; public string T_YES { get { return T_yes; } set { SetProperty(ref T_yes, value); } } private string T_no; public string T_No { get { return T_no; } set { SetProperty(ref T_no, value); } } private string T_onlyTables; public string T_OnlyTables { get { return T_onlyTables; } set { SetProperty(ref T_onlyTables, value); } } private string T_allContents; public string T_AllContents { get { return T_allContents; } set { SetProperty(ref T_allContents, value); } } private string T_eachPageAllContents; public string T_EachPageAllContents { get { return T_eachPageAllContents; } set { SetProperty(ref T_eachPageAllContents, value); } } private string T_fileAllContents; public string T_FileAllContents { get { return T_fileAllContents; } set { SetProperty(ref T_fileAllContents, value); } } private string T_eachTableOnlyTables; public string T_EachTableOnlyTables { get { return T_eachTableOnlyTables; } set { SetProperty(ref T_eachTableOnlyTables, value); } } private string T_eachPageOnlyTables; public string T_EachPageOnlyTables { get { return T_eachPageOnlyTables; } set { SetProperty(ref T_eachPageOnlyTables, value); } } private string T_fileOnlyTables; public string T_FileOnlyTables { get { return T_fileOnlyTables; } set { SetProperty(ref T_fileOnlyTables, value); } } private string T_onlyText; public string T_OnlyText { get { return T_onlyText; } set { SetProperty(ref T_onlyText, value); } } private string T_settings; public string T_Settings { get { return T_settings; } set { SetProperty(ref T_settings, value); } } private string T_pageRange; public string T_PageRange { get { return T_pageRange; } set { SetProperty(ref T_pageRange, value); } } private string _TextUpgrade; public string TextUpgrade { get { return _TextUpgrade; } set { _TextUpgrade = value; } } private void IntString() { TextUpgrade = App.ServiceLoader.GetString("TextUpgrade"); T_Title = App.MainPageLoader.GetString("Convert_PDFToExcelTitle"); T_YES = App.MainPageLoader.GetString("Convert_Yes"); T_No = App.MainPageLoader.GetString("Convert_No"); T_AllContents = App.MainPageLoader.GetString("Convert_PDFToExcelAllContents"); T_EachPageAllContents = App.MainPageLoader.GetString("Convert_PDFToExcelAllContentsEachPage"); T_FileAllContents = App.MainPageLoader.GetString("Convert_PDFToExcelAllContentsFile"); T_OnlyTables = App.MainPageLoader.GetString("Convert_PDFToExcelAllOnlyTables"); T_EachTableOnlyTables = App.MainPageLoader.GetString("Convert_PDFToExcelAllOnlyTablesEachTable"); T_EachPageOnlyTables = App.MainPageLoader.GetString("Convert_PDFToExcelAllOnlyTablesEachPage"); T_FileOnlyTables = App.MainPageLoader.GetString("Convert_PDFToExcelAllOnlyTablesFile"); T_OnlyText = App.MainPageLoader.GetString("Convert_PDFToExcelOnlyText"); T_Settings = App.MainPageLoader.GetString("Convert_PDFToExcelSettings"); T_PageRange = App.MainPageLoader.GetString("Convert_PDFPageRange"); } #endregion 参数和属性 public string Title => ""; public event Action RequestClose; #region 参数和属性 public ConverterExcelDialogModel ConverterExcelModel = new ConverterExcelDialogModel(); public CPDFViewer currentViewer; public string CurrentPageIndex = "1"; public bool IsCurrentPageIndex = false; public IDialogService dialogs; public string PageRangeText { set; get; } = "0"; public string PageRangeSelectIndex { set; get; } = "0"; private int _anyContentSelectIndex = 0; public int AnyContentSelectIndex { get { return _anyContentSelectIndex; } set { SetProperty(ref _anyContentSelectIndex, value); AnyContent(_anyContentSelectIndex); } } private int _onlyTableSelectIndex = 0; public int OnlyTableSelectIndex { get { return _onlyTableSelectIndex; } set { SetProperty(ref _onlyTableSelectIndex, value); OnlyTable(_onlyTableSelectIndex); } } private int maxPageRange = 0; public int MaxPageRange { get { return maxPageRange; } set { SetProperty(ref maxPageRange, value); } } private bool oCRCheckBoxIsCheckBox = false; public bool OCRCheckBoxIsCheckBox { get { return oCRCheckBoxIsCheckBox; } set { SetProperty(ref oCRCheckBoxIsCheckBox, value); ConverterExcelModel.Options.IsAllowOCR = oCRCheckBoxIsCheckBox; } } private bool oCRIsEnable = true; public bool OCRIsEnable { get { return oCRIsEnable; } set { SetProperty(ref oCRIsEnable, value); if (value == true) { ConverterExcelModel.Options.IsAllowOCR = OCRCheckBoxIsCheckBox; } else { ConverterExcelModel.Options.IsAllowOCR = false; } } } private int languageSelectedIndex = 2; public int LanguageSelectedIndex { get { return languageSelectedIndex; } set { SetProperty(ref languageSelectedIndex, value); SelectLanguage(languageSelectedIndex); } } private Dictionary CheckPageSelect = new Dictionary(); private void InitCheckPageSelect() { CheckPageSelect.Clear(); if (!IsCurrentPageIndex) { CheckPageSelect.Add("0", "0"); CheckPageSelect.Add("1", "1"); CheckPageSelect.Add("2", "2"); CheckPageSelect.Add("3", "3"); CheckPageSelect.Add("4", "4"); } else { CheckPageSelect.Add("0", "0"); CheckPageSelect.Add("1", "-2"); CheckPageSelect.Add("2", "1"); CheckPageSelect.Add("3", "2"); CheckPageSelect.Add("4", "3"); } } private Visibility limitationsConvertVisible = Visibility.Hidden; public Visibility LimitationsConvertVisible { get { return limitationsConvertVisible; } set { SetProperty(ref limitationsConvertVisible, value); } } #endregion #region 委托声明 public DelegateCommand RadioButtonCommand { get; set; } public DelegateCommand CancelCommand { get; set; } public DelegateCommand ConverterCommnad { get; set; } public DelegateCommand CmbPageSelectionChanged { get; set; } public DelegateCommand CmbPageTextChanged { get; set; } public DelegateCommand BatchConverterCommand { get; set; } public DelegateCommand PreviewCancelDownCommand { get; set; } public DelegateCommand SetCustomPageRangeCommand { get; set; } public DelegateCommand UnlockMouseDownCommand { get; set; } public DelegateCommand OpenOCRCommand { get; set; } #endregion public ConverterExcelDialogViewModel(IDialogService dialogService) { IntString(); CancelCommand = new DelegateCommand(cancel); ConverterCommnad = new DelegateCommand(converter); RadioButtonCommand = new DelegateCommand(radiobutton); SetCustomPageRangeCommand = new DelegateCommand(SetCustomPageRange); CmbPageSelectionChanged = new DelegateCommand(CmbPageSelectionChangedEvent); CmbPageTextChanged = new DelegateCommand(CmbPageTextChangedEvent); PreviewCancelDownCommand = new DelegateCommand(PreviewCancelDown); BatchConverterCommand = new DelegateCommand(BatchConverter); UnlockMouseDownCommand = new DelegateCommand(UnlockMouseDown); OpenOCRCommand = new DelegateCommand(openOCR); dialogs = dialogService; } #region 逻辑函数 private void openOCR() { if (OCRCheckBoxIsCheckBox == true) { bool folderMD5 = FileComparisonHelper.OCRModelItExist(); if (folderMD5) { string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine"); CPDFConverter.InitOcrLibrary(Path.Combine(folderPath, "x64")); CPDFConverter.SetOCRModelPath(Path.Combine(folderPath, "source", "models")); } else { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("想用OCR,就去下载吧!", "", "取消", "确定"); if (alertsMessage.result == ContentResult.Ok) { FileComparisonHelper.RemoveOCRModel(); DialogParameters oCRDownloadProgress = new DialogParameters(); dialogs.ShowDialog(DialogNames.OCRDownloadProgress, oCRDownloadProgress, e => { if (e.Result == Prism.Services.Dialogs.ButtonResult.OK) { string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine"); CPDFConverter.InitOcrLibrary(Path.Combine(folderPath, "x64")); CPDFConverter.SetOCRModelPath(Path.Combine(folderPath, "source", "models")); } else { OCRCheckBoxIsCheckBox = false; } }); } else { OCRCheckBoxIsCheckBox = false; } } } } public void SetCustomPageRange() { if (PageRangeSelectIndex == CheckPageSelect["4"]) { List PageIndexLists = new List(); if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' })) { //TODO //AlertsMessage alertsMessage = new AlertsMessage(); //alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok")); //if (alertsMessage.result == ContentResult.Ok) //{ // return; //} //else //{ // //this.eventAggregator.GetEvent().Publish(new EnumDeleteUnicode // //{ // // Unicode = Unicode, // // Status = EnumDelete.StatusCreate // //}); // return; //} } } } private void cancel() { RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)); } private void PreviewCancelDown(object e) { var PageRangeComboBox = e as WritableComboBox; if (PageRangeComboBox != null) { PageRangeComboBox.IsloseFocus = false; } } private void converter() { if (PageRangeSelectIndex == CheckPageSelect["4"]) { List PageIndexLists = new List(); if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' })) { //TODO AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok")); if (alertsMessage.result == ContentResult.Ok) { return; } else { //this.eventAggregator.GetEvent().Publish(new EnumDeleteUnicode //{ // Unicode = Unicode, // Status = EnumDelete.StatusCreate //}); return; } } } System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog(); /* *设置这个对话框的起始保存路径 */ sfd.InitialDirectory = currentViewer.Document.FilePath; /* *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;” */ sfd.Filter = "Excel|*.xlsx;"; /* *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮 */ sfd.FileName = currentViewer.Document.FileName + ".xlsx"; /* * 做一些工作 */ if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { ConverterExcelModel.OutputPath = sfd.FileName; try { if (File.Exists(ConverterExcelModel.OutputPath)) File.Delete(ConverterExcelModel.OutputPath); } catch { AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok")); return; } } else { return; } HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterExcelModel.PageRange, PageRangeText, !IsCurrentPageIndex, CurrentPageIndex); if (ConverterExcelModel.PageRange == "") { Trace.WriteLine("输入不对"); AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok")); return; } char[] enumerationSeparator = new char[] { ',' }; char[] rangeSeparator = new char[] { '-' }; if (!CommonHelper.GetPagesInRange(ref ConverterExcelModel.PageIndexLists, ConverterExcelModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator)) { //TODO Trace.WriteLine("输入不对"); AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok")); return; } ConverterExcelModel.excelOptions = ConverterExcelModel.ExcelOptions(); //DialogParameters value = new DialogParameters(); //value.Add(ParameterNames.ConverterType, "Excel"); //value.Add(ParameterNames.ConverterTypeModel, ConverterExcelModel); var dialogresult = new DialogResult(ButtonResult.OK); dialogresult.Parameters.Add(ParameterNames.ConverterType, "Excel"); dialogresult.Parameters.Add(ParameterNames.ConverterTypeModel, ConverterExcelModel); RequestClose.Invoke(dialogresult); //RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); //dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e => //{ //}); } private void CmbPageSelectionChangedEvent(object e) { ///这里采用的是将预览UI控件传递过来的方式,为下下策 ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式 ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里 var ConverterPreview = e as PageTurningPreview; if (ConverterPreview != null) { string PageRangeSelectIndex = this.PageRangeSelectIndex; var currentViewer = this.currentViewer; string PageRange = ""; var PageRangeText = this.PageRangeText; if (PageRangeSelectIndex == CheckPageSelect["0"] || PageRangeSelectIndex == CheckPageSelect["2"] || PageRangeSelectIndex == CheckPageSelect["3"] || PageRangeSelectIndex == CheckPageSelect["4"]) { if (PageRangeSelectIndex == CheckPageSelect["4"]) { HomePageEditHelper.GetPagerange("0", currentViewer, ref PageRange, PageRangeText, !IsCurrentPageIndex); } else { HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, !IsCurrentPageIndex); } char[] enumerationSeparator = new char[] { ',' }; char[] rangeSeparator = new char[] { '-' }; if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator)) { //TODO ConverterPreview.PageIndexLists.Add(0); Trace.WriteLine("输入不对"); AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok")); return; } } if (PageRangeSelectIndex == CheckPageSelect["1"]) { char[] enumerationSeparator = new char[] { ',' }; char[] rangeSeparator = new char[] { '-' }; if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString() , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator)) { //TODO Trace.WriteLine("输入不对"); AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok")); return; } this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last() + 1).ToString(); } // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last() + 1).ToString(); ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString(); ConverterPreview.CurrentIndex = 0; } } private void CmbPageTextChangedEvent(object e) { var ConverterPreview = e as PageTurningPreview; if (ConverterPreview != null) { if (ConverterPreview != null) { string PageRangeSelectIndex = this.PageRangeSelectIndex; var currentViewer = this.currentViewer; string PageRange = ""; var PageRangeText = this.PageRangeText; HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, !IsCurrentPageIndex); char[] enumerationSeparator = new char[] { ',' }; char[] rangeSeparator = new char[] { '-' }; if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator)) { //TODO HomePageEditHelper.GetPagerange("0", currentViewer, ref PageRange, PageRangeText, !IsCurrentPageIndex); if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator)) { //TODO ConverterPreview.PageIndexLists.Add(0); Trace.WriteLine("输入不对"); AlertsMessage alertsMessage = new AlertsMessage(); alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok")); return; } ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString(); ConverterPreview.CurrentIndex = 0; return; } else { // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last() + 1).ToString(); ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString(); ConverterPreview.CurrentIndex = 0; } } } } private void BatchConverter() { DialogParameters convertpdftoword = new DialogParameters(); convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0"); convertpdftoword.Add("ConverterTypeIndex", 1); HomePageBatchProcessingDialogModel.FilePaths = new List { currentViewer.Document.FilePath.ToString() }; HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1; convertpdftoword.Add(ParameterNames.FilePath, new string[] { currentViewer.Document.FilePath.ToString() }); dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { }); } private void UnlockMouseDown() { ConverterHelper.convertUnlock(); } private void radiobutton(string e) { string radioButton = e; if (radioButton != null) { switch (radioButton) { case "AnyContentRadioBtn": OCRIsEnable = true; AnyContent(AnyContentSelectIndex); break; case "OnlyTableRadioBtn": OnlyTable(OnlyTableSelectIndex); OCRIsEnable = false; break; case "OnlyTextRadioBtn": OCRIsEnable = true; ConverterExcelModel.Options.WorksheetOpts = WorksheetOptions.ForTheDocument; ConverterExcelModel.Options.ContentOpts = ContentOptions.OnlyText; break; default: break; } } } private void AnyContent(int SelectedIndex) { switch (SelectedIndex) { case 0: ConverterExcelModel.Options.WorksheetOpts = WorksheetOptions.ForEachPage; ConverterExcelModel.Options.ContentOpts = ContentOptions.AllContent; break; case 1: ConverterExcelModel.Options.WorksheetOpts = WorksheetOptions.ForTheDocument; ConverterExcelModel.Options.ContentOpts = ContentOptions.AllContent; break; default: break; } } private void OnlyTable(int SelectedIndex) { ConverterExcelModel.Options.ContentOpts = ContentOptions.OnlyTable; switch (SelectedIndex) { case 0: ConverterExcelModel.Options.WorksheetOpts = WorksheetOptions.ForEachTable; break; case 1: ConverterExcelModel.Options.WorksheetOpts = WorksheetOptions.ForEachPage; break; case 2: ConverterExcelModel.Options.WorksheetOpts = WorksheetOptions.ForTheDocument; break; default: break; } } private void SelectLanguage(int SelectedIndex) { switch (SelectedIndex) { case 0: ConverterExcelModel.Options.OCRLanguage = COCRLanguage.COCRLanguageChinese; break; case 1: ConverterExcelModel.Options.OCRLanguage = COCRLanguage.COCRLanguageChineseTraditional; break; case 2: ConverterExcelModel.Options.OCRLanguage = COCRLanguage.COCRLanguageEnglish; break; case 3: ConverterExcelModel.Options.OCRLanguage = COCRLanguage.COCRLanguageJapanese; break; case 4: ConverterExcelModel.Options.OCRLanguage = COCRLanguage.COCRLanguageKorean; break; default: break; } } #endregion #region 构架行为 public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { CPDFViewer pdfViewer = null; parameters.TryGetValue(ParameterNames.PDFViewer, out pdfViewer); parameters.TryGetValue("PageRangeComboBoxCurrentPage", out IsCurrentPageIndex); if (pdfViewer != null) { InitCheckPageSelect(); currentViewer = pdfViewer; MaxPageRange = currentViewer.Document.PageCount; if (currentViewer.Tag != null) { ConverterExcelModel.Pawssword = currentViewer.Tag.ToString(); } ConverterExcelModel.InputPath = pdfViewer.Document.FilePath; FileInfo fileinfo = new FileInfo(ConverterExcelModel.InputPath); ConverterExcelModel.OutputPath = fileinfo.DirectoryName; if (Settings.Default.UserDate.subscribestatus != 1) { LimitationsConvertVisible = Visibility.Visible; } } } #endregion } }