ViewContentViewModel.cs 35 KB

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