ViewContentViewModel.cs 108 KB

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