using ComPDFKitViewer.PdfViewer;
using PDF_Office.Helper;
using PDF_Office.Model;
using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
using PDF_Office.Model.PDFTool;
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;
namespace PDF_Office.ViewModels.HomePanel.PDFTools
{
public class QuickToolsContentViewModel : BindableBase
{
#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();
}
#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)
{
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);
CheckPassword(viewer);
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);
CheckPassword(viewer);
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);
CheckPassword(viewer);
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:
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);
CheckPassword(viewer);
DialogParameters printvalue = new DialogParameters();
printvalue.Add(ParameterNames.PDFViewer, viewer);
printvalue.Add(ParameterNames.FilePath, dlg.FileName);
dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printvalue, e => { });
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 => { });
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:
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:
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:
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:
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;
}
}
}
//
//
private void CheckPassword(CPDFViewer viewer) {
if (viewer.Document.IsLocked)
{
DialogParameters value = new DialogParameters();
value.Add(ParameterNames.PDFDocument, viewer.Document);
dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e =>
{
if (e.Result == ButtonResult.OK)
{
if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue(ParameterNames.PassWord) != null)
{
viewer.Tag = e.Parameters.GetValue(ParameterNames.PassWord).ToString();
}
}
});
if (viewer.Document.IsLocked)
{
//未成功解密文档时,释放Document对象,返回
viewer.Document.Release();
return;
}
}
}
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 = "收缩";
}
else
{
item.Header = "展开";
}
item.Command = ExpendCommand;
//btn.ContextMenu = menu;
menu.PlacementTarget = btn;
menu.IsOpen = true;
}
}
}
}
}