using ComPDFKitViewer.PdfViewer;
using Microsoft.Office.Core;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Model.Dialog.ConverterDialogs;
using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
using PDF_Office.Model.Dialog.ToolsDialogs;
using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
using PDF_Office.Model.PDFTool;
using PDF_Office.Views;
using PDFSettings;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
namespace PDF_Office.ViewModels.HomePanel.PDFTools
{
public class QuickToolsContentViewModel : BindableBase
{
#region 文案
private string T_title;
public string T_Title
{
get { return T_title; }
set
{
SetProperty(ref T_title, value);
}
}
private void InitString()
{
T_Title = App.HomePageLoader.GetString("HomeTool_Title");
}
#endregion
#region 属性
///
/// 扩展/收缩
///
private bool _isExpendTools = false;
public bool IsExpendTools
{
get { return _isExpendTools; }
set { SetProperty(ref _isExpendTools, value); }
}
private string regionName;
public string ToolRegionName
{
get { return regionName; }
set { SetProperty(ref regionName, value); }
}
#endregion
#region Command and Event
public IDialogService dialogs;
public DelegateCommand QuickToolsCommand { get; set; }
public DelegateCommand OpenMenuCommand { get; set; }
public DelegateCommand ExpendCommand { get; set; }
public DelegateCommand ShowToolCommand { get; set; }
public IRegionManager toolregion;
public event EventHandler ExpendToolsHanlder;
#endregion
public QuickToolsContentViewModel(IDialogService dialogService, IRegionManager regionManager)
{
toolregion = regionManager;
ToolRegionName = RegionNames.ToolRegionName;
System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
{
toolregion.RequestNavigate(ToolRegionName, "Guid");
}));
dialogs = dialogService;
InitVariable();
InitCommand();
InitString();
}
#region 初始化和绑定
private void InitVariable()
{
}
private void InitCommand()
{
QuickToolsCommand = new DelegateCommand(QuickTools_Click);
OpenMenuCommand = new DelegateCommand(OpenMenu);
ExpendCommand = new DelegateCommand(Expend);
ShowToolCommand = new DelegateCommand(ShowTool);
}
#endregion
private void ShowTool()
{
toolregion.RequestNavigate(ToolRegionName, "Tools");
}
public void QuickTools_Click(ToolItem toolItem)
{
/*
*设置这个对话框的起始保存路径
*/
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "PDF|*.pdf;*.PDF;";
if (toolItem.FnType == PDFFnType.Compress || toolItem.FnType == PDFFnType.Security||
toolItem.FnType ==PDFFnType.BatchRemove||toolItem.FnType==PDFFnType.ConvertPDF||
toolItem.FnType==PDFFnType.Background|| toolItem.FnType == PDFFnType.HeaderFooter||
toolItem.FnType == PDFFnType.BatesNumbers|| toolItem.FnType == PDFFnType.WaterMark||
toolItem.FnType == PDFFnType.PDFToExcel|| toolItem.FnType == PDFFnType.PDFToImage||
toolItem.FnType == PDFFnType.PDFToPPT|| toolItem.FnType == PDFFnType.Batch ||
toolItem.FnType == PDFFnType.Merge)
{
//dlg.Multiselect = true;
}
if (toolItem.FnType == PDFFnType.ImageToPDF)
{
dlg.Multiselect = true;
dlg.Filter = "Picture|*.png;*.PNG;*.jpg;*.JPG;*bmp;*jpeg;*gif;*tiff;";
}
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
CPDFViewer viewer = new CPDFViewer();
switch ((PDFFnType)toolItem.FnType)
{
case PDFFnType.Split:
viewer.InitDocument(dlg.FileName);
if (!CheckPassword(viewer)) {return;}
DialogParameters splitvalue = new DialogParameters();
splitvalue.Add(ParameterNames.PDFViewer, viewer);
splitvalue.Add(ParameterNames.FilePath, dlg.FileName);
dialogs.ShowDialog(DialogNames.HomePageSplitDialog, splitvalue, e => { });
break;
case PDFFnType.Extract:
viewer.InitDocument(dlg.FileName);
if (!CheckPassword(viewer)) {return;}
DialogParameters extractvalue = new DialogParameters();
extractvalue.Add(ParameterNames.PDFViewer, viewer);
extractvalue.Add(ParameterNames.FilePath, dlg.FileName);
dialogs.ShowDialog(DialogNames.HomePageExtractDialog, extractvalue, e => { });
break;
case PDFFnType.Insert:
viewer.InitDocument(dlg.FileName);
if (!CheckPassword(viewer)) {return;}
DialogParameters insertvalue = new DialogParameters();
insertvalue.Add(ParameterNames.PDFViewer, viewer);
insertvalue.Add(ParameterNames.FilePath, dlg.FileName);
dialogs.ShowDialog(DialogNames.HomePageInsertDialog, insertvalue, e => { });
break;
case PDFFnType.Compress:
viewer.InitDocument(dlg.FileName);
if (!CheckPassword(viewer)) { return; }
DialogParameters compresspdf = new DialogParameters();
compresspdf.Add(ParameterNames.PDFViewer, viewer);
dialogs.ShowDialog(DialogNames.CompressDialog, compresspdf, e =>
{
CompressDialogModel compressDialogModel = new CompressDialogModel();
compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
// OnOpened((Prism.Services.Dialogs.DialogResult)e);
});
//批量
//DialogParameters compresspdf = new DialogParameters();
//compresspdf.Add(ParameterNames.BatchProcessing_Name, "1");
//HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
//HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
//compresspdf.Add(ParameterNames.FilePath, dlg.FileNames);
//dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, compresspdf, e => { });
break;
case PDFFnType.ImageToPDF:
DialogParameters picturetopdf = new DialogParameters();
picturetopdf.Add(ParameterNames.FilePath, dlg.FileNames);
dialogs.ShowDialog(DialogNames.HomePagePictureToPDFDialog, picturetopdf, e => { });
break;
case PDFFnType.Print:
viewer.InitDocument(dlg.FileName);
if (!CheckPassword(viewer)) {return;}
DialogParameters printvalue = new DialogParameters();
printvalue.Add(ParameterNames.PDFViewer, viewer);
printvalue.Add(ParameterNames.FilePath, dlg.FileName);
dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printvalue, e => { });
try
{
dialogs.ShowDialog("MainWindow");
}
catch
{
}
break;
case PDFFnType.Security:
//DialogParameters securitypdf = new DialogParameters();
//securitypdf.Add(ParameterNames.BatchProcessing_Name, "2");
//HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
//HomePageBatchProcessingDialogModel.BatchProcessingIndex = 2;
//securitypdf.Add(ParameterNames.FilePath, dlg.FileNames);
//dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, securitypdf, e => { });
CPDFViewer cPDFViewer = new CPDFViewer();
cPDFViewer.InitDocument(dlg.FileName);
VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(cPDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
if (result.IsDiscryptied)
{
if (result.Password != null)
{
cPDFViewer.Document.UnlockWithPassword(result.Password);
}
///TODO:
///此处填入需要执行的代码
///
DialogParameters param = new DialogParameters();
param.Add(ParameterNames.PDFViewer, cPDFViewer);
dialogs.ShowDialog(DialogNames.SetPasswordDialog, param, e => {
string FilePath;
if (e.Parameters.TryGetValue("FilePath",out FilePath))
{
CommonHelper.ShowFileBrowser(FilePath);
}
});
}
break;
case PDFFnType.ConvertPDF:
//DialogParameters convertpdf = new DialogParameters();
//convertpdf.Add(ParameterNames.BatchProcessing_Name, "0");
//HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
//HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
//convertpdf.Add(ParameterNames.FilePath, dlg.FileNames);
//dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdf, e => { });
break;
case PDFFnType.BatchRemove:
DialogParameters batchremovepdf = new DialogParameters();
batchremovepdf.Add(ParameterNames.BatchProcessing_Name, "7");
HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
HomePageBatchProcessingDialogModel.BatchProcessingIndex = 7;
batchremovepdf.Add(ParameterNames.FilePath, dlg.FileNames);
dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batchremovepdf, e => { });
break;
case PDFFnType.HeaderFooter:
DialogParameters headerFooterpdf = new DialogParameters();
headerFooterpdf.Add(ParameterNames.BatchProcessing_Name, "5");
HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
HomePageBatchProcessingDialogModel.BatchProcessingIndex = 5;
headerFooterpdf.Add(ParameterNames.FilePath, dlg.FileNames);
dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, headerFooterpdf, e => { });
break;
case PDFFnType.BatesNumbers:
DialogParameters batesNumberspdf = new DialogParameters();
batesNumberspdf.Add(ParameterNames.BatchProcessing_Name, "6");
HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
HomePageBatchProcessingDialogModel.BatchProcessingIndex = 6;
batesNumberspdf.Add(ParameterNames.FilePath, dlg.FileNames);
dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batesNumberspdf, e => { });
break;
case PDFFnType.Background:
DialogParameters backgroundpdf = new DialogParameters();
backgroundpdf.Add(ParameterNames.BatchProcessing_Name, "4");
HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
HomePageBatchProcessingDialogModel.BatchProcessingIndex = 4;
backgroundpdf.Add(ParameterNames.FilePath, dlg.FileNames);
dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, backgroundpdf, e => { });
break;
case PDFFnType.WaterMark:
DialogParameters watermarkpdf = new DialogParameters();
watermarkpdf.Add(ParameterNames.BatchProcessing_Name, "3");
HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
HomePageBatchProcessingDialogModel.BatchProcessingIndex = 3;
watermarkpdf.Add(ParameterNames.FilePath, dlg.FileNames);
dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, watermarkpdf, e => { });
break;
case PDFFnType.PDFToImage:
viewer.InitDocument(dlg.FileName);
if (!CheckPassword(viewer)) { return; }
DialogParameters convertpdftoimage = new DialogParameters();
convertpdftoimage.Add("PageRangeComboBoxCurrentPage", true);
convertpdftoimage.Add(ParameterNames.PDFViewer, viewer);
dialogs.ShowDialog(DialogNames.ConverterImgDialog, convertpdftoimage, e =>
{
ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
converterDialogsModel.OnOpened((DialogResult)e);
});
//DialogParameters convertpdftoimage = new DialogParameters();
//convertpdftoimage.Add(ParameterNames.BatchProcessing_Name, "0");
//convertpdftoimage.Add("ConverterTypeIndex", 7);
//HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
//HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
//convertpdftoimage.Add(ParameterNames.FilePath, dlg.FileNames);
//dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoimage, e => { });
break;
case PDFFnType.PDFToExcel:
viewer.InitDocument(dlg.FileName);
if (!CheckPassword(viewer)) { return; }
DialogParameters convertpdftoexcel = new DialogParameters();
convertpdftoexcel.Add(ParameterNames.PDFViewer, viewer);
convertpdftoexcel.Add("PageRangeComboBoxCurrentPage", true);
dialogs.ShowDialog(DialogNames.ConverterExcelDialog, convertpdftoexcel, e =>
{
ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
converterDialogsModel.OnOpened((DialogResult)e);
});
//DialogParameters convertpdftoexcel = new DialogParameters();
//convertpdftoexcel.Add(ParameterNames.BatchProcessing_Name, "0");
//convertpdftoexcel.Add("ConverterTypeIndex", 1);
//HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
//HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
//convertpdftoexcel.Add(ParameterNames.FilePath, dlg.FileNames);
//dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoexcel, e => { });
break;
case PDFFnType.PDFToPPT:
viewer.InitDocument(dlg.FileName);
if (!CheckPassword(viewer)) { return; }
DialogParameters convertpdftoPPT = new DialogParameters();
convertpdftoPPT.Add(ParameterNames.PDFViewer, viewer);
convertpdftoPPT.Add("PageRangeComboBoxCurrentPage", true);
dialogs.ShowDialog(DialogNames.ConverterPPTDialog, convertpdftoPPT, e =>
{
ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
converterDialogsModel.OnOpened((DialogResult)e);
});
//批量处理
//DialogParameters convertpdftoppt = new DialogParameters();
//convertpdftoppt.Add(ParameterNames.BatchProcessing_Name, "0");
//convertpdftoppt.Add("ConverterTypeIndex", 2);
//HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
//HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
//convertpdftoppt.Add(ParameterNames.FilePath, dlg.FileNames);
//dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoppt, e => { });
break;
case PDFFnType.PDFToWord:
viewer.InitDocument(dlg.FileName);
if (!CheckPassword(viewer)) { return; }
DialogParameters convertpdftoword = new DialogParameters();
convertpdftoword.Add(ParameterNames.PDFViewer, viewer);
convertpdftoword.Add("PageRangeComboBoxCurrentPage", true);
dialogs.ShowDialog(DialogNames.ConverterWordDialog, convertpdftoword, e =>
{
ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
converterDialogsModel.OnOpened((DialogResult)e);
});
//批量处理
//DialogParameters convertpdftoword = new DialogParameters();
//convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
//convertpdftoword.Add("ConverterTypeIndex", 0);
//HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
//HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
//convertpdftoword.Add(ParameterNames.FilePath, dlg.FileNames);
//dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
break;
case PDFFnType.Batch:
DialogParameters batcpdf = new DialogParameters();
batcpdf.Add(ParameterNames.BatchProcessing_Name, "0");
HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
batcpdf.Add(ParameterNames.FilePath, dlg.FileNames);
dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batcpdf, e => { });
break;
case PDFFnType.Merge:
viewer.InitDocument(dlg.FileName);
if (!CheckPassword(viewer)) { return; }
DialogParameters merge = new DialogParameters();
merge.Add(ParameterNames.PDFViewer, viewer);
merge.Add(ParameterNames.FilePath, dlg.FileNames);
dialogs.ShowDialog(DialogNames.MergeDialog, merge, e =>
{
});
break;
}
}
}
//
//
private bool CheckPassword(CPDFViewer viewer) {
if (viewer.Document == null)
{
//MessageBoxEx.Show(App.MainPageLoader.GetString("Main_OpenFileFailedWarning"));
return false;
}
if (viewer.Document.IsLocked)
{
VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(viewer.Document, EnumPasswordKind.StatusOpenPassword, dialogs);
if (result.IsDiscryptied)
{
if (result.Password != null)
{
string filePath = viewer.Document.FilePath;
viewer.Document.UnlockWithPassword(result.Password);
viewer.Tag = result.Password;
}
///TODO:
///此处填入需要执行的代码
}
if (result.IsDiscryptied == false)
{
//未成功解密文档时,释放Document对象,返回
viewer.Document.Release();
return false;
}
}
return true;
}
private void Expend()
{
IsExpendTools = !IsExpendTools;
ExpendToolsHanlder?.Invoke(null, IsExpendTools);
}
private void OpenMenu(object obj)
{
var menu = App.Current.FindResource("ExpendToolsContextMenu") as ContextMenu;
var btn = obj as Button;
if (menu != null && btn != null)
{
if(menu.Items.Count == 1)
{
var item = menu.Items[0] as MenuItem;
if(_isExpendTools)
{
item.Header = "Collapse";
}
else
{
item.Header = "Expand";
}
item.Command = ExpendCommand;
//btn.ContextMenu = menu;
menu.PlacementTarget = btn;
menu.IsOpen = true;
}
}
}
}
}