ViewContentViewModel.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. /// <summary>
  491. /// 阅读模式
  492. /// </summary>
  493. /// <param name="viewContent"></param>
  494. public async void RbtnReadMode(ViewContent viewContent)
  495. {
  496. App.IsBookMode = true;
  497. IsLoading = Visibility.Visible;
  498. await Task.Delay(1);
  499. NavigationParameters param = new NavigationParameters();
  500. param.Add(ParameterNames.PDFViewer, PDFViewer);
  501. param.Add(ParameterNames.ViewContentViewModel, this);
  502. region.RequestNavigate(ToolContentRegionName, "ReadViewContent", param);
  503. //ShowContent(CurrentBar, true);
  504. if (GridToolRow != 0)
  505. {
  506. GridToolRow = 0;
  507. }
  508. if (GridToolRowSpan != 4)
  509. {
  510. GridToolRowSpan = 4;
  511. }
  512. //isInPageEdit = true;
  513. UpdateShowContent("TabItemPageEdit");
  514. IsLoading = Visibility.Collapsed;
  515. }
  516. /// <summary>
  517. /// 退出阅读模式
  518. /// </summary>
  519. public async void UnReadModel()
  520. {
  521. App.IsBookMode = false;
  522. IsLoading = Visibility.Visible;
  523. await Task.Delay(1);
  524. //region.AddToRegion(ViwerRegionName, PDFViewer);
  525. if (region.Regions.ContainsRegionWithName(ViwerRegionName))
  526. {
  527. if (region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  528. {
  529. var contentRegion = region.Regions[ViwerRegionName];
  530. contentRegion.Remove(PDFViewer);
  531. }
  532. region.AddToRegion(ViwerRegionName, PDFViewer);
  533. }
  534. if (string.IsNullOrEmpty(CurrentBar) || CurrentBar.Equals("TabItemPageEdit", StringComparison.OrdinalIgnoreCase))
  535. {
  536. EnterSelectedBar("TabItemAnnotation");
  537. }
  538. else
  539. {
  540. EnterSelectedBar(CurrentBar);
  541. }
  542. //isInPageEdit = false;
  543. //ShowContent(CurrentBar, false);
  544. //isInPageEdit = false;
  545. IsLoading = Visibility.Collapsed;
  546. }
  547. #region PDFViewer鼠标滚轮缩放事件
  548. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  549. {
  550. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  551. PDFViewer.Zoom(newZoom);
  552. }
  553. private double CheckZoomLevel(double zoom, bool IsGrowth)
  554. {
  555. double standardZoom = 100;
  556. if (zoom <= 0.01)
  557. {
  558. return 0.01;
  559. }
  560. if (zoom >= 10)
  561. {
  562. return 10;
  563. }
  564. zoom *= 100;
  565. for (int i = 0; i < zoomLevel.Length - 1; i++)
  566. {
  567. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  568. {
  569. standardZoom = zoomLevel[i + 1];
  570. break;
  571. }
  572. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  573. {
  574. standardZoom = zoomLevel[i];
  575. break;
  576. }
  577. }
  578. return standardZoom / 100;
  579. }
  580. #endregion PDFViewer鼠标滚轮缩放事件
  581. #region Navigate
  582. public void OnNavigatedTo(NavigationContext navigationContext)
  583. {
  584. if (isOpenFile)
  585. return;
  586. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  587. if (mainVM != null)
  588. {
  589. mainViewModel = mainVM;
  590. }
  591. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  592. if (pdfview != null)
  593. {
  594. PDFViewer = pdfview;
  595. loadFile();
  596. }
  597. isOpenFile = true;
  598. }
  599. public bool IsNavigationTarget(NavigationContext navigationContext)
  600. {
  601. return true;
  602. }
  603. public void OnNavigatedFrom(NavigationContext navigationContext)
  604. {
  605. }
  606. #endregion Navigate
  607. #region 方法
  608. private void Undo()
  609. {
  610. if (isInPageEdit)
  611. {
  612. //执行页面编辑的Undo
  613. PageEditUndo?.Invoke();
  614. }
  615. else
  616. {
  617. PDFViewer.UndoManager.Undo();
  618. }
  619. }
  620. private void Redo()
  621. {
  622. if (isInPageEdit)
  623. {
  624. //执行页面编辑的Redo
  625. PageEditRedo?.Invoke();
  626. }
  627. else
  628. {
  629. PDFViewer.UndoManager.Redo();
  630. }
  631. }
  632. private void LoadControl()
  633. {
  634. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  635. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  636. {
  637. NavigationParameters parameters = new NavigationParameters();
  638. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  639. parameters.Add(ParameterNames.ViewContentViewModel, this);
  640. region.RequestNavigate(BOTARegionName, "BOTAContent", parameters);
  641. region.RequestNavigate(BottomToolRegionName, "BottomToolContent", parameters);
  642. region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
  643. //TODO 根据上一次关闭记录的菜单,选中TabItem
  644. EnterSelectedBar("TabItemAnnotation");
  645. }
  646. ));
  647. }
  648. /// <summary>
  649. /// 各个注释(选中和创建注释)导航到对应注释的属性面板
  650. /// </summary>
  651. /// <param name="Content"></param>
  652. /// <param name="annotPropertyPanel"></param>
  653. public void SelectedPrpoertyPanel(string Content, AnnotPropertyPanel annotPropertyPanel)
  654. {
  655. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  656. {
  657. NavigationParameters parameters = new NavigationParameters();
  658. //传其他参数:文档类,空注释面板;
  659. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  660. parameters.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  661. region.RequestNavigate(PropertyRegionName, Content, parameters);
  662. }
  663. ));
  664. }
  665. /// <summary>
  666. /// 将PDFViwer添加到Region
  667. /// </summary>
  668. private void loadFile()
  669. {
  670. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  671. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  672. CanSave = PDFViewer.UndoManager.CanSave;
  673. CanUndo = PDFViewer.UndoManager.CanUndo;
  674. CanRedo = PDFViewer.UndoManager.CanRedo;
  675. region.AddToRegion(ViwerRegionName, PDFViewer);
  676. }
  677. /// <summary>
  678. /// 已有路径文档的保存逻辑
  679. /// </summary>
  680. private bool saveFile()
  681. {
  682. try
  683. {
  684. if (string.IsNullOrEmpty(PDFViewer.Document.FilePath))
  685. return saveAsFile();
  686. //文档已被修复时 提示另存为
  687. if (PDFViewer.Document.HasRepaired)
  688. {
  689. AlertsMessage alertsMessage = new AlertsMessage();
  690. alertsMessage.ShowDialog("", "文件已被修复,建议另存为", "Cancel", "OK");
  691. if (alertsMessage.result == ContentResult.Ok)
  692. return saveAsFile();
  693. else
  694. return false;
  695. }
  696. //文件路径无法存在时
  697. if (!File.Exists(PDFViewer.Document.FilePath))
  698. {
  699. AlertsMessage alertsMessage = new AlertsMessage();
  700. alertsMessage.ShowDialog("", "文件路径不存在,需要另存为", "Cancel", "OK");
  701. if (alertsMessage.result == ContentResult.Ok)
  702. return saveAsFile();
  703. else
  704. return false;
  705. }
  706. //只读文件无法写入时,提示另存为
  707. FileInfo fileInfo = new FileInfo(PDFViewer.Document.FilePath);
  708. if (fileInfo.IsReadOnly)
  709. {
  710. AlertsMessage alertsMessage = new AlertsMessage();
  711. alertsMessage.ShowDialog("", "文件为只读文件,需要另存为", "Cancel", "OK");
  712. if (alertsMessage.result == ContentResult.Ok)
  713. return saveAsFile();
  714. else
  715. return false;
  716. }
  717. bool result = PDFViewer.Document.WriteToLoadedPath();
  718. if (result)
  719. {
  720. PDFViewer.UndoManager.CanSave = false;
  721. App.Current.Dispatcher.Invoke(() =>
  722. {
  723. //TODO:更新缩略图
  724. //OpenFileInfo info = SettingHelper.GetFileInfo(PdfViewer.Document.FilePath);
  725. //try
  726. //{
  727. // if (!string.IsNullOrEmpty(info.ThumbImgPath) && !PdfViewer.Document.IsEncrypted)//加密的文档不获取缩略图
  728. // {
  729. // var size = PdfViewer.Document.GetPageSize(0);
  730. // System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PdfViewer.Document, (int)size.Width, (int)size.Height, 0, true, true);
  731. // string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  732. // if (File.Exists(folderPath))
  733. // File.Delete(folderPath);
  734. // DirectoryInfo folder = new DirectoryInfo(folderPath);
  735. // if (!folder.Exists)
  736. // folder.Create();
  737. // string imagePath = info.ThumbImgPath;
  738. // if (!File.Exists(imagePath))//由加密文档变为非加密文档时 新建一个路径
  739. // {
  740. // string imageName = Guid.NewGuid().ToString();
  741. // imagePath = System.IO.Path.Combine(folderPath, imageName);
  742. // using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  743. // {
  744. // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  745. // }
  746. // }
  747. // else
  748. // {
  749. // using (FileStream stream = new FileStream(imagePath, FileMode.Open))
  750. // {
  751. // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  752. // }
  753. // }
  754. // info.ThumbImgPath = imagePath;
  755. // SettingHelper.SetFileInfo(info);
  756. // }
  757. //}
  758. //catch
  759. //{
  760. // info.ThumbImgPath = null;
  761. // SettingHelper.SetFileInfo(info);
  762. //}
  763. });
  764. }
  765. else
  766. {
  767. //文件被占用 保存失败时
  768. AlertsMessage alertsMessage = new AlertsMessage();
  769. alertsMessage.ShowDialog("", "文件被占用,需要另存为", "Cancel", "OK");
  770. if (alertsMessage.result == ContentResult.Ok)
  771. return saveAsFile();
  772. else
  773. return false;
  774. }
  775. return result;
  776. }
  777. catch { return false; }
  778. }
  779. /// <summary>
  780. /// 另存为或新文档保存逻辑
  781. /// </summary>
  782. private bool saveAsFile()
  783. {
  784. var dlg = new SaveFileDialog();
  785. dlg.Filter = Properties.Resources.OpenDialogFilter;
  786. dlg.FileName = PDFViewer.Document.FileName;
  787. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  788. {
  789. bool result = false;
  790. if (App.OpenedFileList.Contains(dlg.FileName))
  791. {
  792. //提示文件已经被打开
  793. }
  794. else
  795. {
  796. result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
  797. if (result)
  798. {
  799. DoAfterSaveAs(dlg.FileName);
  800. }
  801. else
  802. {
  803. //提示文件被其他软件占用 无法保存
  804. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileOccupiedWarning"), "", Winform.MessageBoxButtons.OKCancel, new string[] { App.MainPageLoader.GetString("Main_SaveAs"), App.MainPageLoader.GetString("Main_Cancel") })
  805. }
  806. ;
  807. }
  808. return result;
  809. }
  810. else
  811. return false;
  812. }
  813. /// <summary>
  814. /// 另存为后进行的操作
  815. /// 重新打开新文档
  816. /// </summary>
  817. /// <param name="targetPath"></param>
  818. public void DoAfterSaveAs(string targetPath)
  819. {
  820. App.OpenedFileList.Remove(targetPath);
  821. string oldFilePath = targetPath;
  822. PDFViewer.UndoManager.CanSave = false;
  823. mainViewModel.OpenFile(targetPath);
  824. //TODO:通知各模块更新PDFview对象
  825. //var result = OpenFile(targetPath, true);
  826. //if (result)
  827. //{
  828. // FileChanged.Invoke(this, null);
  829. // Zoomer.PdfViewer = PdfViewer;
  830. // PageSelector.PdfViewer = PdfViewer;
  831. // ViewModeSelector.PdfViewer = PdfViewer;
  832. // OpenFileInfo fileInfo = SettingHelper.GetFileInfo(oldFilePath);
  833. // if (fileInfo != null)
  834. // {
  835. // PdfViewer.ChangeViewMode(fileInfo.LastViewMode);
  836. // PdfViewer.ChangeFitMode(fileInfo.LastFitMode);
  837. // if (fileInfo.LastFitMode == FitMode.FitFree)
  838. // PdfViewer.Zoom(fileInfo.LastZoom);
  839. // PdfViewer.GoToPage(fileInfo.LastPageIndex);
  840. // if (fileInfo.LastDrawMode == DrawModes.Draw_Mode_Custom && fileInfo.LastFillColor != 0)
  841. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode, fileInfo.LastFillColor);
  842. // else
  843. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode);
  844. // }
  845. //}
  846. }
  847. /// <summary>
  848. /// 显示前添加内容到Region
  849. /// </summary>
  850. /// <param name="isPageEdit"></param>
  851. private void ShowContent(string currentBar, bool isToolMode = false)
  852. {
  853. GridVisibility = Visibility.Visible;
  854. //显示页面编辑或其他工具
  855. if (currentBar == "TabItemPageEdit" || isToolMode)
  856. {
  857. if (currentBar == "TabItemPageEdit")//进入页面编辑
  858. {
  859. if (GridToolRow != 1)
  860. {
  861. GridToolRow = 1;
  862. }
  863. if (GridToolRowSpan != 3)
  864. {
  865. GridToolRowSpan = 3;
  866. }
  867. }
  868. else//进入水印等其他工具模式
  869. {
  870. GridVisibility = Visibility.Collapsed;
  871. if (GridToolRow != 0)
  872. {
  873. GridToolRow = 0;
  874. }
  875. if (GridToolRowSpan != 4)
  876. {
  877. GridToolRowSpan = 4;
  878. }
  879. }
  880. //ToolContent的visible跟toolsbarContent 的visible是互斥的
  881. UpdateShowContent(currentBar);
  882. }
  883. else
  884. {
  885. if (GridToolRow != 1)
  886. {
  887. GridToolRow = 1;
  888. }
  889. UpdateShowContent(currentBar);
  890. }
  891. }
  892. /// <summary>
  893. /// 从二级工具栏进入需要修改界面布局的场景
  894. /// </summary>
  895. /// <param name="EditToolName"></param>
  896. private void EnterEditTools(StringWithUnicode EditToolName)
  897. {
  898. EnterSelectedEditTool(EditToolName);
  899. }
  900. private void CloseEditTool(EnumCloseModeUnicode enumCloseModeunicode)
  901. {
  902. EnumCloseMode enumCloseMode = enumCloseModeunicode.Status;
  903. if (enumCloseMode == EnumCloseMode.StatusConfirm)
  904. {
  905. PDFViewer.Document.ReleasePages();
  906. PDFViewer.ReloadDocument();
  907. }
  908. CurrentBar = "TabItemTool";
  909. EnterSelectedBar(CurrentBar);
  910. }
  911. /// <summary>
  912. /// 二级菜单指定目标处理
  913. /// </summary>
  914. /// <param name="e"></param>
  915. private void EnterSelectedEditTool(StringWithUnicode EditToolName)
  916. {
  917. CurrentBar = EditToolName.EditToolsContentName;
  918. EnterToolMode(CurrentBar);
  919. }
  920. /// <summary>
  921. /// 进入工具编辑(如页面编辑、水印、密文等)模式
  922. /// </summary>
  923. /// <param name="targetToolMode">要导航过去的控件名称</param>
  924. /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
  925. private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
  926. {
  927. IsLoading = Visibility.Visible;
  928. await Task.Delay(3);
  929. NavigationParameters param = new NavigationParameters();
  930. if (valuePairs == null)
  931. {
  932. param.Add(ParameterNames.PDFViewer, PDFViewer);
  933. param.Add(ParameterNames.ViewContentViewModel, this);
  934. }
  935. else//有传入其他内容的参数时
  936. {
  937. param = valuePairs;
  938. }
  939. region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
  940. ShowContent(CurrentBar, true);
  941. IsLoading = Visibility.Collapsed;
  942. }
  943. private void EnterSelectedBar(string currentBar)
  944. {
  945. NavigationParameters param = new NavigationParameters();
  946. param.Add(ParameterNames.PDFViewer, PDFViewer);
  947. param.Add(ParameterNames.ViewContentViewModel, this);
  948. region.RequestNavigate(regionNameByTabItem[currentBar], barContentByTabItem[currentBar], param);
  949. ShowContent(currentBar);
  950. }
  951. /// <summary>
  952. /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
  953. /// </summary>
  954. public void ExitToolMode()
  955. {
  956. ToolContentVisible = Visibility.Collapsed;
  957. if (isInPageEdit)
  958. {
  959. TabSelectedIndex = 0;
  960. isInPageEdit = false;
  961. }
  962. }
  963. #endregion 方法
  964. }
  965. }