ViewContentViewModel.cs 33 KB

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