123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- 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 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 HomePageCompressDialogViewModel : BindableBase, INavigationAware
- {
- #region 参数和属性
- private List<string> fileNames;
- public IDialogService dialogs;
- private CPDFDocument tempDocument;
- public List<int> fileNamesView = new List<int>();
- private int fileNamesIndex = 0;
- private int FileNameNumber = 0;
- private IntPtr compressingIntpr = IntPtr.Zero;
- private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
- 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 compressGridIsEnabled="True";
- public string CompressGridIsEnabled
- {
- get
- {
- return compressGridIsEnabled;
- }
- set
- {
- SetProperty(ref compressGridIsEnabled, value);
- }
- }
- private string setCompressGridIsEnabled = "True";
- public string SetCompressGridIsEnabled
- {
- get
- {
- return setCompressGridIsEnabled;
- }
- set
- {
- SetProperty(ref setCompressGridIsEnabled, 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();
- }
- }
- #endregion
- #region 委托声明
- public DelegateCommand ADDPDFCommand { get; set; }
- public DelegateCommand ADDOpenedPDFCommand { get; set; }
- public DelegateCommand RemovePDFFileCommand { get; set; }
- public DelegateCommand ADDPDFFilesCommand { get; set; }
- public DelegateCommand CompressCommand { get; set; }
- public DelegateCommand<string> RadioButtonCommand{ get; set; }
- #endregion
- public HomePageCompressDialogViewModel(IDialogService dialogs)
- {
- PDFCurrentDataTable.Columns.Add("FileState");
- PDFDataTable.Columns.Add("FileName");
- PDFDataTable.Columns.Add("FileSize");
- PDFDataTable.Columns.Add("FileState");
- PDFDataTable.Columns.Add("MaxPageRange");
- ADDPDFCommand = new DelegateCommand(addpicture);
- ADDOpenedPDFCommand = new DelegateCommand(addOpenedPDFFiles);
- ADDPDFFilesCommand = new DelegateCommand(addpicturefiles);
- RemovePDFFileCommand = new DelegateCommand(removepdffile);
- CompressCommand = new DelegateCommand(compress);
- RadioButtonCommand = new DelegateCommand<string>(RadioButton);
- this.dialogs = dialogs;
- }
- #region 逻辑函数
- private void RadioButton(string sender) {
- if (sender != "") {
- switch (sender) {
- case "LargeQualityRadioBtn":
- homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusLarge;
- break;
- case "StandardQualityRadioBtn":
- homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusStandard;
- break;
- case "LittleQualityRadioBtn":
- homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusLittle;
- break;
- case "MicroQualityRadioBtn":
- homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusMicro;
- break;
- default:
- homePageCompressDialogModel.CompressQuality = HomePageCompressDialogModel.EnumQualityLevel.StatusDefault;
- break;
- }
- }
- }
- private int GetIndex(int pageindex)
- {
- PDFDataTable.Rows[fileNamesIndex]["FileState"]=pageindex.ToString();
- return 0;
- }
- private async void compress() {
- FolderBrowserDialog dlg = new FolderBrowserDialog();
- string saveSelectedPath = "";
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- saveSelectedPath = dlg.SelectedPath.Trim();
- saveSelectedPath = saveSelectedPath + "\\";
- }
- else { return; }
- HomePageBatchProcessingDialogModel.closeDialog = true;
- CompressGridIsEnabled = "False";
- //GC.KeepAlive(indexDelegate);
- fileNamesIndex = 0;
- foreach (var filename in fileNames)
- {
- 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<string>(ParameterNames.PassWord) != null)
- {
- document.UnlockWithPassword (e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString());
- }
- }
- });
- if (document.IsLocked)
- {
- //未成功解密文档时,释放Document对象,返回
- PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error";
- fileNamesIndex++;
- continue;
- }
- }
- FileInfo fileinfo = new FileInfo(filename);
- string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K";
- indexDelegate += GetIndex;
- compressingIntpr = document.CompressFile_Init(((float)homePageCompressDialogModel.CompressQuality), indexDelegate);
- tempDocument = document;
- await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, CommonHelper.CreateFilePath(saveSelectedPath + document.FileName + "compress.pdf")); });
- PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
- PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete";
- fileNamesIndex++;
- document.Release();
- }
- CompressGridIsEnabled = "True";
- HomePageBatchProcessingDialogModel.closeDialog = false;
- }
- #endregion
- #region 批量处理逻辑函数
- /// <summary>
- /// 添加PDF文件
- /// </summary>
- 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);
- SetCompressGridIsEnabled = "True";
- AddFileVisibility = Visibility.Collapsed;
- updateListview("wait");
- }
- }
- public void addPDFFiles(string filename)
- {
- FileNameNumber = fileNames.Count;
- fileNames.Add(filename);
- RemoveExcess(ref fileNames);
- SetCompressGridIsEnabled = "True";
- AddFileVisibility = Visibility.Collapsed;
- updateListview("wait");
- }
- /// <summary>
- /// 添加已打开PDF文件
- /// </summary>
- public void addOpenedPDFFiles()
- {
- FileNameNumber = fileNames.Count;
- foreach (var filename in App.OpenedFileList)
- {
- fileNames.Add(filename);
- }
- RemoveExcess(ref fileNames);
- SetCompressGridIsEnabled = "True";
- AddFileVisibility = Visibility.Collapsed;
- updateListview("wait");
- }
- /// <summary>
- /// 删除重复的文件
- /// </summary>
- public void RemoveExcess(ref List<string> Filenames)
- {
- List<string> filenames = new List<string>();
- foreach (var fileName in Filenames)
- {
- if (!filenames.Contains(fileName))
- {
- filenames.Add(fileName);
- }
- }
- Filenames.Clear();
- Filenames = filenames;
- }
- /// <summary>
- /// 添加PDF文件夹
- /// </summary>
- 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");
- SetCompressGridIsEnabled = "True";
- AddFileVisibility = Visibility.Collapsed;
- }
-
- }
- /// <summary>
- /// 更新listview显示
- /// state 状态显示字符串
- /// </summary>
- public void updateListview(string state)
- {
- updateCurrentListview();
- DataTable pdfdatatable = new DataTable();
- pdfdatatable.Columns.Add("FileName");
- pdfdatatable.Columns.Add("FileSize");
- pdfdatatable.Columns.Add("FileState");
- pdfdatatable.Columns.Add("MaxPageRange");
- 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";
- int pagecount = CPDFDocument.InitWithFilePath(fileName).PageCount;
- pdfdatatable.Rows.Add(f.Name, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"],pagecount);
- datatableindex++;
- }
- PDFDataTable = pdfdatatable;
- HomePageBatchProcessingDialogModel.FilePaths = fileNames;
- }
- /// <summary>
- /// 更新Currentlistview显示
- /// state 状态显示字符串
- /// </summary>
- 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);
- }
- }
- }
- /// <summary>
- /// 逆序int类型集合
- /// </summary>
- public void Reverseorder(ref List<int> Numbers)
- {
- Numbers = Numbers.OrderBy(a => a).ToList();
- Numbers.Reverse();
- }
- /// <summary>
- /// 打开文件PDF
- /// </summary>
- public void openfiledialog()
- {
- foreach (int filenamesview in fileNamesView)
- {
- System.Diagnostics.Process.Start("Explorer", "/select," + fileNames[filenamesview]);
- }
- }
- /// <summary>
- /// 删除文件PDF
- /// </summary>
- 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)
- {
- SetCompressGridIsEnabled = "False";
- AddFileVisibility = Visibility.Visible;
- }
- updateListview("wait");
- }
- public void removepdffile(int index)
- {
- PDFCurrentDataTable.Rows.RemoveAt(index);
- fileNames.Remove(fileNames[index]);
- if (fileNames.Count < 1)
- {
- SetCompressGridIsEnabled = "False";
- AddFileVisibility = Visibility.Visible;
- }
- updateListview("wait");
- }
- public void PDFFileCount()
- {
- if (fileNames.Count == 0)
- {
- SetCompressGridIsEnabled = "False";
- AddFileVisibility = Visibility.Visible;
- }
- else
- {
- SetCompressGridIsEnabled = "True";
- AddFileVisibility = Visibility.Collapsed;
- }
- }
- #endregion
- #region 构架行为
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- List<string> filepath = new List<string>();
- navigationContext.Parameters.TryGetValue<List<string>>(ParameterNames.FilePath, out filepath);
- if (filepath != null)
- {
- fileNames = filepath;
- PDFFileCount();
- updateListview("wait");
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- if(tempDocument!=null)
- tempDocument.CompressFile_Cancel(compressingIntpr);
- }
- #endregion
- }
- }
|