ViewContentViewModel.cs 85 KB

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