ViewContentViewModel.cs 86 KB

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