ViewContentViewModel.cs 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344
  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. using PDF_Office.Views.PropertyPanel.AnnotPanel;
  29. using System.Linq;
  30. using System.Text;
  31. using PDF_Office.Views.Tools;
  32. using System.Windows.Input;
  33. using PDF_Office.ViewModels.PropertyPanel.AnnotPanel;
  34. using ImTools;
  35. using PDF_Office.Properties;
  36. using PDF_Office.ViewModels.BOTA;
  37. using PDF_Office.Views.BOTA;
  38. using System.Drawing;
  39. using PDF_Office.ViewModels.Tools.AnnotManager;
  40. namespace PDF_Office.ViewModels
  41. {
  42. public class ViewContentViewModel : BindableBase, INavigationAware
  43. {
  44. #region 文案
  45. private string T_file;
  46. public string T_File
  47. {
  48. get { return T_file; }
  49. set
  50. {
  51. SetProperty(ref T_file, value);
  52. }
  53. }
  54. private string T_save;
  55. public string T_Save
  56. {
  57. get { return T_save; }
  58. set
  59. {
  60. SetProperty(ref T_save, value);
  61. }
  62. }
  63. private string T_undo;
  64. public string T_Undo
  65. {
  66. get { return T_undo; }
  67. set
  68. {
  69. SetProperty(ref T_undo, value);
  70. }
  71. }
  72. private string T_redo;
  73. public string T_Redo
  74. {
  75. get { return T_redo; }
  76. set
  77. {
  78. SetProperty(ref T_redo, value);
  79. }
  80. }
  81. private string T_annotation;
  82. public string T_Annotation
  83. {
  84. get { return T_annotation; }
  85. set
  86. {
  87. SetProperty(ref T_annotation, value);
  88. }
  89. }
  90. private string T_pageEdit;
  91. public string T_PageEdit
  92. {
  93. get { return T_pageEdit; }
  94. set
  95. {
  96. SetProperty(ref T_pageEdit, value);
  97. }
  98. }
  99. private string T_converter;
  100. public string T_Converter
  101. {
  102. get { return T_converter; }
  103. set
  104. {
  105. SetProperty(ref T_converter, value);
  106. }
  107. }
  108. private string T_fillSign;
  109. public string T_FillSign
  110. {
  111. get { return T_fillSign; }
  112. set
  113. {
  114. SetProperty(ref T_fillSign, value);
  115. }
  116. }
  117. private string T_tools;
  118. public string T_Tools
  119. {
  120. get { return T_tools; }
  121. set
  122. {
  123. SetProperty(ref T_tools, value);
  124. }
  125. }
  126. private string T_print;
  127. public string T_Print
  128. {
  129. get { return T_print; }
  130. set
  131. {
  132. SetProperty(ref T_print, value);
  133. }
  134. }
  135. private string T_share;
  136. public string T_Share
  137. {
  138. get { return T_share; }
  139. set
  140. {
  141. SetProperty(ref T_share, value);
  142. }
  143. }
  144. private string T_properties;
  145. public string T_Properties
  146. {
  147. get { return T_properties; }
  148. set
  149. {
  150. SetProperty(ref T_properties, value);
  151. }
  152. }
  153. private void InitStirng()
  154. {
  155. T_File = App.MainPageLoader.GetString("ViewTopToolbar_File");
  156. T_Save = App.MainPageLoader.GetString("ViewTopToolbar_Save");
  157. T_Undo = App.MainPageLoader.GetString("ViewTopToolbar_Undo");
  158. T_Redo = App.MainPageLoader.GetString("ViewTopToolbar_Redo");
  159. T_Annotation = App.MainPageLoader.GetString("ViewTopToolbar_Annotation");
  160. T_PageEdit = App.MainPageLoader.GetString("ViewTopToolbar_PageEdit");
  161. T_Converter = App.MainPageLoader.GetString("ViewTopToolbar_Converter");
  162. T_FillSign = App.MainPageLoader.GetString("ViewTopToolbar_Fill&Sign");
  163. T_Tools = App.MainPageLoader.GetString("ViewTopToolbar_Tools");
  164. T_Print = App.MainPageLoader.GetString("ViewTopToolbar_Print");
  165. T_Share = App.MainPageLoader.GetString("ViewTopToolbar_Share");
  166. T_Properties = App.MainPageLoader.GetString("ViewTopToolbar_Properties");
  167. }
  168. #endregion 文案
  169. #region 属性、变量
  170. public OpenFileInfo OpenFileInfo = null;
  171. public CPDFViewer PDFViewer { get; set; }
  172. public MainContentViewModel mainViewModel { get; set; }
  173. public IRegionManager region;
  174. public IDialogService dialogs;
  175. public IEventAggregator events;
  176. public bool IsRightMenuCreateLink { get => isCreateLink; set => isCreateLink = value; }
  177. public string ViwerRegionName { get; set; }
  178. /// <summary>
  179. /// 分屏视图的region名称
  180. /// </summary>
  181. public string SplitViewerRegionName { get; set; }
  182. /// <summary>
  183. /// OCR视图名称
  184. /// </summary>
  185. public string OCRViewerRegionName { get; set; }
  186. public string BOTARegionName { get; set; }
  187. public string PropertyRegionName { get; set; }
  188. public string ToolContentRegionName { get; set; }
  189. public string ToolsBarContentRegionName { get; set; }
  190. /// <summary>
  191. /// 顶部提示的Content
  192. /// </summary>
  193. public string TipContentRegionName { get; set; }
  194. public string LeftTipContentRegionName { get; set; }
  195. //便签弹窗
  196. public string NotePopupRegionName { get; set; }
  197. public string ReadModeRegionName { get; set; }
  198. public string ConverterBarContentRegionName { get; set; }
  199. public string TextEditContentRegionName { get; set; }
  200. public string BackgroundContentRegionName { get; set; }
  201. //若point不赋值或从列表中找不到point,系统会提供默认值为(0,0)的新point,所以改用Tuple<int,int>
  202. public List<Tuple<int, int>> FillAndSign = new List<Tuple<int, int>>();
  203. /// <summary>
  204. /// 文档的密码
  205. /// </summary>
  206. public string PassWord { get; set; }
  207. private Visibility tipVisible = Visibility.Collapsed;
  208. /// <summary>
  209. /// 顶部提示栏的显示状态
  210. /// </summary>
  211. public Visibility TipVisible
  212. {
  213. get { return tipVisible; }
  214. set
  215. {
  216. SetProperty(ref tipVisible, value);
  217. }
  218. }
  219. private Visibility leftTipVisible = Visibility.Collapsed;
  220. public Visibility LeftTipVisible
  221. {
  222. get { return leftTipVisible; }
  223. set
  224. {
  225. SetProperty(ref leftTipVisible, value);
  226. }
  227. }
  228. private Visibility notePopupVisible = Visibility.Collapsed;
  229. public Visibility NotePopupVisible
  230. {
  231. get { return notePopupVisible; }
  232. set
  233. {
  234. SetProperty(ref notePopupVisible, value);
  235. }
  236. }
  237. public bool IsNoteAdd = false;
  238. private double canvasNoteLeft;
  239. public double CanvasNoteLeft
  240. {
  241. get { return canvasNoteLeft; }
  242. set
  243. {
  244. SetProperty(ref canvasNoteLeft, value);
  245. }
  246. }
  247. private double canvasNoteBottom;
  248. public double CanvasNoteBottom
  249. {
  250. get { return canvasNoteBottom; }
  251. set
  252. {
  253. SetProperty(ref canvasNoteBottom, value);
  254. }
  255. }
  256. /// <summary>
  257. /// 底部工具栏 RegionName
  258. /// </summary>
  259. public string BottomToolRegionName { get; set; }
  260. private bool _isInPageEdit = false;
  261. /// <summary>
  262. /// 是否处于页面编辑模式,用于执行undo redo 的具体操作
  263. /// </summary>
  264. public bool isInPageEdit
  265. {
  266. get { return _isInPageEdit; }
  267. set
  268. {
  269. SetProperty(ref _isInPageEdit, value);
  270. if (!value)
  271. {
  272. CanRedo = PDFViewer.UndoManager.CanRedo;
  273. CanUndo = PDFViewer.UndoManager.CanUndo;
  274. }
  275. }
  276. }
  277. public Action PageEditUndo { get; set; }
  278. public Action PageEditRedo { get; set; }
  279. /// <summary>
  280. ///工具条
  281. ///0:收起
  282. ///40:显示
  283. /// </summary>
  284. private int toolRowHeight = 40;
  285. public int ToolRowHeight
  286. {
  287. get { return toolRowHeight; }
  288. set
  289. {
  290. SetProperty(ref toolRowHeight, value);
  291. }
  292. }
  293. /// <summary>
  294. /// 水印,背景侧边栏宽度
  295. /// 0:收起
  296. /// 260:显示
  297. /// </summary>
  298. private int propertyColumnWidth = 0;
  299. public int PropertyColumnWidth
  300. {
  301. get { return propertyColumnWidth; }
  302. set
  303. {
  304. SetProperty(ref propertyColumnWidth, value);
  305. }
  306. }
  307. private int gridToolRow = 1;
  308. /// <summary>
  309. /// 控制ToolContent的Row
  310. /// </summary>
  311. public int GridToolRow
  312. {
  313. get { return gridToolRow; }
  314. set
  315. {
  316. SetProperty(ref gridToolRow, value);
  317. }
  318. }
  319. private int gridToolRowSpan = 3;
  320. /// <summary>
  321. /// 控制ToolContent的RowSpan
  322. /// </summary>
  323. public int GridToolRowSpan
  324. {
  325. get { return gridToolRowSpan; }
  326. set
  327. {
  328. SetProperty(ref gridToolRowSpan, value);
  329. }
  330. }
  331. private Visibility toolContentVisible = Visibility.Collapsed;
  332. /// <summary>
  333. /// 控制Content的显示 用于显示水印、贝茨码、密文等功能模块
  334. /// 留意:显示前需要先注入内容、设置好行和跨行数
  335. /// </summary>
  336. public Visibility ToolContentVisible
  337. {
  338. get { return toolContentVisible; }
  339. set
  340. {
  341. SetProperty(ref toolContentVisible, value);
  342. }
  343. }
  344. private Visibility gridVisibility = Visibility.Visible;
  345. /// <summary>
  346. /// 是否正在加载中
  347. /// </summary>
  348. public Visibility GridVisibility
  349. {
  350. get { return gridVisibility; }
  351. set
  352. {
  353. SetProperty(ref gridVisibility, value);
  354. }
  355. }
  356. private Visibility isLoading = Visibility.Collapsed;
  357. private Visibility ocrContentVisible = Visibility.Collapsed;
  358. public Visibility OCRContentVisible
  359. {
  360. get { return ocrContentVisible; }
  361. set
  362. {
  363. SetProperty(ref ocrContentVisible, value);
  364. }
  365. }
  366. /// <summary>
  367. /// 是否正在加载中
  368. /// </summary>
  369. public Visibility IsLoading
  370. {
  371. get { return isLoading; }
  372. set
  373. {
  374. SetProperty(ref isLoading, value);
  375. }
  376. }
  377. private Visibility converterBarContentVisible = Visibility.Collapsed;
  378. private Visibility toolsbarContentVisible = Visibility.Collapsed;
  379. /// <summary>
  380. /// 控制ToolsBarContent的显示
  381. /// 留意:显示前需要先注入内容、设置好行和跨行数
  382. /// </summary>
  383. public Visibility ConverterBarContentVisible
  384. {
  385. get { return converterBarContentVisible; }
  386. set
  387. {
  388. SetProperty(ref converterBarContentVisible, value);
  389. }
  390. }
  391. private Visibility toolsBarContentVisible = Visibility.Collapsed;
  392. /// <summary>
  393. /// 控制ToolsBarContent的显示
  394. /// 留意:显示前需要先注入内容、设置好行和跨行数
  395. /// </summary>
  396. public Visibility ToolsBarContentVisible
  397. {
  398. get { return toolsBarContentVisible; }
  399. set
  400. {
  401. SetProperty(ref toolsBarContentVisible, value);
  402. }
  403. }
  404. private Visibility textEditToolContentVisible = Visibility.Collapsed;
  405. /// <summary>
  406. /// 控制ToolsBarContent的显示
  407. /// 留意:显示前需要先注入内容、设置好行和跨行数
  408. /// </summary>
  409. public Visibility TextEditToolContentVisible
  410. {
  411. get { return textEditToolContentVisible; }
  412. set
  413. {
  414. SetProperty(ref textEditToolContentVisible, value);
  415. }
  416. }
  417. private bool isPorpertyOpen = false;
  418. /// <summary>
  419. /// 属性栏是否展开
  420. /// </summary>
  421. public bool IsPropertyOpen
  422. {
  423. get { return isPorpertyOpen; }
  424. set
  425. {
  426. //如果设置为手动展开属性面板时,则不响应代码控制的展开
  427. if (Settings.Default.AppProperties.InitialVIew.AutoExpandProperty)
  428. {
  429. SetProperty(ref isPorpertyOpen, value);
  430. }
  431. }
  432. }
  433. private Visibility isReadMode = Visibility.Visible;
  434. /// <summary>
  435. ///是否为阅读模式
  436. /// </summary>
  437. public Visibility IsReadMode
  438. {
  439. get { return isReadMode; }
  440. set
  441. {
  442. SetProperty(ref isReadMode, value);
  443. }
  444. }
  445. private bool canSave;
  446. /// <summary>
  447. /// 是否可以保存
  448. /// </summary>
  449. public bool CanSave
  450. {
  451. get { return canSave; }
  452. set
  453. {
  454. if (events != null && canSave == false)
  455. this.events.GetEvent<FillAndSignEvent>().Publish(new FillAndSigntEventArgs { Unicode = "Clear", AppUnicode = unicode });
  456. SetProperty(ref canSave, value);
  457. }
  458. }
  459. private bool canUndo;
  460. /// <summary>
  461. /// 是否可以进行Undo
  462. /// </summary>
  463. public bool CanUndo
  464. {
  465. get { return canUndo; }
  466. set
  467. {
  468. SetProperty(ref canUndo, value);
  469. }
  470. }
  471. private bool canRedo;
  472. /// <summary>
  473. /// 是否可以进行Redo
  474. /// </summary>
  475. public bool CanRedo
  476. {
  477. get { return canRedo; }
  478. set
  479. {
  480. SetProperty(ref canRedo, value);
  481. }
  482. }
  483. private GridLength botaWidth = new GridLength(48);
  484. /// <summary>
  485. /// BOTA栏的宽度
  486. /// </summary>
  487. public GridLength BOTAWidth
  488. {
  489. get { return botaWidth; }
  490. set
  491. {
  492. SetProperty(ref botaWidth, value);
  493. if (botaWidth.Value <= 48)
  494. {
  495. OpenBOTA = false;
  496. }
  497. }
  498. }
  499. private int selectedIndex;
  500. /// <summary>
  501. /// 工具栏选中项的索引
  502. /// </summary>
  503. public int TabSelectedIndex
  504. {
  505. get { return selectedIndex; }
  506. set
  507. {
  508. SetProperty(ref selectedIndex, value);
  509. }
  510. }
  511. private bool openBOTA = false;
  512. /// <summary>
  513. /// 是否展开BOTA
  514. /// </summary>
  515. public bool OpenBOTA
  516. {
  517. get { return openBOTA; }
  518. set
  519. {
  520. SetProperty(ref openBOTA, value);
  521. if (openBOTA && BOTAWidth.Value <= 48)
  522. {
  523. BOTAWidth = new GridLength(260);
  524. }
  525. //关闭BOTA的逻辑在xaml.cs代码里控制,无法绑定后台
  526. }
  527. }
  528. public SecurityInfo SecurityInfo = new SecurityInfo();
  529. private Dictionary<string, string> regionNameByTabItem;
  530. private Dictionary<string, string> barContentByTabItem;
  531. private string previousBar = "";
  532. public string CurrentBar = "";
  533. public string unicode = null;
  534. /// <summary>
  535. /// 用来避免重复触发导航事件的标志符
  536. /// </summary>
  537. private bool isOpenFile = false;
  538. /// <summary>
  539. /// 鼠标滚轮缩放的缩放值
  540. /// </summary>
  541. private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  542. /// <summary>
  543. /// 注释-链接,提示语
  544. /// </summary>
  545. private Visibility linkAnnotTipVisibility = Visibility.Collapsed;
  546. public Visibility LinkAnnotTipVisibility
  547. {
  548. get { return linkAnnotTipVisibility; }
  549. set
  550. {
  551. SetProperty(ref linkAnnotTipVisibility, value);
  552. }
  553. }
  554. 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";
  555. private bool isCreateLink = false;
  556. public string LinkAnnotTipText
  557. {
  558. get { return linkAnnotTipText; }
  559. set
  560. {
  561. SetProperty(ref linkAnnotTipText, value);
  562. }
  563. }
  564. #endregion 属性、变量
  565. #region 命令
  566. public DelegateCommand LoadFile { get; set; }
  567. public DelegateCommand Load { get; set; }
  568. public DelegateCommand MouseDown { get; set; }
  569. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  570. public DelegateCommand SaveFile { get; set; }
  571. public DelegateCommand SaveAsFile { get; set; }
  572. public DelegateCommand SaveAsFlattenCommand { get; set; }
  573. public DelegateCommand UndoCommand { get; set; }
  574. public DelegateCommand RedoCommand { get; set; }
  575. public DelegateCommand<object> MenuEnterReadMode { get; set; }
  576. public DelegateCommand PrintCommand { get; set; }
  577. public DelegateCommand SelectCommand { get; set; }
  578. public DelegateCommand SettingsCommand { get; set; }
  579. public DelegateCommand ShareCommand { get; set; }
  580. public DelegateCommand<object> CreateLinkCommand { get; set; }
  581. public DelegateCommand PropertyCommand { get; set; }
  582. public DelegateCommand ShowInFolderCommand { get; set; }
  583. public DelegateCommand CompressCommand { get; set; }
  584. public DelegateCommand MergeFileCommand { get; set; }
  585. public DelegateCommand EncryptCommand { get; set; }
  586. public DelegateCommand DecryptCommand { get; set; }
  587. public DelegateCommand<string> ConvertCommand { get; set; }
  588. public DelegateCommand CloseWindowCommand { get; set; }
  589. public DelegateCommand OpenFileCommand { get; set; }
  590. public DelegateCommand<string> HelpCommand { get; set; }
  591. public DelegateCommand CreateBlankFileCommand { get; set; }
  592. public DelegateCommand ClosePropertyCommand { get; set; }
  593. #endregion 命令
  594. public ViewContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  595. {
  596. InitStirng();
  597. region = regionManager;
  598. dialogs = dialogService;
  599. events = eventAggregator;
  600. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  601. LoadFile = new DelegateCommand(loadFile);
  602. Load = new DelegateCommand(LoadControl);
  603. MouseDown = new DelegateCommand(MouseDownEvent);
  604. SaveFile = new DelegateCommand(() => { saveFile(); }, CanSaveExcute).ObservesProperty(() => CanSave);
  605. SaveAsFlattenCommand = new DelegateCommand(saveAsFlatten);
  606. SaveAsFile = new DelegateCommand(() => { saveAsFile(); });
  607. UndoCommand = new DelegateCommand(Undo);
  608. RedoCommand = new DelegateCommand(Redo);
  609. ShareCommand = new DelegateCommand(share);
  610. PropertyCommand = new DelegateCommand(property);
  611. ShowInFolderCommand = new DelegateCommand(ShowInFolder);
  612. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectonChangedEvent);
  613. CompressCommand = new DelegateCommand(compress);
  614. MergeFileCommand = new DelegateCommand(mergeFile);
  615. EncryptCommand = new DelegateCommand(encrypt);
  616. DecryptCommand = new DelegateCommand(decrypt);
  617. ConvertCommand = new DelegateCommand<string>(convert);
  618. CloseWindowCommand = new DelegateCommand(closeWindow);
  619. OpenFileCommand = new DelegateCommand(openfile);
  620. HelpCommand = new DelegateCommand<string>(help);
  621. CreateBlankFileCommand = new DelegateCommand(createBlankFile);
  622. ViwerRegionName = RegionNames.ViwerRegionName;
  623. SplitViewerRegionName = RegionNames.Viewer_SplitRegionName;
  624. BOTARegionName = RegionNames.BOTARegionName;
  625. PropertyRegionName = RegionNames.PropertyRegionName;
  626. BottomToolRegionName = RegionNames.BottomToolRegionName;
  627. ReadModeRegionName = RegionNames.ReadModeRegionName;
  628. MenuEnterReadMode = new DelegateCommand<object>(MenuEnterReadModeEvent);
  629. PrintCommand = new DelegateCommand(ShowPrintDialog);
  630. SelectCommand = new DelegateCommand(SelectClick);
  631. SettingsCommand = new DelegateCommand(SettingsEvent);
  632. CreateLinkCommand = new DelegateCommand<object>(CreateLinkEvent);
  633. ClosePropertyCommand = new DelegateCommand(CloseProperty);
  634. //未显示时无法注册上Region名称
  635. ToolContentVisible = Visibility.Visible;
  636. ToolsBarContentVisible = Visibility.Visible;
  637. OCRContentVisible = Visibility.Visible;
  638. TipVisible = Visibility.Visible;
  639. TipContentRegionName = RegionNames.TipContentRegionName;
  640. LeftTipContentRegionName = RegionNames.LeftTipContentRegionName;
  641. NotePopupRegionName = RegionNames.NotePopupRegionName;
  642. OCRViewerRegionName = RegionNames.OCRViewerRegionName;
  643. ToolContentRegionName = Guid.NewGuid().ToString();
  644. ToolsBarContentRegionName = Guid.NewGuid().ToString();
  645. ConverterBarContentRegionName = Guid.NewGuid().ToString();
  646. TextEditContentRegionName = Guid.NewGuid().ToString();
  647. ToolContentVisible = Visibility.Collapsed;
  648. ToolsBarContentVisible = Visibility.Collapsed;
  649. OCRContentVisible = Visibility.Collapsed;
  650. TipVisible = Visibility.Collapsed;
  651. regionNameByTabItem = new Dictionary<string, string>();
  652. barContentByTabItem = new Dictionary<string, string>();
  653. InitialregionNameByTabItem(ref regionNameByTabItem);
  654. InitialbarContentByTabItem(ref barContentByTabItem);
  655. eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Subscribe(EnterEditTools, e => e.Unicode == unicode);
  656. eventAggregator.GetEvent<CloseEditToolEvent>().Subscribe(CloseEditTool, e => e.Unicode == unicode);
  657. eventAggregator.GetEvent<ShowTipEvent>().Subscribe(ShowSelectedTipAsync, e => e.Unicode == unicode);
  658. //TODO:根据缓存 选择用户上次选择的菜单
  659. EnterSelectedBar("TabItemAnnotation");
  660. }
  661. private void MouseDownEvent()
  662. {
  663. if (NotePopupVisible == Visibility.Visible && IsNoteAdd == false)
  664. {
  665. NotePopupVisible = Visibility.Collapsed;
  666. if (region.Regions.ContainsRegionWithName(NotePopupRegionName))
  667. {
  668. var views = region.Regions[NotePopupRegionName].Views;
  669. foreach (var item in views)
  670. {
  671. if (item is StickyNotePopup sticky)
  672. {
  673. var contentRegion = region.Regions[NotePopupRegionName];
  674. contentRegion.Remove(sticky);
  675. break;
  676. }
  677. }
  678. }
  679. }
  680. }
  681. /// <summary>
  682. ///创建空白文档
  683. /// </summary>
  684. private async void createBlankFile()
  685. {
  686. App.mainWindowViewModel.AddTab.Execute();
  687. await Task.Delay(30);
  688. (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).CreateFile();
  689. }
  690. private void help(string args)
  691. {
  692. string url = "";
  693. switch (args)
  694. {
  695. case "Guid":
  696. //打开内嵌文档
  697. if (File.Exists(App.GuidPDFPath))
  698. {
  699. App.mainWindowViewModel.AddTabItem(App.GuidPDFPath);
  700. }
  701. break;
  702. case "Online":
  703. url = @"https://www.pdfreaderpro.com/windows/help";
  704. break;
  705. case "More":
  706. url = @"https://www.pdfreaderpro.com/product?utm_source=MacApp&utm_campaign=ProductLink&utm_medium=PdfProduct";
  707. break;
  708. case "Template":
  709. url = @"https://www.pdfreaderpro.com/templates?utm_source=MacApp&utm_campaign=TemplatesLink&utm_medium=PdfTemplates";
  710. break;
  711. case "Blog":
  712. //TODO:显示订阅电子报弹窗
  713. break;
  714. case "ComPDF":
  715. url = @"https://www.compdf.com/?utm_source=macapp&utm_medium=pdfmac&utm_campaign=compdfkit-promp";
  716. break;
  717. case "FeedBack":
  718. //调用邮箱 发送邮件
  719. StringBuilder sr = new StringBuilder();
  720. sr.AppendFormat("{0} - {1};{2};{3} - {4}", App.Name, App.Version, "Propose a New Feature", ServiceHelper.ComputerInfo, ServiceHelper.SystemInfo);
  721. System.Diagnostics.Process.Start(string.Format("mailto:support@pdfreaderpro.com?subject={0}", sr));
  722. break;
  723. default:
  724. break;
  725. }
  726. if (!string.IsNullOrEmpty(url))
  727. {
  728. //需要跳转网页的情况
  729. System.Diagnostics.Process.Start(url);
  730. }
  731. }
  732. /// <summary>
  733. /// 打开文件,跟首页的打开文件有区别,需要新开一个页签
  734. /// </summary>
  735. private void openfile()
  736. {
  737. OpenFileDialog openFileDialog = new OpenFileDialog();
  738. openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
  739. openFileDialog.Multiselect = true;
  740. if ((bool)openFileDialog.ShowDialog())
  741. {
  742. if (openFileDialog.FileNames.Count() == 1)
  743. {
  744. if (App.OpenedFileList.Contains(openFileDialog.FileName))
  745. {
  746. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  747. }
  748. else
  749. {
  750. App.mainWindowViewModel.AddTabItem(openFileDialog.FileName);
  751. }
  752. ToolMethod.SetFileThumbImg(openFileDialog.FileName);
  753. }
  754. else
  755. {
  756. var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
  757. if (fileList.Count <= 0)
  758. {
  759. App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
  760. return;
  761. }
  762. mainViewModel.OpenFile(fileList[0]);
  763. for (int i = 1; i < fileList.Count(); i++)
  764. {
  765. if (!App.OpenedFileList.Contains(fileList[i]))
  766. {
  767. App.mainWindowViewModel.AddTabItem(fileList[i]);
  768. }
  769. ToolMethod.SetFileThumbImg(fileList[i]);
  770. }
  771. }
  772. }
  773. }
  774. /// <summary>
  775. /// 关闭当前窗体
  776. /// </summary>
  777. private void closeWindow()
  778. {
  779. App.Current.MainWindow.Close();
  780. }
  781. /// <summary>
  782. /// 转档
  783. /// </summary>
  784. /// <param name="obj"></param>
  785. private void convert(string obj)
  786. {
  787. if (!string.IsNullOrEmpty(obj))
  788. {
  789. DialogParameters value = new DialogParameters();
  790. value.Add(ParameterNames.PDFViewer, PDFViewer);
  791. switch (obj)
  792. {
  793. case "Word":
  794. dialogs.ShowDialog(DialogNames.ConverterWordDialog, value, e =>
  795. {
  796. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  797. converterDialogsModel.OnOpened((DialogResult)e);
  798. });
  799. break;
  800. case "Excel":
  801. dialogs.ShowDialog(DialogNames.ConverterExcelDialog, value, e =>
  802. {
  803. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  804. converterDialogsModel.OnOpened((DialogResult)e);
  805. });
  806. break;
  807. case "PPT":
  808. dialogs.ShowDialog(DialogNames.ConverterPPTDialog, value, e =>
  809. {
  810. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  811. converterDialogsModel.OnOpened((DialogResult)e);
  812. });
  813. break;
  814. case "RTF":
  815. dialogs.ShowDialog(DialogNames.ConverterRTFDialog, value, e =>
  816. {
  817. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  818. converterDialogsModel.OnOpened((DialogResult)e);
  819. });
  820. break;
  821. case "CSV":
  822. dialogs.ShowDialog(DialogNames.ConverterCSVDialog, value, e =>
  823. {
  824. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  825. converterDialogsModel.OnOpened((DialogResult)e);
  826. });
  827. break;
  828. case "HTML":
  829. dialogs.ShowDialog(DialogNames.ConverterHTMLDialog, value, e =>
  830. {
  831. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  832. converterDialogsModel.OnOpened((DialogResult)e);
  833. });
  834. break;
  835. case "Text":
  836. dialogs.ShowDialog(DialogNames.ConverterTextDialog, value, e =>
  837. {
  838. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  839. converterDialogsModel.OnOpened((DialogResult)e);
  840. });
  841. break;
  842. case "Image":
  843. dialogs.ShowDialog(DialogNames.ConverterImgDialog, value, e =>
  844. {
  845. ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
  846. converterDialogsModel.OnOpened((DialogResult)e);
  847. });
  848. break;
  849. default:
  850. break;
  851. }
  852. }
  853. }
  854. /// <summary>
  855. /// 解密
  856. /// </summary>
  857. private void decrypt()
  858. {
  859. if (!PDFViewer.Document.IsEncrypted)
  860. {
  861. MessageBoxEx.Show("No security settings available ");
  862. }
  863. else
  864. {
  865. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  866. if (result.IsDiscryptied)
  867. {
  868. if (result.Password != null)
  869. {
  870. string filePath = PDFViewer.Document.FilePath;
  871. PDFViewer.Document.Release();
  872. PDFViewer.InitDocument(filePath);
  873. PDFViewer.Document.UnlockWithPassword(result.Password);
  874. }
  875. DialogParameters value = new DialogParameters();
  876. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  877. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e => { });
  878. }
  879. }
  880. }
  881. /// <summary>
  882. /// 加密
  883. /// </summary>
  884. private void encrypt()
  885. {
  886. if (!this.SecurityInfo.IsPasswordChanged)
  887. {
  888. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  889. if (result.IsDiscryptied)
  890. {
  891. if (result.Password != null)
  892. {
  893. string filePath = PDFViewer.Document.FilePath;
  894. PDFViewer.Document.Release();
  895. PDFViewer.InitDocument(filePath);
  896. PDFViewer.Document.UnlockWithPassword(result.Password);
  897. }
  898. }
  899. }
  900. DialogParameters value = new DialogParameters();
  901. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  902. value.Add(ParameterNames.ViewContentViewModel, this);
  903. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  904. {
  905. if (e.Result == ButtonResult.OK)
  906. {
  907. SecurityHelper.IsPasswordChanged = true;
  908. if (this.SecurityInfo.IsPasswordChanged)
  909. {
  910. this.PDFViewer.UndoManager.CanSave = true;
  911. }
  912. this.events.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  913. }
  914. });
  915. }
  916. /// <summary>
  917. /// 合并
  918. /// </summary>
  919. private void mergeFile()
  920. {
  921. DialogParameters value = new DialogParameters();
  922. value.Add(ParameterNames.PDFViewer, PDFViewer);
  923. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  924. }
  925. /// <summary>
  926. /// 压缩
  927. /// </summary>
  928. private void compress()
  929. {
  930. DialogParameters value = new DialogParameters();
  931. value.Add(ParameterNames.PDFViewer, PDFViewer);
  932. dialogs.ShowDialog(DialogNames.CompressDialog, value, e =>
  933. {
  934. CompressDialogModel compressDialogModel = new CompressDialogModel();
  935. compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
  936. });
  937. }
  938. /// <summary>
  939. /// 设置Tip状态栏的显示状态
  940. /// </summary>
  941. /// <param name="show">是否显示</param>
  942. public void ShowTip(bool show)
  943. {
  944. if (show)
  945. {
  946. TipVisible = Visibility.Visible;
  947. }
  948. else
  949. {
  950. TipVisible = Visibility.Collapsed;
  951. }
  952. }
  953. /// <summary>
  954. /// 设置LeftTip状态栏的显示状态
  955. /// </summary>
  956. /// <param name="show">是否显示</param>
  957. public void ShowLeftTip(bool show)
  958. {
  959. if (show)
  960. {
  961. LeftTipVisible = Visibility.Visible;
  962. }
  963. else
  964. {
  965. LeftTipVisible = Visibility.Collapsed;
  966. }
  967. }
  968. public void CheckHaveAllPermission()
  969. {
  970. if (!SecurityHelper.CheckHaveAllPermissions(PDFViewer.Document))
  971. {
  972. ShowLeftTip(true);
  973. NavigationParameters param = new NavigationParameters();
  974. param.Add(ParameterNames.PDFViewer, PDFViewer);
  975. region.RequestNavigate(LeftTipContentRegionName, "FileRestrictedTip", param);
  976. }
  977. }
  978. public async void ShowSelectedTipAsync(ShowTipEventArgs showTipEventArgs)
  979. {
  980. switch (showTipEventArgs.enumTipKind)
  981. {
  982. case EnumTipKind.StatusNone:
  983. ShowLeftTip(false);
  984. break;
  985. case EnumTipKind.StatusSetPasswordSuccessfully:
  986. ShowLeftTip(true);
  987. region.RequestNavigate(LeftTipContentRegionName, "SetPasswordSuccessfullyTip");
  988. break;
  989. case EnumTipKind.StatusRemoveSecuritySuccessfully:
  990. ShowLeftTip(false);
  991. region.RequestNavigate(TipContentRegionName, "RemoveSecuritySuccessTip");
  992. IsLoading = Visibility.Collapsed;
  993. ShowTip(true);
  994. await Task.Delay(3000);
  995. //ReadModelTip = Visibility.Collapsed;
  996. TipVisible = Visibility.Collapsed;
  997. break;
  998. default: break;
  999. }
  1000. }
  1001. /// <summary>
  1002. /// 右键菜单创建链接
  1003. /// </summary>
  1004. /// <param name="obj"></param>
  1005. private void CreateLinkEvent(object obj)
  1006. {
  1007. if (obj is object[] objArry)
  1008. {
  1009. if (objArry[0] is AnnotToolContentViewModel toolContentViewModel && objArry[1] is AnnotCommandArgs annotCommand)
  1010. {
  1011. LinkAnnotArgs linkArgs = new LinkAnnotArgs();
  1012. linkArgs.URI = string.Empty;
  1013. linkArgs.LinkType = LINK_TYPE.GOTO;
  1014. linkArgs.PageIndex = annotCommand.PageIndex;
  1015. //linkArgs.InvokeLinkSaveCalled(this, EventArgs.Empty);
  1016. this.IsRightMenuCreateLink = true;
  1017. List<AnnotHandlerEventArgs> lists = new List<AnnotHandlerEventArgs>();
  1018. lists.Add(linkArgs);
  1019. AnnotHandlerEventArgs annotArgs = toolContentViewModel.GetLink(lists);
  1020. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  1021. PDFViewer.SetToolParam(annotArgs);
  1022. this.IsPropertyOpen = true;
  1023. }
  1024. }
  1025. }
  1026. private void CloseProperty()
  1027. {
  1028. IsPropertyOpen = false;
  1029. }
  1030. /// <summary>
  1031. /// 在文件资源管理器中显示
  1032. /// </summary>
  1033. private void ShowInFolder()
  1034. {
  1035. CommonHelper.ShowFileBrowser(PDFViewer.Document.FilePath);
  1036. }
  1037. private void property()
  1038. {
  1039. DialogParameters valuePairs = new DialogParameters();
  1040. valuePairs.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  1041. dialogs.ShowDialog(DialogNames.PropertyDialog, valuePairs, null);
  1042. }
  1043. private void share()
  1044. {
  1045. try
  1046. {
  1047. var path = PDFViewer.Document.FilePath;
  1048. string subject = "分享至" + " " + PDFViewer.Document.FileName;
  1049. System.Diagnostics.Process.Start("outlook", "/a,\"" + path + "\"" + "/m \"" + "&subject=" + subject + "\"");
  1050. }
  1051. catch
  1052. {
  1053. AlertsMessage alertsMessage = new AlertsMessage();
  1054. alertsMessage.ShowDialog("", "未检测到Ooutlook软件,请先安装Outlook", "OK");
  1055. }
  1056. }
  1057. private void SettingsEvent()
  1058. {
  1059. dialogs.ShowDialog(DialogNames.SettingsDialog, null, null);
  1060. if (PDFViewer != null)
  1061. {
  1062. PDFViewer.SetBackgroundBrush(new System.Windows.Media.SolidColorBrush(Settings.Default.AppProperties.InitialVIew.BackGround));
  1063. }
  1064. }
  1065. private bool CanSaveExcute()
  1066. {
  1067. return CanSave;
  1068. }
  1069. private void MenuEnterReadModeEvent(object obj)
  1070. {
  1071. }
  1072. public void ShowPrintDialog()
  1073. {
  1074. DialogParameters printValue = new DialogParameters();
  1075. printValue.Add(ParameterNames.PDFViewer, PDFViewer);
  1076. printValue.Add(ParameterNames.FilePath, PDFViewer.Document.FileName);
  1077. printValue.Add(ParameterNames.PrintCurrentPage, PDFViewer.CurrentIndex);
  1078. dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printValue, e => { });
  1079. }
  1080. public void SelectClick()
  1081. {
  1082. if (OpenBOTA == false)
  1083. {
  1084. OpenBOTA = true;
  1085. }
  1086. bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemSearch");
  1087. if (isBook == false)
  1088. {
  1089. bOTAContent.TabItemSearch.IsSelected = true;
  1090. }
  1091. }
  1092. private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText)
  1093. {
  1094. bool isTabItem = false;
  1095. bOTAContentViewModel = null;
  1096. bOTAContent = null;
  1097. if (region.Regions.ContainsRegionWithName(this.BOTARegionName))
  1098. {
  1099. var views = region.Regions[this.BOTARegionName].Views;
  1100. var model = views.FirstOrDefault(q => q is BOTAContent);
  1101. if (model is BOTAContent bOTAContent1)
  1102. {
  1103. bOTAContent = bOTAContent1;
  1104. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  1105. if (bOTAContentViewModel.CurrentBar == tabItemText)
  1106. {
  1107. isTabItem = true;
  1108. }
  1109. }
  1110. }
  1111. return isTabItem;
  1112. }
  1113. private void InitialregionNameByTabItem(ref Dictionary<string, string> dictionary)
  1114. {
  1115. dictionary.Add("TabItemPageEdit", ToolContentRegionName);
  1116. dictionary.Add("TabItemTool", ToolsBarContentRegionName);
  1117. //其他工具菜单栏共用一个ToolsBarContentRegionName
  1118. dictionary.Add("TabItemAnnotation", ToolsBarContentRegionName);
  1119. dictionary.Add("TabItemConvert", ConverterBarContentRegionName);
  1120. dictionary.Add("TabItemScan", ToolsBarContentRegionName);
  1121. dictionary.Add("TabItemEdit", TextEditContentRegionName);
  1122. dictionary.Add("TabItemForm", ToolsBarContentRegionName);
  1123. dictionary.Add("TabItemFill", ToolsBarContentRegionName);
  1124. }
  1125. private void InitialbarContentByTabItem(ref Dictionary<string, string> dictionary)
  1126. {
  1127. dictionary.Add("TabItemPageEdit", "PageEditContent");
  1128. dictionary.Add("TabItemTool", "ToolsBarContent");
  1129. dictionary.Add("TabItemAnnotation", "AnnotToolContent");
  1130. dictionary.Add("TabItemConvert", "ConverterBarContent");
  1131. dictionary.Add("TabItemScan", "ScanContent");
  1132. dictionary.Add("TabItemEdit", "TextEditToolContent");
  1133. dictionary.Add("TabItemForm", "FormsToolContent");
  1134. dictionary.Add("TabItemFill", "FillAndSignContent");
  1135. }
  1136. private void UpdateShowContent(string currentBar)
  1137. {
  1138. ToolContentVisible = Visibility.Collapsed;
  1139. ToolsBarContentVisible = Visibility.Collapsed;
  1140. ConverterBarContentVisible = Visibility.Collapsed;
  1141. TextEditToolContentVisible = Visibility.Collapsed;
  1142. OCRContentVisible = Visibility.Collapsed;
  1143. switch (currentBar)
  1144. {
  1145. case "TabItemAnnotation":
  1146. case "TabItemTool":
  1147. case "TabItemForm":
  1148. case "TabItemFill":
  1149. ToolsBarContentVisible = Visibility.Visible;
  1150. break;
  1151. case "TabItemScan":
  1152. ToolsBarContentVisible = Visibility.Visible;
  1153. OCRContentVisible = Visibility.Visible;
  1154. break;
  1155. case "TabItemEdit":
  1156. TextEditToolContentVisible = Visibility.Visible;
  1157. break;
  1158. case "TabItemConvert":
  1159. ConverterBarContentVisible = Visibility.Visible;
  1160. break;
  1161. case "TabItemPageEdit":
  1162. case "HeaderFooterContent":
  1163. case "BatesContent":
  1164. case "WatermarkContent":
  1165. case "BackgroundContent":
  1166. case "RedactionContent":
  1167. ToolContentVisible = Visibility.Visible;
  1168. break;
  1169. default:
  1170. break;
  1171. }
  1172. PDFEditMode(currentBar);
  1173. FormMode(currentBar);
  1174. }
  1175. private void PDFEditMode(string currentBar)
  1176. {
  1177. if (currentBar == "TabItemEdit")
  1178. {
  1179. if (PDFViewer != null)
  1180. {
  1181. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  1182. //SelectedPrpoertyPanel("TextEditProperty", null);
  1183. //IsPropertyOpen = true;
  1184. }
  1185. }
  1186. else
  1187. {
  1188. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.PDFEdit)
  1189. {
  1190. IsPropertyOpen = false;
  1191. PDFViewer.SetMouseMode(MouseModes.PanTool);
  1192. }
  1193. }
  1194. }
  1195. private void FormMode(string currentBar)
  1196. {
  1197. if (currentBar == "TabItemForm")
  1198. {
  1199. if (PDFViewer != null)
  1200. {
  1201. PDFViewer.SetMouseMode(MouseModes.FormEditTool);
  1202. }
  1203. }
  1204. else
  1205. {
  1206. if (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool)
  1207. {
  1208. PDFViewer.SetMouseMode(MouseModes.PanTool);
  1209. }
  1210. }
  1211. }
  1212. private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  1213. {
  1214. if (!isInPageEdit)
  1215. {
  1216. //不处于页面编辑模式下时,根据PDFVIewer的undo redo状态来更新按钮状态
  1217. //页面编辑模式下,按钮状态根据页面编辑的undo redo来显示
  1218. if (e.PropertyName == "CanUndo")
  1219. {
  1220. CanUndo = PDFViewer.UndoManager.CanUndo;
  1221. }
  1222. if (e.PropertyName == "CanRedo")
  1223. {
  1224. CanRedo = PDFViewer.UndoManager.CanRedo;
  1225. }
  1226. }
  1227. if (e.PropertyName == "CanSave")
  1228. {
  1229. CanSave = PDFViewer.UndoManager.CanSave;
  1230. }
  1231. }
  1232. /// <summary>
  1233. /// 选项卡切换事件
  1234. /// </summary>
  1235. /// <param name="e"></param>
  1236. private void TabControlSelectonChangedEvent(object e)
  1237. {
  1238. var args = e as SelectionChangedEventArgs;
  1239. if (args != null)
  1240. {
  1241. var item = args.AddedItems[0] as TabItem;
  1242. CurrentBar = item.Name;
  1243. if (previousBar != CurrentBar)
  1244. {
  1245. if (CurrentBar == "TabItemPageEdit")//如果是页面编辑则进入页面编辑模式
  1246. {
  1247. EnterToolMode(barContentByTabItem[CurrentBar]);
  1248. isInPageEdit = true;
  1249. }
  1250. else//其余情况直接导航至对应的工具栏即可,不需要清空之前的content,region里是单例模式
  1251. {
  1252. EnterSelectedBar(CurrentBar);
  1253. isInPageEdit = false;
  1254. }
  1255. previousBar = CurrentBar;
  1256. }
  1257. }
  1258. }
  1259. /// <summary>
  1260. /// 阅读模式
  1261. /// </summary>
  1262. /// <param name="viewContent"></param>
  1263. public async void RbtnReadMode()
  1264. {
  1265. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = true;
  1266. mainViewModel.IsBookMode = true;
  1267. IsLoading = Visibility.Visible;
  1268. await Task.Delay(1);
  1269. PDFViewer.SetMouseMode(MouseModes.PanTool);
  1270. #region 旧版本
  1271. //NavigationParameters param = new NavigationParameters();
  1272. //param.Add(ParameterNames.PDFViewer, PDFViewer);
  1273. //param.Add(ParameterNames.ViewContentViewModel, this);
  1274. //region.RequestNavigate(ToolContentRegionName, "ReadViewContent", param);
  1275. ////ShowContent(CurrentBar, true);
  1276. //if (GridToolRow != 0)
  1277. //{
  1278. // GridToolRow = 0;
  1279. //}
  1280. //if (GridToolRowSpan != 4)
  1281. //{
  1282. // GridToolRowSpan = 4;
  1283. //}
  1284. ////isInPageEdit = true;
  1285. //UpdateShowContent("TabItemPageEdit");
  1286. #endregion 旧版本
  1287. this.events.GetEvent<ReadModeEvent>().Publish(new ReadModeArgs() { Unicode = unicode, IsReadMode = true });
  1288. IsLoading = Visibility.Collapsed;
  1289. }
  1290. /// <summary>
  1291. /// 退出阅读模式
  1292. /// </summary>
  1293. public async void UnReadModel()
  1294. {
  1295. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = false;
  1296. mainViewModel.IsBookMode = false;
  1297. IsLoading = Visibility.Visible;
  1298. await Task.Delay(1);
  1299. //PDFViewer.MouseMode = MouseModes.None;
  1300. #region 旧版本
  1301. //if (region.Regions.ContainsRegionWithName(ViwerRegionName))
  1302. //{
  1303. // if (region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  1304. // {
  1305. // var contentRegion = region.Regions[ViwerRegionName];
  1306. // contentRegion.Remove(PDFViewer);
  1307. // }
  1308. // //还原背景色
  1309. // PDFViewer.SetBackgroundBrush(new System.Windows.Media.SolidColorBrush(Settings.Default.AppProperties.InitialVIew.BackGround));
  1310. // region.AddToRegion(ViwerRegionName, PDFViewer);
  1311. //}
  1312. //if (string.IsNullOrEmpty(CurrentBar) || CurrentBar.Equals("TabItemPageEdit", StringComparison.OrdinalIgnoreCase))
  1313. //{
  1314. // EnterSelectedBar("TabItemAnnotation");
  1315. //}
  1316. //else
  1317. //{
  1318. // EnterSelectedBar(CurrentBar);
  1319. //}
  1320. #endregion
  1321. //还原背景色
  1322. PDFViewer.SetBackgroundBrush(new System.Windows.Media.SolidColorBrush(Settings.Default.AppProperties.InitialVIew.BackGround));
  1323. this.events.GetEvent<ReadModeEvent>().Publish(new ReadModeArgs() { Unicode = unicode, IsReadMode = false });
  1324. bool isExist = false;
  1325. if (region.Regions.ContainsRegionWithName(TipContentRegionName))
  1326. {
  1327. var views = region.Regions[TipContentRegionName].Views;
  1328. foreach (var item in views)
  1329. {
  1330. if (item is Views.TipContent.ReadModelTip readModelTip)
  1331. {
  1332. isExist = true;
  1333. break;
  1334. }
  1335. }
  1336. }
  1337. if (isExist == false)
  1338. {
  1339. region.RequestNavigate(TipContentRegionName, "ReadModelTip");
  1340. }
  1341. IsLoading = Visibility.Collapsed;
  1342. ShowTip(true);
  1343. await Task.Delay(3000);
  1344. //ReadModelTip = Visibility.Collapsed;
  1345. TipVisible = Visibility.Collapsed;
  1346. }
  1347. #region PDFViewer鼠标滚轮缩放事件
  1348. public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
  1349. {
  1350. double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
  1351. PDFViewer.Zoom(newZoom);
  1352. }
  1353. public double CheckZoomLevel(double zoom, bool IsGrowth)
  1354. {
  1355. double standardZoom = 100;
  1356. if (zoom <= 0.01)
  1357. {
  1358. return 0.01;
  1359. }
  1360. if (zoom >= 10)
  1361. {
  1362. return 10;
  1363. }
  1364. zoom *= 100;
  1365. for (int i = 0; i < zoomLevel.Length - 1; i++)
  1366. {
  1367. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  1368. {
  1369. standardZoom = zoomLevel[i + 1];
  1370. break;
  1371. }
  1372. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  1373. {
  1374. standardZoom = zoomLevel[i];
  1375. break;
  1376. }
  1377. }
  1378. return standardZoom / 100;
  1379. }
  1380. #endregion PDFViewer鼠标滚轮缩放事件
  1381. #region Navigate
  1382. public void OnNavigatedTo(NavigationContext navigationContext)
  1383. {
  1384. if (isOpenFile)
  1385. return;
  1386. App.mainWindowViewModel.OphVis = Visibility.Collapsed;
  1387. var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
  1388. if (mainVM != null)
  1389. {
  1390. mainViewModel = mainVM;
  1391. mainViewModel.viewContentViewModel = this;
  1392. }
  1393. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  1394. if (pdfview != null)
  1395. {
  1396. PDFViewer = pdfview;
  1397. if (pdfview.Tag != null)
  1398. {
  1399. //保存密码
  1400. PassWord = pdfview.Tag.ToString();
  1401. }
  1402. loadFile();
  1403. CheckHaveAllPermission();
  1404. }
  1405. isOpenFile = true;
  1406. //快捷键
  1407. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  1408. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  1409. }
  1410. public bool IsNavigationTarget(NavigationContext navigationContext)
  1411. {
  1412. if (navigationContext != null)
  1413. {
  1414. //另存为打开导航进来时 不能新建一个实例
  1415. return true;
  1416. }
  1417. else
  1418. {
  1419. //一次性打开多个文件时,需要新建实例
  1420. return false;
  1421. }
  1422. }
  1423. public void OnNavigatedFrom(NavigationContext navigationContext)
  1424. {
  1425. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  1426. }
  1427. #endregion Navigate
  1428. #region 方法
  1429. /// <summary>
  1430. /// 快捷键
  1431. /// </summary>
  1432. /// <param name="sender"></param>
  1433. /// <param name="e"></param>
  1434. /// <exception cref="NotImplementedException"></exception>
  1435. private void ShortCut_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  1436. {
  1437. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape))
  1438. {
  1439. if (mainViewModel.IsBookMode == true)
  1440. {
  1441. UnReadModel();
  1442. }
  1443. //内容选择 退出
  1444. if (PDFViewer.MouseMode != MouseModes.PanTool)
  1445. {
  1446. AnnotToolContent annotToolContent = GetAnnotToolContent(out AnnotToolContentViewModel toolContentViewModel);
  1447. if (annotToolContent != null && toolContentViewModel != null)
  1448. {
  1449. if (annotToolContent.BtnSelecttool.IsChecked == true)
  1450. {
  1451. if (toolContentViewModel.SnapshotEditMenuViewModel.SnapToolArgs != null)
  1452. {
  1453. toolContentViewModel.SnapshotEditMenuViewModel.SnapToolArgs.ReSnapshot();
  1454. annotToolContent.BtnSelecttool.IsChecked = false;
  1455. }
  1456. }
  1457. }
  1458. }
  1459. }
  1460. if (KeyEventsHelper.IsModifierKey(ModifierKeys.Control, Key.C))
  1461. {
  1462. //内容选择 复制 快捷键
  1463. AnnotToolContent annotToolContent = GetAnnotToolContent(out AnnotToolContentViewModel toolContentViewModel);
  1464. if (annotToolContent != null && toolContentViewModel != null)
  1465. {
  1466. if (annotToolContent.BtnSelecttool.IsChecked == true)
  1467. {
  1468. toolContentViewModel.SnapshotEditMenuViewModel.SnapCopyCommand.Execute();
  1469. }
  1470. }
  1471. }
  1472. if (KeyEventsHelper.IsModifierKey(ModifierKeys.Control, Key.P))
  1473. {
  1474. AnnotToolContent annotToolContent = GetAnnotToolContent(out AnnotToolContentViewModel toolContentViewModel);
  1475. if (annotToolContent != null && toolContentViewModel != null)
  1476. {
  1477. if (annotToolContent.BtnSelecttool.IsChecked == true)
  1478. {
  1479. toolContentViewModel.SnapshotEditMenuViewModel.PrintCommand.Execute();
  1480. }
  1481. }
  1482. }
  1483. }
  1484. private AnnotToolContent GetAnnotToolContent(out AnnotToolContentViewModel toolContentViewModel)
  1485. {
  1486. AnnotToolContent toolContent = null;
  1487. toolContentViewModel = null;
  1488. if (region.Regions.ContainsRegionWithName(ToolsBarContentRegionName))
  1489. {
  1490. var views = region.Regions[ToolsBarContentRegionName].Views;
  1491. var model = views.FirstOrDefault(q => q is AnnotToolContent);
  1492. if (model is AnnotToolContent annotToolContent)
  1493. {
  1494. toolContent = annotToolContent;
  1495. toolContentViewModel = annotToolContent.DataContext as AnnotToolContentViewModel;
  1496. }
  1497. }
  1498. return toolContent;
  1499. }
  1500. /// <summary>
  1501. /// 视图面板 切换分屏模式
  1502. /// </summary>
  1503. /// <param name="mode"></param>
  1504. public void EnterSplitMode(SplitMode mode)
  1505. {
  1506. ///通知UI层更改布局
  1507. this.events.GetEvent<SplitEvent>().Publish(new SplitEventArgs() { Unicode = unicode, Mode = mode });
  1508. }
  1509. private void Undo()
  1510. {
  1511. if (isInPageEdit)
  1512. {
  1513. //执行页面编辑的Undo
  1514. PageEditUndo?.Invoke();
  1515. }
  1516. else
  1517. {
  1518. PDFViewer.UndoManager.Undo();
  1519. }
  1520. }
  1521. private void Redo()
  1522. {
  1523. if (isInPageEdit)
  1524. {
  1525. //执行页面编辑的Redo
  1526. PageEditRedo?.Invoke();
  1527. }
  1528. else
  1529. {
  1530. PDFViewer.UndoManager.Redo();
  1531. }
  1532. }
  1533. private void LoadControl()
  1534. {
  1535. //在构造函数中使用Region需要借助Dispatcher 确保UI已经加载完成,加载BOTA区域
  1536. // 非必要情况不要使用该异步方法,可能会导致一次性加载多个文件时出现因异步引起的regionname 错乱问题
  1537. //System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  1538. // {
  1539. NavigationParameters parameters = new NavigationParameters();
  1540. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  1541. parameters.Add(ParameterNames.ViewContentViewModel, this);
  1542. region.RequestNavigate(BOTARegionName, "BOTAContent", parameters);
  1543. region.RequestNavigate(BottomToolRegionName, "BottomToolContent", parameters);
  1544. region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
  1545. region.RequestNavigate(SplitViewerRegionName, "SplitScreenContent", parameters);
  1546. //TODO 根据上一次关闭记录的菜单,选中TabItem
  1547. EnterSelectedBar("TabItemAnnotation");
  1548. //}
  1549. //));
  1550. //if (App.IsBookMode)
  1551. //{
  1552. // RbtnReadMode();
  1553. //}
  1554. //else
  1555. //{
  1556. // UnReadModel();
  1557. //}
  1558. }
  1559. /// <summary>
  1560. /// 各个注释(选中和创建注释)导航到对应注释的属性面板
  1561. /// </summary>
  1562. /// <param name="Content"></param>
  1563. /// <param name="annotPropertyPanel"></param>
  1564. public void SelectedPrpoertyPanel(string Content, AnnotTransfer annotPropertyPanel)
  1565. {
  1566. NavigationParameters parameters = new NavigationParameters();
  1567. //传其他参数:文档类,空注释面板;
  1568. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  1569. parameters.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  1570. parameters.Add(ParameterNames.ViewContentViewModel, this);
  1571. region.RequestNavigate(PropertyRegionName, Content, parameters);
  1572. }
  1573. /// <summary>
  1574. /// 将PDFViwer添加到Region
  1575. /// </summary>
  1576. private void loadFile()
  1577. {
  1578. PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
  1579. PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
  1580. CanSave = PDFViewer.UndoManager.CanSave;
  1581. CanUndo = PDFViewer.UndoManager.CanUndo;
  1582. CanRedo = PDFViewer.UndoManager.CanRedo;
  1583. //OpenFileInfo 内容赋值给PDFViewer
  1584. GetOpenFileInfo();
  1585. region.AddToRegion(ViwerRegionName, PDFViewer);
  1586. }
  1587. /// <summary>
  1588. /// 还原上一次浏览的视图相关设置
  1589. /// </summary>
  1590. private void GetOpenFileInfo()
  1591. {
  1592. OpenFileInfo = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  1593. //偏好设置里设置了记录上次视图设置时
  1594. if (OpenFileInfo != null && Settings.Default.AppProperties.Description.RecoveryViewWhenOpen)
  1595. {
  1596. if (OpenFileInfo.LastViewMode != PDFViewer.ModeView)
  1597. {
  1598. PDFViewer.ChangeViewMode(OpenFileInfo.LastViewMode);
  1599. }
  1600. if (OpenFileInfo.LastFitMode != PDFViewer.ModeFit)
  1601. {
  1602. PDFViewer.ChangeFitMode(OpenFileInfo.LastFitMode);
  1603. }
  1604. if (OpenFileInfo.LastFitMode == ComPDFKitViewer.FitMode.FitFree)
  1605. {
  1606. PDFViewer.Zoom(OpenFileInfo.LastZoom);
  1607. }
  1608. if (OpenFileInfo.LastPageIndex != PDFViewer.CurrentIndex)
  1609. {
  1610. PDFViewer.GoToPage(OpenFileInfo.LastPageIndex);
  1611. }
  1612. PDFViewer.SetSplitMode(OpenFileInfo.LastSplitMode);
  1613. PDFViewer.SetShowLink(OpenFileInfo.ShowHighLightLink);
  1614. if (OpenFileInfo.LastPageSpace)
  1615. {
  1616. PDFViewer.SetPageSpacing(8);
  1617. }
  1618. else
  1619. {
  1620. PDFViewer.SetPageSpacing(0);
  1621. }
  1622. if (OpenFileInfo.LastDrawMode == ComPDFKitViewer.DrawModes.Draw_Mode_Custom && OpenFileInfo.LastFillColor != 0)
  1623. {
  1624. PDFViewer.SetDrawMode(OpenFileInfo.LastDrawMode, OpenFileInfo.LastFillColor);
  1625. }
  1626. else
  1627. {
  1628. PDFViewer.SetDrawMode(OpenFileInfo.LastDrawMode);
  1629. }
  1630. }
  1631. else
  1632. {
  1633. //新文档应用默认设置
  1634. PDFViewer.ChangeViewMode(Settings.Default.AppProperties.InitialVIew.PageView);
  1635. PDFViewer.ChangeFitMode(Settings.Default.AppProperties.InitialVIew.ZoomMode);
  1636. PDFViewer.SetShowLink(Settings.Default.AppProperties.InitialVIew.HighlightLink);
  1637. }
  1638. }
  1639. /// <summary>
  1640. /// 已有路径文档的保存逻辑
  1641. /// </summary>
  1642. public bool saveFile()
  1643. {
  1644. try
  1645. {
  1646. if (string.IsNullOrEmpty(PDFViewer.Document.FilePath))
  1647. return saveAsFile();
  1648. //文档已被修复时 提示另存为
  1649. if (PDFViewer.Document.HasRepaired)
  1650. {
  1651. AlertsMessage alertsMessage = new AlertsMessage();
  1652. alertsMessage.ShowDialog("", "文件已被修复,建议另存为", "Cancel", "OK");
  1653. if (alertsMessage.result == ContentResult.Ok)
  1654. return saveAsFile();
  1655. else
  1656. return false;
  1657. }
  1658. //文件路径无法存在时
  1659. if (!File.Exists(PDFViewer.Document.FilePath))
  1660. {
  1661. AlertsMessage alertsMessage = new AlertsMessage();
  1662. alertsMessage.ShowDialog("", "文件路径不存在,需要另存为", "Cancel", "OK");
  1663. if (alertsMessage.result == ContentResult.Ok)
  1664. return saveAsFile();
  1665. else
  1666. return false;
  1667. }
  1668. //只读文件无法写入时,提示另存为
  1669. FileInfo fileInfo = new FileInfo(PDFViewer.Document.FilePath);
  1670. if (fileInfo.IsReadOnly)
  1671. {
  1672. AlertsMessage alertsMessage = new AlertsMessage();
  1673. alertsMessage.ShowDialog("", "文件为只读文件,需要另存为", "Cancel", "OK");
  1674. if (alertsMessage.result == ContentResult.Ok)
  1675. return saveAsFile();
  1676. else
  1677. return false;
  1678. }
  1679. //Security Fixed:
  1680. //修改密码后原文档保存的步骤:
  1681. //在设置密码窗口传出“已修改”信号,并允许延迟保存
  1682. //
  1683. if (SecurityInfo.IsPasswordChanged)
  1684. {
  1685. string currentFilePath = PDFViewer.Document.FilePath;
  1686. string tempFilePath = PDFViewer.Document.FilePath + ".temp.pdf";
  1687. string openPassword = null;
  1688. string permissionsPassword = null;
  1689. CPDFPermissionsInfo cPDFPermissionsInfo = CreateDefaultPermissionsInfo();
  1690. if (SecurityInfo.NeedOpenPassword)
  1691. {
  1692. openPassword = SecurityInfo.OpenPassword;
  1693. }
  1694. if (SecurityInfo.NeedPermissionsPassword)
  1695. {
  1696. permissionsPassword = SecurityInfo.PermissionsPassword;
  1697. cPDFPermissionsInfo = SecurityInfo.CPDFPermissionsInfo;
  1698. }
  1699. try
  1700. {
  1701. string currentPath = PDFViewer.Document.FilePath;
  1702. string tempPath = PDFViewer.Document.FilePath + ".temp.pdf";
  1703. PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1704. PDFViewer.Document.WriteToFilePath(tempPath);
  1705. PDFViewer.CloseDocument();
  1706. PDFViewer.InitDocument(tempPath);
  1707. if (!string.IsNullOrEmpty(permissionsPassword))
  1708. {
  1709. PDFViewer.Document.UnlockWithPassword(permissionsPassword);
  1710. }
  1711. else if (!string.IsNullOrEmpty(openPassword))
  1712. {
  1713. PDFViewer.Document.UnlockWithPassword(openPassword);
  1714. }
  1715. PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
  1716. PDFViewer.Document.WriteToFilePath(currentPath);
  1717. PDFViewer.CloseDocument();
  1718. PDFViewer.InitDocument(currentPath);
  1719. System.IO.File.Delete(tempPath);
  1720. if (!string.IsNullOrEmpty(permissionsPassword))
  1721. {
  1722. PDFViewer.Document.UnlockWithPassword(permissionsPassword);
  1723. }
  1724. else if (!string.IsNullOrEmpty(openPassword))
  1725. {
  1726. PDFViewer.Document.UnlockWithPassword(openPassword);
  1727. }
  1728. try
  1729. {
  1730. PDFViewer.Load();
  1731. }
  1732. catch
  1733. {
  1734. }
  1735. PDFViewer.UndoManager.CanSave = false;
  1736. SecurityInfo.IsPasswordChanged = false;
  1737. return true;
  1738. }
  1739. catch
  1740. {
  1741. return false;
  1742. }
  1743. }
  1744. else if (SecurityInfo.IsPasswordRemoved)
  1745. {
  1746. try
  1747. {
  1748. string currentPath = PDFViewer.Document.FilePath;
  1749. string tempPath = PDFViewer.Document.FilePath + ".temp.pdf";
  1750. PDFViewer.Document.Descrypt(tempPath);
  1751. PDFViewer.CloseDocument();
  1752. PDFViewer.InitDocument(tempPath);
  1753. PDFViewer.Document.WriteToFilePath(currentPath);
  1754. PDFViewer.CloseDocument();
  1755. PDFViewer.InitDocument(currentPath);
  1756. PDFViewer.Load();
  1757. System.IO.File.Delete(tempPath);
  1758. }
  1759. catch
  1760. {
  1761. return false;
  1762. }
  1763. }
  1764. bool result = PDFViewer.Document.WriteToLoadedPath();
  1765. if (result)
  1766. {
  1767. PDFViewer.UndoManager.CanSave = false;
  1768. App.Current.Dispatcher.Invoke(() =>
  1769. {
  1770. //保存时更新缩略图
  1771. OpenFileInfo info = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  1772. try
  1773. {
  1774. if (!string.IsNullOrEmpty(info.ThumbImgPath) && !PDFViewer.Document.IsEncrypted)//加密的文档不获取缩略图
  1775. {
  1776. var size = PDFViewer.Document.GetPageSize(0);
  1777. System.Drawing.Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)size.Width, (int)size.Height, 0, true, true);
  1778. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  1779. if (File.Exists(folderPath))
  1780. File.Delete(folderPath);
  1781. DirectoryInfo folder = new DirectoryInfo(folderPath);
  1782. if (!folder.Exists)
  1783. folder.Create();
  1784. string imagePath = info.ThumbImgPath;
  1785. if (!File.Exists(imagePath))//由加密文档变为非加密文档时 新建一个路径
  1786. {
  1787. string imageName = Guid.NewGuid().ToString();
  1788. imagePath = System.IO.Path.Combine(folderPath, imageName);
  1789. using (FileStream stream = new FileStream(imagePath, FileMode.Create))
  1790. {
  1791. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  1792. }
  1793. }
  1794. else
  1795. {
  1796. using (FileStream stream = new FileStream(imagePath, FileMode.Open))
  1797. {
  1798. bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  1799. }
  1800. }
  1801. info.ThumbImgPath = imagePath;
  1802. SettingHelper.SetFileInfo(info);
  1803. }
  1804. }
  1805. catch
  1806. {
  1807. info.ThumbImgPath = null;
  1808. SettingHelper.SetFileInfo(info);
  1809. }
  1810. });
  1811. PDFViewer.ReloadDocument();
  1812. }
  1813. else
  1814. {
  1815. //文件被占用 保存失败时
  1816. AlertsMessage alertsMessage = new AlertsMessage();
  1817. alertsMessage.ShowDialog("", "文件被占用,需要另存为", "Cancel", "OK");
  1818. if (alertsMessage.result == ContentResult.Ok)
  1819. return saveAsFile();
  1820. else
  1821. return false;
  1822. }
  1823. return result;
  1824. }
  1825. catch { return false; }
  1826. }
  1827. /// <summary>
  1828. /// 另存为或新文档保存逻辑
  1829. /// </summary>
  1830. public bool saveAsFile(Action RedactionAction = null)
  1831. {
  1832. var dlg = new Microsoft.Win32.SaveFileDialog();
  1833. dlg.Filter = Properties.Resources.OpenDialogFilter;
  1834. dlg.FileName = PDFViewer.Document.FileName;
  1835. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  1836. {
  1837. bool result = false;
  1838. if (RedactionAction != null)
  1839. {
  1840. //保存前进行标记密文处理应用或擦除
  1841. RedactionAction.Invoke();
  1842. }
  1843. if (App.OpenedFileList.Contains(dlg.FileName))
  1844. {
  1845. //提示文件已经被打开
  1846. }
  1847. else
  1848. {
  1849. result = PDFViewer.Document.WriteToFilePath(dlg.FileName);
  1850. if (result)
  1851. {
  1852. DoAfterSaveAs(dlg.FileName);
  1853. }
  1854. else
  1855. {
  1856. //提示文件被其他软件占用 无法保存
  1857. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileOccupiedWarning"), "", Winform.MessageBoxButtons.OKCancel, new string[] { App.MainPageLoader.GetString("Main_SaveAs"), App.MainPageLoader.GetString("Main_Cancel") })
  1858. }
  1859. ;
  1860. }
  1861. return result;
  1862. }
  1863. else
  1864. return false;
  1865. }
  1866. /// <summary>
  1867. /// 另存为Flatten
  1868. /// </summary>
  1869. private void saveAsFlatten()
  1870. {
  1871. var dlg = new SaveFileDialog();
  1872. dlg.Filter = Properties.Resources.OpenDialogFilter;
  1873. dlg.FileName = PDFViewer.Document.FileName.Substring(0, PDFViewer.Document.FileName.LastIndexOf(".")) + "_Flatten.pdf";
  1874. if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
  1875. {
  1876. saveFile();
  1877. CPDFDocument kmdoc = CPDFDocument.InitWithFilePath(PDFViewer.Document.FileName);
  1878. if (kmdoc == null)
  1879. return;
  1880. bool result = kmdoc.WriteFlattenToFilePath(dlg.FileName);
  1881. if (result)
  1882. System.Diagnostics.Process.Start("explorer", "/select,\"" + dlg.FileName + "\"");
  1883. kmdoc.Release();
  1884. }
  1885. }
  1886. /// <summary>
  1887. /// 另存为后进行的操作
  1888. /// 重新打开新文档
  1889. /// </summary>
  1890. /// <param name="targetPath"></param>
  1891. public void DoAfterSaveAs(string targetPath)
  1892. {
  1893. App.OpenedFileList.Remove(targetPath);
  1894. string oldFilePath = targetPath;
  1895. PDFViewer.UndoManager.CanSave = false;
  1896. mainViewModel.OpenFile(targetPath);
  1897. //TODO:通知各模块更新PDFview对象
  1898. //var result = OpenFile(targetPath, true);
  1899. //if (result)
  1900. //{
  1901. // FileChanged.Invoke(this, null);
  1902. // Zoomer.PdfViewer = PdfViewer;
  1903. // PageSelector.PdfViewer = PdfViewer;
  1904. // ViewModeSelector.PdfViewer = PdfViewer;
  1905. // OpenFileInfo fileInfo = SettingHelper.GetFileInfo(oldFilePath);
  1906. // if (fileInfo != null)
  1907. // {
  1908. // PdfViewer.ChangeViewMode(fileInfo.LastViewMode);
  1909. // PdfViewer.ChangeFitMode(fileInfo.LastFitMode);
  1910. // if (fileInfo.LastFitMode == FitMode.FitFree)
  1911. // PdfViewer.Zoom(fileInfo.LastZoom);
  1912. // PdfViewer.GoToPage(fileInfo.LastPageIndex);
  1913. // if (fileInfo.LastDrawMode == DrawModes.Draw_Mode_Custom && fileInfo.LastFillColor != 0)
  1914. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode, fileInfo.LastFillColor);
  1915. // else
  1916. // PdfViewer.SetDrawMode(fileInfo.LastDrawMode);
  1917. // }
  1918. //}
  1919. }
  1920. /// <summary>
  1921. /// 显示前添加内容到Region
  1922. /// </summary>
  1923. /// <param name="isPageEdit"></param>
  1924. private void ShowContent(string currentBar, bool isToolMode = false)
  1925. {
  1926. GridVisibility = Visibility.Visible;
  1927. //显示页面编辑或其他工具
  1928. if (currentBar == "TabItemPageEdit" || isToolMode)
  1929. {
  1930. if (currentBar == "TabItemPageEdit")//进入页面编辑
  1931. {
  1932. if (GridToolRow != 1)
  1933. {
  1934. GridToolRow = 1;
  1935. }
  1936. if (GridToolRowSpan != 3)
  1937. {
  1938. GridToolRowSpan = 3;
  1939. }
  1940. }
  1941. else//进入水印等其他工具模式
  1942. {
  1943. GridVisibility = Visibility.Collapsed;
  1944. if (GridToolRow != 0)
  1945. {
  1946. GridToolRow = 0;
  1947. }
  1948. if (GridToolRowSpan != 4)
  1949. {
  1950. GridToolRowSpan = 4;
  1951. }
  1952. }
  1953. //ToolContent的visible跟toolsbarContent 的visible是互斥的
  1954. UpdateShowContent(currentBar);
  1955. }
  1956. else
  1957. {
  1958. if (GridToolRow != 1)
  1959. {
  1960. GridToolRow = 1;
  1961. }
  1962. UpdateShowContent(currentBar);
  1963. }
  1964. }
  1965. /// <summary>
  1966. /// 从二级工具栏进入需要修改界面布局的场景
  1967. /// </summary>
  1968. /// <param name="EditToolName"></param>
  1969. private void EnterEditTools(StringWithUnicode EditToolName)
  1970. {
  1971. EnterSelectedEditTool(EditToolName);
  1972. }
  1973. private void CloseEditTool(EnumCloseModeUnicode enumCloseModeunicode)
  1974. {
  1975. EnumCloseMode enumCloseMode = enumCloseModeunicode.Status;
  1976. if (enumCloseMode == EnumCloseMode.StatusConfirm)
  1977. {
  1978. PDFViewer.Document.ReleasePages();
  1979. PDFViewer.ReloadDocument();
  1980. }
  1981. //如果是其他工具共用一个PDFview,退出工具模式后,需要重新添加到Region,否则不会显示
  1982. if (!region.Regions[ViwerRegionName].Views.Contains(PDFViewer))
  1983. {
  1984. region.AddToRegion(ViwerRegionName, PDFViewer);
  1985. }
  1986. CurrentBar = "TabItemTool";
  1987. EnterSelectedBar(CurrentBar);
  1988. }
  1989. /// <summary>
  1990. /// 二级菜单指定目标处理
  1991. /// </summary>
  1992. /// <param name="e"></param>
  1993. private void EnterSelectedEditTool(StringWithUnicode EditToolName)
  1994. {
  1995. CurrentBar = EditToolName.EditToolsContentName;
  1996. EnterToolMode(CurrentBar);
  1997. }
  1998. /// <summary>
  1999. /// 进入工具编辑(如页面编辑、水印、密文等)模式
  2000. /// </summary>
  2001. /// <param name="targetToolMode">要导航过去的控件名称</param>
  2002. /// <param name="valuePairs">导航需要传送的参数,为空时,默认传送PDFView和ViewContentViewModel</param>
  2003. private async void EnterToolMode(string targetToolMode, NavigationParameters valuePairs = null)
  2004. {
  2005. IsLoading = Visibility.Visible;
  2006. await Task.Delay(3);
  2007. NavigationParameters param = new NavigationParameters();
  2008. if (valuePairs == null)
  2009. {
  2010. param.Add(ParameterNames.PDFViewer, PDFViewer);
  2011. param.Add(ParameterNames.ViewContentViewModel, this);
  2012. }
  2013. else//有传入其他内容的参数时
  2014. {
  2015. param = valuePairs;
  2016. }
  2017. region.RequestNavigate(ToolContentRegionName, targetToolMode, param);
  2018. ShowContent(CurrentBar, true);
  2019. IsLoading = Visibility.Collapsed;
  2020. }
  2021. private void EnterSelectedBar(string currentBar)
  2022. {
  2023. NavigationParameters param = new NavigationParameters();
  2024. param.Add(ParameterNames.PDFViewer, PDFViewer);
  2025. param.Add(ParameterNames.ViewContentViewModel, this);
  2026. region.RequestNavigate(regionNameByTabItem[currentBar], barContentByTabItem[currentBar], param);
  2027. if (currentBar == "TabItemScan")
  2028. {
  2029. region.RequestNavigate(RegionNames.OCRViewerRegionName, "ScanViwer", param);
  2030. }
  2031. ShowContent(currentBar);
  2032. }
  2033. /// <summary>
  2034. /// 退出工具(水印、密文等)编辑模式,隐藏ToolContent
  2035. /// </summary>
  2036. public void ExitToolMode()
  2037. {
  2038. ToolContentVisible = Visibility.Collapsed;
  2039. if (isInPageEdit)
  2040. {
  2041. TabSelectedIndex = 0;
  2042. isInPageEdit = false;
  2043. }
  2044. }
  2045. #endregion 方法
  2046. }
  2047. }