ViewContentViewModel.cs 39 KB

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