ViewContentViewModel.cs 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. using Microsoft.Win32;
  2. using Prism.Commands;
  3. using Prism.Mvvm;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using ComPDFKitViewer.PdfViewer;
  10. using Prism.Regions;
  11. using DryIoc;
  12. using System.Diagnostics;
  13. using Prism.Services.Dialogs;
  14. using PDF_Office.CustomControl;
  15. using PDF_Office.Model;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.IO;
  19. using PDFSettings;
  20. using ComPDFKitViewer.AnnotEvent;
  21. using PDF_Office.ViewModels.Tools;
  22. using PDF_Office.Views;
  23. using Prism.Events;
  24. using PDF_Office.EventAggregators;
  25. using PDF_Office.Views.EditTools.Background;
  26. namespace PDF_Office.ViewModels
  27. {
  28. public class ViewContentViewModel : BindableBase, INavigationAware
  29. {
  30. #region 属性、变量
  31. public CPDFViewer PDFViewer { get; set; }
  32. private MainContentViewModel mainViewModel { get; set; }
  33. public IRegionManager region;
  34. public IDialogService dialogs;
  35. public IEventAggregator events;
  36. public string ViwerRegionName { get; set; }
  37. /// <summary>
  38. /// 分屏视图的region名称
  39. /// </summary>
  40. public string SplitViewerRegionName { get; set; }
  41. /// <summary>
  42. /// OCR视图名称
  43. /// </summary>
  44. public string OCRViewerRegionName { get; set; }
  45. public string BOTARegionName { get; set; }
  46. public string PropertyRegionName { get; set; }
  47. public string ToolContentRegionName { get; set; }
  48. public string ToolsBarContentRegionName { get; set; }
  49. public string ReadModeRegionName { get; set; }
  50. public string ConverterBarContentRegionName { get; set; }
  51. public string TextEditContentRegionName { get; set; }
  52. public string BackgroundContentRegionName { get; set; }
  53. /// <summary>
  54. /// 底部工具栏 RegionName
  55. /// </summary>
  56. public string BottomToolRegionName { get; set; }
  57. private bool _isInPageEdit = false;
  58. /// <summary>
  59. /// 是否处于页面编辑模式,用于执行undo redo 的具体操作
  60. /// </summary>
  61. public bool isInPageEdit
  62. {
  63. get { return _isInPageEdit; }
  64. set
  65. {
  66. _isInPageEdit = value;
  67. if (!value)
  68. {
  69. CanRedo = PDFViewer.UndoManager.CanRedo;
  70. CanUndo = PDFViewer.UndoManager.CanUndo;
  71. }
  72. }
  73. }
  74. public Action PageEditUndo { get; set; }
  75. public Action PageEditRedo { get; set; }
  76. /// <summary>
  77. ///工具条
  78. ///0:收起
  79. ///40:显示
  80. /// </summary>
  81. private int toolRowHeight = 40;
  82. public int ToolRowHeight
  83. {
  84. get { return toolRowHeight; }
  85. set
  86. {
  87. SetProperty(ref toolRowHeight, value);
  88. }
  89. }
  90. /// <summary>
  91. /// 水印,背景侧边栏宽度
  92. /// 0:收起
  93. /// 260:显示
  94. /// </summary>
  95. private int propertyColumnWidth = 0;
  96. public int PropertyColumnWidth
  97. {
  98. get { return propertyColumnWidth; }
  99. set
  100. {
  101. SetProperty(ref propertyColumnWidth, value);
  102. }
  103. }
  104. private int gridToolRow = 1;
  105. /// <summary>
  106. /// 控制ToolContent的Row
  107. /// </summary>
  108. public int GridToolRow
  109. {
  110. get { return gridToolRow; }
  111. set
  112. {
  113. SetProperty(ref gridToolRow, value);
  114. }
  115. }
  116. private int gridToolRowSpan = 3;
  117. /// <summary>
  118. /// 控制ToolContent的RowSpan
  119. /// </summary>
  120. public int GridToolRowSpan
  121. {
  122. get { return gridToolRowSpan; }
  123. set
  124. {
  125. SetProperty(ref gridToolRowSpan, value);
  126. }
  127. }
  128. private Visibility toolContentVisible = Visibility.Collapsed;
  129. /// <summary>
  130. /// 控制Content的显示 用于显示水印、贝茨码、密文等功能模块
  131. /// 留意:显示前需要先注入内容、设置好行和跨行数
  132. /// </summary>
  133. public Visibility ToolContentVisible
  134. {
  135. get { return toolContentVisible; }
  136. set
  137. {
  138. SetProperty(ref toolContentVisible, value);
  139. }
  140. }
  141. private Visibility gridVisibility = Visibility.Visible;
  142. /// <summary>
  143. /// 是否正在加载中
  144. /// </summary>
  145. public Visibility GridVisibility
  146. {
  147. get { return gridVisibility; }
  148. set
  149. {
  150. SetProperty(ref gridVisibility, value);
  151. }
  152. }
  153. private Visibility isLoading = Visibility.Collapsed;
  154. private Visibility ocrContentVisible = Visibility.Collapsed;
  155. public Visibility OCRContentVisible
  156. {
  157. get { return ocrContentVisible; }
  158. set
  159. {
  160. SetProperty(ref ocrContentVisible, value);
  161. }
  162. }
  163. /// <summary>
  164. /// 是否正在加载中
  165. /// </summary>
  166. public Visibility IsLoading
  167. {
  168. get { return isLoading; }
  169. set
  170. {
  171. SetProperty(ref isLoading, value);
  172. }
  173. }
  174. private Visibility converterBarContentVisible = Visibility.Collapsed;
  175. private Visibility toolsbarContentVisible = Visibility.Collapsed;
  176. /// <summary>
  177. /// 控制ToolsBarContent的显示
  178. /// 留意:显示前需要先注入内容、设置好行和跨行数
  179. /// </summary>
  180. public Visibility ConverterBarContentVisible
  181. {
  182. get { return converterBarContentVisible; }
  183. set
  184. {
  185. SetProperty(ref converterBarContentVisible, value);
  186. }
  187. }
  188. private Visibility toolsBarContentVisible = Visibility.Collapsed;
  189. /// <summary>
  190. /// 控制ToolsBarContent的显示
  191. /// 留意:显示前需要先注入内容、设置好行和跨行数
  192. /// </summary>
  193. public Visibility ToolsBarContentVisible
  194. {
  195. get { return toolsBarContentVisible; }
  196. set
  197. {
  198. SetProperty(ref toolsBarContentVisible, value);
  199. }
  200. }
  201. private Visibility textEditToolContentVisible = Visibility.Collapsed;
  202. /// <summary>
  203. /// 控制ToolsBarContent的显示
  204. /// 留意:显示前需要先注入内容、设置好行和跨行数
  205. /// </summary>
  206. public Visibility TextEditToolContentVisible
  207. {
  208. get { return textEditToolContentVisible; }
  209. set
  210. {
  211. SetProperty(ref textEditToolContentVisible, value);
  212. }
  213. }
  214. private bool isPorpertyOpen = false;
  215. /// <summary>
  216. /// 属性栏是否展开
  217. /// </summary>
  218. public bool IsPropertyOpen
  219. {
  220. get { return isPorpertyOpen; }
  221. set
  222. {
  223. SetProperty(ref isPorpertyOpen, value);
  224. }
  225. }
  226. private Visibility isReadMode = Visibility.Visible;
  227. /// <summary>
  228. ///是否为阅读模式
  229. /// </summary>
  230. public Visibility IsReadMode
  231. {
  232. get { return isReadMode; }
  233. set
  234. {
  235. SetProperty(ref isReadMode, value);
  236. }
  237. }
  238. private bool canSave;
  239. /// <summary>
  240. /// 是否可以保存
  241. /// </summary>
  242. public bool CanSave
  243. {
  244. get { return canSave; }
  245. set
  246. {
  247. SetProperty(ref canSave, value);
  248. }
  249. }
  250. private bool canUndo;
  251. /// <summary>
  252. /// 是否可以进行Undo
  253. /// </summary>
  254. public bool CanUndo
  255. {
  256. get { return canUndo; }
  257. set
  258. {
  259. SetProperty(ref canUndo, value);
  260. }
  261. }
  262. private bool canRedo;
  263. /// <summary>
  264. /// 是否可以进行Redo
  265. /// </summary>
  266. public bool CanRedo
  267. {
  268. get { return canRedo; }
  269. set
  270. {
  271. SetProperty(ref canRedo, value);
  272. }
  273. }
  274. private GridLength botaWidth = new GridLength(48);
  275. /// <summary>
  276. /// BOTA栏的宽度
  277. /// </summary>
  278. public GridLength BOTAWidth
  279. {
  280. get { return botaWidth; }
  281. set
  282. {
  283. SetProperty(ref botaWidth, value);
  284. if (botaWidth.Value <= 48)
  285. {
  286. OpenBOTA = false;
  287. }
  288. }
  289. }
  290. private int selectedIndex;
  291. /// <summary>
  292. /// 工具栏选中项的索引
  293. /// </summary>
  294. public int TabSelectedIndex
  295. {
  296. get { return selectedIndex; }
  297. set
  298. {
  299. SetProperty(ref selectedIndex, value);
  300. }
  301. }
  302. private bool openBOTA = false;
  303. /// <summary>
  304. /// 是否展开BOTA
  305. /// </summary>
  306. public bool OpenBOTA
  307. {
  308. get { return openBOTA; }
  309. set
  310. {
  311. openBOTA = value;
  312. if (openBOTA && BOTAWidth.Value <= 48)
  313. {
  314. BOTAWidth = new GridLength(256);
  315. }
  316. }
  317. }
  318. private Dictionary<string, string> regionNameByTabItem;
  319. private Dictionary<string, string> barContentByTabItem;
  320. private string previousBar = "";
  321. public string CurrentBar = "";
  322. public string unicode = null;
  323. /// <summary>
  324. /// 用来避免重复触发导航事件的标志符
  325. /// </summary>
  326. private bool isOpenFile = false;
  327. /// <summary>
  328. /// 鼠标滚轮缩放的缩放值
  329. /// </summary>
  330. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  331. /// <summary>
  332. /// 注释-链接,提示语
  333. /// </summary>
  334. private Visibility linkAnnotTipVisibility = Visibility.Collapsed;
  335. public Visibility LinkAnnotTipVisibility
  336. {
  337. get { return linkAnnotTipVisibility; }
  338. set
  339. {
  340. SetProperty(ref linkAnnotTipVisibility, value);
  341. }
  342. }
  343. private string linkAnnotTipText = "Please use the scroll bar, thumbnail tool to locate the target page, click or box the area to select the target range";
  344. public string LinkAnnotTipText
  345. {
  346. get { return linkAnnotTipText; }
  347. set
  348. {
  349. SetProperty(ref linkAnnotTipText, value);
  350. }
  351. }
  352. #endregion 属性、变量
  353. #region 命令
  354. public DelegateCommand LoadFile { get; set; }
  355. public DelegateCommand Load { get; set; }
  356. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  357. public DelegateCommand SaveFile { get; set; }
  358. public DelegateCommand SaveAsFile { get; set; }
  359. public DelegateCommand UndoCommand { get; set; }
  360. public DelegateCommand RedoCommand { get; set; }
  361. public DelegateCommand<object> MenuEnterReadMode { get; set; }
  362. public DelegateCommand PrintCommand { get; set; }
  363. public DelegateCommand ShareCommand { get; set; }
  364. #endregion 命令
  365. public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  366. {
  367. region = regionManager;
  368. dialogs = dialogService;
  369. events = eventAggregator;
  370. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  371. LoadFile = new DelegateCommand(loadFile);
  372. Load = new DelegateCommand(LoadControl);
  373. SaveFile = new DelegateCommand(() => { saveFile(); }, CanSaveExcute).ObservesProperty(() => CanSave);
  374. SaveAsFile = new DelegateCommand(() => { saveAsFile(); });
  375. UndoCommand = new DelegateCommand(Undo);
  376. RedoCommand = new DelegateCommand(Redo);
  377. ShareCommand = new DelegateCommand(share);
  378. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
  379. ViwerRegionName = RegionNames.ViwerRegionName;
  380. SplitViewerRegionName = RegionNames.SplitScreenViewRegionName;
  381. BOTARegionName = RegionNames.BOTARegionName;
  382. PropertyRegionName = RegionNames.PropertyRegionName;
  383. BottomToolRegionName = RegionNames.BottomToolRegionName;
  384. ReadModeRegionName = RegionNames.ReadModeRegionName;
  385. MenuEnterReadMode = new DelegateCommand<object>(MenuEnterReadModeEvent);
  386. PrintCommand = new DelegateCommand(ShowPrintDialog);
  387. //未显示时无法注册上Region名称
  388. ToolContentVisible = Visibility.Visible;
  389. ToolsBarContentVisible = Visibility.Visible;
  390. OCRContentVisible = Visibility.Visible;
  391. OCRViewerRegionName = RegionNames.OCRViewerRegionName;
  392. ToolContentRegionName = Guid.NewGuid().ToString();
  393. ToolsBarContentRegionName = Guid.NewGuid().ToString();
  394. ConverterBarContentRegionName = Guid.NewGuid().ToString();
  395. TextEditContentRegionName = Guid.NewGuid().ToString();
  396. ToolContentVisible = Visibility.Collapsed;
  397. ToolsBarContentVisible = Visibility.Collapsed;
  398. OCRContentVisible = Visibility.Collapsed;
  399. regionNameByTabItem = new Dictionary<string, string>();
  400. barContentByTabItem = new Dictionary<string, string>();
  401. InitialregionNameByTabItem(ref regionNameByTabItem);
  402. InitialbarContentByTabItem(ref barContentByTabItem);
  403. eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Subscribe(EnterEditTools, e => e.Unicode == unicode);
  404. eventAggregator.GetEvent<CloseEditToolEvent>().Subscribe(CloseEditTool, e => e.Unicode == unicode);
  405. //TODO:根据缓存 选择用户上次选择的菜单
  406. EnterSelectedBar("TabItemAnnotation");
  407. }
  408. private void share()
  409. {
  410. try
  411. {
  412. var path = PDFViewer.Document.FilePath;
  413. string subject = "分享至" + " " + PDFViewer.Document.FileName;
  414. System.Diagnostics.Process.Start("outlook", "/a,\"" + path + "\"" + "/m \"" + "&subject=" + subject + "\"");
  415. }
  416. catch
  417. {
  418. AlertsMessage alertsMessage = new AlertsMessage();
  419. alertsMessage.ShowDialog("","未检测到Ooutlook软件,请先安装Outlook","OK");
  420. }
  421. }
  422. private bool CanSaveExcute()
  423. {
  424. return CanSave;
  425. }
  426. private void MenuEnterReadModeEvent(object obj)
  427. {
  428. }
  429. public void ShowPrintDialog()
  430. {
  431. DialogParameters printValue = new DialogParameters();
  432. printValue.Add(ParameterNames.PDFViewer, PDFViewer);
  433. printValue.Add(ParameterNames.FilePath, PDFViewer.Document.FileName);
  434. printValue.Add(ParameterNames.PrintCurrentPage, PDFViewer.CurrentIndex);
  435. dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printValue, e => { });
  436. }
  437. private void InitialregionNameByTabItem(ref Dictionary<string, string> dictionary)
  438. {
  439. dictionary.Add("TabItemPageEdit", ToolContentRegionName);
  440. dictionary.Add("TabItemTool", ToolsBarContentRegionName);
  441. //其他工具菜单栏共用一个ToolsBarContentRegionName
  442. dictionary.Add("TabItemAnnotation", ToolsBarContentRegionName);
  443. dictionary.Add("TabItemConvert", ConverterBarContentRegionName);
  444. dictionary.Add("TabItemScan", ToolsBarContentRegionName);
  445. dictionary.Add("TabItemEdit", TextEditContentRegionName);
  446. dictionary.Add("TabItemForm", ToolsBarContentRegionName);
  447. dictionary.Add("TabItemFill", ToolsBarContentRegionName);
  448. }
  449. private void InitialbarContentByTabItem(ref Dictionary<string, string> dictionary)
  450. {
  451. dictionary.Add("TabItemPageEdit", "PageEditContent");
  452. dictionary.Add("TabItemTool", "ToolsBarContent");
  453. dictionary.Add("TabItemAnnotation", "AnnotToolContent");
  454. dictionary.Add("TabItemConvert", "ConverterBarContent");
  455. dictionary.Add("TabItemScan", "ScanContent");
  456. dictionary.Add("TabItemEdit", "TextEditToolContent");
  457. dictionary.Add("TabItemForm", "FormsToolContent");
  458. dictionary.Add("TabItemFill", "FillAndSignContent");
  459. }
  460. private void UpdateShowContent(string currentBar)
  461. {
  462. ToolContentVisible = Visibility.Collapsed;
  463. ToolsBarContentVisible = Visibility.Collapsed;
  464. ConverterBarContentVisible = Visibility.Collapsed;
  465. TextEditToolContentVisible = Visibility.Collapsed;
  466. OCRContentVisible = Visibility.Collapsed;
  467. switch (currentBar)
  468. {
  469. case "TabItemAnnotation":
  470. case "TabItemTool":
  471. case "TabItemForm":
  472. case "TabItemFill":
  473. ToolsBarContentVisible = Visibility.Visible;
  474. break;
  475. case "TabItemScan":
  476. ToolsBarContentVisible = Visibility.Visible;
  477. OCRContentVisible = Visibility.Visible;
  478. break;
  479. case "TabItemEdit":
  480. TextEditToolContentVisible = Visibility.Visible;
  481. break;
  482. case "TabItemConvert":
  483. ConverterBarContentVisible = Visibility.Visible;
  484. break;
  485. case "TabItemPageEdit":
  486. case "HeaderFooterContent":
  487. case "BatesContent":
  488. case "WatermarkContent":
  489. case "BackgroundContent":
  490. case "RedactionContent":
  491. ToolContentVisible = Visibility.Visible;
  492. break;
  493. default:
  494. break;
  495. }
  496. PDFEditMode(currentBar);
  497. FormMode(currentBar);
  498. }
  499. private void PDFEditMode(string currentBar)
  500. {
  501. if (currentBar == "TabItemEdit")
  502. {
  503. if (PDFViewer != null)
  504. {
  505. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  506. //SelectedPrpoertyPanel("TextEditProperty", null);
  507. //IsPropertyOpen = true;
  508. }
  509. }
  510. else
  511. {
  512. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.PDFEdit)
  513. {
  514. IsPropertyOpen = false;
  515. PDFViewer.SetMouseMode(MouseModes.PanTool);
  516. }
  517. }
  518. }
  519. private void FormMode(string currentBar)
  520. {
  521. if (currentBar == "TabItemForm")
  522. {
  523. if (PDFViewer != null)
  524. {
  525. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  526. }
  527. }
  528. else
  529. {
  530. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool)
  531. {
  532. PDFViewer.SetMouseMode(MouseModes.PanTool);
  533. }
  534. }
  535. }
  536. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  537. {
  538. if (!isInPageEdit)
  539. {
  540. //不处于页面编辑模式下时,根据PDFVIewer的undo redo状态来更新按钮状态
  541. //页面编辑模式下,按钮状态根据页面编辑的undo redo来显示
  542. if (e.PropertyName == "CanUndo")
  543. {
  544. CanUndo = PDFViewer.UndoManager.CanUndo;
  545. }
  546. if (e.PropertyName == "CanRedo")
  547. {
  548. CanRedo = PDFViewer.UndoManager.CanRedo;
  549. }
  550. }
  551. if (e.PropertyName == "CanSave")
  552. {
  553. CanSave = PDFViewer.UndoManager.CanSave;
  554. }
  555. }
  556. /// <summary>
  557. /// 选项卡切换事件
  558. /// </summary>
  559. /// <param name="e"></param>
  560. private void TabControlSelectonChangedEvent(object e)
  561. {
  562. var args = e as SelectionChangedEventArgs;
  563. if (args != null)
  564. {
  565. var item = args.AddedItems[0] as TabItem;
  566. CurrentBar = item.Name;
  567. if (previousBar != CurrentBar)
  568. {
  569. if (CurrentBar == "TabItemPageEdit")//如果是页面编辑则进入页面编辑模式
  570. {
  571. EnterToolMode(barContentByTabItem[CurrentBar]);
  572. isInPageEdit = true;
  573. }
  574. else//其余情况直接导航至对应的工具栏即可,不需要清空之前的content,region里是单例模式
  575. {
  576. EnterSelectedBar(CurrentBar);
  577. isInPageEdit = false;
  578. }
  579. previousBar = CurrentBar;
  580. }
  581. }
  582. }
  583. /// <summary>
  584. /// 阅读模式
  585. /// </summary>
  586. /// <param name="viewContent"></param>
  587. public async void RbtnReadMode(ViewContent viewContent)
  588. {
  589. App.IsBookMode = true;
  590. IsLoading = Visibility.Visible;
  591. await Task.Delay(1);
  592. NavigationParameters param = new NavigationParameters();
  593. param.Add(ParameterNames.PDFViewer, PDFViewer);
  594. param.Add(ParameterNames.ViewContentViewModel, this);
  595. region.RequestNavigate(ToolContentRegionName, "ReadViewContent", param);
  596. //ShowContent(CurrentBar, true);
  597. if (GridToolRow != 0)
  598. {
  599. GridToolRow = 0;
  600. }
  601. if (GridToolRowSpan != 4)
  602. {
  603. GridToolRowSpan = 4;
  604. }
  605. //isInPageEdit = true;
  606. UpdateShowContent("TabItemPageEdit");
  607. IsLoading = Visibility.Collapsed;
  608. }
  609. /// <summary>
  610. /// 退出阅读模式
  611. /// </summary>
  612. public async void UnReadModel()
  613. {
  614. App.IsBookMode = false;
  615. IsLoading = Visibility.Visible;
  616. await Task.Delay(1);
  617. //PDFViewer.MouseMode = MouseModes.None;
  618. if (region.Regions.ContainsRegionWithName(ViwerRegionName))
  619. {
  620. if (region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  621. {
  622. var contentRegion = region.Regions[ViwerRegionName];
  623. contentRegion.Remove(PDFViewer);
  624. }
  625. region.AddToRegion(ViwerRegionName, PDFViewer);
  626. }
  627. if (string.IsNullOrEmpty(CurrentBar) || CurrentBar.Equals("TabItemPageEdit", StringComparison.OrdinalIgnoreCase))
  628. {
  629. EnterSelectedBar("TabItemAnnotation");
  630. }
  631. else
  632. {
  633. EnterSelectedBar(CurrentBar);
  634. }
  635. //isInPageEdit = false;
  636. //ShowContent(CurrentBar, false);
  637. //isInPageEdit = false;
  638. IsLoading = Visibility.Collapsed;
  639. }
  640. #region PDFViewer鼠标滚轮缩放事件
  641. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  642. {
  643. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  644. PDFViewer.Zoom(newZoom);
  645. }
  646. private double CheckZoomLevel(double zoom, bool IsGrowth)
  647. {
  648. double standardZoom = 100;
  649. if (zoom <= 0.01)
  650. {
  651. return 0.01;
  652. }
  653. if (zoom >= 10)
  654. {
  655. return 10;
  656. }
  657. zoom *= 100;
  658. for (int i = 0; i < zoomLevel.Length - 1; i++)
  659. {
  660. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  661. {
  662. standardZoom = zoomLevel[i + 1];
  663. break;
  664. }
  665. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  666. {
  667. standardZoom = zoomLevel[i];
  668. break;
  669. }
  670. }
  671. return standardZoom / 100;
  672. }
  673. #endregion PDFViewer鼠标滚轮缩放事件
  674. #region Navigate
  675. public void OnNavigatedTo(NavigationContext navigationContext)
  676. {
  677. if (isOpenFile)
  678. return;
  679. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  680. if (mainVM != null)
  681. {
  682. mainViewModel = mainVM;
  683. }
  684. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  685. if (pdfview != null)
  686. {
  687. PDFViewer = pdfview;
  688. loadFile();
  689. }
  690. isOpenFile = true;
  691. }
  692. public bool IsNavigationTarget(NavigationContext navigationContext)
  693. {
  694. return true;
  695. }
  696. public void OnNavigatedFrom(NavigationContext navigationContext)
  697. {
  698. }
  699. #endregion Navigate
  700. #region 方法
  701. /// <summary>
  702. /// 视图面板 切换分屏模式
  703. /// </summary>
  704. /// <param name="mode"></param>
  705. public void EnterSplitMode(SplitMode mode)
  706. {
  707. ///通知UI层更改布局
  708. this.events.GetEvent<SplitEvent>().Publish(new SplitEventArgs() { Unicode = unicode, Mode = mode });
  709. }
  710. private void Undo()
  711. {
  712. if (isInPageEdit)
  713. {
  714. //执行页面编辑的Undo
  715. PageEditUndo?.Invoke();
  716. }
  717. else
  718. {
  719. PDFViewer.UndoManager.Undo();
  720. }
  721. }
  722. private void Redo()
  723. {
  724. if (isInPageEdit)
  725. {
  726. //执行页面编辑的Redo
  727. PageEditRedo?.Invoke();
  728. }
  729. else
  730. {
  731. PDFViewer.UndoManager.Redo();
  732. }
  733. }
  734. private void LoadControl()
  735. {
  736. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  737. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  738. {
  739. NavigationParameters parameters = new NavigationParameters();
  740. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  741. parameters.Add(ParameterNames.ViewContentViewModel, this);
  742. region.RequestNavigate(BOTARegionName, "BOTAContent", parameters);
  743. region.RequestNavigate(BottomToolRegionName, "BottomToolContent", parameters);
  744. region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
  745. region.RequestNavigate(SplitViewerRegionName, "SplitScreenContent", parameters);
  746. //TODO 根据上一次关闭记录的菜单,选中TabItem
  747. EnterSelectedBar("TabItemAnnotation");
  748. }
  749. ));
  750. }
  751. /// <summary>
  752. /// 各个注释(选中和创建注释)导航到对应注释的属性面板
  753. /// </summary>
  754. /// <param name="Content"></param>
  755. /// <param name="annotPropertyPanel"></param>
  756. public void SelectedPrpoertyPanel(string Content, AnnotPropertyPanel annotPropertyPanel)
  757. {
  758. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  759. {
  760. NavigationParameters parameters = new NavigationParameters();
  761. //传其他参数:文档类,空注释面板;
  762. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  763. parameters.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  764. parameters.Add(ParameterNames.ViewContentViewModel, this);
  765. region.RequestNavigate(PropertyRegionName, Content, parameters);
  766. }
  767. ));
  768. }
  769. /// <summary>
  770. /// 将PDFViwer添加到Region
  771. /// </summary>
  772. private void loadFile()
  773. {
  774. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  775. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  776. CanSave = PDFViewer.UndoManager.CanSave;
  777. CanUndo = PDFViewer.UndoManager.CanUndo;
  778. CanRedo = PDFViewer.UndoManager.CanRedo;
  779. region.AddToRegion(ViwerRegionName, PDFViewer);
  780. }
  781. /// <summary>
  782. /// 已有路径文档的保存逻辑
  783. /// </summary>
  784. private bool saveFile()
  785. {
  786. try
  787. {
  788. if (string.IsNullOrEmpty(PDFViewer.Document.FilePath))
  789. return saveAsFile();
  790. //文档已被修复时 提示另存为
  791. if (PDFViewer.Document.HasRepaired)
  792. {
  793. AlertsMessage alertsMessage = new AlertsMessage();
  794. alertsMessage.ShowDialog("", "文件已被修复,建议另存为", "Cancel", "OK");
  795. if (alertsMessage.result == ContentResult.Ok)
  796. return saveAsFile();
  797. else
  798. return false;
  799. }
  800. //文件路径无法存在时
  801. if (!File.Exists(PDFViewer.Document.FilePath))
  802. {
  803. AlertsMessage alertsMessage = new AlertsMessage();
  804. alertsMessage.ShowDialog("", "文件路径不存在,需要另存为", "Cancel", "OK");
  805. if (alertsMessage.result == ContentResult.Ok)
  806. return saveAsFile();
  807. else
  808. return false;
  809. }
  810. //只读文件无法写入时,提示另存为
  811. FileInfo fileInfo = new FileInfo(PDFViewer.Document.FilePath);
  812. if (fileInfo.IsReadOnly)
  813. {
  814. AlertsMessage alertsMessage = new AlertsMessage();
  815. alertsMessage.ShowDialog("", "文件为只读文件,需要另存为", "Cancel", "OK");
  816. if (alertsMessage.result == ContentResult.Ok)
  817. return saveAsFile();
  818. else
  819. return false;
  820. }
  821. bool result = PDFViewer.Document.WriteToLoadedPath();
  822. if (result)
  823. {
  824. PDFViewer.UndoManager.CanSave = false;
  825. App.Current.Dispatcher.Invoke(() =>
  826. {
  827. //TODO:更新缩略图
  828. //OpenFileInfo info = SettingHelper.GetFileInfo(PdfViewer.Document.FilePath);
  829. //try
  830. //{
  831. // if (!string.IsNullOrEmpty(info.ThumbImgPath) && !PdfViewer.Document.IsEncrypted)//加密的文档不获取缩略图
  832. // {
  833. // var size = PdfViewer.Document.GetPageSize(0);
  834. // System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PdfViewer.Document, (int)size.Width, (int)size.Height, 0, true, true);
  835. // string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  836. // if (File.Exists(folderPath))
  837. // File.Delete(folderPath);
  838. // DirectoryInfo folder = new DirectoryInfo(folderPath);
  839. // if (!folder.Exists)
  840. // folder.Create();
  841. // string imagePath = info.ThumbImgPath;
  842. // if (!File.Exists(imagePath))//由加密文档变为非加密文档时 新建一个路径
  843. // {
  844. // string imageName = Guid.NewGuid().ToString();
  845. // imagePath = System.IO.Path.Combine(folderPath, imageName);
  846. // using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  847. // {
  848. // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  849. // }
  850. // }
  851. // else
  852. // {
  853. // using (FileStream stream = new FileStream(imagePath, FileMode.Open))
  854. // {
  855. // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  856. // }
  857. // }
  858. // info.ThumbImgPath = imagePath;
  859. // SettingHelper.SetFileInfo(info);
  860. // }
  861. //}
  862. //catch
  863. //{
  864. // info.ThumbImgPath = null;
  865. // SettingHelper.SetFileInfo(info);
  866. //}
  867. });
  868. }
  869. else
  870. {
  871. //文件被占用 保存失败时
  872. AlertsMessage alertsMessage = new AlertsMessage();
  873. alertsMessage.ShowDialog("", "文件被占用,需要另存为", "Cancel", "OK");
  874. if (alertsMessage.result == ContentResult.Ok)
  875. return saveAsFile();
  876. else
  877. return false;
  878. }
  879. return result;
  880. }
  881. catch { return false; }
  882. }
  883. /// <summary>
  884. /// 另存为或新文档保存逻辑
  885. /// </summary>
  886. public bool saveAsFile(bool isApplyRedaction = false,bool isEraseRedaction = false)
  887. {
  888. var dlg = new SaveFileDialog();
  889. dlg.Filter = Properties.Resources.OpenDialogFilter;
  890. dlg.FileName = PDFViewer.Document.FileName;
  891. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  892. {
  893. bool result = false;
  894. //标记密文后的保存
  895. if (isApplyRedaction)
  896. {
  897. //应用标记密文并另存为保存
  898. try
  899. {
  900. PDFViewer.Document.ApplyRedaction();
  901. PDFViewer.Document.ReleasePages();
  902. PDFViewer.ReloadDocument();
  903. }
  904. catch { }
  905. }
  906. else if(isEraseRedaction)
  907. {
  908. EraseReadction();
  909. }
  910. if (App.OpenedFileList.Contains(dlg.FileName))
  911. {
  912. //提示文件已经被打开
  913. }
  914. else
  915. {
  916. result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
  917. if (result)
  918. {
  919. DoAfterSaveAs(dlg.FileName);
  920. }
  921. else
  922. {
  923. //提示文件被其他软件占用 无法保存
  924. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileOccupiedWarning"), "", Winform.MessageBoxButtons.OKCancel, new string[] { App.MainPageLoader.GetString("Main_SaveAs"), App.MainPageLoader.GetString("Main_Cancel") })
  925. }
  926. ;
  927. }
  928. return result;
  929. }
  930. else
  931. return false;
  932. }
  933. private void EraseReadction()
  934. {
  935. for(int i=0;i<PDFViewer.Document.PageCount;i++)
  936. {
  937. //获取页面Page对象
  938. var page = PDFViewer.Document.PageAtIndex(i);
  939. var annots = PDFViewer.GetAnnotCommentList(i,PDFViewer.Document);
  940. //循环擦除当前页的标记密文
  941. for (int j = 0; j < annots.Count; j++)
  942. {
  943. if(annots[j].EventType == AnnotArgsType.AnnotRedaction)
  944. {
  945. foreach(var rect in (annots[j] as RedactionAnnotArgs).QuardRects)
  946. {
  947. page.ErasureRedaction(rect);
  948. }
  949. }
  950. }
  951. }
  952. }
  953. /// <summary>
  954. /// 另存为后进行的操作
  955. /// 重新打开新文档
  956. /// </summary>
  957. /// <param name="targetPath"></param>
  958. public void DoAfterSaveAs(string targetPath)
  959. {
  960. App.OpenedFileList.Remove(targetPath);
  961. string oldFilePath = targetPath;
  962. PDFViewer.UndoManager.CanSave = false;
  963. mainViewModel.OpenFile(targetPath);
  964. //TODO:通知各模块更新PDFview对象
  965. //var result = OpenFile(targetPath, true);
  966. //if (result)
  967. //{
  968. // FileChanged.Invoke(this, null);
  969. // Zoomer.PdfViewer = PdfViewer;
  970. // PageSelector.PdfViewer = PdfViewer;
  971. // ViewModeSelector.PdfViewer = PdfViewer;
  972. // OpenFileInfo fileInfo = SettingHelper.GetFileInfo(oldFilePath);
  973. // if (fileInfo != null)
  974. // {
  975. // PdfViewer.ChangeViewMode(fileInfo.LastViewMode);
  976. // PdfViewer.ChangeFitMode(fileInfo.LastFitMode);
  977. // if (fileInfo.LastFitMode == FitMode.FitFree)
  978. // PdfViewer.Zoom(fileInfo.LastZoom);
  979. // PdfViewer.GoToPage(fileInfo.LastPageIndex);
  980. // if (fileInfo.LastDrawMode == DrawModes.Draw_Mode_Custom && fileInfo.LastFillColor != 0)
  981. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode, fileInfo.LastFillColor);
  982. // else
  983. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode);
  984. // }
  985. //}
  986. }
  987. /// <summary>
  988. /// 显示前添加内容到Region
  989. /// </summary>
  990. /// <param name="isPageEdit"></param>
  991. private void ShowContent(string currentBar, bool isToolMode = false)
  992. {
  993. GridVisibility = Visibility.Visible;
  994. //显示页面编辑或其他工具
  995. if (currentBar == "TabItemPageEdit" || isToolMode)
  996. {
  997. if (currentBar == "TabItemPageEdit")//进入页面编辑
  998. {
  999. if (GridToolRow != 1)
  1000. {
  1001. GridToolRow = 1;
  1002. }
  1003. if (GridToolRowSpan != 3)
  1004. {
  1005. GridToolRowSpan = 3;
  1006. }
  1007. }
  1008. else//进入水印等其他工具模式
  1009. {
  1010. GridVisibility = Visibility.Collapsed;
  1011. if (GridToolRow != 0)
  1012. {
  1013. GridToolRow = 0;
  1014. }
  1015. if (GridToolRowSpan != 4)
  1016. {
  1017. GridToolRowSpan = 4;
  1018. }
  1019. }
  1020. //ToolContent的visible跟toolsbarContent 的visible是互斥的
  1021. UpdateShowContent(currentBar);
  1022. }
  1023. else
  1024. {
  1025. if (GridToolRow != 1)
  1026. {
  1027. GridToolRow = 1;
  1028. }
  1029. UpdateShowContent(currentBar);
  1030. }
  1031. }
  1032. /// <summary>
  1033. /// 从二级工具栏进入需要修改界面布局的场景
  1034. /// </summary>
  1035. /// <param name="EditToolName"></param>
  1036. private void EnterEditTools(StringWithUnicode EditToolName)
  1037. {
  1038. EnterSelectedEditTool(EditToolName);
  1039. }
  1040. private void CloseEditTool(EnumCloseModeUnicode enumCloseModeunicode)
  1041. {
  1042. EnumCloseMode enumCloseMode = enumCloseModeunicode.Status;
  1043. if (enumCloseMode == EnumCloseMode.StatusConfirm)
  1044. {
  1045. PDFViewer.Document.ReleasePages();
  1046. PDFViewer.ReloadDocument();
  1047. }
  1048. //如果是其他工具共用一个PDFview,退出工具模式后,需要重新添加到Region,否则不会显示
  1049. if (!region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  1050. {
  1051. region.AddToRegion(ViwerRegionName, PDFViewer);
  1052. }
  1053. CurrentBar = "TabItemTool";
  1054. EnterSelectedBar(CurrentBar);
  1055. }
  1056. /// <summary>
  1057. /// 二级菜单指定目标处理
  1058. /// </summary>
  1059. /// <param name="e"></param>
  1060. private void EnterSelectedEditTool(StringWithUnicode EditToolName)
  1061. {
  1062. CurrentBar = EditToolName.EditToolsContentName;
  1063. EnterToolMode(CurrentBar);
  1064. }
  1065. /// <summary>
  1066. /// 进入工具编辑(如页面编辑、水印、密文等)模式
  1067. /// </summary>
  1068. /// <param name="targetToolMode">要导航过去的控件名称</param>
  1069. /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
  1070. private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
  1071. {
  1072. IsLoading = Visibility.Visible;
  1073. await Task.Delay(3);
  1074. NavigationParameters param = new NavigationParameters();
  1075. if (valuePairs == null)
  1076. {
  1077. param.Add(ParameterNames.PDFViewer, PDFViewer);
  1078. param.Add(ParameterNames.ViewContentViewModel, this);
  1079. }
  1080. else//有传入其他内容的参数时
  1081. {
  1082. param = valuePairs;
  1083. }
  1084. region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
  1085. ShowContent(CurrentBar, true);
  1086. IsLoading = Visibility.Collapsed;
  1087. }
  1088. private void EnterSelectedBar(string currentBar)
  1089. {
  1090. NavigationParameters param = new NavigationParameters();
  1091. param.Add(ParameterNames.PDFViewer, PDFViewer);
  1092. param.Add(ParameterNames.ViewContentViewModel, this);
  1093. region.RequestNavigate(regionNameByTabItem[currentBar], barContentByTabItem[currentBar], param);
  1094. if (currentBar == "TabItemScan")
  1095. {
  1096. region.RequestNavigate(RegionNames.OCRViewerRegionName, "ScanViwer", param);
  1097. }
  1098. ShowContent(currentBar);
  1099. }
  1100. /// <summary>
  1101. /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
  1102. /// </summary>
  1103. public void ExitToolMode()
  1104. {
  1105. ToolContentVisible = Visibility.Collapsed;
  1106. if (isInPageEdit)
  1107. {
  1108. TabSelectedIndex = 0;
  1109. isInPageEdit = false;
  1110. }
  1111. }
  1112. #endregion 方法
  1113. }
  1114. }