ViewContentViewModel.cs 39 KB

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