ViewContentViewModel.cs 45 KB

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