ViewContentViewModel.cs 92 KB

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