ViewContentViewModel.cs 43 KB

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