MergeDialogViewModel.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Dropbox.Api.TeamLog;
  5. using Microsoft.Office.Interop.Word;
  6. using Microsoft.Win32;
  7. using PDF_Master.CustomControl;
  8. using PDF_Master.EventAggregators;
  9. using PDF_Master.Helper;
  10. using PDF_Master.Model;
  11. using PDF_Master.Model.Dialog.ToolsDialogs;
  12. using PDF_Master.Properties;
  13. using PDF_Master.Views.Dialog;
  14. using Prism.Commands;
  15. using Prism.Events;
  16. using Prism.Mvvm;
  17. using Prism.Services.Dialogs;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Collections.ObjectModel;
  21. using System.Diagnostics;
  22. using System.IO;
  23. using System.Linq;
  24. using System.Reflection;
  25. using System.Text;
  26. using System.Threading.Tasks;
  27. using System.Windows;
  28. using System.Windows.Controls;
  29. using System.Windows.Media;
  30. using System.Windows.Media.Imaging;
  31. using static Dropbox.Api.TeamLog.PaperDownloadFormat;
  32. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  33. namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
  34. {
  35. internal class MergeDialogViewModel : BindableBase, IDialogAware
  36. {
  37. // Fix:存储当前程序加载的文档的路径和tag,
  38. //如果路径相同则用tag里的密码解锁,
  39. //解锁失败再另行提权
  40. private string CurrentFilePath = string.Empty;
  41. private string currentLoadedPassword = string.Empty;
  42. private IEventAggregator eventAggregator;
  43. private enum PageSizeType
  44. {
  45. kDefault = 0,
  46. A4 = 1,
  47. A3 = 2,
  48. Letter = 3,
  49. Legal = 4,
  50. Customized = 5,
  51. }
  52. #region 框架内容
  53. public string Title => "";
  54. public event Action<IDialogResult> RequestClose;
  55. public bool CanCloseDialog()
  56. {
  57. return true;
  58. }
  59. public void OnDialogClosed()
  60. {
  61. }
  62. public void OnDialogOpened(IDialogParameters parameters)
  63. {
  64. CPDFViewer pdfViewer = null;
  65. if (parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer) && pdfViewer != null && pdfViewer.Document != null)
  66. {
  67. VerifyPasswordResult condition = SecurityHelper.VerifyPasswordByPasswordKind(pdfViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  68. if (condition.IsDiscryptied)
  69. {
  70. if (!string.IsNullOrEmpty(condition.Password))
  71. {
  72. if (pdfViewer.Document.UnlockWithPassword(condition.Password))
  73. {
  74. pdfViewer.Document.CheckOwnerPassword(condition.Password);
  75. }
  76. }
  77. string unicode;
  78. if (parameters.TryGetValue<string>(ParameterNames.Unicode, out unicode))
  79. {
  80. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusNone, Unicode = unicode });
  81. }
  82. }
  83. currentLoadedPassword = pdfViewer.Document.Password;
  84. if (Settings.Default.UserDate.subscribestatus != 1)
  85. {
  86. LimitationsConvertVisible = Visibility.Visible;
  87. }
  88. }
  89. if (pdfViewer != null && pdfViewer.Document != null)
  90. {
  91. CurrentFilePath = pdfViewer.Document.FilePath;
  92. List<string> list = new List<string>();
  93. list.Add(CurrentFilePath);
  94. AddFiles(list.ToArray());
  95. }
  96. string[] Paths;
  97. parameters.TryGetValue<string[]>(ParameterNames.FilePath, out Paths);
  98. if (Paths != null)
  99. {
  100. AddFiles(Paths);
  101. }
  102. }
  103. #endregion 框架内容
  104. #region 定义与初始化
  105. public ObservableCollection<MergeObject> MergeObjectlist { get; set; }
  106. public DelegateCommand CancelCommand { get; set; }
  107. public DelegateCommand<object> PreviewCancelDownCommand { get; set; }
  108. public DelegateCommand MergeCommand { get; set; }
  109. public DelegateCommand<object> AddFilesCommand { get; set; }
  110. public DelegateCommand ClearCommand { get; set; }
  111. public DelegateCommand UnlockMouseDownCommand { get; set; }
  112. public DelegateCommand<object> SetPageSizeTypeCommand { get; set; }
  113. public DelegateCommand CancelAddFileCommand { get; set; }
  114. public IDialogService dialogs;
  115. private PageSizeType pageSizeType = PageSizeType.kDefault;
  116. private string inputWidth;
  117. public string InputWidth
  118. {
  119. get { return inputWidth; }
  120. set
  121. {
  122. SetProperty(ref inputWidth, value);
  123. }
  124. }
  125. private Visibility processVisible = Visibility.Collapsed;
  126. /// <summary>
  127. /// 是否显示进度条
  128. /// </summary>
  129. public Visibility ProcessVisible
  130. {
  131. get { return processVisible; }
  132. set
  133. {
  134. SetProperty(ref processVisible, value);
  135. }
  136. }
  137. private bool _AddOpenFileIsEn = false;
  138. /// <summary>
  139. /// 添加已打开文件是否置灰
  140. /// </summary>
  141. public bool AddOpenFileIsEn
  142. {
  143. get { return _AddOpenFileIsEn; }
  144. set
  145. {
  146. SetProperty(ref _AddOpenFileIsEn, value);
  147. }
  148. }
  149. private string inputHeight;
  150. public string InputHeight
  151. {
  152. get { return inputHeight; }
  153. set
  154. {
  155. SetProperty(ref inputHeight, value);
  156. }
  157. }
  158. private double maxValue;
  159. /// <summary>
  160. /// 最大文件数 用于显示进度条
  161. /// </summary>
  162. public double MaxValue
  163. {
  164. get { return maxValue; }
  165. set
  166. {
  167. SetProperty(ref maxValue, value);
  168. }
  169. }
  170. private double currentvalue;
  171. /// <summary>
  172. /// 当前进度值
  173. /// </summary>
  174. public double CurrentValue
  175. {
  176. get { return currentvalue; }
  177. set
  178. {
  179. SetProperty(ref currentvalue, value);
  180. }
  181. }
  182. /// <summary>
  183. /// 订阅按钮
  184. /// </summary>
  185. private string _TextUpgrade;
  186. public string TextUpgrade
  187. {
  188. get { return _TextUpgrade; }
  189. set { _TextUpgrade = value; }
  190. }
  191. /// <summary>
  192. /// 是否显示限制字段
  193. /// </summary>
  194. private Visibility limitationsConvertVisible = Visibility.Hidden;
  195. public Visibility LimitationsConvertVisible
  196. {
  197. get { return limitationsConvertVisible; }
  198. set
  199. {
  200. SetProperty(ref limitationsConvertVisible, value);
  201. }
  202. }
  203. /// <summary>
  204. /// 是否取消添加文件
  205. /// </summary>
  206. private bool CancelAddFiles = false;
  207. public MergeDialogViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
  208. {
  209. IntString();
  210. this.eventAggregator = eventAggregator;
  211. dialogs = dialogService;
  212. MergeObjectlist = new ObservableCollection<MergeObject>();
  213. CancelCommand = new DelegateCommand(Cancel);
  214. PreviewCancelDownCommand = new DelegateCommand<object>(PreviewCancelDown);
  215. AddFilesCommand = new DelegateCommand<object>(ButtonAddFiles);
  216. MergeCommand = new DelegateCommand(Merge);
  217. ClearCommand = new DelegateCommand(Clear);
  218. SetPageSizeTypeCommand = new DelegateCommand<object>(SetPageSizeType);
  219. CancelAddFileCommand = new DelegateCommand(CancelAddFileOpention);
  220. UnlockMouseDownCommand = new DelegateCommand(UnlockMouseDown);
  221. }
  222. /// <summary>
  223. /// 多语
  224. /// </summary>
  225. private void IntString()
  226. {
  227. TextUpgrade = App.ServiceLoader.GetString("TextUpgrade");
  228. }
  229. #endregion 定义与初始化
  230. #region 私有方法
  231. private void UnlockMouseDown()
  232. {
  233. ConverterHelper.convertUnlock();
  234. }
  235. private void CancelAddFileOpention()
  236. {
  237. CancelAddFiles = true;
  238. }
  239. private void UpDataMergeObjectIndex()
  240. {
  241. for (int i = 0; i < MergeObjectlist.Count; i++)
  242. {
  243. MergeObjectlist[i].ItemIndex = i + 1;
  244. }
  245. AddOpenFileIsEnChange();
  246. }
  247. private void Cancel()
  248. {
  249. Clear();
  250. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  251. }
  252. /// <summary>
  253. /// 点击取消页面范围控件使失去焦点不生效
  254. /// </summary>
  255. private void PreviewCancelDown(object e)
  256. {
  257. var listView = e as ListView;
  258. if (listView != null)
  259. {
  260. if (listView.ItemsSource != null)
  261. {
  262. foreach (var item in listView.ItemsSource)
  263. {
  264. var pagerange = listView.ItemContainerGenerator.ContainerFromItem(item) as ListViewItem;
  265. if (pagerange != null)
  266. {
  267. var pagerangecombobox = CommonHelper.FindVisualChild<WritableComboBox>(pagerange);
  268. if (pagerangecombobox != null)
  269. pagerangecombobox.IsloseFocus = false;
  270. }
  271. }
  272. }
  273. }
  274. }
  275. public void ButtonAddFiles(object data)
  276. {
  277. int index = Convert.ToInt32(data);
  278. CancelAddFiles = false;
  279. switch (index)
  280. {
  281. case 0:
  282. //打开文件
  283. AddFiles(OpenFile());
  284. break;
  285. case 1:
  286. //打开文件夹
  287. AddFiles(OpenFileFolder());
  288. break;
  289. case 2:
  290. //打开当前文件
  291. List<string> list = new List<string>();
  292. for (int i = 0; i < App.OpenedFileList.Count; i++)
  293. {
  294. list.Add(App.OpenedFileList[i]);
  295. }
  296. AddFiles(list.ToArray());
  297. break;
  298. default:
  299. break;
  300. }
  301. }
  302. /// <summary>
  303. /// 改变添加已打开文件按钮置灰状态
  304. /// </summary>
  305. private void AddOpenFileIsEnChange()
  306. {
  307. AddOpenFileIsEn = false;
  308. if (MergeObjectlist.Count > App.OpenedFileList.Count)
  309. {
  310. AddOpenFileIsEn = true;
  311. return;
  312. }
  313. for (int i = 0; i < App.OpenedFileList.Count; i++)
  314. {
  315. bool found = false;
  316. for (int j = 0; j < MergeObjectlist.Count; j++)
  317. {
  318. if (App.OpenedFileList[i] == MergeObjectlist[j].FilePath)
  319. {
  320. found = true;
  321. break;
  322. }
  323. }
  324. if (!found)
  325. {
  326. AddOpenFileIsEn = true;
  327. break;
  328. }
  329. }
  330. }
  331. private string[] OpenFileFolder()
  332. {
  333. System.Windows.Forms.FolderBrowserDialog openFile = new System.Windows.Forms.FolderBrowserDialog();
  334. if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
  335. {
  336. return null;
  337. }
  338. DirectoryInfo tempfolder = new DirectoryInfo(openFile.SelectedPath);
  339. FileInfo[] fileInfos = tempfolder.GetFiles();
  340. List<string> list = new List<string>();
  341. foreach (FileInfo item in fileInfos)
  342. {
  343. list.Add(item.FullName);
  344. }
  345. DirectoryInfo[] directoryInfos = tempfolder.GetDirectories();
  346. foreach (DirectoryInfo fileInfo in directoryInfos)
  347. {
  348. FileInfo[] fileinfo = fileInfo.GetFiles();
  349. foreach (FileInfo item in fileinfo)
  350. {
  351. list.Add(item.FullName);
  352. }
  353. }
  354. return list.ToArray();
  355. }
  356. private string[] OpenFile()
  357. {
  358. OpenFileDialog openFile = new OpenFileDialog();
  359. openFile.Multiselect = true;
  360. openFile.Filter = Properties.Resources.imageex.ToLower() + "|*";
  361. if (openFile.ShowDialog() == false)
  362. {
  363. return null;
  364. }
  365. return openFile.FileNames;
  366. }
  367. private void Merge()
  368. {
  369. bool result = true;
  370. CPDFDocument SaveDoc = CPDFDocument.CreateDocument();
  371. int sum = MergeObjectlist.Count;
  372. if (Settings.Default.UserDate.subscribestatus != 1)
  373. {
  374. sum = 2;
  375. }
  376. for (int i = 0; i < sum; i++)
  377. {
  378. List<int> listnum = new List<int>();
  379. if (MergeObjectlist[i].SetPageRange == null || MergeObjectlist[i].SetPageRange.Count == 0)
  380. {
  381. if (MergeObjectlist[i].SDKPageCount == 0)
  382. {
  383. AlertsMessage alertsMessage = new AlertsMessage();
  384. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  385. return;
  386. }
  387. List<int> pageRangeList = new List<int>();
  388. for (int page1 = 0; page1 < MergeObjectlist[i].SDKPageCount; page1++)
  389. {
  390. pageRangeList.Add(page1);
  391. }
  392. MergeObjectlist[i].SetPageRange = pageRangeList;
  393. }
  394. }
  395. for (int i = 0; i < sum; i++)
  396. {
  397. if (MergeObjectlist[i].SetPageRange == null)
  398. {
  399. AlertsMessage alertsMessage = new AlertsMessage();
  400. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  401. }
  402. //图片
  403. if (Path.GetExtension(MergeObjectlist[i].FilePath).Trim().ToLower() != ".pdf")
  404. {
  405. if (Path.GetExtension(MergeObjectlist[i].FilePath).Trim().ToLower() == ".gif")
  406. {//GIF下面方法产生虚影改成与图片转PDF一致
  407. try
  408. {
  409. System.Drawing.Image img = System.Drawing.Image.FromFile(MergeObjectlist[i].FilePath);
  410. string tempFileName = Path.GetTempPath() + "pngtemp.jpg";
  411. if (!PictureConverter.SaveJpeg(MergeObjectlist[i].FilePath, tempFileName))
  412. {
  413. MessageBoxEx.Show("图片格式有问题");
  414. }
  415. // 之前插入位置为零点导致插入为第一页,改为尾插 2023/5/24
  416. result = SaveDoc.InsertPage(SaveDoc.PageCount, img.Width, img.Height, tempFileName);
  417. try { if (File.Exists(tempFileName)) File.Delete(tempFileName); }
  418. catch
  419. { }
  420. if (!result)
  421. {
  422. SaveDoc.Release();
  423. return;
  424. }
  425. continue;
  426. }
  427. catch
  428. {
  429. continue;
  430. }
  431. }
  432. else
  433. {
  434. try
  435. {
  436. BitmapSource frame = MergeObjectlist[i].DocThumbnail;
  437. byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  438. if (frame.Format != PixelFormats.Bgra32)
  439. {
  440. FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
  441. covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  442. }
  443. else
  444. {
  445. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  446. }
  447. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  448. // 之前插入位置为零点导致插入为第一页,改为尾插 2023/5/24
  449. result = SaveDoc.InsertPage(SaveDoc.PageCount, frame.PixelWidth, frame.PixelHeight, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
  450. if (!result)
  451. {
  452. SaveDoc.Release();
  453. return;
  454. }
  455. continue;
  456. }
  457. catch
  458. {
  459. continue;
  460. }
  461. }
  462. }
  463. else
  464. {
  465. CPDFDocument tempDoc = CPDFDocument.InitWithFilePath(MergeObjectlist[i].FilePath);
  466. if (!string.IsNullOrEmpty(MergeObjectlist[i].Password))
  467. {
  468. tempDoc.UnlockWithPassword(MergeObjectlist[i].Password);
  469. }
  470. if (!string.IsNullOrEmpty(MergeObjectlist[i].SetPageRangeStr))
  471. {
  472. result = SaveDoc.ImportPages(tempDoc, MergeObjectlist[i].SetPageRangeStr);
  473. }
  474. else
  475. {
  476. //下拉框控件传出来的list是页面索引集合,
  477. //此处传给SDK的合并接口的是页码集合,因此需要将集合项遍历+1,只有合并接口是接收页码集合,其余接口是接受页面索引集合
  478. List<int> pageIndexs = new List<int>();
  479. for (int j = 0; j < MergeObjectlist[i].SetPageRange.Count; j++)
  480. {
  481. pageIndexs.Add(MergeObjectlist[i].SetPageRange[j] + 1);
  482. }
  483. result = SaveDoc.ImportPages(tempDoc, CommonHelper.GetPageParmFromList(pageIndexs));
  484. }
  485. if (!result)
  486. {
  487. SaveDoc.Release();
  488. tempDoc.Release();
  489. return;
  490. }
  491. tempDoc.Release();
  492. }
  493. }
  494. SaveFileDialog saveFileDialog = new SaveFileDialog();
  495. saveFileDialog.FileName = "Untitle";
  496. saveFileDialog.Filter = "PDF|*.pdf;";
  497. if (saveFileDialog.ShowDialog() == false)
  498. {
  499. return;
  500. }
  501. string path = saveFileDialog.FileName;
  502. System.Windows.Rect rect = new System.Windows.Rect();
  503. switch (pageSizeType)
  504. {
  505. case PageSizeType.kDefault:
  506. break;
  507. case PageSizeType.A4:
  508. rect.Width = CommonHelper.GetPageSizeFomrUnit(210);
  509. rect.Height = CommonHelper.GetPageSizeFomrUnit(297);
  510. break;
  511. case PageSizeType.A3:
  512. rect.Width = CommonHelper.GetPageSizeFomrUnit(297);
  513. rect.Height = CommonHelper.GetPageSizeFomrUnit(420);
  514. break;
  515. case PageSizeType.Letter:
  516. rect.Width = CommonHelper.GetPageSizeFomrUnit(216);
  517. rect.Height = CommonHelper.GetPageSizeFomrUnit(279);
  518. break;
  519. case PageSizeType.Legal:
  520. rect.Width = CommonHelper.GetPageSizeFomrUnit(216);
  521. rect.Height = CommonHelper.GetPageSizeFomrUnit(356);
  522. break;
  523. case PageSizeType.Customized:
  524. if (!string.IsNullOrEmpty(InputWidth) && !string.IsNullOrEmpty(InputHeight))
  525. {
  526. rect.Width = CommonHelper.GetPageSizeFomrUnit(Convert.ToInt32(InputWidth));
  527. rect.Height = CommonHelper.GetPageSizeFomrUnit(Convert.ToInt32(InputHeight));
  528. }
  529. else
  530. {
  531. rect.Width = CommonHelper.GetPageSizeFomrUnit(595);
  532. rect.Height = CommonHelper.GetPageSizeFomrUnit(841);
  533. }
  534. break;
  535. default:
  536. break;
  537. }
  538. if (rect.Width > 0 && rect.Height > 0)
  539. {
  540. //裁剪
  541. for (int i = 0; i < SaveDoc.PageCount; i++)
  542. {
  543. CPDFPage page = SaveDoc.PageAtIndex(i);
  544. page.CropPage(CPDFDisplayBox.MediaBox, rect);
  545. }
  546. }
  547. bool saveResult = SaveDoc.WriteToFilePath(path);
  548. SaveDoc.Release();
  549. CommonHelper.ShowFileBrowser(path);
  550. DialogParameters valuePairs = new DialogParameters();
  551. valuePairs.Add(ParameterNames.FilePath, path);
  552. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  553. }
  554. private void Clear()
  555. {
  556. MergeObjectlist.Clear();
  557. AddOpenFileIsEn = false;
  558. }
  559. private void SetPageSizeType(object button)
  560. {
  561. if (button is RadioButton)
  562. {
  563. pageSizeType = (PageSizeType)Convert.ToInt32((button as RadioButton).Tag);
  564. }
  565. }
  566. #endregion 私有方法
  567. #region 公开方法
  568. public void DeleteItem(MergeObject merge)
  569. {
  570. MergeObjectlist.Remove(merge);
  571. UpDataMergeObjectIndex();
  572. }
  573. /// <summary>
  574. /// 拖拽插入
  575. /// </summary>
  576. public void MoveMerge(MergeObject targetNode, MergeObject soureNode)
  577. {
  578. int targetindex = MergeObjectlist.IndexOf(targetNode);
  579. MergeObjectlist.Remove(soureNode);
  580. if (targetNode.IsForward)
  581. {
  582. if (targetindex + 1 < MergeObjectlist.Count)
  583. {
  584. MergeObjectlist.Insert(targetindex + 1, soureNode);
  585. }
  586. else
  587. {
  588. MergeObjectlist.Add(soureNode);
  589. }
  590. }
  591. else
  592. {
  593. MergeObjectlist.Insert(targetindex, soureNode);
  594. }
  595. UpDataMergeObjectIndex();
  596. }
  597. public async void AddFiles(string[] FilePath)
  598. {
  599. if (FilePath == null)
  600. {
  601. return;
  602. }
  603. // 免费用户 限制只选两个文件的逻辑
  604. //if (Settings.Default.UserDate.subscribestatus != 1 && MergeObjectlist.Count >= 2)
  605. //{
  606. // return;
  607. //}
  608. bool showDialog = false;
  609. ProcessVisible = Visibility.Visible;
  610. MaxValue = FilePath.Length;
  611. // 免费用户 限制只选两个文件的逻辑
  612. //int sum = SetFilesLength(FilePath);
  613. for (int i = 0; i < FilePath.Length; i++)
  614. {
  615. //加入异步语句,防止UI卡死,显示加载进度条
  616. await System.Threading.Tasks.Task.Delay(1);
  617. //如果中断了文件添加,则取消循环
  618. if (CancelAddFiles)
  619. {
  620. break;
  621. }
  622. MergeObject mergeObject = new MergeObject();
  623. mergeObject.FilePath = FilePath[i];
  624. //通过路径判断文件是否已添加
  625. bool IsExists = false;
  626. for (int j = 0; j < MergeObjectlist.Count; j++)
  627. {
  628. if (MergeObjectlist[j].FilePath == mergeObject.FilePath)
  629. {
  630. IsExists = true;
  631. }
  632. }
  633. if (IsExists)
  634. {
  635. continue;
  636. }
  637. string FileType = Path.GetExtension(mergeObject.FilePath).Trim().ToLower();
  638. if (string.IsNullOrEmpty(FileType))
  639. {
  640. showDialog = true;
  641. //获取不到文件类型
  642. continue;
  643. }
  644. if (FileType != ".pdf")
  645. {
  646. string imagetype = "*" + FileType;
  647. string[] x = Properties.Resources.imageex.ToLower().Split(';');
  648. List<string> list = x.ToList();
  649. int imageindex = list.IndexOf(imagetype);
  650. if (imageindex < 0)
  651. {
  652. showDialog = true;
  653. //图片格式不支持
  654. continue;
  655. };
  656. mergeObject.DocName = Path.GetFileName(mergeObject.FilePath);
  657. mergeObject.DocPageCount = 1 + " " + App.MainPageLoader.GetString("Merge_ItemPages");
  658. mergeObject.SDKPageCount = 1;
  659. mergeObject.SetPageRange = new List<int>() { 0 };
  660. mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
  661. try
  662. {
  663. mergeObject.DocThumbnail = new BitmapImage(new Uri(mergeObject.FilePath));
  664. }
  665. catch (Exception)
  666. {
  667. showDialog = true;
  668. //解码错误
  669. continue;
  670. }
  671. }
  672. else
  673. {
  674. CPDFDocument doc = CPDFDocument.InitWithFilePath(mergeObject.FilePath);
  675. if (doc == null)
  676. {
  677. showDialog = true;
  678. //PDF打开失败
  679. continue;
  680. }
  681. ///Fix:
  682. ///情况分为:
  683. ///开启路径是当前路径:
  684. ///已解锁或本身就无密码->直接加入
  685. ///未解锁-> release
  686. ///
  687. /// 不是当前路径
  688. ///解锁或取消->
  689. ///
  690. if (CurrentFilePath == doc.FilePath)
  691. {
  692. if ((!(!doc.IsLocked && (SecurityHelper.CheckHaveAllPermissions(doc)))) &&
  693. (!(!string.IsNullOrEmpty(currentLoadedPassword) &&
  694. doc.UnlockWithPassword(currentLoadedPassword) &&
  695. (doc.CheckOwnerPassword(currentLoadedPassword) || SecurityHelper.CheckHaveAllPermissions(doc)))))
  696. {
  697. doc.Release();
  698. continue;
  699. }
  700. //添加的第一个文档有权限密码时,保存密码,避免合并时因没有密码导致合并失败
  701. //表现为在首页打开一个带权限的文档进入合并,点击合并按钮没有反应
  702. if (!string.IsNullOrEmpty(currentLoadedPassword))
  703. {
  704. mergeObject.Password = currentLoadedPassword;
  705. }
  706. }
  707. else
  708. {
  709. VerifyPasswordResult condition = new VerifyPasswordResult();
  710. App.Current.Dispatcher.Invoke(() =>
  711. {
  712. //切换一下UI线程,避免解密弹窗异常
  713. condition = SecurityHelper.VerifyPasswordByPasswordKind(doc, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  714. });
  715. if (condition.IsDiscryptied)
  716. {
  717. if (condition.Password != null)
  718. {
  719. mergeObject.Password = condition.Password;
  720. if (doc.UnlockWithPassword(condition.Password) && doc.CheckOwnerPassword(condition.Password))
  721. {
  722. }
  723. }
  724. }
  725. else
  726. {
  727. doc.Release();
  728. continue;
  729. }
  730. }
  731. mergeObject.DocName = doc.FileName;
  732. if (doc.PageCount > 1)
  733. {
  734. mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPages");
  735. mergeObject.IsEvenPageIsEnabled = true;
  736. }
  737. else
  738. {
  739. mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPage");
  740. mergeObject.IsEvenPageIsEnabled = false;
  741. }
  742. mergeObject.SDKPageCount = doc.PageCount;
  743. mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
  744. //获取第一页缩略图
  745. CPDFPage page = doc.PageAtIndex(0);
  746. Size size = doc.GetPageSize(0);
  747. byte[] bmpData = new byte[(int)(size.Width * size.Height * 4)];
  748. WriteableBitmap WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null);
  749. page.RenderPageBitmap(0, 0, (int)size.Width, (int)size.Height, 0xFFFFFFFF, bmpData, 1);
  750. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0);
  751. WirteBitmap.Freeze();
  752. mergeObject.DocThumbnail = WirteBitmap;
  753. doc.Release();
  754. }
  755. App.Current.Dispatcher.Invoke(() =>
  756. {
  757. MergeObjectlist.Add(mergeObject);
  758. });
  759. CurrentValue = i;
  760. UpDataMergeObjectIndex();
  761. }
  762. ProcessVisible = Visibility.Collapsed;
  763. if (showDialog)
  764. {
  765. AlertsMessage alertsMessage = new AlertsMessage();
  766. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
  767. if (alertsMessage.result == ContentResult.Ok)
  768. {
  769. }
  770. }
  771. }
  772. /// <summary>
  773. /// 未购买的用户 设置文件数量
  774. /// </summary>
  775. /// <param name="filePath"></param>
  776. /// <returns></returns>
  777. private int SetFilesLength(string[] filePath)
  778. {
  779. int sum = filePath.Length;
  780. if (Settings.Default.UserDate.subscribestatus != 1 && sum >= 2)
  781. {
  782. if (MergeObjectlist.Count >= 1)
  783. {
  784. sum = 1;
  785. }
  786. else
  787. {
  788. sum = 2;
  789. }
  790. }
  791. return sum;
  792. }
  793. #endregion 公开方法
  794. }
  795. }