ViewContentViewModel.cs 36 KB

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