ViewContentViewModel.cs 38 KB

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