ViewContentViewModel.cs 34 KB

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