ViewContentViewModel.cs 37 KB

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