using ComPDFKit.PDFDocument; using ComPDFKitViewer.PdfViewer; using PDF_Master.CustomControl; using PDF_Master.Helper; using PDF_Master.Model; using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing; using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Windows; using System.Windows.Forms; using DataTable = System.Data.DataTable; using DialogResult = Prism.Services.Dialogs.DialogResult; using Task = System.Threading.Tasks.Task; namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing { public class HomePageRemoveDialogViewModel : BindableBase, INavigationAware { #region 参数和属性 public IDialogService dialogs; private List fileNames; public List fileNamesView = new List(); private int fileNamesIndex = 0; private int FileNameNumber = 0; HomePageCompressDialogModel homePageCompressDialogModel = new HomePageCompressDialogModel(); private DataTable pdfDataTable = new DataTable(); public DataTable PDFDataTable { get { return pdfDataTable; } set { SetProperty(ref pdfDataTable, value); } } private DataTable pdfCurrentDataTable = new DataTable(); public DataTable PDFCurrentDataTable { get { return pdfCurrentDataTable; } set { SetProperty(ref pdfCurrentDataTable, value); } } private string optionsRemoveGridIsEnabled = "True"; public string OptionsRemoveGridIsEnabled { get { return optionsRemoveGridIsEnabled; } set { SetProperty(ref optionsRemoveGridIsEnabled, value); } } private string setOptionsRemoveGridIsEnabled = "True"; public string SetOptionsRemoveGridIsEnabled { get { return setOptionsRemoveGridIsEnabled; } set { SetProperty(ref setOptionsRemoveGridIsEnabled, value); } } private string removeIsEnable = "False"; public string RemoveIsEnable { get { return removeIsEnable; } set { SetProperty(ref removeIsEnable, value); } } private string selectFileName = "False"; public string SelectFileName { get { return selectFileName; } set { SetProperty(ref selectFileName, value); } } private Visibility addFileVisibility = Visibility.Hidden; public Visibility AddFileVisibility { get { return addFileVisibility; } set { SetProperty(ref addFileVisibility, value); RaisePropertyChanged(); } } private bool securityIsRemove = false; public bool SecurityIsRemove { get { return securityIsRemove; } set { SetProperty(ref securityIsRemove, value); } } private bool watermarkIsRemove = false; public bool WatermarkIsRemove { get { return watermarkIsRemove; } set { SetProperty(ref watermarkIsRemove, value); } } private bool backgroundIsRemove = false; public bool BackgroundIsRemove { get { return backgroundIsRemove; } set { SetProperty(ref backgroundIsRemove, value); } } private bool headerFooterIsRemove = false; public bool HeaderFooterIsRemove { get { return headerFooterIsRemove; } set { SetProperty(ref headerFooterIsRemove, value); } } private bool batesIsRemove = false; public bool BatesIsRemove { get { return batesIsRemove; } set { SetProperty(ref batesIsRemove, value); } } #endregion #region 委托声明 public DelegateCommand ADDPDFCommand { get; set; } public DelegateCommand ADDOpenedPDFCommand { get; set; } public DelegateCommand RemovePDFFileCommand { get; set; } public DelegateCommand ADDPDFFilesCommand { get; set; } public DelegateCommand OptionsRemoveCommand { get; set; } #endregion public HomePageRemoveDialogViewModel(IDialogService dialogService) { dialogs = dialogService; PDFCurrentDataTable.Columns.Add("FileState"); PDFDataTable.Columns.Add("FileName"); PDFDataTable.Columns.Add("FileSize"); PDFDataTable.Columns.Add("FileState"); ADDPDFCommand = new DelegateCommand(addpicture); ADDOpenedPDFCommand = new DelegateCommand(addOpenedPDFFiles); ADDPDFFilesCommand = new DelegateCommand(addpicturefiles); RemovePDFFileCommand = new DelegateCommand(removepdffile); OptionsRemoveCommand = new DelegateCommand(OptionsRemove); } #region 逻辑函数 private bool CheckHaveAllPermissions(CPDFDocument document) { CPDFPermissionsInfo permissionsInfo = document.GetPermissionsInfo(); if (permissionsInfo.AllowsDocumentChanges && permissionsInfo.AllowsPrinting && permissionsInfo.AllowsHighQualityPrinting && permissionsInfo.AllowsCopying && //permissionsInfo.AllowsDocumentAssembly&& //TODO: 这个权限有问题 permissionsInfo.AllowsCommenting) { return true; } else { return false; } } private DeleteSafetySettintgsModel.EnumNeedPassword CheckNeedPassword(CPDFDocument document) { if (document.IsLocked) { return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword; } else { } if (document.IsEncrypted) { if (CheckHaveAllPermissions(document)) { return DeleteSafetySettintgsModel.EnumNeedPassword.StatusDirectDescrypt; } else { return DeleteSafetySettintgsModel.EnumNeedPassword.StatusVerifyPassword; } } else { return DeleteSafetySettintgsModel.EnumNeedPassword.StatusCannotDecrypt; } } private void OptionsRemove() { if (!(WatermarkIsRemove || BackgroundIsRemove || HeaderFooterIsRemove || BatesIsRemove || SecurityIsRemove)) { MessageBoxEx.Show("请选择Option"); return; } FolderBrowserDialog dlg = new FolderBrowserDialog(); string saveSelectedPath = ""; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { saveSelectedPath = dlg.SelectedPath.Trim(); saveSelectedPath = saveSelectedPath + "\\"; } else { return; } OptionsRemoveGridIsEnabled = "False"; //GC.KeepAlive(indexDelegate); fileNamesIndex = 0; HomePageBatchProcessingDialogModel.closeDialog = true; foreach (var filename in fileNames) { PDFDataTable.Rows[fileNamesIndex]["FileState"] = "wait"; CPDFDocument document = CPDFDocument.InitWithFilePath(filename); if (document == null) { PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error"; PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error"; fileNamesIndex++; continue; } if (document.IsLocked) { DialogParameters value = new DialogParameters(); value.Add(ParameterNames.PDFDocument, document); dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e => { if (e.Result == ButtonResult.OK) { if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue(ParameterNames.PassWord) != null) { document.UnlockWithPassword(e.Parameters.GetValue(ParameterNames.PassWord).ToString()); } } }); if (document.IsLocked) { //未成功解密文档时,释放Document对象,返回 PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error"; PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error"; fileNamesIndex++; continue; } } FileInfo fileinfo = new FileInfo(filename); if (WatermarkIsRemove) { document.DeleteWatermarks(); } if (BackgroundIsRemove) { document.GetBackground().Clear(); } if (HeaderFooterIsRemove) { document.GetHeaderFooter().Clear(); } if (BatesIsRemove) { document.GetBates().Clear(); } if (SecurityIsRemove) { document.Descrypt(CommonHelper.CreateFilePath(saveSelectedPath + document.FileName + "_Remove.pdf")); } else { document.WriteToFilePath(CommonHelper.CreateFilePath(saveSelectedPath + document.FileName + "_Remove.pdf")); } PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete"; PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete"; fileNamesIndex++; document.Release(); } OptionsRemoveGridIsEnabled = "True"; HomePageBatchProcessingDialogModel.closeDialog = false; } #endregion #region 批量处理逻辑函数 /// /// 添加PDF文件 /// private void addpicture() { FileNameNumber = fileNames.Count; System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog(); dlg.Multiselect = true; dlg.Filter = "PDF|*.pdf;*.PDF;"; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { fileNames.AddRange(dlg.FileNames.ToList()); RemoveExcess(ref fileNames); SetOptionsRemoveGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; updateListview("wait"); } } public void addPDFFiles(string filename) { FileNameNumber = fileNames.Count; fileNames.Add(filename); RemoveExcess(ref fileNames); SetOptionsRemoveGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; updateListview("wait"); } /// /// 添加已打开PDF文件 /// public void addOpenedPDFFiles() { FileNameNumber = fileNames.Count; foreach (var filename in App.OpenedFileList) { fileNames.Add(filename); } RemoveExcess(ref fileNames); SetOptionsRemoveGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; updateListview("wait"); } /// /// 删除重复的文件 /// public void RemoveExcess(ref List Filenames) { List filenames = new List(); foreach (var fileName in Filenames) { if (!filenames.Contains(fileName)) { filenames.Add(fileName); } } Filenames.Clear(); Filenames = filenames; } /// /// 添加PDF文件夹 /// private void addpicturefiles() { FileNameNumber = fileNames.Count; FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string foldPath = dialog.SelectedPath; var apps = System.IO.Directory.GetFiles(foldPath); foreach (string app in apps) { var fi = new FileInfo(app); if (fi.Extension == ".pdf" || fi.Extension == ".PDF") { fileNames.Add(app); } } RemoveExcess(ref fileNames); updateListview("wait"); SetOptionsRemoveGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; } } /// /// 更新listview显示 /// state 状态显示字符串 /// public void updateListview(string state) { updateCurrentListview(); DataTable pdfdatatable = new DataTable(); pdfdatatable.Columns.Add("FileName"); pdfdatatable.Columns.Add("FileSize"); pdfdatatable.Columns.Add("FileState"); int datatableindex = 0; foreach (var fileName in fileNames) { string file_all = fileName; FileInfo f = new FileInfo(file_all); string file_size = (((float)f.Length) / 1024).ToString() + " K"; pdfdatatable.Rows.Add(f.Name, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]); datatableindex++; } PDFDataTable = pdfdatatable; HomePageBatchProcessingDialogModel.FilePaths = fileNames; } /// /// 更新Currentlistview显示 /// state 状态显示字符串 /// public void updateCurrentListview(string state = "wait") { if (fileNames.Count >= FileNameNumber) { for (int i = 0; fileNames.Count - FileNameNumber > i; i++) { PDFCurrentDataTable.Rows.Add(state); } } else { Reverseorder(ref fileNamesView); foreach (int filenamesview in fileNamesView) { PDFCurrentDataTable.Rows.RemoveAt(filenamesview); } } } /// /// 逆序int类型集合 /// public void Reverseorder(ref List Numbers) { Numbers = Numbers.OrderBy(a => a).ToList(); Numbers.Reverse(); } /// /// 打开文件PDF /// public void openfiledialog() { foreach (int filenamesview in fileNamesView) { System.Diagnostics.Process.Start("Explorer", "/select," + fileNames[filenamesview]); } } /// /// 删除文件PDF /// public void removepdffile() { FileNameNumber = fileNames.Count; Reverseorder(ref fileNamesView); foreach (int filenamesview in fileNamesView) { //Trace.WriteLine(filenamesview); fileNames.Remove(fileNames[filenamesview]); } if (fileNames.Count < 1) { SetOptionsRemoveGridIsEnabled = "False"; AddFileVisibility = Visibility.Visible; } updateListview("wait"); } public void removepdffile(int index) { PDFCurrentDataTable.Rows.RemoveAt(index); fileNames.Remove(fileNames[index]); if (fileNames.Count < 1) { SetOptionsRemoveGridIsEnabled = "False"; AddFileVisibility = Visibility.Visible; } updateListview("wait"); } public void PDFFileCount() { if (fileNames.Count == 0) { SetOptionsRemoveGridIsEnabled = "False"; AddFileVisibility = Visibility.Visible; } else { SetOptionsRemoveGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; } } #endregion #region 构架行为 public void OnNavigatedTo(NavigationContext navigationContext) { List filepath = new List(); navigationContext.Parameters.TryGetValue>(ParameterNames.FilePath, out filepath); if (filepath != null) { fileNames = filepath; PDFFileCount(); updateListview("wait"); } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } #endregion } }