ViewContentViewModel.cs 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. using Microsoft.Win32;
  2. using Prism.Commands;
  3. using Prism.Mvvm;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Threading.Tasks;
  7. using ComPDFKitViewer.PdfViewer;
  8. using Prism.Regions;
  9. using Prism.Services.Dialogs;
  10. using PDF_Office.CustomControl;
  11. using PDF_Office.Model;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.IO;
  15. using ComPDFKitViewer.AnnotEvent;
  16. using PDF_Office.ViewModels.Tools;
  17. using Prism.Events;
  18. using PDF_Office.EventAggregators;
  19. using PDF_Office.Helper;
  20. using ComPDFKit.PDFDocument;
  21. using PDFSettings.Settings;
  22. using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
  23. using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.SetPasswordDialogModel;
  24. using PDF_Office.Model.Dialog.ToolsDialogs;
  25. using DryIoc;
  26. using PDF_Office.Model.Dialog.ConverterDialogs;
  27. using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  28. namespace PDF_Office.ViewModels
  29. {
  30. public class ViewContentViewModel : BindableBase, INavigationAware
  31. {
  32. #region 属性、变量
  33. public CPDFViewer PDFViewer { get; set; }
  34. public MainContentViewModel mainViewModel { get; set; }
  35. public IRegionManager region;
  36. public IDialogService dialogs;
  37. public IEventAggregator events;
  38. public bool IsRightMenuCreateLink { get => isCreateLink; set => isCreateLink = value; }
  39. public string ViwerRegionName { get; set; }
  40. /// <summary>
  41. /// 分屏视图的region名称
  42. /// </summary>
  43. public string SplitViewerRegionName { get; set; }
  44. /// <summary>
  45. /// OCR视图名称
  46. /// </summary>
  47. public string OCRViewerRegionName { get; set; }
  48. public string BOTARegionName { get; set; }
  49. public string PropertyRegionName { get; set; }
  50. public string ToolContentRegionName { get; set; }
  51. public string ToolsBarContentRegionName { get; set; }
  52. /// <summary>
  53. /// 顶部提示的Content
  54. /// </summary>
  55. public string TipContentRegionName { get; set; }
  56. public string LeftTipContentRegionName { get; set; }
  57. public string ReadModeRegionName { get; set; }
  58. public string ConverterBarContentRegionName { get; set; }
  59. public string TextEditContentRegionName { get; set; }
  60. public string BackgroundContentRegionName { get; set; }
  61. public List<Point> FillAndSign = new List<Point>();
  62. /// <summary>
  63. /// 文档的密码
  64. /// </summary>
  65. public string PassWord { get; set; }
  66. private Visibility tipVisible = Visibility.Collapsed;
  67. /// <summary>
  68. /// 顶部提示栏的显示状态
  69. /// </summary>
  70. public Visibility TipVisible
  71. {
  72. get { return tipVisible; }
  73. set
  74. {
  75. SetProperty(ref tipVisible, value);
  76. }
  77. }
  78. private Visibility leftTipVisible = Visibility.Collapsed;
  79. public Visibility LeftTipVisible
  80. {
  81. get { return leftTipVisible; }
  82. set
  83. {
  84. SetProperty(ref leftTipVisible, value);
  85. }
  86. }
  87. private Visibility readModelTip = Visibility.Collapsed;
  88. public Visibility ReadModelTip
  89. {
  90. get { return readModelTip; }
  91. set
  92. {
  93. SetProperty(ref readModelTip, value);
  94. }
  95. }
  96. /// <summary>
  97. /// 底部工具栏 RegionName
  98. /// </summary>
  99. public string BottomToolRegionName { get; set; }
  100. private bool _isInPageEdit = false;
  101. /// <summary>
  102. /// 是否处于页面编辑模式,用于执行undo redo 的具体操作
  103. /// </summary>
  104. public bool isInPageEdit
  105. {
  106. get { return _isInPageEdit; }
  107. set
  108. {
  109. _isInPageEdit = value;
  110. if (!value)
  111. {
  112. CanRedo = PDFViewer.UndoManager.CanRedo;
  113. CanUndo = PDFViewer.UndoManager.CanUndo;
  114. }
  115. }
  116. }
  117. public Action PageEditUndo { get; set; }
  118. public Action PageEditRedo { get; set; }
  119. /// <summary>
  120. ///工具条
  121. ///0:收起
  122. ///40:显示
  123. /// </summary>
  124. private int toolRowHeight = 40;
  125. public int ToolRowHeight
  126. {
  127. get { return toolRowHeight; }
  128. set
  129. {
  130. SetProperty(ref toolRowHeight, value);
  131. }
  132. }
  133. /// <summary>
  134. /// 水印,背景侧边栏宽度
  135. /// 0:收起
  136. /// 260:显示
  137. /// </summary>
  138. private int propertyColumnWidth = 0;
  139. public int PropertyColumnWidth
  140. {
  141. get { return propertyColumnWidth; }
  142. set
  143. {
  144. SetProperty(ref propertyColumnWidth, value);
  145. }
  146. }
  147. private int gridToolRow = 1;
  148. /// <summary>
  149. /// 控制ToolContent的Row
  150. /// </summary>
  151. public int GridToolRow
  152. {
  153. get { return gridToolRow; }
  154. set
  155. {
  156. SetProperty(ref gridToolRow, value);
  157. }
  158. }
  159. private int gridToolRowSpan = 3;
  160. /// <summary>
  161. /// 控制ToolContent的RowSpan
  162. /// </summary>
  163. public int GridToolRowSpan
  164. {
  165. get { return gridToolRowSpan; }
  166. set
  167. {
  168. SetProperty(ref gridToolRowSpan, value);
  169. }
  170. }
  171. private Visibility toolContentVisible = Visibility.Collapsed;
  172. /// <summary>
  173. /// 控制Content的显示 用于显示水印、贝茨码、密文等功能模块
  174. /// 留意:显示前需要先注入内容、设置好行和跨行数
  175. /// </summary>
  176. public Visibility ToolContentVisible
  177. {
  178. get { return toolContentVisible; }
  179. set
  180. {
  181. SetProperty(ref toolContentVisible, value);
  182. }
  183. }
  184. private Visibility gridVisibility = Visibility.Visible;
  185. /// <summary>
  186. /// 是否正在加载中
  187. /// </summary>
  188. public Visibility GridVisibility
  189. {
  190. get { return gridVisibility; }
  191. set
  192. {
  193. SetProperty(ref gridVisibility, value);
  194. }
  195. }
  196. private Visibility isLoading = Visibility.Collapsed;
  197. private Visibility ocrContentVisible = Visibility.Collapsed;
  198. public Visibility OCRContentVisible
  199. {
  200. get { return ocrContentVisible; }
  201. set
  202. {
  203. SetProperty(ref ocrContentVisible, value);
  204. }
  205. }
  206. /// <summary>
  207. /// 是否正在加载中
  208. /// </summary>
  209. public Visibility IsLoading
  210. {
  211. get { return isLoading; }
  212. set
  213. {
  214. SetProperty(ref isLoading, value);
  215. }
  216. }
  217. private Visibility converterBarContentVisible = Visibility.Collapsed;
  218. private Visibility toolsbarContentVisible = Visibility.Collapsed;
  219. /// <summary>
  220. /// 控制ToolsBarContent的显示
  221. /// 留意:显示前需要先注入内容、设置好行和跨行数
  222. /// </summary>
  223. public Visibility ConverterBarContentVisible
  224. {
  225. get { return converterBarContentVisible; }
  226. set
  227. {
  228. SetProperty(ref converterBarContentVisible, value);
  229. }
  230. }
  231. private Visibility toolsBarContentVisible = Visibility.Collapsed;
  232. /// <summary>
  233. /// 控制ToolsBarContent的显示
  234. /// 留意:显示前需要先注入内容、设置好行和跨行数
  235. /// </summary>
  236. public Visibility ToolsBarContentVisible
  237. {
  238. get { return toolsBarContentVisible; }
  239. set
  240. {
  241. SetProperty(ref toolsBarContentVisible, value);
  242. }
  243. }
  244. private Visibility textEditToolContentVisible = Visibility.Collapsed;
  245. /// <summary>
  246. /// 控制ToolsBarContent的显示
  247. /// 留意:显示前需要先注入内容、设置好行和跨行数
  248. /// </summary>
  249. public Visibility TextEditToolContentVisible
  250. {
  251. get { return textEditToolContentVisible; }
  252. set
  253. {
  254. SetProperty(ref textEditToolContentVisible, value);
  255. }
  256. }
  257. private bool isPorpertyOpen = false;
  258. /// <summary>
  259. /// 属性栏是否展开
  260. /// </summary>
  261. public bool IsPropertyOpen
  262. {
  263. get { return isPorpertyOpen; }
  264. set
  265. {
  266. SetProperty(ref isPorpertyOpen, value);
  267. }
  268. }
  269. private Visibility isReadMode = Visibility.Visible;
  270. /// <summary>
  271. ///是否为阅读模式
  272. /// </summary>
  273. public Visibility IsReadMode
  274. {
  275. get { return isReadMode; }
  276. set
  277. {
  278. SetProperty(ref isReadMode, value);
  279. }
  280. }
  281. private bool canSave;
  282. /// <summary>
  283. /// 是否可以保存
  284. /// </summary>
  285. public bool CanSave
  286. {
  287. get { return canSave; }
  288. set
  289. {
  290. SetProperty(ref canSave, value);
  291. }
  292. }
  293. private bool canUndo;
  294. /// <summary>
  295. /// 是否可以进行Undo
  296. /// </summary>
  297. public bool CanUndo
  298. {
  299. get { return canUndo; }
  300. set
  301. {
  302. SetProperty(ref canUndo, value);
  303. }
  304. }
  305. private bool canRedo;
  306. /// <summary>
  307. /// 是否可以进行Redo
  308. /// </summary>
  309. public bool CanRedo
  310. {
  311. get { return canRedo; }
  312. set
  313. {
  314. SetProperty(ref canRedo, value);
  315. }
  316. }
  317. private GridLength botaWidth = new GridLength(48);
  318. /// <summary>
  319. /// BOTA栏的宽度
  320. /// </summary>
  321. public GridLength BOTAWidth
  322. {
  323. get { return botaWidth; }
  324. set
  325. {
  326. SetProperty(ref botaWidth, value);
  327. if (botaWidth.Value <= 48)
  328. {
  329. OpenBOTA = false;
  330. }
  331. }
  332. }
  333. private int selectedIndex;
  334. /// <summary>
  335. /// 工具栏选中项的索引
  336. /// </summary>
  337. public int TabSelectedIndex
  338. {
  339. get { return selectedIndex; }
  340. set
  341. {
  342. SetProperty(ref selectedIndex, value);
  343. }
  344. }
  345. private bool openBOTA = false;
  346. /// <summary>
  347. /// 是否展开BOTA
  348. /// </summary>
  349. public bool OpenBOTA
  350. {
  351. get { return openBOTA; }
  352. set
  353. {
  354. SetProperty(ref openBOTA, value);
  355. if (openBOTA && BOTAWidth.Value <= 48)
  356. {
  357. BOTAWidth = new GridLength(260);
  358. }
  359. //关闭BOTA的逻辑在xaml.cs代码里控制,无法绑定后台
  360. }
  361. }
  362. public SecurityInfo SecurityInfo = new SecurityInfo();
  363. private Dictionary<string, string> regionNameByTabItem;
  364. private Dictionary<string, string> barContentByTabItem;
  365. private string previousBar = "";
  366. public string CurrentBar = "";
  367. public string unicode = null;
  368. /// <summary>
  369. /// 用来避免重复触发导航事件的标志符
  370. /// </summary>
  371. private bool isOpenFile = false;
  372. /// <summary>
  373. /// 鼠标滚轮缩放的缩放值
  374. /// </summary>
  375. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  376. /// <summary>
  377. /// 注释-链接,提示语
  378. /// </summary>
  379. private Visibility linkAnnotTipVisibility = Visibility.Collapsed;
  380. public Visibility LinkAnnotTipVisibility
  381. {
  382. get { return linkAnnotTipVisibility; }
  383. set
  384. {
  385. SetProperty(ref linkAnnotTipVisibility, value);
  386. }
  387. }
  388. 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";
  389. private bool isCreateLink = false;
  390. public string LinkAnnotTipText
  391. {
  392. get { return linkAnnotTipText; }
  393. set
  394. {
  395. SetProperty(ref linkAnnotTipText, value);
  396. }
  397. }
  398. #endregion 属性、变量
  399. #region 命令
  400. public DelegateCommand LoadFile { get; set; }
  401. public DelegateCommand Load { get; set; }
  402. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  403. public DelegateCommand SaveFile { get; set; }
  404. public DelegateCommand SaveAsFile { get; set; }
  405. public DelegateCommand SaveAsFlattenCommand { get; set; }
  406. public DelegateCommand UndoCommand { get; set; }
  407. public DelegateCommand RedoCommand { get; set; }
  408. public DelegateCommand<object> MenuEnterReadMode { get; set; }
  409. public DelegateCommand PrintCommand { get; set; }
  410. public DelegateCommand SettingsCommand { get; set; }
  411. public DelegateCommand ShareCommand { get; set; }
  412. public DelegateCommand<object> CreateLinkCommand { get; set; }
  413. public DelegateCommand PropertyCommand { get; set; }
  414. public DelegateCommand ShowInFolderCommand { get; set; }
  415. public DelegateCommand CompressCommand { get; set; }
  416. public DelegateCommand MergeFileCommand { get; set; }
  417. public DelegateCommand EncryptCommand { get; set; }
  418. public DelegateCommand DecryptCommand { get; set; }
  419. public DelegateCommand<string> ConvertCommand { get; set; }
  420. public DelegateCommand CloseWindowCommand { get; set; }
  421. #endregion 命令
  422. public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  423. {
  424. region = regionManager;
  425. dialogs = dialogService;
  426. events = eventAggregator;
  427. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  428. LoadFile = new DelegateCommand(loadFile);
  429. Load = new DelegateCommand(LoadControl);
  430. SaveFile = new DelegateCommand(() => { saveFile(); }, CanSaveExcute).ObservesProperty(() => CanSave);
  431. SaveAsFlattenCommand = new DelegateCommand(saveAsFlatten);
  432. SaveAsFile = new DelegateCommand(() => { saveAsFile(); });
  433. UndoCommand = new DelegateCommand(Undo);
  434. RedoCommand = new DelegateCommand(Redo);
  435. ShareCommand = new DelegateCommand(share);
  436. PropertyCommand = new DelegateCommand(property);
  437. ShowInFolderCommand = new DelegateCommand(ShowInFolder);
  438. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
  439. CompressCommand = new DelegateCommand(compress);
  440. MergeFileCommand = new DelegateCommand(mergeFile);
  441. EncryptCommand = new DelegateCommand(encrypt);
  442. DecryptCommand = new DelegateCommand(decrypt);
  443. ConvertCommand = new DelegateCommand<string>(convert);
  444. CloseWindowCommand = new DelegateCommand(closeWindow);
  445. ViwerRegionName = RegionNames.ViwerRegionName;
  446. SplitViewerRegionName = RegionNames.Viewer_SplitRegionName;
  447. BOTARegionName = RegionNames.BOTARegionName;
  448. PropertyRegionName = RegionNames.PropertyRegionName;
  449. BottomToolRegionName = RegionNames.BottomToolRegionName;
  450. ReadModeRegionName = RegionNames.ReadModeRegionName;
  451. MenuEnterReadMode = new DelegateCommand<object>(MenuEnterReadModeEvent);
  452. PrintCommand = new DelegateCommand(ShowPrintDialog);
  453. SettingsCommand = new DelegateCommand(SettingsEvent);
  454. CreateLinkCommand = new DelegateCommand<object>(CreateLinkEvent);
  455. //未显示时无法注册上Region名称
  456. ToolContentVisible = Visibility.Visible;
  457. ToolsBarContentVisible = Visibility.Visible;
  458. OCRContentVisible = Visibility.Visible;
  459. TipVisible = Visibility.Visible;
  460. TipContentRegionName = RegionNames.TipContentRegionName;
  461. LeftTipContentRegionName = RegionNames.LeftTipContentRegionName;
  462. OCRViewerRegionName = RegionNames.OCRViewerRegionName;
  463. ToolContentRegionName = Guid.NewGuid().ToString();
  464. ToolsBarContentRegionName = Guid.NewGuid().ToString();
  465. ConverterBarContentRegionName = Guid.NewGuid().ToString();
  466. TextEditContentRegionName = Guid.NewGuid().ToString();
  467. ToolContentVisible = Visibility.Collapsed;
  468. ToolsBarContentVisible = Visibility.Collapsed;
  469. OCRContentVisible = Visibility.Collapsed;
  470. TipVisible = Visibility.Collapsed;
  471. regionNameByTabItem = new Dictionary<string, string>();
  472. barContentByTabItem = new Dictionary<string, string>();
  473. InitialregionNameByTabItem(ref regionNameByTabItem);
  474. InitialbarContentByTabItem(ref barContentByTabItem);
  475. eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Subscribe(EnterEditTools, e => e.Unicode == unicode);
  476. eventAggregator.GetEvent<CloseEditToolEvent>().Subscribe(CloseEditTool, e => e.Unicode == unicode);
  477. eventAggregator.GetEvent<ShowTipEvent>().Subscribe(ShowSelectedTip, e => e.Unicode == unicode);
  478. //TODO:根据缓存 选择用户上次选择的菜单
  479. EnterSelectedBar("TabItemAnnotation");
  480. }
  481. /// <summary>
  482. /// 关闭当前窗体
  483. /// </summary>
  484. private void closeWindow()
  485. {
  486. App.Current.MainWindow.Close();
  487. }
  488. /// <summary>
  489. /// 转档
  490. /// </summary>
  491. /// <param name="obj"></param>
  492. private void convert(string obj)
  493. {
  494. if(!string.IsNullOrEmpty(obj))
  495. {
  496. DialogParameters value = new DialogParameters();
  497. value.Add(ParameterNames.PDFViewer, PDFViewer);
  498. switch (obj)
  499. {
  500. case "Word":
  501. dialogs.ShowDialog(DialogNames.ConverterWordDialog, value, e =>
  502. {
  503. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  504. converterDialogsModel.OnOpened((DialogResult)e);
  505. });
  506. break;
  507. case "Excel":
  508. dialogs.ShowDialog(DialogNames.ConverterExcelDialog, value, e => {
  509. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  510. converterDialogsModel.OnOpened((DialogResult)e);
  511. });
  512. break;
  513. case "PPT":
  514. dialogs.ShowDialog(DialogNames.ConverterPPTDialog, value, e => {
  515. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  516. converterDialogsModel.OnOpened((DialogResult)e);
  517. });
  518. break;
  519. case "RTF":
  520. dialogs.ShowDialog(DialogNames.ConverterRTFDialog, value, e => {
  521. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  522. converterDialogsModel.OnOpened((DialogResult)e);
  523. });
  524. break;
  525. case "CSV":
  526. dialogs.ShowDialog(DialogNames.ConverterCSVDialog, value, e => {
  527. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  528. converterDialogsModel.OnOpened((DialogResult)e);
  529. });
  530. break;
  531. case "HTML":
  532. dialogs.ShowDialog(DialogNames.ConverterHTMLDialog, value, e => {
  533. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  534. converterDialogsModel.OnOpened((DialogResult)e);
  535. });
  536. break;
  537. case "Text":
  538. dialogs.ShowDialog(DialogNames.ConverterTextDialog, value, e => {
  539. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  540. converterDialogsModel.OnOpened((DialogResult)e);
  541. });
  542. break;
  543. case "Image":
  544. dialogs.ShowDialog(DialogNames.ConverterImgDialog, value, e => {
  545. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  546. converterDialogsModel.OnOpened((DialogResult)e);
  547. });
  548. break;
  549. default:
  550. break;
  551. }
  552. }
  553. }
  554. /// <summary>
  555. /// 解密
  556. /// </summary>
  557. private void decrypt()
  558. {
  559. if (!PDFViewer.Document.IsEncrypted)
  560. {
  561. MessageBoxEx.Show("No security settings available ");
  562. }
  563. else
  564. {
  565. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  566. if (result.IsDiscryptied)
  567. {
  568. if (result.Password != null)
  569. {
  570. string filePath = PDFViewer.Document.FilePath;
  571. PDFViewer.Document.Release();
  572. PDFViewer.InitDocument(filePath);
  573. PDFViewer.Document.UnlockWithPassword(result.Password);
  574. }
  575. DialogParameters value = new DialogParameters();
  576. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  577. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });
  578. }
  579. }
  580. }
  581. /// <summary>
  582. /// 加密
  583. /// </summary>
  584. private void encrypt()
  585. {
  586. if (!this.SecurityInfo.IsPasswordChanged)
  587. {
  588. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  589. if (result.IsDiscryptied)
  590. {
  591. if (result.Password != null)
  592. {
  593. string filePath = PDFViewer.Document.FilePath;
  594. PDFViewer.Document.Release();
  595. PDFViewer.InitDocument(filePath);
  596. PDFViewer.Document.UnlockWithPassword(result.Password);
  597. }
  598. }
  599. }
  600. DialogParameters value = new DialogParameters();
  601. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  602. value.Add(ParameterNames.ViewContentViewModel, this);
  603. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  604. {
  605. if (e.Result == ButtonResult.OK)
  606. {
  607. SecurityHelper.IsPasswordChanged = true;
  608. if (this.SecurityInfo.IsPasswordChanged)
  609. {
  610. this.PDFViewer.UndoManager.CanSave = true;
  611. }
  612. this.events.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  613. }
  614. });
  615. }
  616. /// <summary>
  617. /// 合并
  618. /// </summary>
  619. private void mergeFile()
  620. {
  621. DialogParameters value = new DialogParameters();
  622. value.Add(ParameterNames.PDFViewer, PDFViewer);
  623. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  624. }
  625. /// <summary>
  626. /// 压缩
  627. /// </summary>
  628. private void compress()
  629. {
  630. DialogParameters value = new DialogParameters();
  631. value.Add(ParameterNames.PDFViewer, PDFViewer);
  632. dialogs.ShowDialog(DialogNames.CompressDialog, value, e =>
  633. {
  634. CompressDialogModel compressDialogModel = new CompressDialogModel();
  635. compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
  636. });
  637. }
  638. /// <summary>
  639. /// 设置Tip状态栏的显示状态
  640. /// </summary>
  641. /// <param name="show">是否显示</param>
  642. public void ShowTip(bool show)
  643. {
  644. if (show)
  645. {
  646. TipVisible = Visibility.Visible;
  647. }
  648. else
  649. {
  650. TipVisible = Visibility.Collapsed;
  651. }
  652. }
  653. /// <summary>
  654. /// 设置LeftTip状态栏的显示状态
  655. /// </summary>
  656. /// <param name="show">是否显示</param>
  657. public void ShowLeftTip(bool show)
  658. {
  659. if (show)
  660. {
  661. LeftTipVisible = Visibility.Visible;
  662. }
  663. else
  664. {
  665. LeftTipVisible = Visibility.Collapsed;
  666. }
  667. }
  668. public void CheckHaveAllPermission()
  669. {
  670. if (!SecurityHelper.CheckHaveAllPermissions(PDFViewer.Document))
  671. {
  672. ShowLeftTip(true);
  673. NavigationParameters param = new NavigationParameters();
  674. param.Add(ParameterNames.PDFViewer, PDFViewer);
  675. region.RequestNavigate(LeftTipContentRegionName, "FileRestrictedTip", param);
  676. }
  677. }
  678. public void ShowSelectedTip(ShowTipEventArgs showTipEventArgs)
  679. {
  680. switch (showTipEventArgs.enumTipKind)
  681. {
  682. case EnumTipKind.StatusNone:
  683. ShowLeftTip(false);
  684. break;
  685. case EnumTipKind.StatusSetPasswordSuccessfully:
  686. ShowLeftTip(true);
  687. region.RequestNavigate(LeftTipContentRegionName, "SetPasswordSuccessfullyTip");
  688. break;
  689. default: break;
  690. }
  691. }
  692. /// <summary>
  693. /// 右键菜单创建链接
  694. /// </summary>
  695. /// <param name="obj"></param>
  696. private void CreateLinkEvent(object obj)
  697. {
  698. if (obj is object[] objArry)
  699. {
  700. if (objArry[0] is AnnotToolContentViewModel toolContentViewModel && objArry[1] is AnnotCommandArgs annotCommand)
  701. {
  702. LinkAnnotArgs linkArgs = new LinkAnnotArgs();
  703. linkArgs.URI = string.Empty;
  704. linkArgs.LinkType = LINK_TYPE.GOTO;
  705. linkArgs.PageIndex = annotCommand.PageIndex;
  706. //linkArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  707. this.IsRightMenuCreateLink = true;
  708. List<AnnotHandlerEventArgs> lists = new List<AnnotHandlerEventArgs>();
  709. lists.Add(linkArgs);
  710. AnnotHandlerEventArgs annotArgs = toolContentViewModel.GetLink(lists);
  711. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  712. PDFViewer.SetToolParam(annotArgs);
  713. this.IsPropertyOpen = true;
  714. }
  715. }
  716. }
  717. /// <summary>
  718. /// 在文件资源管理器中显示
  719. /// </summary>
  720. private void ShowInFolder()
  721. {
  722. CommonHelper.ShowFileBrowser(PDFViewer.Document.FilePath);
  723. }
  724. private void property()
  725. {
  726. DialogParameters valuePairs = new DialogParameters();
  727. valuePairs.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  728. dialogs.ShowDialog(DialogNames.PropertyDialog, valuePairs, null);
  729. }
  730. private void share()
  731. {
  732. try
  733. {
  734. var path = PDFViewer.Document.FilePath;
  735. string subject = "分享至" + " " + PDFViewer.Document.FileName;
  736. System.Diagnostics.Process.Start("outlook", "/a,\"" + path + "\"" + "/m \"" + "&subject=" + subject + "\"");
  737. }
  738. catch
  739. {
  740. AlertsMessage alertsMessage = new AlertsMessage();
  741. alertsMessage.ShowDialog("", "未检测到Ooutlook软件,请先安装Outlook", "OK");
  742. }
  743. }
  744. private void SettingsEvent()
  745. {
  746. dialogs.ShowDialog(DialogNames.SettingsDialog, null, null);
  747. }
  748. private bool CanSaveExcute()
  749. {
  750. return CanSave;
  751. }
  752. private void MenuEnterReadModeEvent(object obj)
  753. {
  754. }
  755. public void ShowPrintDialog()
  756. {
  757. DialogParameters printValue = new DialogParameters();
  758. printValue.Add(ParameterNames.PDFViewer, PDFViewer);
  759. printValue.Add(ParameterNames.FilePath, PDFViewer.Document.FileName);
  760. printValue.Add(ParameterNames.PrintCurrentPage, PDFViewer.CurrentIndex);
  761. dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printValue, e => { });
  762. }
  763. private void InitialregionNameByTabItem(ref Dictionary<string, string> dictionary)
  764. {
  765. dictionary.Add("TabItemPageEdit", ToolContentRegionName);
  766. dictionary.Add("TabItemTool", ToolsBarContentRegionName);
  767. //其他工具菜单栏共用一个ToolsBarContentRegionName
  768. dictionary.Add("TabItemAnnotation", ToolsBarContentRegionName);
  769. dictionary.Add("TabItemConvert", ConverterBarContentRegionName);
  770. dictionary.Add("TabItemScan", ToolsBarContentRegionName);
  771. dictionary.Add("TabItemEdit", TextEditContentRegionName);
  772. dictionary.Add("TabItemForm", ToolsBarContentRegionName);
  773. dictionary.Add("TabItemFill", ToolsBarContentRegionName);
  774. }
  775. private void InitialbarContentByTabItem(ref Dictionary<string, string> dictionary)
  776. {
  777. dictionary.Add("TabItemPageEdit", "PageEditContent");
  778. dictionary.Add("TabItemTool", "ToolsBarContent");
  779. dictionary.Add("TabItemAnnotation", "AnnotToolContent");
  780. dictionary.Add("TabItemConvert", "ConverterBarContent");
  781. dictionary.Add("TabItemScan", "ScanContent");
  782. dictionary.Add("TabItemEdit", "TextEditToolContent");
  783. dictionary.Add("TabItemForm", "FormsToolContent");
  784. dictionary.Add("TabItemFill", "FillAndSignContent");
  785. }
  786. private void UpdateShowContent(string currentBar)
  787. {
  788. ToolContentVisible = Visibility.Collapsed;
  789. ToolsBarContentVisible = Visibility.Collapsed;
  790. ConverterBarContentVisible = Visibility.Collapsed;
  791. TextEditToolContentVisible = Visibility.Collapsed;
  792. OCRContentVisible = Visibility.Collapsed;
  793. switch (currentBar)
  794. {
  795. case "TabItemAnnotation":
  796. case "TabItemTool":
  797. case "TabItemForm":
  798. case "TabItemFill":
  799. ToolsBarContentVisible = Visibility.Visible;
  800. break;
  801. case "TabItemScan":
  802. ToolsBarContentVisible = Visibility.Visible;
  803. OCRContentVisible = Visibility.Visible;
  804. break;
  805. case "TabItemEdit":
  806. TextEditToolContentVisible = Visibility.Visible;
  807. break;
  808. case "TabItemConvert":
  809. ConverterBarContentVisible = Visibility.Visible;
  810. break;
  811. case "TabItemPageEdit":
  812. case "HeaderFooterContent":
  813. case "BatesContent":
  814. case "WatermarkContent":
  815. case "BackgroundContent":
  816. case "RedactionContent":
  817. ToolContentVisible = Visibility.Visible;
  818. break;
  819. default:
  820. break;
  821. }
  822. PDFEditMode(currentBar);
  823. FormMode(currentBar);
  824. }
  825. private void PDFEditMode(string currentBar)
  826. {
  827. if (currentBar == "TabItemEdit")
  828. {
  829. if (PDFViewer != null)
  830. {
  831. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  832. //SelectedPrpoertyPanel("TextEditProperty", null);
  833. //IsPropertyOpen = true;
  834. }
  835. }
  836. else
  837. {
  838. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.PDFEdit)
  839. {
  840. IsPropertyOpen = false;
  841. PDFViewer.SetMouseMode(MouseModes.PanTool);
  842. }
  843. }
  844. }
  845. private void FormMode(string currentBar)
  846. {
  847. if (currentBar == "TabItemForm")
  848. {
  849. if (PDFViewer != null)
  850. {
  851. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  852. }
  853. }
  854. else
  855. {
  856. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool)
  857. {
  858. PDFViewer.SetMouseMode(MouseModes.PanTool);
  859. }
  860. }
  861. }
  862. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  863. {
  864. if (!isInPageEdit)
  865. {
  866. //不处于页面编辑模式下时,根据PDFVIewer的undo redo状态来更新按钮状态
  867. //页面编辑模式下,按钮状态根据页面编辑的undo redo来显示
  868. if (e.PropertyName == "CanUndo")
  869. {
  870. CanUndo = PDFViewer.UndoManager.CanUndo;
  871. }
  872. if (e.PropertyName == "CanRedo")
  873. {
  874. CanRedo = PDFViewer.UndoManager.CanRedo;
  875. }
  876. }
  877. if (e.PropertyName == "CanSave")
  878. {
  879. CanSave = PDFViewer.UndoManager.CanSave;
  880. }
  881. }
  882. /// <summary>
  883. /// 选项卡切换事件
  884. /// </summary>
  885. /// <param name="e"></param>
  886. private void TabControlSelectonChangedEvent(object e)
  887. {
  888. var args = e as SelectionChangedEventArgs;
  889. if (args != null)
  890. {
  891. var item = args.AddedItems[0] as TabItem;
  892. CurrentBar = item.Name;
  893. if (previousBar != CurrentBar)
  894. {
  895. if (CurrentBar == "TabItemPageEdit")//如果是页面编辑则进入页面编辑模式
  896. {
  897. EnterToolMode(barContentByTabItem[CurrentBar]);
  898. isInPageEdit = true;
  899. }
  900. else//其余情况直接导航至对应的工具栏即可,不需要清空之前的content,region里是单例模式
  901. {
  902. EnterSelectedBar(CurrentBar);
  903. isInPageEdit = false;
  904. }
  905. previousBar = CurrentBar;
  906. }
  907. }
  908. }
  909. /// <summary>
  910. /// 阅读模式
  911. /// </summary>
  912. /// <param name="viewContent"></param>
  913. public async void RbtnReadMode()
  914. {
  915. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = true;
  916. App.IsBookMode = true;
  917. IsLoading = Visibility.Visible;
  918. await Task.Delay(1);
  919. PDFViewer.SetMouseMode(MouseModes.PanTool);
  920. NavigationParameters param = new NavigationParameters();
  921. param.Add(ParameterNames.PDFViewer, PDFViewer);
  922. param.Add(ParameterNames.ViewContentViewModel, this);
  923. region.RequestNavigate(ToolContentRegionName, "ReadViewContent", param);
  924. //ShowContent(CurrentBar, true);
  925. if (GridToolRow != 0)
  926. {
  927. GridToolRow = 0;
  928. }
  929. if (GridToolRowSpan != 4)
  930. {
  931. GridToolRowSpan = 4;
  932. }
  933. //isInPageEdit = true;
  934. UpdateShowContent("TabItemPageEdit");
  935. IsLoading = Visibility.Collapsed;
  936. }
  937. /// <summary>
  938. /// 退出阅读模式
  939. /// </summary>
  940. public async void UnReadModel()
  941. {
  942. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = false;
  943. App.IsBookMode = false;
  944. IsLoading = Visibility.Visible;
  945. await Task.Delay(1);
  946. //PDFViewer.MouseMode = MouseModes.None;
  947. if (region.Regions.ContainsRegionWithName(ViwerRegionName))
  948. {
  949. if (region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  950. {
  951. var contentRegion = region.Regions[ViwerRegionName];
  952. contentRegion.Remove(PDFViewer);
  953. }
  954. region.AddToRegion(ViwerRegionName, PDFViewer);
  955. }
  956. if (string.IsNullOrEmpty(CurrentBar) || CurrentBar.Equals("TabItemPageEdit", StringComparison.OrdinalIgnoreCase))
  957. {
  958. EnterSelectedBar("TabItemAnnotation");
  959. }
  960. else
  961. {
  962. EnterSelectedBar(CurrentBar);
  963. }
  964. bool isExist = false;
  965. if (region.Regions.ContainsRegionWithName(TipContentRegionName))
  966. {
  967. var views = region.Regions[TipContentRegionName].Views;
  968. foreach (var item in views)
  969. {
  970. if (item is Views.TipContent.ReadModelTip readModelTip)
  971. {
  972. isExist = true;
  973. break;
  974. }
  975. }
  976. }
  977. if (isExist == false)
  978. {
  979. region.RequestNavigate(TipContentRegionName, "ReadModelTip");
  980. }
  981. IsLoading = Visibility.Collapsed;
  982. //ReadModelTip = Visibility.Visible;
  983. ShowTip(true);
  984. await Task.Delay(3000);
  985. //ReadModelTip = Visibility.Collapsed;
  986. TipVisible = Visibility.Collapsed;
  987. }
  988. #region PDFViewer鼠标滚轮缩放事件
  989. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  990. {
  991. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  992. PDFViewer.Zoom(newZoom);
  993. }
  994. public double CheckZoomLevel(double zoom, bool IsGrowth)
  995. {
  996. double standardZoom = 100;
  997. if (zoom <= 0.01)
  998. {
  999. return 0.01;
  1000. }
  1001. if (zoom >= 10)
  1002. {
  1003. return 10;
  1004. }
  1005. zoom *= 100;
  1006. for (int i = 0; i < zoomLevel.Length - 1; i++)
  1007. {
  1008. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  1009. {
  1010. standardZoom = zoomLevel[i + 1];
  1011. break;
  1012. }
  1013. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  1014. {
  1015. standardZoom = zoomLevel[i];
  1016. break;
  1017. }
  1018. }
  1019. return standardZoom / 100;
  1020. }
  1021. #endregion PDFViewer鼠标滚轮缩放事件
  1022. #region Navigate
  1023. public void OnNavigatedTo(NavigationContext navigationContext)
  1024. {
  1025. if (isOpenFile)
  1026. return;
  1027. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  1028. if (mainVM != null)
  1029. {
  1030. mainViewModel = mainVM;
  1031. mainViewModel.viewContentViewModel = this;
  1032. }
  1033. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  1034. if (pdfview != null)
  1035. {
  1036. PDFViewer = pdfview;
  1037. if (pdfview.Tag != null)
  1038. {
  1039. //保存密码
  1040. PassWord = pdfview.Tag.ToString();
  1041. }
  1042. loadFile();
  1043. CheckHaveAllPermission();
  1044. }
  1045. isOpenFile = true;
  1046. }
  1047. public bool IsNavigationTarget(NavigationContext navigationContext)
  1048. {
  1049. return false;
  1050. }
  1051. public void OnNavigatedFrom(NavigationContext navigationContext)
  1052. {
  1053. }
  1054. #endregion Navigate
  1055. #region 方法
  1056. /// <summary>
  1057. /// 视图面板 切换分屏模式
  1058. /// </summary>
  1059. /// <param name="mode"></param>
  1060. public void EnterSplitMode(SplitMode mode)
  1061. {
  1062. ///通知UI层更改布局
  1063. this.events.GetEvent<SplitEvent>().Publish(new SplitEventArgs() { Unicode = unicode, Mode = mode });
  1064. }
  1065. private void Undo()
  1066. {
  1067. if (isInPageEdit)
  1068. {
  1069. //执行页面编辑的Undo
  1070. PageEditUndo?.Invoke();
  1071. }
  1072. else
  1073. {
  1074. PDFViewer.UndoManager.Undo();
  1075. }
  1076. }
  1077. private void Redo()
  1078. {
  1079. if (isInPageEdit)
  1080. {
  1081. //执行页面编辑的Redo
  1082. PageEditRedo?.Invoke();
  1083. }
  1084. else
  1085. {
  1086. PDFViewer.UndoManager.Redo();
  1087. }
  1088. }
  1089. private void LoadControl()
  1090. {
  1091. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  1092. // 非必要情况不要使用该异步方法,可能会导致一次性加载多个文件时出现因异步引起的regionname 错乱问题
  1093. //System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  1094. // {
  1095. NavigationParameters parameters = new NavigationParameters();
  1096. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  1097. parameters.Add(ParameterNames.ViewContentViewModel, this);
  1098. region.RequestNavigate(BOTARegionName, "BOTAContent", parameters);
  1099. region.RequestNavigate(BottomToolRegionName, "BottomToolContent", parameters);
  1100. region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
  1101. region.RequestNavigate(SplitViewerRegionName, "SplitScreenContent", parameters);
  1102. //TODO 根据上一次关闭记录的菜单,选中TabItem
  1103. EnterSelectedBar("TabItemAnnotation");
  1104. //}
  1105. //));
  1106. }
  1107. /// <summary>
  1108. /// 各个注释(选中和创建注释)导航到对应注释的属性面板
  1109. /// </summary>
  1110. /// <param name="Content"></param>
  1111. /// <param name="annotPropertyPanel"></param>
  1112. public void SelectedPrpoertyPanel(string Content, AnnotPropertyPanel annotPropertyPanel)
  1113. {
  1114. NavigationParameters parameters = new NavigationParameters();
  1115. //传其他参数:文档类,空注释面板;
  1116. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  1117. parameters.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  1118. parameters.Add(ParameterNames.ViewContentViewModel, this);
  1119. region.RequestNavigate(PropertyRegionName, Content, parameters);
  1120. }
  1121. /// <summary>
  1122. /// 将PDFViwer添加到Region
  1123. /// </summary>
  1124. private void loadFile()
  1125. {
  1126. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  1127. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  1128. CanSave = PDFViewer.UndoManager.CanSave;
  1129. CanUndo = PDFViewer.UndoManager.CanUndo;
  1130. CanRedo = PDFViewer.UndoManager.CanRedo;
  1131. region.AddToRegion(ViwerRegionName, PDFViewer);
  1132. }
  1133. /// <summary>
  1134. /// 已有路径文档的保存逻辑
  1135. /// </summary>
  1136. public bool saveFile()
  1137. {
  1138. try
  1139. {
  1140. if (string.IsNullOrEmpty(PDFViewer.Document.FilePath))
  1141. return saveAsFile();
  1142. //文档已被修复时 提示另存为
  1143. if (PDFViewer.Document.HasRepaired)
  1144. {
  1145. AlertsMessage alertsMessage = new AlertsMessage();
  1146. alertsMessage.ShowDialog("", "文件已被修复,建议另存为", "Cancel", "OK");
  1147. if (alertsMessage.result == ContentResult.Ok)
  1148. return saveAsFile();
  1149. else
  1150. return false;
  1151. }
  1152. //文件路径无法存在时
  1153. if (!File.Exists(PDFViewer.Document.FilePath))
  1154. {
  1155. AlertsMessage alertsMessage = new AlertsMessage();
  1156. alertsMessage.ShowDialog("", "文件路径不存在,需要另存为", "Cancel", "OK");
  1157. if (alertsMessage.result == ContentResult.Ok)
  1158. return saveAsFile();
  1159. else
  1160. return false;
  1161. }
  1162. //只读文件无法写入时,提示另存为
  1163. FileInfo fileInfo = new FileInfo(PDFViewer.Document.FilePath);
  1164. if (fileInfo.IsReadOnly)
  1165. {
  1166. AlertsMessage alertsMessage = new AlertsMessage();
  1167. alertsMessage.ShowDialog("", "文件为只读文件,需要另存为", "Cancel", "OK");
  1168. if (alertsMessage.result == ContentResult.Ok)
  1169. return saveAsFile();
  1170. else
  1171. return false;
  1172. }
  1173. if (SecurityInfo.IsPasswordChanged)
  1174. {
  1175. string currentFilePath = PDFViewer.Document.FilePath;
  1176. string tempFilePath = PDFViewer.Document.FilePath + ".temp.pdf";
  1177. string openPassword = null;
  1178. string permissionsPassword = null;
  1179. CPDFPermissionsInfo cPDFPermissionsInfo = CreateDefaultPermissionsInfo();
  1180. if (SecurityInfo.NeedOpenPassword)
  1181. {
  1182. openPassword = SecurityInfo.OpenPassword;
  1183. }
  1184. if (SecurityInfo.NeedPermissionsPassword)
  1185. {
  1186. permissionsPassword = SecurityInfo.PermissionsPassword;
  1187. cPDFPermissionsInfo = SecurityInfo.CPDFPermissionsInfo;
  1188. }
  1189. if (true)
  1190. {
  1191. PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1192. AlertsMessage alertsMessage = new AlertsMessage();
  1193. alertsMessage.ShowDialog("", "文件安全设置已修改,需要另存为", "Cancel", "OK");
  1194. if (alertsMessage.result == ContentResult.Ok)
  1195. return saveAsFile();
  1196. else
  1197. return false;
  1198. }
  1199. else
  1200. {
  1201. string currentPath = PDFViewer.Document.FilePath;
  1202. string tempPath = PDFViewer.Document.FilePath + ".temp.pdf";
  1203. PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1204. PDFViewer.Document.WriteToFilePath(tempPath);
  1205. PDFViewer.InitDocument(tempPath);
  1206. //PDFViewer.Document.UnlockWithPassword(permissionsPassword);
  1207. //PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1208. PDFViewer.Document.WriteToFilePath(currentPath);
  1209. System.IO.File.Delete(tempPath);
  1210. PDFViewer.CloseDocument();
  1211. PDFViewer.InitDocument(currentPath);
  1212. PDFViewer.Document.UnlockWithPassword(permissionsPassword);
  1213. }
  1214. }
  1215. bool result = PDFViewer.Document.WriteToLoadedPath();
  1216. if (result)
  1217. {
  1218. PDFViewer.UndoManager.CanSave = false;
  1219. App.Current.Dispatcher.Invoke(() =>
  1220. {
  1221. //保存时更新缩略图
  1222. OpenFileInfo info = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  1223. try
  1224. {
  1225. if (!string.IsNullOrEmpty(info.ThumbImgPath) && !PDFViewer.Document.IsEncrypted)//加密的文档不获取缩略图
  1226. {
  1227. var size = PDFViewer.Document.GetPageSize(0);
  1228. System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)size.Width, (int)size.Height, 0, true, true);
  1229. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  1230. if (File.Exists(folderPath))
  1231. File.Delete(folderPath);
  1232. DirectoryInfo folder = new DirectoryInfo(folderPath);
  1233. if (!folder.Exists)
  1234. folder.Create();
  1235. string imagePath = info.ThumbImgPath;
  1236. if (!File.Exists(imagePath))//由加密文档变为非加密文档时 新建一个路径
  1237. {
  1238. string imageName = Guid.NewGuid().ToString();
  1239. imagePath = System.IO.Path.Combine(folderPath, imageName);
  1240. using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  1241. {
  1242. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  1243. }
  1244. }
  1245. else
  1246. {
  1247. using (FileStream stream = new FileStream(imagePath, FileMode.Open))
  1248. {
  1249. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  1250. }
  1251. }
  1252. info.ThumbImgPath = imagePath;
  1253. SettingHelper.SetFileInfo(info);
  1254. }
  1255. }
  1256. catch
  1257. {
  1258. info.ThumbImgPath = null;
  1259. SettingHelper.SetFileInfo(info);
  1260. }
  1261. });
  1262. }
  1263. else
  1264. {
  1265. //文件被占用 保存失败时
  1266. AlertsMessage alertsMessage = new AlertsMessage();
  1267. alertsMessage.ShowDialog("", "文件被占用,需要另存为", "Cancel", "OK");
  1268. if (alertsMessage.result == ContentResult.Ok)
  1269. return saveAsFile();
  1270. else
  1271. return false;
  1272. }
  1273. return result;
  1274. }
  1275. catch { return false; }
  1276. }
  1277. /// <summary>
  1278. /// 另存为或新文档保存逻辑
  1279. /// </summary>
  1280. public bool saveAsFile(Action RedactionAction = null)
  1281. {
  1282. var dlg = new Microsoft.Win32.SaveFileDialog();
  1283. dlg.Filter = Properties.Resources.OpenDialogFilter;
  1284. dlg.FileName = PDFViewer.Document.FileName;
  1285. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  1286. {
  1287. bool result = false;
  1288. if (RedactionAction != null)
  1289. {
  1290. //保存前进行标记密文处理应用或擦除
  1291. RedactionAction.Invoke();
  1292. }
  1293. if (App.OpenedFileList.Contains(dlg.FileName))
  1294. {
  1295. //提示文件已经被打开
  1296. }
  1297. else
  1298. {
  1299. result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
  1300. if (result)
  1301. {
  1302. DoAfterSaveAs(dlg.FileName);
  1303. }
  1304. else
  1305. {
  1306. //提示文件被其他软件占用 无法保存
  1307. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileOccupiedWarning"), "", Winform.MessageBoxButtons.OKCancel, new string[] { App.MainPageLoader.GetString("Main_SaveAs"), App.MainPageLoader.GetString("Main_Cancel") })
  1308. }
  1309. ;
  1310. }
  1311. return result;
  1312. }
  1313. else
  1314. return false;
  1315. }
  1316. /// <summary>
  1317. /// 另存为Flatten
  1318. /// </summary>
  1319. private void saveAsFlatten()
  1320. {
  1321. var dlg = new SaveFileDialog();
  1322. dlg.Filter = Properties.Resources.OpenDialogFilter;
  1323. dlg.FileName = PDFViewer.Document.FileName.Substring(0, PDFViewer.Document.FileName.LastIndexOf(".")) + "_Flatten.pdf";
  1324. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  1325. {
  1326. saveFile();
  1327. CPDFDocument kmdoc = CPDFDocument.InitWithFilePath(PDFViewer.Document.FileName);
  1328. if (kmdoc == null)
  1329. return;
  1330. bool result = kmdoc.WriteFlattenToFilePath(dlg.FileName);
  1331. if (result)
  1332. System.Diagnostics.Process.Start("explorer", "/select,\"" + dlg.FileName + "\"");
  1333. kmdoc.Release();
  1334. }
  1335. }
  1336. /// <summary>
  1337. /// 另存为后进行的操作
  1338. /// 重新打开新文档
  1339. /// </summary>
  1340. /// <param name="targetPath"></param>
  1341. public void DoAfterSaveAs(string targetPath)
  1342. {
  1343. App.OpenedFileList.Remove(targetPath);
  1344. string oldFilePath = targetPath;
  1345. PDFViewer.UndoManager.CanSave = false;
  1346. mainViewModel.OpenFile(targetPath);
  1347. //TODO:通知各模块更新PDFview对象
  1348. //var result = OpenFile(targetPath, true);
  1349. //if (result)
  1350. //{
  1351. // FileChanged.Invoke(this, null);
  1352. // Zoomer.PdfViewer = PdfViewer;
  1353. // PageSelector.PdfViewer = PdfViewer;
  1354. // ViewModeSelector.PdfViewer = PdfViewer;
  1355. // OpenFileInfo fileInfo = SettingHelper.GetFileInfo(oldFilePath);
  1356. // if (fileInfo != null)
  1357. // {
  1358. // PdfViewer.ChangeViewMode(fileInfo.LastViewMode);
  1359. // PdfViewer.ChangeFitMode(fileInfo.LastFitMode);
  1360. // if (fileInfo.LastFitMode == FitMode.FitFree)
  1361. // PdfViewer.Zoom(fileInfo.LastZoom);
  1362. // PdfViewer.GoToPage(fileInfo.LastPageIndex);
  1363. // if (fileInfo.LastDrawMode == DrawModes.Draw_Mode_Custom && fileInfo.LastFillColor != 0)
  1364. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode, fileInfo.LastFillColor);
  1365. // else
  1366. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode);
  1367. // }
  1368. //}
  1369. }
  1370. /// <summary>
  1371. /// 显示前添加内容到Region
  1372. /// </summary>
  1373. /// <param name="isPageEdit"></param>
  1374. private void ShowContent(string currentBar, bool isToolMode = false)
  1375. {
  1376. GridVisibility = Visibility.Visible;
  1377. //显示页面编辑或其他工具
  1378. if (currentBar == "TabItemPageEdit" || isToolMode)
  1379. {
  1380. if (currentBar == "TabItemPageEdit")//进入页面编辑
  1381. {
  1382. if (GridToolRow != 1)
  1383. {
  1384. GridToolRow = 1;
  1385. }
  1386. if (GridToolRowSpan != 3)
  1387. {
  1388. GridToolRowSpan = 3;
  1389. }
  1390. }
  1391. else//进入水印等其他工具模式
  1392. {
  1393. GridVisibility = Visibility.Collapsed;
  1394. if (GridToolRow != 0)
  1395. {
  1396. GridToolRow = 0;
  1397. }
  1398. if (GridToolRowSpan != 4)
  1399. {
  1400. GridToolRowSpan = 4;
  1401. }
  1402. }
  1403. //ToolContent的visible跟toolsbarContent 的visible是互斥的
  1404. UpdateShowContent(currentBar);
  1405. }
  1406. else
  1407. {
  1408. if (GridToolRow != 1)
  1409. {
  1410. GridToolRow = 1;
  1411. }
  1412. UpdateShowContent(currentBar);
  1413. }
  1414. }
  1415. /// <summary>
  1416. /// 从二级工具栏进入需要修改界面布局的场景
  1417. /// </summary>
  1418. /// <param name="EditToolName"></param>
  1419. private void EnterEditTools(StringWithUnicode EditToolName)
  1420. {
  1421. EnterSelectedEditTool(EditToolName);
  1422. }
  1423. private void CloseEditTool(EnumCloseModeUnicode enumCloseModeunicode)
  1424. {
  1425. EnumCloseMode enumCloseMode = enumCloseModeunicode.Status;
  1426. if (enumCloseMode == EnumCloseMode.StatusConfirm)
  1427. {
  1428. PDFViewer.Document.ReleasePages();
  1429. PDFViewer.ReloadDocument();
  1430. }
  1431. //如果是其他工具共用一个PDFview,退出工具模式后,需要重新添加到Region,否则不会显示
  1432. if (!region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  1433. {
  1434. region.AddToRegion(ViwerRegionName, PDFViewer);
  1435. }
  1436. CurrentBar = "TabItemTool";
  1437. EnterSelectedBar(CurrentBar);
  1438. }
  1439. /// <summary>
  1440. /// 二级菜单指定目标处理
  1441. /// </summary>
  1442. /// <param name="e"></param>
  1443. private void EnterSelectedEditTool(StringWithUnicode EditToolName)
  1444. {
  1445. CurrentBar = EditToolName.EditToolsContentName;
  1446. EnterToolMode(CurrentBar);
  1447. }
  1448. /// <summary>
  1449. /// 进入工具编辑(如页面编辑、水印、密文等)模式
  1450. /// </summary>
  1451. /// <param name="targetToolMode">要导航过去的控件名称</param>
  1452. /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
  1453. private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
  1454. {
  1455. IsLoading = Visibility.Visible;
  1456. await Task.Delay(3);
  1457. NavigationParameters param = new NavigationParameters();
  1458. if (valuePairs == null)
  1459. {
  1460. param.Add(ParameterNames.PDFViewer, PDFViewer);
  1461. param.Add(ParameterNames.ViewContentViewModel, this);
  1462. }
  1463. else//有传入其他内容的参数时
  1464. {
  1465. param = valuePairs;
  1466. }
  1467. region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
  1468. ShowContent(CurrentBar, true);
  1469. IsLoading = Visibility.Collapsed;
  1470. }
  1471. private void EnterSelectedBar(string currentBar)
  1472. {
  1473. NavigationParameters param = new NavigationParameters();
  1474. param.Add(ParameterNames.PDFViewer, PDFViewer);
  1475. param.Add(ParameterNames.ViewContentViewModel, this);
  1476. region.RequestNavigate(regionNameByTabItem[currentBar], barContentByTabItem[currentBar], param);
  1477. if (currentBar == "TabItemScan")
  1478. {
  1479. region.RequestNavigate(RegionNames.OCRViewerRegionName, "ScanViwer", param);
  1480. }
  1481. ShowContent(currentBar);
  1482. }
  1483. /// <summary>
  1484. /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
  1485. /// </summary>
  1486. public void ExitToolMode()
  1487. {
  1488. ToolContentVisible = Visibility.Collapsed;
  1489. if (isInPageEdit)
  1490. {
  1491. TabSelectedIndex = 0;
  1492. isInPageEdit = false;
  1493. }
  1494. }
  1495. #endregion 方法
  1496. }
  1497. }