ViewContentViewModel.cs 106 KB

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