MergeDialogViewModel.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. 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. 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. AlertsMessage alertsMessage = new AlertsMessage();
  383. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  384. return;
  385. }
  386. List<int> pageRangeList = new List<int>();
  387. for (int page1 = 0; page1 < MergeObjectlist[i].SDKPageCount; page1++)
  388. {
  389. pageRangeList.Add(page1);
  390. }
  391. MergeObjectlist[i].SetPageRange = pageRangeList;
  392. }
  393. }
  394. for (int i = 0; i < sum; i++)
  395. {
  396. if (MergeObjectlist[i].SetPageRange == null)
  397. {
  398. AlertsMessage alertsMessage = new AlertsMessage();
  399. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  400. }
  401. //图片
  402. if (Path.GetExtension(MergeObjectlist[i].FilePath).Trim().ToLower() != ".pdf")
  403. {
  404. if (Path.GetExtension(MergeObjectlist[i].FilePath).Trim().ToLower() == ".gif")
  405. {//GIF下面方法产生虚影改成与图片转PDF一致
  406. try
  407. {
  408. System.Drawing.Image img = System.Drawing.Image.FromFile(MergeObjectlist[i].FilePath);
  409. string tempFileName = Path.GetTempPath() + "pngtemp.jpg";
  410. if (!PictureConverter.SaveJpeg(MergeObjectlist[i].FilePath, tempFileName))
  411. {
  412. MessageBoxEx.Show("图片格式有问题");
  413. }
  414. // 之前插入位置为零点导致插入为第一页,改为尾插 2023/5/24
  415. result = SaveDoc.InsertPage(SaveDoc.PageCount, img.Width, img.Height, tempFileName);
  416. try { if (File.Exists(tempFileName)) File.Delete(tempFileName); }
  417. catch
  418. { }
  419. if (!result)
  420. {
  421. SaveDoc.Release();
  422. return;
  423. }
  424. continue;
  425. }
  426. catch
  427. {
  428. continue;
  429. }
  430. }
  431. else
  432. {
  433. try
  434. {
  435. BitmapSource frame = MergeObjectlist[i].DocThumbnail;
  436. byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  437. if (frame.Format != PixelFormats.Bgra32)
  438. {
  439. FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
  440. covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  441. }
  442. else
  443. {
  444. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  445. }
  446. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  447. // 之前插入位置为零点导致插入为第一页,改为尾插 2023/5/24
  448. result = SaveDoc.InsertPage(SaveDoc.PageCount, frame.PixelWidth, frame.PixelHeight, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
  449. if (!result)
  450. {
  451. SaveDoc.Release();
  452. return;
  453. }
  454. continue;
  455. }
  456. catch
  457. {
  458. continue;
  459. }
  460. }
  461. }
  462. else
  463. {
  464. CPDFDocument tempDoc = CPDFDocument.InitWithFilePath(MergeObjectlist[i].FilePath);
  465. if (!string.IsNullOrEmpty(MergeObjectlist[i].Password))
  466. {
  467. tempDoc.UnlockWithPassword(MergeObjectlist[i].Password);
  468. }
  469. if (!string.IsNullOrEmpty(MergeObjectlist[i].SetPageRangeStr))
  470. {
  471. result = SaveDoc.ImportPages(tempDoc, MergeObjectlist[i].SetPageRangeStr);
  472. }
  473. else
  474. {
  475. //下拉框控件传出来的list是页面索引集合,
  476. //此处传给SDK的合并接口的是页码集合,因此需要将集合项遍历+1,只有合并接口是接收页码集合,其余接口是接受页面索引集合
  477. List<int> pageIndexs = new List<int>();
  478. for (int j = 0; j < MergeObjectlist[i].SetPageRange.Count; j++)
  479. {
  480. pageIndexs.Add(MergeObjectlist[i].SetPageRange[j] + 1);
  481. }
  482. result = SaveDoc.ImportPages(tempDoc, CommonHelper.GetPageParmFromList(pageIndexs));
  483. }
  484. if (!result)
  485. {
  486. SaveDoc.Release();
  487. tempDoc.Release();
  488. return;
  489. }
  490. tempDoc.Release();
  491. }
  492. }
  493. SaveFileDialog saveFileDialog = new SaveFileDialog();
  494. saveFileDialog.FileName = "Untitle";
  495. saveFileDialog.Filter = "PDF|*.pdf;";
  496. if (saveFileDialog.ShowDialog() == false)
  497. {
  498. return;
  499. }
  500. string path = saveFileDialog.FileName;
  501. System.Windows.Rect rect = new System.Windows.Rect();
  502. switch (pageSizeType)
  503. {
  504. case PageSizeType.kDefault:
  505. break;
  506. case PageSizeType.A4:
  507. rect.Width = CommonHelper.GetPageSizeFomrUnit(210);
  508. rect.Height = CommonHelper.GetPageSizeFomrUnit(297);
  509. break;
  510. case PageSizeType.A3:
  511. rect.Width = CommonHelper.GetPageSizeFomrUnit(297);
  512. rect.Height = CommonHelper.GetPageSizeFomrUnit(420);
  513. break;
  514. case PageSizeType.Letter:
  515. rect.Width = CommonHelper.GetPageSizeFomrUnit(216);
  516. rect.Height = CommonHelper.GetPageSizeFomrUnit(279);
  517. break;
  518. case PageSizeType.Legal:
  519. rect.Width = CommonHelper.GetPageSizeFomrUnit(216);
  520. rect.Height = CommonHelper.GetPageSizeFomrUnit(356);
  521. break;
  522. case PageSizeType.Customized:
  523. if (!string.IsNullOrEmpty(InputWidth) && !string.IsNullOrEmpty(InputHeight))
  524. {
  525. rect.Width = CommonHelper.GetPageSizeFomrUnit(Convert.ToInt32(InputWidth));
  526. rect.Height = CommonHelper.GetPageSizeFomrUnit(Convert.ToInt32(InputHeight));
  527. }
  528. else
  529. {
  530. rect.Width = CommonHelper.GetPageSizeFomrUnit(595);
  531. rect.Height = CommonHelper.GetPageSizeFomrUnit(841);
  532. }
  533. break;
  534. default:
  535. break;
  536. }
  537. if (rect.Width > 0 && rect.Height > 0)
  538. {
  539. //裁剪
  540. for (int i = 0; i < SaveDoc.PageCount; i++)
  541. {
  542. CPDFPage page = SaveDoc.PageAtIndex(i);
  543. page.CropPage(CPDFDisplayBox.MediaBox, rect);
  544. }
  545. }
  546. bool saveResult = SaveDoc.WriteToFilePath(path);
  547. SaveDoc.Release();
  548. CommonHelper.ShowFileBrowser(path);
  549. DialogParameters valuePairs = new DialogParameters();
  550. valuePairs.Add(ParameterNames.FilePath, path);
  551. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  552. }
  553. private void Clear()
  554. {
  555. MergeObjectlist.Clear();
  556. AddOpenFileIsEn = false;
  557. }
  558. private void SetPageSizeType(object button)
  559. {
  560. if (button is RadioButton)
  561. {
  562. pageSizeType = (PageSizeType)Convert.ToInt32((button as RadioButton).Tag);
  563. }
  564. }
  565. #endregion
  566. #region 公开方法
  567. public void DeleteItem(MergeObject merge)
  568. {
  569. MergeObjectlist.Remove(merge);
  570. UpDataMergeObjectIndex();
  571. }
  572. /// <summary>
  573. /// 拖拽插入
  574. /// </summary>
  575. public void MoveMerge(MergeObject targetNode, MergeObject soureNode)
  576. {
  577. int targetindex = MergeObjectlist.IndexOf(targetNode);
  578. MergeObjectlist.Remove(soureNode);
  579. if (targetNode.IsForward)
  580. {
  581. if (targetindex + 1 < MergeObjectlist.Count)
  582. {
  583. MergeObjectlist.Insert(targetindex + 1, soureNode);
  584. }
  585. else
  586. {
  587. MergeObjectlist.Add(soureNode);
  588. }
  589. }
  590. else
  591. {
  592. MergeObjectlist.Insert(targetindex, soureNode);
  593. }
  594. UpDataMergeObjectIndex();
  595. }
  596. public async void AddFiles(string[] FilePath)
  597. {
  598. if (FilePath == null)
  599. {
  600. return;
  601. }
  602. bool showDialog = false;
  603. ProcessVisible = Visibility.Visible;
  604. MaxValue = FilePath.Length;
  605. for (int i = 0; i < FilePath.Length; i++)
  606. {
  607. //加入异步语句,防止UI卡死,显示加载进度条
  608. await System.Threading.Tasks.Task.Delay(1);
  609. //如果中断了文件添加,则取消循环
  610. if (CancelAddFiles)
  611. {
  612. break;
  613. }
  614. MergeObject mergeObject = new MergeObject();
  615. mergeObject.FilePath = FilePath[i];
  616. //通过路径判断文件是否已添加
  617. bool IsExists = false;
  618. for (int j = 0; j < MergeObjectlist.Count; j++)
  619. {
  620. if (MergeObjectlist[j].FilePath == mergeObject.FilePath)
  621. {
  622. IsExists = true;
  623. }
  624. }
  625. if (IsExists)
  626. {
  627. continue;
  628. }
  629. string FileType = Path.GetExtension(mergeObject.FilePath).Trim().ToLower();
  630. if (string.IsNullOrEmpty(FileType))
  631. {
  632. showDialog = true;
  633. //获取不到文件类型
  634. continue;
  635. }
  636. if (FileType != ".pdf")
  637. {
  638. string imagetype = "*" + FileType;
  639. string[] x = Properties.Resources.imageex.ToLower().Split(';');
  640. List<string> list = x.ToList();
  641. int imageindex = list.IndexOf(imagetype);
  642. if (imageindex < 0)
  643. {
  644. showDialog = true;
  645. //图片格式不支持
  646. continue;
  647. };
  648. mergeObject.DocName = Path.GetFileName(mergeObject.FilePath);
  649. mergeObject.DocPageCount = 1 + " " + App.MainPageLoader.GetString("Merge_ItemPages");
  650. mergeObject.SDKPageCount = 1;
  651. mergeObject.SetPageRange = new List<int>() { 0 };
  652. mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
  653. try
  654. {
  655. mergeObject.DocThumbnail = new BitmapImage(new Uri(mergeObject.FilePath));
  656. }
  657. catch (Exception)
  658. {
  659. showDialog = true;
  660. //解码错误
  661. continue;
  662. }
  663. }
  664. else
  665. {
  666. CPDFDocument doc = CPDFDocument.InitWithFilePath(mergeObject.FilePath);
  667. if (doc == null)
  668. {
  669. showDialog = true;
  670. //PDF打开失败
  671. continue;
  672. }
  673. ///Fix:
  674. ///情况分为:
  675. ///开启路径是当前路径:
  676. ///已解锁或本身就无密码->直接加入
  677. ///未解锁-> release
  678. ///
  679. /// 不是当前路径
  680. ///解锁或取消->
  681. ///
  682. if (CurrentFilePath == doc.FilePath)
  683. {
  684. if ((!(!doc.IsLocked && (SecurityHelper.CheckHaveAllPermissions(doc)))) &&
  685. (!(!string.IsNullOrEmpty(currentLoadedPassword) &&
  686. doc.UnlockWithPassword(currentLoadedPassword) &&
  687. (doc.CheckOwnerPassword(currentLoadedPassword) || SecurityHelper.CheckHaveAllPermissions(doc)))))
  688. {
  689. doc.Release();
  690. continue;
  691. }
  692. //添加的第一个文档有权限密码时,保存密码,避免合并时因没有密码导致合并失败
  693. //表现为在首页打开一个带权限的文档进入合并,点击合并按钮没有反应
  694. if (!string.IsNullOrEmpty(currentLoadedPassword))
  695. {
  696. mergeObject.Password = currentLoadedPassword;
  697. }
  698. }
  699. else
  700. {
  701. VerifyPasswordResult condition = new VerifyPasswordResult();
  702. App.Current.Dispatcher.Invoke(() =>
  703. {
  704. //切换一下UI线程,避免解密弹窗异常
  705. condition = SecurityHelper.VerifyPasswordByPasswordKind(doc, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  706. });
  707. if (condition.IsDiscryptied)
  708. {
  709. if (condition.Password != null)
  710. {
  711. mergeObject.Password = condition.Password;
  712. if (doc.UnlockWithPassword(condition.Password) && doc.CheckOwnerPassword(condition.Password))
  713. {
  714. }
  715. }
  716. }
  717. else
  718. {
  719. doc.Release();
  720. continue;
  721. }
  722. }
  723. mergeObject.DocName = doc.FileName;
  724. if (doc.PageCount > 1)
  725. {
  726. mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPages");
  727. mergeObject.IsEvenPageIsEnabled = true;
  728. }
  729. else
  730. {
  731. mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPage");
  732. mergeObject.IsEvenPageIsEnabled = false;
  733. }
  734. mergeObject.SDKPageCount = doc.PageCount;
  735. mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
  736. //获取第一页缩略图
  737. CPDFPage page = doc.PageAtIndex(0);
  738. Size size = doc.GetPageSize(0);
  739. byte[] bmpData = new byte[(int)(size.Width * size.Height * 4)];
  740. WriteableBitmap WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null);
  741. page.RenderPageBitmap(0, 0, (int)size.Width, (int)size.Height, 0xFFFFFFFF, bmpData, 1);
  742. WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0);
  743. WirteBitmap.Freeze();
  744. mergeObject.DocThumbnail = WirteBitmap;
  745. doc.Release();
  746. }
  747. App.Current.Dispatcher.Invoke(() =>
  748. {
  749. MergeObjectlist.Add(mergeObject);
  750. });
  751. CurrentValue = i;
  752. UpDataMergeObjectIndex();
  753. }
  754. ProcessVisible = Visibility.Collapsed;
  755. if (showDialog)
  756. {
  757. AlertsMessage alertsMessage = new AlertsMessage();
  758. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
  759. if (alertsMessage.result == ContentResult.Ok)
  760. {
  761. }
  762. }
  763. }
  764. #endregion
  765. }
  766. }