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
{
///
/// 文档地址;
///
public string InputPath = "";
///
/// 输出地址;
///
public string OutputPath = "";
///
/// 进度;
///
public OnProgress GetProgress = null;
///
/// 页码 页码范围;
///
public List PageIndexLists = new List { };
///
/// 页面信息
///
public string PageRange = "";
///
/// 文档密码;
///
public string Pawssword = "";
///
/// 文档密码;
///
public bool IsCustomFileName = true;
///
/// 文档密码;
///
public string ConvertType = "";
///
/// OCR,与部分功能;
///
public ConvertOptions Options = new ConvertOptions();
///
/// 取消进度条
///
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; }
}
///
/// 转档限制转页面前10页
///
///
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(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(ParameterNames.ConverterTypeModel);
if (ConverterCSVModel != null)
{
App.mainWindowViewModel.MaxValue = ConverterCSVModel.PageIndexLists.Count();
limitationConvert(ConverterCSVModel);
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(ParameterNames.ConverterTypeModel);
if (ConverterExcelModel != null)
{
App.mainWindowViewModel.MaxValue = ConverterExcelModel.PageIndexLists.Count();
limitationConvert(ConverterExcelModel);
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(ParameterNames.ConverterTypeModel);
if (ConverterHTMLModel != null)
{
App.mainWindowViewModel.MaxValue = ConverterHTMLModel.PageIndexLists.Count();
limitationConvert(ConverterHTMLModel);
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(ParameterNames.ConverterTypeModel);
if (ConverterImgModel != null)
{
App.mainWindowViewModel.MaxValue = ConverterImgModel.PageIndexLists.Count();
limitationConvert(ConverterImgModel);
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(ParameterNames.ConverterTypeModel);
if (ConverterPPTModel != null)
{
App.mainWindowViewModel.MaxValue = ConverterPPTModel.PageIndexLists.Count();
limitationConvert(ConverterPPTModel);
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(ParameterNames.ConverterTypeModel);
if (ConverterRTFModel != null)
{
App.mainWindowViewModel.MaxValue = ConverterRTFModel.PageIndexLists.Count();
limitationConvert(ConverterRTFModel);
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(ParameterNames.ConverterTypeModel);
if (ConverterTextModel != null)
{
App.mainWindowViewModel.MaxValue = ConverterTextModel.PageIndexLists.Count();
limitationConvert(ConverterTextModel);
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(ParameterNames.ConverterTypeModel);
if (ConverterWordModel != null)
{
App.mainWindowViewModel.MaxValue = ConverterWordModel.PageIndexLists.Count();
limitationConvert(ConverterWordModel);
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;
}
}
}