PageEditContentViewModel.cs 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit_Conversion.Converter;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Dropbox.Api.Team;
  5. using Microsoft.Win32;
  6. using PDF_Master.CustomControl;
  7. using PDF_Master.EventAggregators;
  8. using PDF_Master.Helper;
  9. using PDF_Master.Model;
  10. using PDF_Master.Model.HomePageToolsDialogs;
  11. using PDF_Master.Model.PageEdit;
  12. using PDF_Master.Properties;
  13. using PDFReader_WPF.Helper;
  14. using Prism.Commands;
  15. using Prism.Events;
  16. using Prism.Mvvm;
  17. using Prism.Regions;
  18. using Prism.Services.Dialogs;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Collections.ObjectModel;
  22. using System.Diagnostics;
  23. using System.IO;
  24. using System.Linq;
  25. using System.Threading;
  26. using System.Threading.Tasks;
  27. using System.Windows;
  28. using System.Windows.Controls;
  29. using System.Windows.Input;
  30. using System.Windows.Media;
  31. using System.Windows.Media.Imaging;
  32. using WIA;
  33. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  34. namespace PDF_Master.ViewModels.PageEdit
  35. {
  36. public class PageEditContentViewModel : BindableBase, INavigationAware
  37. {
  38. #region 文案
  39. private string T_title;
  40. public string T_Title
  41. {
  42. get { return T_title; }
  43. set
  44. {
  45. SetProperty(ref T_title, value);
  46. }
  47. }
  48. private string T_rotateLeftToolTip;
  49. public string T_RotateLeftToolTip
  50. {
  51. get { return T_rotateLeftToolTip; }
  52. set
  53. {
  54. SetProperty(ref T_rotateLeftToolTip, value);
  55. }
  56. }
  57. private string T_rotateRightToolTip;
  58. public string T_RotateRightToolTip
  59. {
  60. get { return T_rotateRightToolTip; }
  61. set
  62. {
  63. SetProperty(ref T_rotateRightToolTip, value);
  64. }
  65. }
  66. private string T_deleteToolTip;
  67. public string T_DeleteToolTip
  68. {
  69. get { return T_deleteToolTip; }
  70. set
  71. {
  72. SetProperty(ref T_deleteToolTip, value);
  73. }
  74. }
  75. private string T_deleteLook = "";
  76. public string T_DeleteLook
  77. {
  78. get { return T_deleteLook; }
  79. set
  80. {
  81. SetProperty(ref T_deleteLook, value);
  82. }
  83. }
  84. private string t_Insert;
  85. public string T_Insert
  86. {
  87. get { return t_Insert; }
  88. set
  89. {
  90. SetProperty(ref t_Insert, value);
  91. }
  92. }
  93. private string t_InsertFile;
  94. public string T_InsertFile
  95. {
  96. get { return t_InsertFile; }
  97. set
  98. {
  99. SetProperty(ref t_InsertFile, value);
  100. }
  101. }
  102. private string t_InsertaBlankPage;
  103. public string T_InsertaBlankPage
  104. {
  105. get { return t_InsertaBlankPage; }
  106. set
  107. {
  108. SetProperty(ref t_InsertaBlankPage, value);
  109. }
  110. }
  111. private string t_InsertaCustomPage;
  112. public string T_InsertaCustomPage
  113. {
  114. get { return t_InsertaCustomPage; }
  115. set
  116. {
  117. SetProperty(ref t_InsertaCustomPage, value);
  118. }
  119. }
  120. private string t_Extract;
  121. public string T_Extract
  122. {
  123. get { return t_Extract; }
  124. set
  125. {
  126. SetProperty(ref t_Extract, value);
  127. }
  128. }
  129. private string t_Replace;
  130. public string T_Replace
  131. {
  132. get { return t_Replace; }
  133. set
  134. {
  135. SetProperty(ref t_Replace, value);
  136. }
  137. }
  138. private string t_Split;
  139. public string T_Split
  140. {
  141. get { return t_Split; }
  142. set
  143. {
  144. SetProperty(ref t_Split, value);
  145. }
  146. }
  147. private string t_InsertPage;
  148. public string T_InsertPage
  149. {
  150. get { return t_InsertPage; }
  151. set
  152. {
  153. SetProperty(ref t_InsertPage, value);
  154. }
  155. }
  156. private string t_Reverse;
  157. public string T_Reverse
  158. {
  159. get { return t_Reverse; }
  160. set
  161. {
  162. SetProperty(ref t_Reverse, value);
  163. }
  164. }
  165. private string t_ExtractPage;
  166. public string T_ExtractPage
  167. {
  168. get { return t_ExtractPage; }
  169. set
  170. {
  171. SetProperty(ref t_ExtractPage, value);
  172. }
  173. }
  174. private string t_ReplacePage;
  175. public string T_ReplacePage
  176. {
  177. get { return t_ReplacePage; }
  178. set
  179. {
  180. SetProperty(ref t_ReplacePage, value);
  181. }
  182. }
  183. private void InitString()
  184. {
  185. T_InsertFile = App.MainPageLoader.GetString("PageEdit_Insert");
  186. T_Insert = App.MainPageLoader.GetString("PageEdit_InsertFile");
  187. T_InsertaBlankPage = App.MainPageLoader.GetString("PageEdit_InsertaBlankPage");
  188. T_InsertaCustomPage = App.MainPageLoader.GetString("PageEdit_InsertaCustomPage");
  189. T_Extract = App.MainPageLoader.GetString("PageEdit_Extract");
  190. T_Replace = App.MainPageLoader.GetString("PageEdit_Replace");
  191. T_Split = App.MainPageLoader.GetString("PageEdit_Split");
  192. T_InsertPage = App.MainPageLoader.GetString("PageEdit_InsertPage");
  193. T_Reverse = App.MainPageLoader.GetString("PageEdit_Reverse");
  194. T_ExtractPage = App.MainPageLoader.GetString("PageEdit_ExtractPage");
  195. T_ReplacePage = App.MainPageLoader.GetString("PageEdit_ReplacePage");
  196. T_Title = App.MainPageLoader.GetString("Thumbnails_Title");
  197. T_DeleteToolTip = App.MainPageLoader.GetString("ToolTipsPageEdit_Delete");
  198. T_DeleteLook = App.MainPageLoader.GetString("T_DeleteLook");
  199. T_RotateRightToolTip = App.MainPageLoader.GetString("ToolTipsPageEdit_RotateRight");
  200. T_RotateLeftToolTip = App.MainPageLoader.GetString("ToolTipsPageEdit_RotateLeft");
  201. }
  202. #endregion 文案
  203. public CPDFViewer PDFViewer;
  204. public PageEditItem CurrentPageEditItem = null;
  205. public ViewContentViewModel viewContentViewModel;
  206. private IDialogService dialogs;
  207. private IEventAggregator eventor;
  208. /// <summary>
  209. /// 用于筛选多窗体时间的唯一识别码
  210. /// </summary>
  211. private string unicode = null;
  212. /// <summary>
  213. /// 项数据集合
  214. /// </summary>
  215. public ObservableCollection<PageEditItem> PageEditItems { get; set; }
  216. /// <summary>
  217. /// 页面范围的项集合
  218. /// </summary>
  219. public ObservableCollection<string> PageRangeItems { get; set; }
  220. #region 属性
  221. /// <summary>
  222. /// 判断是否是添加页面
  223. /// </summary>
  224. public bool IsAddPages { get; set; } = false;
  225. /// <summary>
  226. /// 增加两个变量,用于记录当前控件内已经刷新图片的索引值,用于显示页面尺寸大小时,先显示当前范围可见Item的页面尺寸
  227. /// </summary>
  228. private int startRefreshedIndex = 0;
  229. private int endRefreshedIndex = 0;
  230. /// <summary>
  231. /// 正在初始化(正在初始化过程中 不发布通知BOTA刷新的事件)
  232. /// </summary>
  233. public bool Initializing = false;
  234. /// <summary>
  235. /// 是否是第一次加载
  236. /// </summary>
  237. private bool isFirstLoad = true;
  238. /// <summary>
  239. /// 记录多选项中最后一项的索引
  240. /// </summary>
  241. public int maxSelectedIndex = -1;
  242. private Visibility showTip = Visibility.Collapsed;
  243. /// <summary>
  244. /// 是否显示渐隐提示
  245. /// </summary>
  246. public Visibility ShowTip
  247. {
  248. get { return showTip; }
  249. set
  250. {
  251. SetProperty(ref showTip, value);
  252. }
  253. }
  254. private Visibility isLoading = Visibility.Collapsed;
  255. /// <summary>
  256. /// 是否正在处理中
  257. /// </summary>
  258. public Visibility IsLoading
  259. {
  260. get { return isLoading; }
  261. set
  262. {
  263. SetProperty(ref isLoading, value);
  264. }
  265. }
  266. private int listSelecedIndex;
  267. /// <summary>
  268. /// 缩略图选中项的索引
  269. /// </summary>
  270. public int ListSelectedIndex
  271. {
  272. get { return listSelecedIndex; }
  273. set
  274. {
  275. SetProperty(ref listSelecedIndex, value);
  276. if (!IsSelectFormSelf && value >= 0)
  277. {
  278. //显示页面范围选择控件
  279. TxtPageInputVisibility = Visibility.Visible;
  280. TbPageRange = (ListSelectedIndex + 1).ToString();
  281. }
  282. }
  283. }
  284. /// <summary>
  285. ///右键不会选中页面, 不要跳转
  286. /// </summary>
  287. /// 判断是否是右键选择
  288. private bool isMouseRightButtonDown = false;
  289. public bool IsMouseRightButtonDown
  290. {
  291. get { return isMouseRightButtonDown; }
  292. set { SetProperty(ref isMouseRightButtonDown, value); }
  293. }
  294. /// <summary>
  295. /// 右键选择索引
  296. /// </summary>
  297. public int RightSelectedIndex = 0;
  298. /// <summary>
  299. /// 右键选择 集合
  300. /// </summary>
  301. public List<int> RightSelectedIndexs = new List<int>();
  302. /// <summary>
  303. /// 多选
  304. /// </summary>
  305. private bool isMultiSelected;
  306. /// <summary>
  307. /// 是否有多选 用于判断倒序和替换功能
  308. /// </summary>
  309. public bool IsMultiSelected
  310. {
  311. get { return isMultiSelected; }
  312. set
  313. {
  314. SetProperty(ref isMultiSelected, value);
  315. }
  316. }
  317. private int comboxSelectIndex;
  318. /// <summary>
  319. /// 下拉框选项集合
  320. /// </summary>
  321. public int ComboxSelectIndex
  322. {
  323. get { return comboxSelectIndex; }
  324. set
  325. {
  326. SetProperty(ref comboxSelectIndex, value);
  327. //隐藏tb页面范围显示控件
  328. TxtPageInputVisibility = Visibility.Collapsed;
  329. //选中指定的页面范围
  330. SuitPageRange(comboxSelectIndex);
  331. }
  332. }
  333. private Visibility tbPageRangeVisibility = Visibility.Visible;
  334. /// <summary>
  335. /// 页面范围的文本显示
  336. /// </summary>
  337. public Visibility TbPageRangeVisibility
  338. {
  339. get { return tbPageRangeVisibility; }
  340. set
  341. {
  342. SetProperty(ref tbPageRangeVisibility, value);
  343. if (value == Visibility.Visible)
  344. {
  345. comboxSelectIndex = -1;
  346. }
  347. }
  348. }
  349. private string tbPageRange;
  350. /// <summary>
  351. /// 页面范围显示的内容
  352. /// </summary>
  353. public string TbPageRange
  354. {
  355. get { return tbPageRange; }
  356. set
  357. {
  358. SetProperty(ref tbPageRange, value);
  359. }
  360. }
  361. private Visibility txtPageInputVisibility = Visibility.Collapsed;
  362. /// <summary>
  363. /// 页面自定义输入框的显示
  364. /// </summary>
  365. public Visibility TxtPageInputVisibility
  366. {
  367. get { return txtPageInputVisibility; }
  368. set
  369. {
  370. SetProperty(ref txtPageInputVisibility, value);
  371. }
  372. }
  373. /// <summary>
  374. /// 页面缩放状态,-1 为最小值,1为最大值
  375. /// </summary>
  376. private int zoomIndex = 0;
  377. public int ZoomIndex
  378. {
  379. get { return zoomIndex; }
  380. set
  381. {
  382. SetProperty(ref zoomIndex, value);
  383. }
  384. }
  385. private string toastText = App.MainPageLoader.GetString("PageEdit_Failed");
  386. /// <summary>
  387. /// Toast提示
  388. /// </summary>
  389. public string ToastText
  390. {
  391. get { return toastText; }
  392. set
  393. {
  394. SetProperty(ref toastText, value);
  395. }
  396. }
  397. private Visibility isBOTAThumb = Visibility.Collapsed;
  398. /// <summary>
  399. /// 是否是BOTA侧边栏的缩略图
  400. /// </summary>
  401. public Visibility IsBOTAThumb
  402. {
  403. get { return isBOTAThumb; }
  404. set
  405. {
  406. SetProperty(ref isBOTAThumb, value);
  407. }
  408. }
  409. private Visibility showedPageSize = Visibility.Collapsed;
  410. /// <summary>
  411. /// 显示页面尺寸
  412. /// </summary>
  413. public Visibility ShowedPageSize
  414. {
  415. get { return showedPageSize; }
  416. set
  417. {
  418. SetProperty(ref showedPageSize, value);
  419. //if (IsBOTAThumb != Visibility.Visible)
  420. //{
  421. // if (value == Visibility.Visible)
  422. // {
  423. // HidePageSize = Visibility.Collapsed;
  424. // }
  425. // else
  426. // {
  427. // HidePageSize = Visibility.Visible;
  428. // }
  429. //}
  430. }
  431. }
  432. private Visibility hidePageSize = Visibility.Collapsed;
  433. /// <summary>
  434. /// 隐藏页面尺寸
  435. /// </summary>
  436. public Visibility HidePageSize
  437. {
  438. get { return hidePageSize; }
  439. set
  440. {
  441. SetProperty(ref hidePageSize, value);
  442. }
  443. }
  444. private bool canPaste = false;
  445. /// <summary>
  446. /// 是否可以粘贴
  447. /// </summary>
  448. public bool CanPaste
  449. {
  450. get { return canPaste; }
  451. set
  452. {
  453. SetProperty(ref canPaste, value);
  454. }
  455. }
  456. /// <summary>
  457. /// 页面缩放的范围
  458. /// </summary>
  459. private List<Size> ItemSizes = new List<Size>() {
  460. new Size(104,142),
  461. new Size(208, 294),
  462. new Size(312,441)
  463. };
  464. //选中的页面集合
  465. private List<int> selectedPageList = new List<int>();
  466. /// <summary>
  467. /// 是否是下拉框选项改变引起的选项变更事件
  468. /// </summary>
  469. private bool IsSelectFormSelf = false;
  470. public bool IsShowPageSize = false;
  471. public string Txt = Properties.Resources.txtex;
  472. public string Word = Properties.Resources.wordex;
  473. public string Ppt = Properties.Resources.pptex;
  474. public string Excel = Properties.Resources.excelex;
  475. public string Html = Properties.Resources.htmlex;
  476. public string Image = Properties.Resources.imageex;
  477. public string Pdf = Properties.Resources.pdf;
  478. #endregion 属性
  479. #region 命令
  480. public DelegateCommand<object> SelectionChangedCommand { get; set; }
  481. /// <summary>
  482. /// 拦截键盘输入的事件
  483. /// </summary>
  484. public DelegateCommand<object> PreviewKeyDown { get; set; }
  485. /// <summary>
  486. /// 粘贴
  487. /// </summary>
  488. public DelegateCommand PasteCommand { get; set; }
  489. /// <summary>
  490. /// 复制
  491. /// </summary>
  492. public DelegateCommand CopyCommand { get; set; }
  493. /// <summary>
  494. /// 剪切
  495. /// </summary>
  496. public DelegateCommand CutCommand { get; set; }
  497. /// <summary>
  498. /// 替换文件
  499. /// </summary>
  500. public DelegateCommand ReplaceCommand { get; set; }
  501. /// <summary>
  502. /// 插入文件命令
  503. /// </summary>
  504. public DelegateCommand<object> InsertCommand { get; set; }
  505. /// <summary>
  506. /// 拆分文件
  507. /// </summary>
  508. public DelegateCommand SplitCommand { get; set; }
  509. /// <summary>
  510. /// 提取文件
  511. /// </summary>
  512. public DelegateCommand ExtractCommand { get; set; }
  513. /// <summary>
  514. /// 逆时针旋转
  515. /// </summary>
  516. public DelegateCommand LeftRotateCommand { get; set; }
  517. /// <summary>
  518. /// 顺时针旋转
  519. /// </summary>
  520. public DelegateCommand RightRotateCommand { get; set; }
  521. /// <summary>
  522. /// 删除
  523. /// </summary>
  524. public DelegateCommand DeleteCommand { get; set; }
  525. /// <summary>
  526. /// 倒序
  527. /// </summary>
  528. public DelegateCommand ReverseCommand { get; set; }
  529. /// <summary>
  530. /// 放大
  531. /// </summary>
  532. public DelegateCommand ZoomInCommand { get; set; }
  533. /// <summary>
  534. /// 缩小
  535. /// </summary>
  536. public DelegateCommand ZoomOutCommand { get; set; }
  537. /// <summary>
  538. /// 显示页面大小
  539. /// </summary>
  540. public DelegateCommand ShowPageSizeCommand { get; set; }
  541. public DelegateCommand ClearSelectedCommand { get; set; }
  542. /// <summary>
  543. /// 进入页面编辑
  544. /// </summary>
  545. public DelegateCommand EnterPageEditCommand { get; set; }
  546. #endregion 命令
  547. public PageEditContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
  548. {
  549. //变量初始化
  550. dialogs = dialogService;
  551. eventor = eventAggregator;
  552. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  553. PageEditItems = new ObservableCollection<PageEditItem>();
  554. PageRangeItems = new ObservableCollection<string>();
  555. //绑定命令
  556. SelectionChangedCommand = new DelegateCommand<object>(SelectionChangedEvent);
  557. PreviewKeyDown = new DelegateCommand<object>(PreviewKeyDownEvent);
  558. SplitCommand = new DelegateCommand(SplitCommandEvent);
  559. InsertCommand = new DelegateCommand<object>(InsertCommandEvent);
  560. ReplaceCommand = new DelegateCommand(ReplaceCommandEvent, CanFileCommandExcute).ObservesProperty(() => IsMouseRightButtonDown);
  561. ExtractCommand = new DelegateCommand(ExtractCommandEvent, CanFileCommandExcute).ObservesProperty(() => IsMouseRightButtonDown);
  562. DeleteCommand = new DelegateCommand(DeleteCommandEvent, CanFileCommandExcute).ObservesProperty(() => IsMouseRightButtonDown);
  563. LeftRotateCommand = new DelegateCommand(LeftRotateCommandEvent, CanFileCommandExcute).ObservesProperty(() => IsMouseRightButtonDown);
  564. RightRotateCommand = new DelegateCommand(RightRotateCommandEvent, CanFileCommandExcute).ObservesProperty(() => IsMouseRightButtonDown);
  565. ReverseCommand = new DelegateCommand(ReverseCommandEvent, CanReverseFileCommandExcute).ObservesProperty(() => IsMultiSelected);
  566. ZoomInCommand = new DelegateCommand(ZoomInCommandEvent, CanZoomIn).ObservesProperty(() => ZoomIndex);
  567. ZoomOutCommand = new DelegateCommand(ZoomOutCommandEvent, CanZoomOut).ObservesProperty(() => ZoomIndex);
  568. ShowPageSizeCommand = new DelegateCommand(ShowPageSize);
  569. CopyCommand = new DelegateCommand(copy);
  570. PasteCommand = new DelegateCommand(() => { Paste(); }, CanPasteExcute).ObservesProperty(() => CanPaste);
  571. CutCommand = new DelegateCommand(cut);
  572. ClearSelectedCommand = new DelegateCommand(clearSelected);
  573. EnterPageEditCommand = new DelegateCommand(enterpageeditr);
  574. //订阅页面刷新事件
  575. eventAggregator.GetEvent<PageEditRefreshEvent>().Subscribe(OnPageEditRefreshEvent, e => e.Unicode == unicode);
  576. eventAggregator.GetEvent<ViewContentEvent>().Subscribe(ViewContentChangedEvent, e => e.Unicode == unicode);
  577. CacheFilePath.Instance.PropertyChanged += Instance_PropertyChanged;
  578. RefreshPageRangeItem();
  579. InitString();
  580. }
  581. #region 事件
  582. private void ViewContentChangedEvent(ViewContentEventArgs obj)
  583. {
  584. //只有在缩略图模式下响应刷新,进入页面编辑时 会刷新可见范围内图片,不需要响应这个事件
  585. if (IsBOTAThumb == Visibility.Visible)
  586. {
  587. PageEditItems[PDFViewer.CurrentIndex].IsGetImage = false;
  588. PageEditItems[PDFViewer.CurrentIndex].Size = PDFViewer.Document.GetPageSize(PDFViewer.CurrentIndex);
  589. RefreshItemImage(PDFViewer.CurrentIndex, PDFViewer.CurrentIndex);
  590. }
  591. }
  592. /// <summary>
  593. /// 进入页面编辑
  594. /// </summary>
  595. private void enterpageeditr()
  596. {
  597. viewContentViewModel.EnterPageEdit();
  598. }
  599. private void Instance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  600. {
  601. if (e.PropertyName == "CopyDoc")
  602. {
  603. if (CacheFilePath.Instance.CopyDoc != null)
  604. {
  605. CanPaste = true;
  606. }
  607. }
  608. }
  609. /// <summary>
  610. /// 替换功能的逻辑
  611. /// 实现效果是,先删除后插入
  612. /// 跨页多选后替换的位置是多选的第一页位置
  613. /// </summary>
  614. private async void ReplaceCommandEvent()
  615. {
  616. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  617. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Replace);
  618. DataTrackingHelper.IsClearEntryPath = true;
  619. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Replace");
  620. //支持插入格式:PDF、图片、Office格式文件
  621. string allfiles = Pdf + Txt + Word + Excel + Ppt + Image + Html;
  622. OpenFileDialog dialog = new OpenFileDialog();
  623. //dialog.Multiselect = false;
  624. //dialog.Filter = PDF_Master.Properties.Resources.OpenDialogFilter;
  625. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  626. $"PDF({Pdf})|{Pdf}|" +
  627. $"Microsoft Office Word({Word})|{Word}|" +
  628. $"Microsoft Office Excel({Excel})|{Excel}|" +
  629. $"Microsoft Office PowerPoint({Ppt})|{Ppt}|" +
  630. $"Txt({Txt})|{Txt}|" +
  631. $"Picture({Image})|{Image}|" +
  632. $"Html({Html})|{Html}"
  633. );
  634. List<int> indexList = new List<int>();
  635. for (int i = 0; i < PageEditItems.Count; i++)
  636. {
  637. if (PageEditItems[i].Selected)
  638. {
  639. indexList.Add(i);
  640. }
  641. }
  642. //因为虚拟化 可能获取不到所有选中项的页码 需要结合选中项的集合判断一下
  643. if (selectedPageList.Count > 0 && selectedPageList.Count > indexList.Count)
  644. {
  645. indexList = new List<int>();
  646. for (int j = 0; j < selectedPageList.Count; j++)
  647. {
  648. indexList.Add(selectedPageList[j] - 1);
  649. }
  650. }
  651. indexList.Sort();
  652. //全选页面 提示不能全选,230711不要提示
  653. //if (indexList.Count > 0 && indexList.Count == PDFViewer.Document.PageCount)
  654. //{
  655. // AlertsMessage alertsMessage = new AlertsMessage();
  656. // alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageEdit_DeleteAllPage"), App.ServiceLoader.GetString("Text_ok"));
  657. // return;
  658. //}
  659. if ((bool)dialog.ShowDialog())
  660. {
  661. //付费锁
  662. if (!ServiceHelper.IAPBeforeFunction())
  663. {
  664. return;
  665. }
  666. IsLoading = Visibility.Visible;
  667. await Task.Delay(200);
  668. int pageCount = PDFViewer.Document.PageCount;
  669. int insertIndex = listSelecedIndex;
  670. string fileName = GetInsertFromFile(dialog.FileName);
  671. #region 历史版本
  672. //从文件插入到指定位置
  673. //var result = await InsertFromFile(listSelecedIndex + 1, fileName);
  674. //if (result)
  675. //{
  676. // //插入成功后再删除原有页面
  677. // //2023-06-05 索引需要-1,因为上面的listSelecedIndex + 1,需要还原
  678. // //DoRemoveSelectedPages(new List<int>() { ListSelectedIndex - 1 });
  679. // DoRemoveSelectedPages(indexList);
  680. //}
  681. #endregion 历史版本
  682. //先记录索引(第一个选定页面)
  683. int index = ListSelectedIndex;
  684. if (IsMouseRightButtonDown)
  685. {
  686. index = RightSelectedIndex;
  687. if (IsMultiSelected)
  688. {
  689. indexList = RightSelectedIndexs;
  690. }
  691. else
  692. {
  693. indexList = new List<int> { index };
  694. }
  695. }
  696. //替换页面 先看文件是否加密,如果加密,先解密后,再替换
  697. //将文档验证是否为空,是否解密,置前,若为空直接退出
  698. bool isDiscryptied = GetDocumentIsDiscryptied(fileName, null, out CPDFDocument doc, out string password);
  699. if (isDiscryptied)
  700. {
  701. //先删除
  702. int flag = DoRemoveSelectedPages(indexList, false, true);
  703. if (flag == -1)
  704. {
  705. return;
  706. }
  707. if (PDFViewer.Document.PageCount == 0)
  708. {
  709. index = 0;
  710. }
  711. //再新增,避免索引串号的情况
  712. var result = await InsertFromFile(index, fileName, doc, password);
  713. if (result)
  714. {
  715. ReloadAfterOption(true, false, Tuple.Create(insertIndex - 1, insertIndex + pageCount));
  716. }
  717. }
  718. IsLoading = Visibility.Collapsed;
  719. }
  720. }
  721. /// <summary>
  722. /// 拆分功能的逻辑
  723. /// </summary>
  724. private void SplitCommandEvent()
  725. {
  726. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  727. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Split);
  728. DataTrackingHelper.IsClearEntryPath = true;
  729. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Split");
  730. DialogParameters valuePairs = new DialogParameters();
  731. valuePairs.Add(ParameterNames.PDFViewer, PDFViewer);
  732. valuePairs.Add(ParameterNames.PageEditSelectedIndex, ListSelectedIndex);
  733. valuePairs.Add(ParameterNames.PageEditSelectedItems, TbPageRange);
  734. dialogs.ShowDialog(DialogNames.SplitDialog, valuePairs, e =>
  735. {
  736. if (e.Result == ButtonResult.OK && e.Parameters != null)
  737. {
  738. //付费锁
  739. if (!ServiceHelper.IAPBeforeFunction())
  740. {
  741. return;
  742. }
  743. //保存并继续,使用了高级功能则弹窗权益弹窗
  744. if (!viewContentViewModel.saveFile(true))
  745. {
  746. return;
  747. }
  748. var model = e.Parameters.GetValue<HomePageSplitDialogModel>(ParameterNames.DataModel);
  749. DoSplitPages(model);
  750. }
  751. });
  752. }
  753. /// <summary>
  754. /// 提取功能的逻辑
  755. /// </summary>
  756. private void ExtractCommandEvent()
  757. {
  758. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  759. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Extract);
  760. DataTrackingHelper.IsClearEntryPath = true;
  761. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Extract");
  762. DialogParameters keyValuePairs = new DialogParameters();
  763. //因无法删除所有页面,若选中所有页面提取,则需要将「提取后删除页面」复选项置灰
  764. if (selectedPageList.Count == PageEditItems.Count)
  765. {
  766. keyValuePairs.Add(ParameterNames.IsDeleteAfterExtract, false);
  767. }
  768. else
  769. {
  770. keyValuePairs.Add(ParameterNames.IsDeleteAfterExtract, true);
  771. }
  772. dialogs.ShowDialog(DialogNames.ExtractDialog, keyValuePairs, e =>
  773. {
  774. if (e.Result == ButtonResult.OK && e.Parameters != null)
  775. {
  776. //付费锁
  777. if (!ServiceHelper.IAPBeforeFunction())
  778. {
  779. return;
  780. }
  781. //保存并继续,使用了高级功能则弹窗权益弹窗
  782. if (!viewContentViewModel.saveFile(true))
  783. {
  784. return;
  785. }
  786. var model = e.Parameters.GetValue<ExtractModel>(ParameterNames.DataModel);
  787. DoExtractPages(model.IsEveryPageToFile, model.IsDeleteAfterExtract);
  788. }
  789. });
  790. }
  791. /// <summary>
  792. /// 插入功能的逻辑
  793. /// </summary>
  794. private async void InsertCommandEvent(object type)
  795. {
  796. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  797. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Insert);
  798. DataTrackingHelper.IsClearEntryPath = true;
  799. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Insert");
  800. if ((string)type == null)
  801. {
  802. return;
  803. }
  804. int pageIndex = 0;
  805. if (IsMouseRightButtonDown)
  806. {
  807. pageIndex = RightSelectedIndex;
  808. }
  809. else
  810. {
  811. pageIndex = ListSelectedIndex;
  812. }
  813. //获取插入位置
  814. int insertIndex = 0;
  815. if (pageIndex < 0)
  816. {
  817. insertIndex = PageEditItems.Count;
  818. }
  819. else
  820. {
  821. insertIndex = pageIndex + 1;
  822. if (insertIndex >= PDFViewer.Document.PageCount)
  823. {
  824. insertIndex = PDFViewer.Document.PageCount;
  825. }
  826. if (isMultiSelected && maxSelectedIndex != -1)
  827. {
  828. insertIndex = maxSelectedIndex + 1;
  829. }
  830. }
  831. IsLoading = Visibility.Visible;
  832. await System.Threading.Tasks.Task.Delay(3);
  833. PageEditHistory history = new PageEditHistory();
  834. history.InsertIndex = insertIndex;
  835. #region 插入空白页面
  836. if ((string)type == ParameterNames.InsertType_Blank)
  837. {
  838. //付费锁
  839. if (!ServiceHelper.IAPBeforeFunction())
  840. {
  841. return;
  842. }
  843. history.Type = ActionType.InsertCustomPages;
  844. history.InsertFilePath = "";
  845. //插入空白页面
  846. bool result;
  847. if (insertIndex == 0)
  848. {
  849. result = PDFViewer.Document.InsertPage(insertIndex, PageEditItems[insertIndex].Size.Width, PageEditItems[insertIndex].Size.Height, null);
  850. history.PageSize = PageEditItems[insertIndex].Size;
  851. }
  852. else
  853. {
  854. result = PDFViewer.Document.InsertPage(insertIndex, PageEditItems[insertIndex - 1].Size.Width, PageEditItems[insertIndex - 1].Size.Height, null);
  855. history.PageSize = PageEditItems[insertIndex - 1].Size;
  856. }
  857. if (!result)
  858. {
  859. IsLoading = Visibility.Collapsed;
  860. ShowToast();
  861. return;
  862. }
  863. //UI层插入Item
  864. var item = GetNewPageEditItem(insertIndex);
  865. PageEditItems.Insert(insertIndex, item);
  866. //插入后 滚动到 选中的页面
  867. ReloadAfterOption(true, false, Tuple.Create(0, insertIndex + 1), true);
  868. await Task.Delay(10);
  869. //选中插入的页面
  870. ListSelectedIndex = insertIndex;
  871. }
  872. #endregion 插入空白页面
  873. #region 插入文件
  874. else if ((string)type == ParameterNames.InsertType_File)
  875. {
  876. //支持插入格式:PDF、图片、Office格式文件
  877. string allfiles = Pdf + Txt + Word + Excel + Ppt + Image + Html;
  878. OpenFileDialog dialog = new OpenFileDialog();
  879. dialog.Multiselect = false;
  880. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  881. $"PDF({Pdf})|{Pdf}|" +
  882. $"Microsoft Office Word({Word})|{Word}|" +
  883. $"Microsoft Office Excel({Excel})|{Excel}|" +
  884. $"Microsoft Office PowerPoint({Ppt})|{Ppt}|" +
  885. $"Txt({Txt})|{Txt}|" +
  886. $"Picture({Image})|{Image}|" +
  887. $"Html({Html})|{Html}"
  888. );
  889. //OpenFileDialog dialog = new OpenFileDialog();
  890. //dialog.Multiselect = false;
  891. //dialog.Filter = PDF_Master.Properties.Resources.OpenDialogFilter;
  892. if ((bool)dialog.ShowDialog())
  893. {
  894. //付费锁
  895. if (!ServiceHelper.IAPBeforeFunction())
  896. {
  897. return;
  898. }
  899. string fileName = GetInsertFromFile(dialog.FileName);
  900. //将文档验证是否为空,是否解密,置前,若为空直接退出
  901. bool IsLocked = GetDocumentIsDiscryptied(fileName, null, out CPDFDocument doc, out string password);
  902. if (IsLocked == false)
  903. {
  904. return;
  905. }
  906. //ToolMethod.SetFileThumbImg(fileName);
  907. //插入文件
  908. var result = await InsertFromFile(insertIndex, fileName, doc, password);
  909. if (!result)
  910. {
  911. IsLoading = Visibility.Collapsed;
  912. ShowToast();
  913. return;
  914. }
  915. ReloadAfterOption(true, false, Tuple.Create(0, PDFViewer.Document.PageCount));
  916. }
  917. IsLoading = Visibility.Collapsed;
  918. //里面附带了Undo栈记录,需要提前返回,避免重复记录
  919. return;
  920. }
  921. #endregion 插入文件
  922. #region 插入自定义页面
  923. else
  924. {
  925. DialogParameters keyValuePairs = new DialogParameters();
  926. Size size = new Size();
  927. size.Width = CommonHelper.GetUnitsFromPageSize(PDFViewer.Document.GetPageSize(listSelecedIndex).Width);
  928. size.Height = CommonHelper.GetUnitsFromPageSize(PDFViewer.Document.GetPageSize(listSelecedIndex).Height);
  929. keyValuePairs.Add(ParameterNames.CurrentPageSize, size);
  930. keyValuePairs.Add(ParameterNames.PageEditSelectedIndex, pageIndex);
  931. //int o = ListSelectedIndex;
  932. //插入自定义页面
  933. dialogs.ShowDialog(DialogNames.InsertDialog, keyValuePairs, e =>
  934. {
  935. if (e.Result == ButtonResult.OK && e.Parameters != null)
  936. {
  937. //付费锁
  938. if (!ServiceHelper.IAPBeforeFunction())
  939. {
  940. return;
  941. }
  942. var model = e.Parameters.GetValue<CustomInsertModel>(ParameterNames.DataModel);
  943. var result = InsertFromCustomPage(insertIndex, model.width, model.height, model.filepath);
  944. if (!result)
  945. {
  946. return;
  947. }
  948. history.Type = ActionType.InsertCustomPages;
  949. history.InsertFilePath = model.filepath;
  950. history.PageSize = new Size(model.width, model.height);
  951. }
  952. });
  953. }
  954. #endregion 插入自定义页面
  955. IsLoading = Visibility.Collapsed;
  956. //添加记录到Undo栈,清除Redo栈
  957. history.viewmodel = this;
  958. PDFViewer.UndoManager.AddHistory(history);
  959. }
  960. public string GetInsertFromFile(string file)
  961. {
  962. var fileType = System.IO.Path.GetExtension(file).ToLower();
  963. string fileName = string.Empty;
  964. if (Pdf.Contains(fileType))
  965. {
  966. fileName = file;
  967. }
  968. else if (Image.Contains(fileType))
  969. {
  970. fileName = GetImageFile(file);
  971. }
  972. else
  973. {
  974. fileName = GetOfficeFile(file);
  975. }
  976. if (string.IsNullOrEmpty(fileName))
  977. {
  978. AlertsMessage alertsMessage = new AlertsMessage();
  979. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
  980. IsLoading = Visibility.Collapsed;
  981. return fileName;
  982. }
  983. if (File.Exists(fileName) == false)
  984. {
  985. AlertsMessage alertsMessage = new AlertsMessage();
  986. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("ErrorFile_Warning"), App.ServiceLoader.GetString("Text_ok"));
  987. IsLoading = Visibility.Collapsed;
  988. return fileName;
  989. }
  990. return fileName;
  991. }
  992. private string GetOfficeFile(string sourcepath, Microsoft.Office.Interop.Word.WdPaperSize paperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4, double margin = 0)
  993. {
  994. try
  995. {
  996. string fileName = null;
  997. CPDFViewer pDFViewer = new CPDFViewer();
  998. pDFViewer.CreateDocument();
  999. string targetPath = "";
  1000. targetPath = ConverterHelper.ConvertOfficeToPDF(sourcepath, paperSize, margin);
  1001. if (pDFViewer.Document == null)
  1002. {
  1003. AlertsMessage alertsMessage = new AlertsMessage();
  1004. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Create PDF failed"), App.ServiceLoader.GetString("Text_ok"));
  1005. return null;
  1006. }
  1007. string file1 = sourcepath.Substring(sourcepath.LastIndexOf("\\") + 1, sourcepath.LastIndexOf(".") - sourcepath.LastIndexOf("\\") - 1) + ".pdf";
  1008. var tempdoc = CPDFDocument.InitWithFilePath(targetPath);
  1009. if (tempdoc == null)
  1010. {
  1011. AlertsMessage alertsMessage = new AlertsMessage();
  1012. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Create PDF failed"), App.ServiceLoader.GetString("Text_ok"));
  1013. return null;
  1014. }
  1015. pDFViewer.Document.ImportPages(tempdoc, "");
  1016. string tempfile = CacheFilePath.Instance.CreatedFilePath;
  1017. fileName = CommonHelper.CreateFilePath(Path.Combine(tempfile, App.UntitledFileNmae));
  1018. pDFViewer.Document.WriteToFilePath(fileName);
  1019. pDFViewer.CloseDocument();
  1020. pDFViewer.InitDocument(fileName);
  1021. return fileName;
  1022. }
  1023. catch
  1024. {
  1025. string str = "没有安装";
  1026. //判断是否有安装office软件
  1027. var officeType = Type.GetTypeFromProgID("Word.Application");
  1028. if (officeType == null)
  1029. {
  1030. str += "Word;";
  1031. }
  1032. officeType = Type.GetTypeFromProgID("Excel.Application");
  1033. if (officeType == null)
  1034. {
  1035. str += "Excel;";
  1036. }
  1037. officeType = Type.GetTypeFromProgID("Powerpoint.Application");
  1038. if (officeType == null)
  1039. {
  1040. str += "Excel;";
  1041. }
  1042. AlertsMessage alertsMessage = new AlertsMessage();
  1043. if (str != "没有安装")
  1044. {
  1045. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("NoOffice_Warning"), App.ServiceLoader.GetString("Text_ok"));
  1046. }
  1047. else
  1048. {
  1049. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("CreateFileFailedWarning"), App.ServiceLoader.GetString("Text_ok"));
  1050. }
  1051. return null;
  1052. }
  1053. }
  1054. private string GetImageFile(string fileName)
  1055. {
  1056. CPDFViewer pDFViewer = new CPDFViewer();
  1057. pDFViewer.CreateDocument();
  1058. string path = fileName.Substring(fileName.LastIndexOf("\\") + 1, fileName.LastIndexOf(".") - fileName.LastIndexOf("\\") - 1) + ".pdf";
  1059. System.Drawing.Bitmap pic = new System.Drawing.Bitmap(fileName);
  1060. int width = pic.Size.Width;
  1061. int height = pic.Size.Height;
  1062. string ex = System.IO.Path.GetExtension(fileName);
  1063. //其他格式或者名称中含空格的图片 在本地先转存一下
  1064. if ((ex != ".jpg" && ex != ".jpeg") || !Uri.IsWellFormedUriString(fileName, UriKind.Absolute))
  1065. {
  1066. //将其他格式图片转换成jpg
  1067. string folderPath = Path.GetTempPath();
  1068. if (File.Exists(folderPath))
  1069. {
  1070. File.Delete(folderPath);
  1071. }
  1072. DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
  1073. if (!tempfolder.Exists)
  1074. {
  1075. tempfolder.Create();
  1076. }
  1077. string targetPath = System.IO.Path.Combine(folderPath, Guid.NewGuid().ToString());
  1078. fileName = targetPath;
  1079. pic.Save(targetPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  1080. }
  1081. pic.Dispose();
  1082. var result = pDFViewer.Document.InsertPage(0, width, height, fileName);
  1083. if (!result)
  1084. {
  1085. AlertsMessage alertsMessage = new AlertsMessage();
  1086. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Create PDF failed"), App.ServiceLoader.GetString("Text_ok"));
  1087. return null;
  1088. }
  1089. string tempfile = CacheFilePath.Instance.CreatedFilePath;
  1090. string file = CommonHelper.CreateFilePath(Path.Combine(tempfile, App.UntitledFileNmae));
  1091. pDFViewer.Document.WriteToFilePath(file);
  1092. pDFViewer.CloseDocument();
  1093. pDFViewer.InitDocument(file);
  1094. return file;
  1095. }
  1096. /// <summary>
  1097. /// 删除选中页面
  1098. /// </summary>
  1099. private async void DeleteCommandEvent()
  1100. {
  1101. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  1102. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Delete);
  1103. DataTrackingHelper.IsClearEntryPath = true;
  1104. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Delete");
  1105. AlertsMessage alertsMessage = new AlertsMessage();
  1106. alertsMessage.ShowDialog("", T_DeleteLook, App.ServiceLoader.GetString("Text_no"), App.ServiceLoader.GetString("Text_ok"));
  1107. if (alertsMessage.result == ContentResult.Ok)
  1108. {
  1109. IsLoading = Visibility.Visible;
  1110. await Task.Delay(3);
  1111. int pageIndex = 0;
  1112. int index = 0;
  1113. if (IsMouseRightButtonDown)
  1114. {
  1115. int count = RightSelectedIndexs.Count;
  1116. pageIndex = RightSelectedIndex;
  1117. if (IsMultiSelected)
  1118. {
  1119. index = DoRemoveSelectedPages(RightSelectedIndexs);
  1120. }
  1121. else
  1122. {
  1123. index = DoRemoveSelectedPages(new List<int> { pageIndex });
  1124. }
  1125. }
  1126. else
  1127. {
  1128. index = DoRemoveSelectedPages();
  1129. }
  1130. //删除后 选中项
  1131. if (index != -1)
  1132. {
  1133. if (index <= PDFViewer.Document.PageCount)
  1134. {
  1135. index = index + 1;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. index = 0;
  1141. }
  1142. if (IsBOTAThumb == Visibility.Visible)
  1143. {
  1144. GoToPage(new List<int>() { index });
  1145. }
  1146. ReloadAfterOption(true, true, Tuple.Create(0, PDFViewer.Document.PageCount));
  1147. //通知Xaml删除后 选中的页面,从VM设置选中项会有问题
  1148. NotifyUIToRefresh(new List<int>() { index });
  1149. IsLoading = Visibility.Collapsed;
  1150. }
  1151. }
  1152. /// <summary>
  1153. /// 左旋转页面的逻辑
  1154. /// </summary>
  1155. private void LeftRotateCommandEvent()
  1156. {
  1157. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  1158. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Rotate);
  1159. DataTrackingHelper.IsClearEntryPath = true;
  1160. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Rotate");
  1161. List<int> pageitems = new List<int>();
  1162. if (IsMouseRightButtonDown)
  1163. {
  1164. if (IsMultiSelected)
  1165. {
  1166. pageitems.AddRange(RightSelectedIndexs);
  1167. }
  1168. else
  1169. {
  1170. pageitems.Add(RightSelectedIndex);
  1171. }
  1172. RotateSelectedPages(false, pageitems);
  1173. }
  1174. else
  1175. {
  1176. RotateSelectedPages(false);
  1177. }
  1178. }
  1179. /// <summary>
  1180. /// 右旋转页面的逻辑
  1181. /// </summary>
  1182. private void RightRotateCommandEvent()
  1183. {
  1184. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  1185. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Rotate);
  1186. DataTrackingHelper.IsClearEntryPath = true;
  1187. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Rotate");
  1188. List<int> pageitems = new List<int>();
  1189. if (IsMouseRightButtonDown)
  1190. {
  1191. if (IsMultiSelected)
  1192. {
  1193. pageitems.AddRange(RightSelectedIndexs);
  1194. }
  1195. else
  1196. {
  1197. pageitems.Add(RightSelectedIndex);
  1198. }
  1199. RotateSelectedPages(true, pageitems);
  1200. }
  1201. else
  1202. {
  1203. RotateSelectedPages();
  1204. }
  1205. }
  1206. /// <summary>
  1207. /// 页面倒序的逻辑
  1208. /// </summary>
  1209. private async void ReverseCommandEvent()
  1210. {
  1211. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  1212. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Reverse);
  1213. DataTrackingHelper.IsClearEntryPath = true;
  1214. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Reverse");
  1215. IsLoading = Visibility.Visible;
  1216. await Task.Delay(3);
  1217. List<int> pages = new List<int>();
  1218. for (int i = 0; i < PageEditItems.Count; i++)
  1219. {
  1220. if (PageEditItems[i].Selected)
  1221. {
  1222. pages.Add(i);
  1223. }
  1224. }
  1225. DoReverse(pages);
  1226. IsLoading = Visibility.Collapsed;
  1227. }
  1228. /// <summary>
  1229. /// 将指定页面结合进行倒序
  1230. /// </summary>
  1231. private async void DoReverse(List<int> pages, bool isFromUndoRedo = false)
  1232. {
  1233. if (pages.Count <= 1)
  1234. {
  1235. ShowToast("请至少选择两个图片进行倒序操作。");
  1236. return;
  1237. }
  1238. //付费锁
  1239. if (!ServiceHelper.IAPBeforeFunction())
  1240. {
  1241. return;
  1242. }
  1243. pages.Sort();
  1244. //记录原位置和目标位置 用于undo,redo
  1245. Dictionary<int, int> sourceTarget = new Dictionary<int, int>();
  1246. for (int i = 0; i < pages.Count; i++)
  1247. {
  1248. int preindex = pages[i];
  1249. int laterIndex = pages[pages.Count - i - 1];//尾部的索引
  1250. if (laterIndex <= preindex)//交换完成
  1251. {
  1252. //用于记录选中集合为奇数时,最中间Item的还原
  1253. sourceTarget.Add(preindex, preindex);
  1254. break;
  1255. }
  1256. var result = ExchangeItem(preindex, laterIndex);
  1257. if (!result)
  1258. {
  1259. ShowToast();
  1260. IsLoading = Visibility.Collapsed;
  1261. return;
  1262. }
  1263. sourceTarget.Add(preindex, laterIndex);
  1264. }
  1265. await Task.Delay(20);
  1266. //转换成页码数据集合,传递给UI,选中目标页面
  1267. //因虚拟化加载的问题,通过Selected来控制Item选中效果会有问题
  1268. List<int> pageRange = new List<int>();
  1269. for (int i = 0; i < pages.Count; i++)
  1270. {
  1271. pageRange.Add(pages[i] + 1);
  1272. }
  1273. NotifyUIToRefresh(pageRange);
  1274. ReloadAfterOption(true, false, null);
  1275. if (!isFromUndoRedo)
  1276. {
  1277. PageEditHistory history = new PageEditHistory();
  1278. history.Type = ActionType.Reverse;
  1279. history.SourceTargetValue = sourceTarget;
  1280. history.viewmodel = this;
  1281. PDFViewer.UndoManager.AddHistory(history);
  1282. }
  1283. }
  1284. /// <summary>
  1285. /// 交换两个Item顺序
  1286. /// </summary>
  1287. /// <param name="preiIndex"></param>
  1288. /// <param name="laterIndex"></param>
  1289. /// <returns></returns>
  1290. private bool ExchangeItem(int preiIndex, int laterIndex)
  1291. {
  1292. if (preiIndex == laterIndex)
  1293. {
  1294. return true;
  1295. }
  1296. var sourceitem = PageEditItems[preiIndex];
  1297. var targetitem = PageEditItems[laterIndex];
  1298. var result = PDFViewer.Document.ExchangePage(preiIndex, laterIndex);
  1299. if (!result)
  1300. {
  1301. return false;
  1302. }
  1303. PageEditItems.Remove(sourceitem);
  1304. PageEditItems.Remove(targetitem);
  1305. PageEditItems.Insert(preiIndex, targetitem);
  1306. PageEditItems.Insert(laterIndex, sourceitem);
  1307. return true;
  1308. }
  1309. /// <summary>
  1310. /// 是否可以执行文件操作命令 除插入、拆分外
  1311. /// </summary>
  1312. /// <returns></returns>
  1313. private bool CanFileCommandExcute()
  1314. {
  1315. //增加是否有选中多页的判断
  1316. //修复问题:全选页面后,ctrl取消第一页的选中时,其余操作会因为ListSelectedIndex为-1被禁用的问题
  1317. if (IsMouseRightButtonDown)
  1318. {
  1319. if (RightSelectedIndex < 0 && !IsMultiSelected)
  1320. {
  1321. return false;
  1322. }
  1323. else
  1324. {
  1325. return true;
  1326. }
  1327. }
  1328. else
  1329. {
  1330. if (ListSelectedIndex < 0 && !IsMultiSelected)
  1331. {
  1332. return false;
  1333. }
  1334. else
  1335. {
  1336. return true;
  1337. }
  1338. }
  1339. }
  1340. private bool CanReverseFileCommandExcute()
  1341. {
  1342. if (IsMouseRightButtonDown)
  1343. {
  1344. if (RightSelectedIndex >= 0 && IsMultiSelected)
  1345. {
  1346. return true;
  1347. }
  1348. else
  1349. {
  1350. return false;
  1351. }
  1352. }
  1353. else
  1354. {
  1355. if (ListSelectedIndex >= 0 && IsMultiSelected)
  1356. {
  1357. return true;
  1358. }
  1359. else
  1360. {
  1361. return false;
  1362. }
  1363. }
  1364. }
  1365. /// <summary>
  1366. /// 校验是否可以替换
  1367. /// </summary>
  1368. /// <returns></returns>
  1369. private bool CanReplaceCommandExcute()
  1370. {
  1371. //return !IsMultiSelected;
  1372. return true;
  1373. }
  1374. /// <summary>
  1375. /// 放大页面
  1376. /// </summary>
  1377. private void ZoomInCommandEvent()
  1378. {
  1379. ZoomIndex++;
  1380. ChangeItemSize(ItemSizes[ZoomIndex + 1]);
  1381. NotifyUIToRefresh();
  1382. }
  1383. /// <summary>
  1384. /// 缩小页面
  1385. /// </summary>
  1386. private async void ZoomOutCommandEvent()
  1387. {
  1388. ZoomIndex--;
  1389. ChangeItemSize(ItemSizes[ZoomIndex + 1]);
  1390. //增加一定延时,计算页面显示范围时,用新的尺寸进行计算
  1391. await Task.Delay(3);
  1392. NotifyUIToRefresh();
  1393. }
  1394. /// <summary>
  1395. ///
  1396. /// </summary>
  1397. /// <param name="newsize"></param>
  1398. public void ChangeItemSize(Size newsize)
  1399. {
  1400. for (int i = 0; i < PageEditItems.Count; i++)
  1401. {
  1402. PageEditItems[i].ItemSize = newsize;
  1403. }
  1404. RefreshPageNum();
  1405. List<int> indexList = GetSelectedItemPageNumbers();
  1406. if (indexList.Count > 0)
  1407. {
  1408. //选中项集合 也需要 同步更新
  1409. selectedPageList.Clear();
  1410. selectedPageList.AddRange(indexList);
  1411. }
  1412. if (selectedPageList.Count > 0)
  1413. {
  1414. //缩略图模式 调整item大小的时候,更新选中项
  1415. //如果注释掉,调整BOTA宽度后,缩略图视图中不会显示选中项
  1416. if (selectedPageList.Count < 1)
  1417. {
  1418. NotifyUIToRefresh(new List<int>() { PDFViewer.CurrentIndex + 1 });
  1419. }
  1420. else
  1421. {
  1422. NotifyUIToRefresh(selectedPageList);
  1423. }
  1424. }
  1425. }
  1426. private bool CanZoomIn()
  1427. {
  1428. if (ZoomIndex >= 1)
  1429. {
  1430. return false;
  1431. }
  1432. else
  1433. {
  1434. return true;
  1435. }
  1436. }
  1437. private bool CanZoomOut()
  1438. {
  1439. if (ZoomIndex <= -1)
  1440. {
  1441. return false;
  1442. }
  1443. else
  1444. {
  1445. return true;
  1446. }
  1447. }
  1448. /// <summary>
  1449. /// 响应UI发送刷新图片的事件
  1450. /// </summary>
  1451. /// <param name="e"></param>
  1452. private void OnPageEditRefreshEvent(PageEditRefreshEventArgs e)
  1453. {
  1454. //UI发送事件过来通知刷新图片时
  1455. //调整逻辑,前后预加载5页
  1456. RefreshItemImage(e.PageRange.Item1 - 2, e.PageRange.Item2 + 2);
  1457. }
  1458. /// <summary>
  1459. /// 选项改变事件
  1460. /// 虚拟化的时候不会触发选中事件
  1461. /// </summary>
  1462. /// <param name="e"></param>
  1463. private void SelectionChangedEvent(object e)
  1464. {
  1465. if (IsSelectFormSelf)
  1466. {
  1467. //避免频繁的触发该事件
  1468. return;
  1469. }
  1470. var args = e as SelectionChangedEventArgs;
  1471. //下下策 从UI控件获取所有选项的集合
  1472. var listbox = args.OriginalSource as ListBox;
  1473. if (args != null)
  1474. {
  1475. selectedPageList = new List<int>();
  1476. for (int i = 0; i < listbox.SelectedItems.Count; i++)
  1477. {
  1478. if (!selectedPageList.Contains((listbox.SelectedItems[i] as PageEditItem).PageNumber))
  1479. {
  1480. selectedPageList.Add((listbox.SelectedItems[i] as PageEditItem).PageNumber);
  1481. }
  1482. }
  1483. ////根据选中的范围 适配全部页面范围、奇数页面范围等
  1484. //if (pagelist.Count == PageEditItems.Count)
  1485. //{
  1486. // ComboxSelectIndex = 0;
  1487. // return;
  1488. //}
  1489. //只选中单个页面是,listselectedIndex 更准确
  1490. if (selectedPageList.Count == 1)
  1491. {
  1492. TbPageRange = (listSelecedIndex + 1).ToString();
  1493. //缩放 平移移动页面,页面大小出现不全的问题
  1494. listbox.ScrollIntoView(listbox.SelectedItems[0]);
  1495. //需要刷新 startRefreshIndex 、endRefreshedIndex
  1496. //这个方法 会有串联地方,然后不起作用
  1497. //NotifyUIToRefresh();
  1498. //保险起见,暂时只能用这个方法
  1499. if (listbox != null)
  1500. {
  1501. var itemSize = new Size((listbox.Items[0] as PageEditItem).ItemSize.Width + 32, (listbox.Items[0] as PageEditItem).ItemSize.Height + 30);
  1502. Tuple<int, int, int> range = (Tuple<int, int, int>)GetRoughFromView(listbox, itemSize, new Thickness(5, 10, 5, 10));
  1503. //RefreshItemImage(range.Item1, range.Item2);
  1504. startRefreshedIndex = range.Item1;
  1505. endRefreshedIndex = range.Item2;
  1506. }
  1507. }
  1508. else
  1509. {
  1510. //根据选中的页面转换成1,3-10类似的字符串
  1511. string pageparm = CommonHelper.GetPageParmFromList(selectedPageList);
  1512. TbPageRange = pageparm;
  1513. }
  1514. }
  1515. //Ctrl+A时让所有item选中
  1516. var isCtrlorShiftDown = ((Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.A)) || (Keyboard.IsKeyDown(Key.RightCtrl) && Keyboard.IsKeyDown(Key.A)));
  1517. if (isCtrlorShiftDown)
  1518. {
  1519. for (int i = 0; i < PageEditItems.Count; i++)
  1520. {
  1521. PageEditItems[i].Selected = true;
  1522. }
  1523. }
  1524. //判断是否有选中多项
  1525. if (listbox.SelectedItems.Count > 1)
  1526. {
  1527. IsMultiSelected = true;
  1528. maxSelectedIndex = listbox.Items.IndexOf(listbox.SelectedItems[listbox.SelectedItems.Count - 1]);
  1529. }
  1530. else
  1531. {
  1532. IsMultiSelected = false;
  1533. //非多选的时候需要重置多选页码的标志符
  1534. //否则会有多选页面复制后,粘贴位置不准确的问题
  1535. maxSelectedIndex = -1;
  1536. }
  1537. }
  1538. private object GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
  1539. {
  1540. var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
  1541. if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
  1542. return new Tuple<int, int, int>(0, 0, 0);
  1543. try
  1544. {
  1545. var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
  1546. var currentWidth = scrollViewer.ActualWidth;
  1547. //计算当前窗口大小能显示的行数和列数
  1548. var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
  1549. var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
  1550. var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
  1551. preItemCount = preItemCount * columnCount;
  1552. var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
  1553. preEnd = preEnd * columnCount + columnCount - 1;
  1554. var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
  1555. return new Tuple<int, int, int>(
  1556. Math.Max(preItemCount, 0),
  1557. Math.Min(view.Items.Count, preEnd),
  1558. middle);
  1559. }
  1560. catch { }
  1561. return new Tuple<int, int, int>(0, 0, 0);
  1562. }
  1563. /// <summary>
  1564. /// 限制文本框输入内容
  1565. /// </summary>
  1566. /// <param name="e"></param>
  1567. private void PreviewKeyDownEvent(object e)
  1568. {
  1569. var args = e as KeyEventArgs;
  1570. if (args == null)
  1571. {
  1572. return;
  1573. }
  1574. ////枚举 过滤所有不合理的输入
  1575. //if (args.Key != Key.Enter && args.Key != Key.Back && args.Key != Key.OemComma && args.Key != Key.OemMinus && args.Key != Key.Subtract && !((args.Key >= Key.D0 && args.Key <= Key.D9) || (args.Key >= Key.NumPad0 && args.Key <= Key.NumPad9)))
  1576. //{
  1577. // args.Handled = true;
  1578. //}
  1579. if (args.Key == Key.Enter)
  1580. {
  1581. if (PageRangeItems.Contains((args.OriginalSource as TextBox).Text))
  1582. {
  1583. return;
  1584. }
  1585. List<int> TargetPageIndexs = new List<int>();
  1586. var result = CommonHelper.GetPagesInRange(ref TargetPageIndexs, (args.OriginalSource as TextBox).Text, PDFViewer.Document.PageCount, new char[] { ',', ',' }, new char[] { '-' });
  1587. if (!result)
  1588. {
  1589. ListSelectedIndex = -1;
  1590. TbPageRange = string.Empty;
  1591. AlertsMessage alertsMessage = new AlertsMessage();
  1592. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  1593. return;
  1594. }
  1595. //选中输入的页面范围
  1596. List<int> pageRange = new List<int>();
  1597. for (int i = 0; i < PageEditItems.Count; i++)
  1598. {
  1599. if (TargetPageIndexs.Contains(PageEditItems[i].PageNumber - 1))
  1600. {
  1601. pageRange.Add(i + 1);
  1602. }
  1603. }
  1604. NotifyUIToRefresh(pageRange);
  1605. }
  1606. }
  1607. /// <summary>
  1608. /// 失去焦点后选中页面
  1609. /// </summary>
  1610. /// <param name="obj"></param>
  1611. public void lostfocus(object obj)
  1612. {
  1613. var args = obj as RoutedEventArgs;
  1614. if (args == null || (args.OriginalSource as TextBox) == null)
  1615. {
  1616. return;
  1617. }
  1618. List<int> TargetPageIndexs = new List<int>();
  1619. if (PageRangeItems.Contains((args.OriginalSource as TextBox).Text) && ComboxSelectIndex != 5)
  1620. {
  1621. //选中其他项,失去焦点时不报错
  1622. return;
  1623. }
  1624. var result = CommonHelper.GetPagesInRange(ref TargetPageIndexs, (args.OriginalSource as TextBox).Text, PDFViewer.Document.PageCount, new char[] { ',', ',' }, new char[] { '-' });
  1625. if (!result)
  1626. {
  1627. ListSelectedIndex = -1;
  1628. TbPageRange = string.Empty;
  1629. AlertsMessage alertsMessage = new AlertsMessage();
  1630. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  1631. return;
  1632. }
  1633. //选中输入的页面范围
  1634. List<int> pageRange = new List<int>();
  1635. for (int i = 0; i < PageEditItems.Count; i++)
  1636. {
  1637. if (TargetPageIndexs.Contains(PageEditItems[i].PageNumber - 1))
  1638. {
  1639. pageRange.Add(i + 1);
  1640. }
  1641. }
  1642. NotifyUIToRefresh(pageRange);
  1643. }
  1644. #endregion 事件
  1645. #region 方法
  1646. /// <summary>
  1647. /// Esc退出多选
  1648. /// </summary>
  1649. private void clearSelected()
  1650. {
  1651. //缩略图不支持esc取消选中;
  1652. if (IsBOTAThumb == Visibility.Visible)
  1653. {
  1654. return;
  1655. }
  1656. else
  1657. {
  1658. ListSelectedIndex = -1;
  1659. }
  1660. }
  1661. /// <summary>
  1662. /// 调用打印机
  1663. /// </summary>
  1664. public async void print(List<int> SelectedPages)
  1665. {
  1666. //保存并继续,使用了高级功能则弹窗权益弹窗
  1667. if (!viewContentViewModel.saveFile(true))
  1668. {
  1669. return;
  1670. }
  1671. if (ListSelectedIndex != -1)
  1672. {
  1673. await App.Current.Dispatcher.InvokeAsync(() =>
  1674. {
  1675. PDFViewer.GoToPage(ListSelectedIndex);
  1676. });
  1677. }
  1678. DialogParameters printValue = new DialogParameters();
  1679. printValue.Add(ParameterNames.PDFViewer, PDFViewer);
  1680. printValue.Add(ParameterNames.FilePath, PDFViewer.Document.FileName);
  1681. printValue.Add(ParameterNames.PrintCurrentPage, PDFViewer.CurrentIndex);
  1682. printValue.Add(ParameterNames.PageList, SelectedPages);
  1683. dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printValue, e => { });
  1684. }
  1685. /// <summary>
  1686. /// 剪切
  1687. /// </summary>
  1688. private void cut()
  1689. {
  1690. //没有权限时,拦截剪切操作
  1691. if (!viewContentViewModel.CheckPermissionBeforeOption())
  1692. {
  1693. return;
  1694. }
  1695. CacheFilePath.Instance.CopyDoc = null;
  1696. CacheFilePath.Instance.CopyDoc = CPDFDocument.CreateDocument();
  1697. List<int> pages = new List<int>();
  1698. for (int i = 0; i < PageEditItems.Count; i++)
  1699. {
  1700. if (PageEditItems[i].Selected)
  1701. {
  1702. pages.Add(i + 1);
  1703. }
  1704. }
  1705. bool result = CacheFilePath.Instance.CopyDoc.ImportPages(PDFViewer.Document, String.Join(",", pages));
  1706. if (!result)
  1707. {
  1708. ShowToast();
  1709. return;
  1710. }
  1711. List<int> pageitems = new List<int>();
  1712. if (IsMouseRightButtonDown)
  1713. {
  1714. if (IsMultiSelected)
  1715. {
  1716. pageitems.AddRange(RightSelectedIndexs);
  1717. }
  1718. else
  1719. {
  1720. pageitems.Add(RightSelectedIndex);
  1721. }
  1722. DoRemoveSelectedPages(pageitems);
  1723. }
  1724. else
  1725. {
  1726. DoRemoveSelectedPages();
  1727. }
  1728. ReloadAfterOption(true, true, Tuple.Create(0, PDFViewer.Document.PageCount));
  1729. }
  1730. /// <summary>
  1731. /// 复制
  1732. /// </summary>
  1733. private void copy()
  1734. {
  1735. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_PageEdit, DataTrackingHelper.EntryPathKeyType.SubTbr_PageEdit);
  1736. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.Copy);
  1737. DataTrackingHelper.IsClearEntryPath = true;
  1738. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_PageEdit, "SubTbr_Btn", "Btn_SubTbr_Copy");
  1739. if (!ServiceHelper.IAPBeforeFunction())
  1740. {
  1741. return;
  1742. }
  1743. if (viewContentViewModel.CheckPermissionBeforeOption())
  1744. {
  1745. int pageIndex = 0;
  1746. if (IsMouseRightButtonDown)
  1747. {
  1748. pageIndex = RightSelectedIndex;
  1749. }
  1750. else
  1751. {
  1752. pageIndex = ListSelectedIndex;
  1753. }
  1754. CacheFilePath.Instance.CopyDoc?.Release();
  1755. CacheFilePath.Instance.CopyDoc = CPDFDocument.CreateDocument();
  1756. List<int> pages = new List<int>();
  1757. if (isMultiSelected)
  1758. {
  1759. //确认是多选了页面时,通过页面的选中状态获取选中页面
  1760. for (int i = 0; i < PageEditItems.Count; i++)
  1761. {
  1762. if (PageEditItems[i].Selected)
  1763. {
  1764. pages.Add(i + 1);
  1765. }
  1766. }
  1767. }
  1768. else
  1769. {
  1770. //没有多选时,根据listbox的选中索引获取选中页面
  1771. pages.Add(pageIndex + 1);
  1772. }
  1773. bool result = CacheFilePath.Instance.CopyDoc.ImportPages(PDFViewer.Document, String.Join(",", pages));
  1774. if (!result)
  1775. {
  1776. ShowToast();
  1777. return;
  1778. }
  1779. }
  1780. }
  1781. /// <summary>
  1782. /// 是否可以粘贴
  1783. /// </summary>
  1784. /// <returns></returns>
  1785. private bool CanPasteExcute()
  1786. {
  1787. return this.CanPaste;
  1788. }
  1789. /// <summary>
  1790. /// 粘贴
  1791. /// </summary>
  1792. private void Paste(int Index = -1)
  1793. {
  1794. if (!ServiceHelper.IAPBeforeFunction())
  1795. {
  1796. return;
  1797. }
  1798. int pageIndex = 0;
  1799. if (IsMouseRightButtonDown)
  1800. {
  1801. pageIndex = RightSelectedIndex;
  1802. }
  1803. else
  1804. {
  1805. pageIndex = ListSelectedIndex;
  1806. }
  1807. IsLoading = Visibility.Visible;
  1808. int insertindex = maxSelectedIndex > pageIndex ? maxSelectedIndex : pageIndex;
  1809. insertindex++;
  1810. if (Index >= 0)
  1811. {
  1812. insertindex = Index;
  1813. }
  1814. else if (pageIndex == -1)
  1815. {
  1816. insertindex = PageEditItems.Count;
  1817. }
  1818. if (CacheFilePath.Instance.CopyDoc == null)
  1819. {
  1820. return;
  1821. }
  1822. int pagecount = CacheFilePath.Instance.CopyDoc.PageCount;
  1823. bool result = PDFViewer.Document.ImportPagesAtIndex(CacheFilePath.Instance.CopyDoc, "1-" + pagecount, insertindex);
  1824. if (!result)
  1825. {
  1826. IsLoading = Visibility.Collapsed;
  1827. ShowToast();
  1828. return;
  1829. }
  1830. //UI层插入
  1831. for (int i = 0; i < pagecount; i++)
  1832. {
  1833. var item = GetNewPageEditItem(i);
  1834. PageEditItems.Insert(insertindex + i, item);
  1835. }
  1836. //加一点延时才能显示选中效果-2023.4.12,为解决卡顿问题,注释掉,注释掉未出现该选中问题
  1837. //await Task.Delay(200);
  1838. List<int> pageRange = new List<int>();
  1839. for (int i = 0; i < pagecount; i++)
  1840. {
  1841. pageRange.Add(insertindex + i + 1);
  1842. }
  1843. PageEditHistory history = new PageEditHistory();
  1844. history.InsertIndex = insertindex;
  1845. history.InsertPageCount = pagecount;
  1846. history.Type = ActionType.Paste;
  1847. history.viewmodel = this;
  1848. PDFViewer.UndoManager.AddHistory(history);
  1849. if (IsBOTAThumb == Visibility.Visible)
  1850. {
  1851. GoToPage(pageRange);
  1852. }
  1853. //刷新页码等
  1854. ReloadAfterOption(true, false, new Tuple<int, int>(0, PDFViewer.Document.PageCount));
  1855. //通知Xaml选中新增的页面,从VM设置选中项会有问题
  1856. NotifyUIToRefresh(pageRange);
  1857. IsLoading = Visibility.Collapsed;
  1858. IsAddPages = true;
  1859. }
  1860. public void GoToPage(List<int> pageRange)
  1861. {
  1862. int index = pageRange.Last() - 1;
  1863. //跳转页面 需要写在ReloadAfterOption()之前,不然跳转失败的概率大
  1864. //插入页面需要重新加载 ,不然会有bug(bug:选中最后 一页,粘贴,跳转失败)
  1865. PDFViewer.ReloadDocument();
  1866. //await Task.Delay(500);
  1867. PDFViewer.GoToPage(index);
  1868. }
  1869. /// <summary>
  1870. /// 页面编辑执行Undo操作的方法
  1871. /// </summary>
  1872. public async void Undo(PageEditHistory history)
  1873. {
  1874. switch (history.Type)
  1875. {
  1876. case ActionType.RightRotate:
  1877. var rightlist = history.SourceTargetValue.Keys.ToList<int>();
  1878. RotateSelectedPages(false, rightlist, true);
  1879. break;
  1880. case ActionType.LeftRotate:
  1881. var leftlist = history.SourceTargetValue.Keys.ToList<int>();
  1882. RotateSelectedPages(true, leftlist, true);
  1883. break;
  1884. case ActionType.InsertFromFile:
  1885. case ActionType.Paste:
  1886. List<int> pageList = new List<int>();
  1887. for (int i = 0; i < history.InsertPageCount; i++)
  1888. {
  1889. pageList.Add(history.InsertIndex + i);
  1890. }
  1891. int index = DoRemoveSelectedPages(pageList, true);
  1892. UndoPageOperations(index);
  1893. break;
  1894. case ActionType.InsertCustomPages:
  1895. index = DoRemoveSelectedPages(new List<int> { history.InsertIndex }, true);
  1896. UndoPageOperations(index);
  1897. break;
  1898. case ActionType.DragSort:
  1899. //将字典倒序后,将每个Source的值恢复到Key的位置
  1900. var SourceTargets = history.SourceTargetValue;
  1901. var keys = SourceTargets.Keys.ToArray();
  1902. Array.Reverse(keys);
  1903. foreach (var key in keys)
  1904. {
  1905. MoveItemToIndex(SourceTargets[key], key);
  1906. }
  1907. //刷新页码等
  1908. ReloadAfterOption(true, false, new Tuple<int, int>(0, PDFViewer.Document.PageCount));
  1909. List<int> selectedPages = new List<int>();
  1910. //将索引集合转换成页码集合
  1911. foreach (var key in SourceTargets.Keys)
  1912. {
  1913. selectedPages.Add(key + 1);
  1914. }
  1915. //通知UI选中Item
  1916. NotifyUIToRefresh(selectedPages);
  1917. break;
  1918. case ActionType.Reverse:
  1919. //将字典倒序后,将每个Source的值恢复到Key的位置
  1920. var SourceTarget = history.SourceTargetValue;
  1921. var pres = SourceTarget.Keys.ToArray();
  1922. foreach (var key in pres)
  1923. {
  1924. if (key != SourceTarget[key])
  1925. {
  1926. ExchangeItem(key, SourceTarget[key]);
  1927. }
  1928. }
  1929. await Task.Delay(20);
  1930. //刷新页码等
  1931. ReloadAfterOption(true, false, null);
  1932. List<int> Pages = new List<int>();
  1933. //将索引集合转换成页码集合
  1934. foreach (var key in SourceTarget.Keys)
  1935. {
  1936. Pages.Add(key + 1);
  1937. }
  1938. foreach (var key in SourceTarget.Values)
  1939. {
  1940. Pages.Add(key + 1);
  1941. }
  1942. //通知UI选中Item
  1943. NotifyUIToRefresh(Pages);
  1944. break;
  1945. default:
  1946. break;
  1947. }
  1948. }
  1949. private void UndoPageOperations(int index)
  1950. {
  1951. //【页面编辑】插入页面后,undo,页码更新
  1952. ReloadAfterOption(true, false, new Tuple<int, int>(0, PDFViewer.Document.PageCount));
  1953. //删除后 选中项
  1954. if (index != -1)
  1955. {
  1956. if (index <= PDFViewer.Document.PageCount)
  1957. {
  1958. index = index + 1;
  1959. }
  1960. }
  1961. else
  1962. {
  1963. index = 0;
  1964. }
  1965. //通知Xaml删除后 选中的页面,从VM设置选中项会有问题
  1966. NotifyUIToRefresh(new List<int>() { index });
  1967. }
  1968. /// <summary>
  1969. /// 页面编辑执行Redo操作的方法
  1970. /// </summary>
  1971. public async void Redo(PageEditHistory history)
  1972. {
  1973. switch (history.Type)
  1974. {
  1975. case ActionType.RightRotate:
  1976. var rightlist = history.SourceTargetValue.Keys.ToList<int>();
  1977. RotateSelectedPages(true, rightlist, true);
  1978. break;
  1979. case ActionType.LeftRotate:
  1980. var leftlist = history.SourceTargetValue.Keys.ToList<int>();
  1981. RotateSelectedPages(false, leftlist, true);
  1982. break;
  1983. case ActionType.InsertFromFile:
  1984. //将文档验证是否为空,是否解密,置前,若为空直接退出
  1985. bool IsLocked = GetDocumentIsDiscryptied(history.InsertFilePath, history.InsertFilePassWord, out CPDFDocument doc, out string password);
  1986. if (IsLocked == false)
  1987. {
  1988. return;
  1989. }
  1990. await InsertFromFile(history.InsertIndex, history.InsertFilePath, doc, history.InsertFilePassWord, true);
  1991. ReloadAfterOption(true, false, Tuple.Create(history.InsertIndex - 1, history.InsertIndex + history.InsertPageCount));
  1992. break;
  1993. case ActionType.InsertCustomPages:
  1994. InsertFromCustomPage(history.InsertIndex, history.PageSize.Width, history.PageSize.Height, history.InsertFilePath);
  1995. ListSelectedIndex = history.InsertIndex;
  1996. break;
  1997. case ActionType.DragSort:
  1998. DragToSort(history.InsertIndex, history.SourceTargetValue.Keys.ToList<int>(), true);
  1999. break;
  2000. case ActionType.Reverse:
  2001. var list = history.SourceTargetValue.Keys.ToList<int>().Union(history.SourceTargetValue.Values.ToList<int>()).ToList<int>();
  2002. DoReverse(list, true);
  2003. break;
  2004. case ActionType.Paste:
  2005. Paste(history.InsertIndex);
  2006. break;
  2007. default:
  2008. break;
  2009. }
  2010. }
  2011. /// <summary>
  2012. /// 退出页面编辑面试,回到阅读页
  2013. /// </summary>
  2014. public void BackToPDFViewer(int PageIndex)
  2015. {
  2016. PDFViewer.GoToPage(PageIndex);
  2017. viewContentViewModel.ExitToolMode();
  2018. }
  2019. /// <summary>
  2020. /// 显示Toast提示
  2021. /// </summary>
  2022. public void ShowToast(string content = "")
  2023. {
  2024. if (!string.IsNullOrEmpty(content))
  2025. {
  2026. ToastText = content;
  2027. }
  2028. else
  2029. {
  2030. ToastText = App.MainPageLoader.GetString("OpentonFail_Warning");
  2031. }
  2032. ShowTip = Visibility.Visible;
  2033. }
  2034. /// <summary>
  2035. /// 通知UI刷新页面范围
  2036. /// 或通过xaml来设置选中项
  2037. /// </summary>vb nmjvcx
  2038. private void NotifyUIToRefresh(List<int> pageRange = null)
  2039. {
  2040. if (pageRange == null || pageRange.Count <= 0)
  2041. {
  2042. //通知UI刷新页面范围
  2043. eventor.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
  2044. }
  2045. else
  2046. {
  2047. //通知UI刷新页面范围
  2048. eventor.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode, NotifyType.SelectItems, pageRange));
  2049. }
  2050. }
  2051. /// <summary>
  2052. /// 获取已选中的页面集合
  2053. /// </summary>
  2054. /// <returns></returns>
  2055. private List<int> getSelectedPage()
  2056. {
  2057. List<int> pages = new List<int>();
  2058. for (int i = 0; i < PageEditItems.Count; i++)
  2059. {
  2060. if (PageEditItems[i].Selected)
  2061. {
  2062. pages.Add(i);
  2063. }
  2064. }
  2065. return pages;
  2066. }
  2067. /// <summary>
  2068. /// 删除选中项的方法 不带Reload操作
  2069. /// 方便其他逻辑中调用
  2070. /// </summary>
  2071. private int DoRemoveSelectedPages(List<int> pageList = null, bool isFromUndoRedo = false, bool isReplace = false)
  2072. {
  2073. PageEditItem pageEditItem = null;
  2074. int index = -1;
  2075. if (!ServiceHelper.IAPBeforeFunction())
  2076. {
  2077. return index;
  2078. }
  2079. //删除后 的选中项
  2080. index = maxSelectedIndex;
  2081. List<int> indexList = new List<int>();
  2082. if (pageList == null || pageList.Count == 0)
  2083. {
  2084. for (int i = 0; i < PageEditItems.Count; i++)
  2085. {
  2086. if (PageEditItems[i].Selected)
  2087. {
  2088. indexList.Add(i);
  2089. }
  2090. }
  2091. //因为虚拟化 可能获取不到所有选中项的页码 需要结合选中项的集合判断一下
  2092. if (selectedPageList.Count > 0 && selectedPageList.Count > indexList.Count)
  2093. {
  2094. indexList = new List<int>();
  2095. for (int j = 0; j < selectedPageList.Count; j++)
  2096. {
  2097. indexList.Add(selectedPageList[j] - 1);
  2098. }
  2099. }
  2100. }
  2101. else
  2102. {
  2103. indexList = pageList;
  2104. }
  2105. if (indexList.Count > 0 && indexList.Count == PDFViewer.Document.PageCount)
  2106. {
  2107. // 全部替换 不显示 不能删除所有的弹窗
  2108. if (isReplace == false)
  2109. {
  2110. AlertsMessage alertsMessage = new AlertsMessage();
  2111. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageEdit_DeleteAllPage"), App.ServiceLoader.GetString("Text_ok"));
  2112. return -1;
  2113. }
  2114. }
  2115. bool isLast = false;
  2116. if (index == -1 && indexList.Count > 0)
  2117. {
  2118. if (isMultiSelected)
  2119. {
  2120. indexList.Sort();
  2121. index = indexList.First();
  2122. }
  2123. else
  2124. {
  2125. index = indexList.Last();
  2126. }
  2127. }
  2128. if (index == PageEditItems.Count)
  2129. {
  2130. pageEditItem = PageEditItems[index - 1];
  2131. }
  2132. else if (index < PageEditItems.Count)
  2133. {
  2134. if ((index + 1) >= PageEditItems.Count)
  2135. {
  2136. pageEditItem = PageEditItems[PageEditItems.Count - 1];
  2137. isLast = true;
  2138. }
  2139. else
  2140. {
  2141. pageEditItem = PageEditItems[index + 1];
  2142. }
  2143. }
  2144. var result = PDFViewer.Document.RemovePages(indexList.ToArray());
  2145. if (!result)
  2146. {
  2147. ShowToast();
  2148. return -1;
  2149. }
  2150. //文档层操作成功后再删除UI层
  2151. for (int i = indexList.Count - 1; i >= 0; i--)
  2152. {
  2153. PageEditItems.RemoveAt(indexList[i]);
  2154. }
  2155. if (!isFromUndoRedo)
  2156. {
  2157. ////清除页面编辑Undo,Redo栈
  2158. //UndoStack.Clear();
  2159. //RedoStack.Clear();
  2160. //this.CanUndo = false;
  2161. //this.CanRedo = false;
  2162. }
  2163. index = PageEditItems.IndexOf(pageEditItem);
  2164. if (index == -1 && indexList.Count > 0)
  2165. {
  2166. if (isLast)
  2167. {
  2168. index = PageEditItems.Count - 1;
  2169. }
  2170. else
  2171. {
  2172. index = indexList.Last();
  2173. }
  2174. }
  2175. return index;
  2176. }
  2177. /// <summary>
  2178. /// 提取选中页面的方法
  2179. /// </summary>
  2180. /// <param name="isEveryPageToFile">是否每页生成一个文件</param>
  2181. /// <param name="isDeleteAfterExtract">是否提取后删除页面</param>
  2182. private async void DoExtractPages(bool isEveryPageToFile, bool isDeleteAfterExtract)
  2183. {
  2184. System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
  2185. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  2186. {
  2187. if (string.IsNullOrEmpty(dialog.SelectedPath))
  2188. {
  2189. AlertsMessage alertsMessage = new AlertsMessage();
  2190. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("OperationFailWarning"), App.ServiceLoader.GetString("Text_ok"));//补充提示文案
  2191. return;
  2192. }
  2193. }
  2194. else
  2195. return;
  2196. string selectedfile = "";
  2197. IsLoading = Visibility.Visible;
  2198. await Task.Delay(3);
  2199. List<int> pagenums = new List<int>();
  2200. for (int i = 0; i < PageEditItems.Count; i++)
  2201. {
  2202. if (PageEditItems[i].Selected)
  2203. {
  2204. pagenums.Add(PageEditItems[i].PageNumber);
  2205. }
  2206. }
  2207. if (IsMouseRightButtonDown)
  2208. {
  2209. if (IsMultiSelected)
  2210. {
  2211. for (int i = 0; i < RightSelectedIndexs.Count; i++)
  2212. {
  2213. RightSelectedIndexs[i] = RightSelectedIndexs[i] + 1;
  2214. }
  2215. pagenums = RightSelectedIndexs;
  2216. }
  2217. else
  2218. {
  2219. pagenums = new List<int>() { RightSelectedIndex + 1 };
  2220. }
  2221. }
  2222. if (isEveryPageToFile)
  2223. {
  2224. //每页生成的单独的文件
  2225. for (int i = 0; i < pagenums.Count; i++)
  2226. {
  2227. string filename = PDFViewer.Document.FileName + " " + "pages " + pagenums[i] + ".pdf";
  2228. string path = System.IO.Path.Combine(dialog.SelectedPath, filename);
  2229. path = CommonHelper.CreateFilePath(path);
  2230. selectedfile = path;
  2231. CPDFDocument savedoc = CPDFDocument.CreateDocument();
  2232. bool result = savedoc.ImportPages(PDFViewer.Document, pagenums[i].ToString());
  2233. if (!result)
  2234. {
  2235. savedoc.Release();
  2236. continue;
  2237. }
  2238. result = savedoc.WriteToFilePath(path);
  2239. if (!result)
  2240. {
  2241. savedoc.Release();
  2242. continue;
  2243. }
  2244. savedoc.Release();//释放内存
  2245. }
  2246. }
  2247. else//全部生成一个文件
  2248. {
  2249. string filename = PDFViewer.Document.FileName + " " + "pages " + CommonHelper.GetPageParmFromList(pagenums) + ".pdf";
  2250. string path = System.IO.Path.Combine(dialog.SelectedPath, filename);
  2251. path = CommonHelper.CreateFilePath(path);
  2252. selectedfile = path;
  2253. CPDFDocument savedoc = CPDFDocument.CreateDocument();
  2254. string range = String.Join(",", pagenums);
  2255. bool result = savedoc.ImportPages(PDFViewer.Document, range);
  2256. if (!result)
  2257. {
  2258. ShowToast();
  2259. IsLoading = Visibility.Collapsed;
  2260. return;
  2261. }
  2262. result = savedoc.WriteToFilePath(path);
  2263. if (!result)
  2264. {
  2265. ShowToast();
  2266. }
  2267. savedoc.Release();//释放内存
  2268. }
  2269. if (isDeleteAfterExtract)
  2270. {
  2271. //先提取后删除,index和其他地方设置不一样
  2272. int index = DoRemoveSelectedPages() + 1;
  2273. if (index > PDFViewer.Document.PageCount)
  2274. {
  2275. index = PDFViewer.Document.PageCount;
  2276. }
  2277. ReloadAfterOption(true, false, Tuple.Create(0, PDFViewer.Document.PageCount));
  2278. //通知Xaml删除后 选中的页面,从VM设置选中项会有问题
  2279. NotifyUIToRefresh(new List<int>() { index });
  2280. }
  2281. CommonHelper.ShowFileBrowser(selectedfile);
  2282. IsLoading = Visibility.Collapsed;
  2283. }
  2284. private async void DoSplitPages(HomePageSplitDialogModel model)
  2285. {
  2286. try
  2287. {
  2288. var pdfViewer = PDFViewer;
  2289. System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
  2290. //dialog.Description = App.MainPageLoader.GetString("Main_OpenFolderNoteWarning"); ;
  2291. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  2292. {
  2293. if (string.IsNullOrEmpty(dialog.SelectedPath))
  2294. {
  2295. return;
  2296. }
  2297. }
  2298. else
  2299. return;
  2300. var data = model;
  2301. int PageCount = pdfViewer.Document.PageCount;
  2302. string selectedfile = "";
  2303. string directory = System.IO.Path.Combine(dialog.SelectedPath, pdfViewer.Document.FileName);
  2304. if (Directory.Exists(directory) == false)
  2305. {
  2306. Directory.CreateDirectory(directory);
  2307. }
  2308. App.mainWindowViewModel.ProcessCloseAction = PageSplitClear;
  2309. string filename = "";
  2310. int index = 0;
  2311. if (data.Mode == HomePageSplitDialogModel.SplitMode.AveragePages)
  2312. {
  2313. int perPages = Convert.ToInt32(data.GetModeCount);
  2314. int splitCount = (int)Math.Ceiling((double)PageCount / (double)perPages);
  2315. index = splitCount;
  2316. ChatGTPAIHelper.SetIntProcess(true, "Splitting...", index, System.Windows.Visibility.Visible);
  2317. //TODO:命名规则是否要调整
  2318. selectedfile = data.FileName + " " + "1.pdf";
  2319. for (int i = 0; i < splitCount; i++)
  2320. {
  2321. App.mainWindowViewModel.Value = i;
  2322. await Task.Delay(50); // 模拟长时间操作
  2323. if (App.mainWindowViewModel.ProgressTitle == "Canceling")
  2324. {
  2325. return;
  2326. }
  2327. int startNum = i * perPages + 1;
  2328. int endNum = Math.Min(PageCount, (i + 1) * perPages);
  2329. string range = startNum.ToString() + "-" + endNum.ToString();
  2330. CPDFDocument savedoc = CPDFDocument.CreateDocument();
  2331. bool result = savedoc.ImportPages(pdfViewer.Document, range);
  2332. if (!result)
  2333. {
  2334. savedoc.Release();
  2335. continue;
  2336. }
  2337. filename = GetFileName(data, i);
  2338. string path = System.IO.Path.Combine(directory, filename);
  2339. path = CommonHelper.CreateFilePath(path);
  2340. result = savedoc.WriteToFilePath(path);
  2341. if (!result)
  2342. {
  2343. savedoc.Release();
  2344. continue;
  2345. }
  2346. savedoc.Release();
  2347. selectedfile = path;
  2348. }
  2349. }
  2350. else if (data.Mode == HomePageSplitDialogModel.SplitMode.AverageFiles)
  2351. {
  2352. int docCount = Convert.ToInt32(data.GetModeCount);
  2353. int avePages = (int)Math.Floor((double)PageCount / (double)docCount);
  2354. index = avePages;
  2355. ChatGTPAIHelper.SetIntProcess(true, "Splitting...", index, System.Windows.Visibility.Visible);
  2356. int remain = PageCount % docCount;
  2357. int startNum = 1;
  2358. //TODO:命名规则是否要调整
  2359. selectedfile = data.FileName + " " + "1.pdf";
  2360. for (int i = 0; i < docCount; i++)
  2361. {
  2362. App.mainWindowViewModel.Value = i;
  2363. await Task.Delay(50); // 模拟长时间操作
  2364. if (App.mainWindowViewModel.ProgressTitle == "Canceling")
  2365. {
  2366. return;
  2367. }
  2368. int endNum = startNum + (avePages - 1);
  2369. if (i < remain) endNum++;
  2370. endNum = Math.Min(endNum, PageCount);
  2371. string range = startNum + "-" + endNum;
  2372. startNum = endNum + 1;
  2373. filename = GetFileName(data, i);
  2374. string path = System.IO.Path.Combine(directory, filename);
  2375. path = CommonHelper.CreateFilePath(path);
  2376. CPDFDocument savedoc = CPDFDocument.CreateDocument();
  2377. bool result = savedoc.ImportPages(pdfViewer.Document, range);
  2378. if (!result)
  2379. {
  2380. savedoc.Release();
  2381. continue;
  2382. }
  2383. result = savedoc.WriteToFilePath(path);
  2384. if (!result)
  2385. {
  2386. savedoc.Release();
  2387. continue;
  2388. }
  2389. savedoc.Release();
  2390. selectedfile = path;
  2391. }
  2392. }
  2393. else//自定义页码范围
  2394. {
  2395. string pageRange = "";
  2396. List<int> pageList1 = new List<int>();
  2397. if (data.PageMode == HomePageSplitDialogModel.PageRangeMode.OddPage)
  2398. {
  2399. int[] page = new int[(PageCount + 1) / 2];
  2400. index = page.Length;
  2401. ChatGTPAIHelper.SetIntProcess(true, "Splitting...", index, System.Windows.Visibility.Visible);
  2402. for (int i = 0; i < page.Length; i++)
  2403. {
  2404. App.mainWindowViewModel.Value = i;
  2405. await Task.Delay(50); // 模拟长时间操作
  2406. if (App.mainWindowViewModel.ProgressTitle == "Canceling")
  2407. {
  2408. return;
  2409. }
  2410. page[i] = i * 2 + 1;
  2411. }
  2412. pageRange = string.Join(",", page);
  2413. }
  2414. else if (data.PageMode == HomePageSplitDialogModel.PageRangeMode.EvenPage)
  2415. {
  2416. int[] page = new int[PageCount / 2];
  2417. index = page.Length;
  2418. ChatGTPAIHelper.SetIntProcess(true, "Splitting...", index, System.Windows.Visibility.Visible);
  2419. for (int i = 0; i < page.Length; i++)
  2420. {
  2421. App.mainWindowViewModel.Value = i;
  2422. await Task.Delay(50); // 模拟长时间操作
  2423. if (App.mainWindowViewModel.ProgressTitle == "Canceling")
  2424. {
  2425. return;
  2426. }
  2427. page[i] = i * 2 + 2;
  2428. }
  2429. pageRange = string.Join(",", page);
  2430. }
  2431. else if (data.PageMode == HomePageSplitDialogModel.PageRangeMode.CustomPage)
  2432. {
  2433. ChatGTPAIHelper.SetIntProcess(true, "Splitting...", PageCount, System.Windows.Visibility.Visible);
  2434. pageRange = data.PageRange;
  2435. }
  2436. //如果是自定义输入页面范围的话,直接解析
  2437. CommonHelper.GetPagesInRange(ref pageList1, pageRange, PageCount, new char[] { ',' }, new char[] { '-' });
  2438. index = pageList1.Count;
  2439. #region //part1
  2440. selectedfile = "";
  2441. CPDFDocument saveDoc1 = CPDFDocument.CreateDocument();
  2442. //string filepath = data.FileName + " " + "1.pdf";
  2443. filename = GetFileName(data, 0);
  2444. string path1 = Path.Combine(directory, filename);
  2445. path1 = CommonHelper.CreateFilePath(path1);
  2446. bool result = saveDoc1.ImportPages(pdfViewer.Document, pageRange);
  2447. if (!result)
  2448. {
  2449. saveDoc1.Release();
  2450. ShowToast();
  2451. ChatGTPAIHelper.clossProcess(index);
  2452. return;
  2453. }
  2454. result = saveDoc1.WriteToFilePath(path1);
  2455. if (!result)
  2456. {
  2457. saveDoc1.Release();
  2458. ShowToast();
  2459. ChatGTPAIHelper.clossProcess(index);
  2460. return;
  2461. }
  2462. saveDoc1.Release();
  2463. selectedfile = path1;
  2464. #endregion //part1
  2465. #region 方案二 //Part2 除了选定之外的页面
  2466. List<int> pageList2 = new List<int>();
  2467. for (int i = 1; i <= pdfViewer.Document.PageCount; i++)
  2468. {
  2469. if (!pageList1.Contains(i - 1))//pagelist1 存放的是index
  2470. pageList2.Add(i);
  2471. }
  2472. string pageRange2 = string.Join(",", pageList2);
  2473. CPDFDocument saveDoc2 = CPDFDocument.CreateDocument();
  2474. string filepath2 = /*data.FileName + " " + "2.pdf";*/ GetFileName(data, 1);
  2475. string path2 = /*Path.Combine(dialog.SelectedPath, filepath2);*/Path.Combine(directory, filepath2);
  2476. path2 = CommonHelper.CreateFilePath(path2);
  2477. bool result1 = saveDoc2.ImportPages(pdfViewer.Document, pageRange2);
  2478. if (!result1)
  2479. {
  2480. saveDoc2.Release();
  2481. ShowToast();
  2482. return;
  2483. }
  2484. result1 = saveDoc2.WriteToFilePath(path2);
  2485. if (!result1)
  2486. {
  2487. saveDoc2.Release();
  2488. ShowToast();
  2489. return;
  2490. }
  2491. saveDoc2.Release();
  2492. //selectedfile = path2;
  2493. }
  2494. #endregion 方案二 //Part2 除了选定之外的页面
  2495. //显示文件夹,并选中一个文件
  2496. Process.Start("explorer", "/select,\"" + selectedfile + "\"");
  2497. ChatGTPAIHelper.clossProcess(index);
  2498. }
  2499. catch
  2500. {
  2501. }
  2502. }
  2503. private void PageSplitClear()
  2504. {
  2505. App.mainWindowViewModel.ProgressTitle = "Canceling";
  2506. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  2507. }
  2508. private string GetFileName(HomePageSplitDialogModel model, int index)
  2509. {
  2510. string filename = "";
  2511. string label = model.HasLabel ? model.FileNameLabel : "";
  2512. string deimiter = model.HasDeimiter ? model.FileNameDeimiter : "";
  2513. if (model.IsSourceNameFront)
  2514. {
  2515. filename = model.SourceFileName + deimiter + label + (index + 1) + ".pdf";
  2516. }
  2517. else
  2518. {
  2519. filename = label + (index + 1) + deimiter + model.SourceFileName + ".pdf";
  2520. }
  2521. return filename;
  2522. }
  2523. /// <summary>
  2524. /// 将文件插入到指定位置
  2525. /// </summary>
  2526. /// <param name="insertIndex"></param>
  2527. public async Task<bool> InsertFromFile(int insertIndex, string filePath, CPDFDocument doc = null, string oldPassWord = null, bool isFromUndoRedo = false)
  2528. {
  2529. if (doc == null)
  2530. {
  2531. return false;
  2532. }
  2533. int pageCount = doc.PageCount;
  2534. //插入整个新文档
  2535. //文档层插入
  2536. var insertresult = PDFViewer.Document.ImportPagesAtIndex(doc, "1-" + pageCount, insertIndex);
  2537. if (!insertresult)
  2538. {
  2539. ShowToast();
  2540. return false;
  2541. }
  2542. //插入成功后释放临时Document对象
  2543. doc.Release();
  2544. //UI层插入
  2545. for (int i = 0; i < pageCount; i++)
  2546. {
  2547. var item = GetNewPageEditItem(i);
  2548. PageEditItems.Insert(insertIndex + i, item);
  2549. }
  2550. //加一点延时才能显示选中效果
  2551. await Task.Delay(200);
  2552. List<int> pageRange = new List<int>();
  2553. for (int i = 0; i < pageCount; i++)
  2554. {
  2555. pageRange.Add(insertIndex + i + 1);
  2556. }
  2557. if (IsBOTAThumb == Visibility.Visible)
  2558. {
  2559. GoToPage(pageRange);
  2560. }
  2561. NotifyUIToRefresh(pageRange);
  2562. //如果是undo redo里调用本方法,则不需要添加到Undo栈
  2563. if (!isFromUndoRedo)
  2564. {
  2565. //添加记录到Undo栈,清空Redo栈
  2566. PageEditHistory history = new PageEditHistory();
  2567. history.Type = ActionType.InsertFromFile;
  2568. history.InsertIndex = insertIndex;
  2569. history.InsertFilePath = filePath;
  2570. history.InsertFilePassWord = oldPassWord;
  2571. history.InsertPageCount = pageCount;
  2572. history.viewmodel = this;
  2573. PDFViewer.UndoManager.AddHistory(history);
  2574. }
  2575. return true;
  2576. }
  2577. public bool GetDocumentIsDiscryptied(string filePath, string oldPassWord, out CPDFDocument doc, out string password)
  2578. {
  2579. doc = CPDFDocument.InitWithFilePath(filePath);
  2580. password = "";
  2581. if (doc == null)
  2582. {
  2583. //文件损坏
  2584. AlertsMessage alertsMessage = new AlertsMessage();
  2585. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Main_OpenFileFailedWarning"), App.ServiceLoader.GetString("Text_ok"));
  2586. return false;
  2587. }
  2588. if (doc.IsLocked)
  2589. {
  2590. if (!string.IsNullOrEmpty(oldPassWord))
  2591. {
  2592. var result = doc.UnlockWithPassword(oldPassWord);
  2593. if (result)
  2594. {
  2595. password = oldPassWord;
  2596. }
  2597. }
  2598. //没有传入密码,或者用传入密码解密失败是,需要弹出界面弹窗
  2599. if (doc.IsLocked)
  2600. {
  2601. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(doc, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  2602. if (result.IsDiscryptied)
  2603. {
  2604. if (result.Password != null)
  2605. {
  2606. string filePath1 = doc.FilePath;
  2607. if (doc.IsLocked)
  2608. {
  2609. doc.UnlockWithPassword(result.Password);
  2610. }
  2611. else
  2612. {
  2613. doc.CheckOwnerPassword(result.Password);
  2614. }
  2615. password = result.Password;
  2616. }
  2617. }
  2618. else
  2619. {
  2620. return false;
  2621. }
  2622. }
  2623. }
  2624. return true;
  2625. }
  2626. /// <summary>
  2627. /// 插入自定义页面
  2628. /// </summary>
  2629. /// <param name="insertIndex"></param>
  2630. /// <param name="width"></param>
  2631. /// <param name="height"></param>
  2632. /// <param name="filePath"></param>
  2633. /// <returns></returns>
  2634. private bool InsertFromCustomPage(int insertIndex, double width, double height, string filePath)
  2635. {
  2636. try
  2637. {
  2638. bool result = false;
  2639. //文档层插入
  2640. if (!string.IsNullOrEmpty(filePath))
  2641. {
  2642. BitmapFrame frame = BitmapFrame.Create(new Uri(filePath));
  2643. byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  2644. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  2645. result = PDFViewer.Document.InsertPage(insertIndex, frame.Width, frame.Height, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
  2646. }
  2647. else
  2648. {
  2649. result = PDFViewer.Document.InsertPage(insertIndex, width, height, null);
  2650. }
  2651. if (!result)
  2652. {
  2653. ShowToast();
  2654. return false;
  2655. }
  2656. //UI层插入Item ,需要处理一下Item
  2657. var item = GetNewPageEditItem(insertIndex);
  2658. PageEditItems.Insert(insertIndex, item);
  2659. ReloadAfterOption(true, false, Tuple.Create(insertIndex - 1, insertIndex + 1), true);
  2660. //选中插入的页面
  2661. ListSelectedIndex = insertIndex;
  2662. return true;
  2663. }
  2664. catch
  2665. { return false; }
  2666. }
  2667. /// <summary>
  2668. /// 根据索引创建新的Item
  2669. /// </summary>
  2670. /// <param name="index"></param>
  2671. /// <returns></returns>
  2672. private PageEditItem GetNewPageEditItem(int index)
  2673. {
  2674. PageEditItem item = new PageEditItem();
  2675. if (PDFViewer.Document.PageAtIndex(index).Rotation % 2 != 0)
  2676. {
  2677. ///标记为横向页面
  2678. item.isVertical = false;
  2679. }
  2680. item.PageNumber = index + 1;
  2681. var pagesize = PDFViewer.Document.GetPageSize(index);
  2682. item.Size = pagesize;
  2683. //修复 缩放后粘贴的页面大小不一致的问题
  2684. item.ItemSize = ItemSizes[ZoomIndex + 1];
  2685. return item;
  2686. }
  2687. /// <summary>
  2688. /// 进行文件操作后刷新PDFView
  2689. /// 清除undo 栈
  2690. /// 重加载Document
  2691. /// 设置保存状态
  2692. /// </summary>
  2693. /// <param name="needRefreshPageNum">是否需要刷新页码</param>
  2694. /// <param name="needClearUndoHistory">是否需要清空注释Undo栈</param>
  2695. /// <param name="pageRange">需要刷新的页面范围图片</param>
  2696. public void ReloadAfterOption(bool needRefreshPageNum = false, bool needClearUndoHistory = false, Tuple<int, int> pageRange = null, bool isScrollIntoView = false)
  2697. {
  2698. if (needRefreshPageNum)
  2699. {
  2700. RefreshPageNum();
  2701. //从页面编辑退出的值,根据内容走,进页面编辑是这一页内容,退出来还是
  2702. if (CurrentPageEditItem != null && IsBOTAThumb != Visibility.Visible)
  2703. {
  2704. int pageIndex = CurrentPageEditItem.PageNumber - 1;
  2705. if (pageIndex != PDFViewer.CurrentIndex)
  2706. {
  2707. PDFViewer.GoToPage(pageIndex);
  2708. }
  2709. }
  2710. //插入、替换等更新页码,页码输入框的值未更新
  2711. RefreshTbPageRange();
  2712. }
  2713. if (needClearUndoHistory)
  2714. {
  2715. //只有删除操作清空undo栈
  2716. //可能会引起注释模块联动问题 需要留意
  2717. PDFViewer.UndoManager.ClearHistory();
  2718. }
  2719. if (pageRange != null)
  2720. {
  2721. if (pageRange.Item2 - pageRange.Item1 > 30)
  2722. {
  2723. if (isScrollIntoView)
  2724. {
  2725. //插入后 滚动到 选中的页面
  2726. List<int> pageRanges = new List<int>() { pageRange.Item2 };
  2727. NotifyUIToRefresh(pageRanges);
  2728. }
  2729. else
  2730. {
  2731. //范围较大时,通知UI获取更准确的刷新范围
  2732. NotifyUIToRefresh();
  2733. }
  2734. }
  2735. else
  2736. {
  2737. RefreshItemImage(pageRange.Item1, pageRange.Item2);
  2738. if (isScrollIntoView)
  2739. {
  2740. if (ListSelectedIndex == -1)
  2741. {
  2742. //插入后 滚动到 选中的页面
  2743. List<int> pageRanges = new List<int>() { pageRange.Item2 };
  2744. NotifyUIToRefresh(pageRanges);
  2745. }
  2746. }
  2747. }
  2748. }
  2749. PDFViewer.UndoManager.CanSave = true;
  2750. PDFViewer.ReloadDocument();
  2751. //解决:阅读页放大到只能看到最后一页时,从缩略图删除最后一页,阅读页面没有联动刷新
  2752. if (PDFViewer.GetVerticalOffset() >= PDFViewer.GetExtendHeight())
  2753. {
  2754. PDFViewer.ScrollToVerticalOffset(PDFViewer.GetExtendHeight());
  2755. }
  2756. }
  2757. /// <summary>
  2758. /// 重新获取选中页码
  2759. /// /插入、替换等更新页码,页码输入框的值未更新
  2760. /// </summary>
  2761. private void RefreshTbPageRange()
  2762. {
  2763. List<int> indexList = GetSelectedItemPageNumbers();
  2764. if (indexList.Count > 0)
  2765. {
  2766. //选中项集合 也需要 同步更新
  2767. selectedPageList.Clear();
  2768. selectedPageList.AddRange(indexList);
  2769. //只选中单个页面是,listselectedIndex 更准确
  2770. if (indexList.Count == 1)
  2771. {
  2772. TbPageRange = (listSelecedIndex + 1).ToString();
  2773. }
  2774. else
  2775. {
  2776. //根据选中的页面转换成1,3-10类似的字符串
  2777. string pageparm = CommonHelper.GetPageParmFromList(indexList);
  2778. TbPageRange = pageparm;
  2779. }
  2780. }
  2781. }
  2782. private List<int> GetSelectedItemPageNumbers()
  2783. {
  2784. List<int> indexList = new List<int>();
  2785. for (int i = 0; i < PageEditItems.Count; i++)
  2786. {
  2787. if (PageEditItems[i].Selected)
  2788. {
  2789. indexList.Add(PageEditItems[i].PageNumber);
  2790. }
  2791. }
  2792. return indexList;
  2793. }
  2794. /// <summary>
  2795. /// 旋转所有选中页面
  2796. /// </summary>
  2797. /// <param name="rightRotate"></param>
  2798. private async void RotateSelectedPages(bool rightRotate = true, List<int> pageitems = null, bool isFromUndoRedo = false)
  2799. {
  2800. if (!ServiceHelper.IAPBeforeFunction())
  2801. {
  2802. return;
  2803. }
  2804. IsLoading = Visibility.Visible;
  2805. await Task.Delay(2);
  2806. Dictionary<int, int> items = new Dictionary<int, int>();
  2807. if (pageitems == null)
  2808. {
  2809. //旋转选中页面
  2810. items = SetRotateSelectedPagesEvent(rightRotate);
  2811. }
  2812. else
  2813. {
  2814. if (pageitems.Count == 0)
  2815. {
  2816. items = SetRotateSelectedPagesEvent(rightRotate);
  2817. }
  2818. else
  2819. {
  2820. //旋转对应Items集合
  2821. for (int i = 0; i < pageitems.Count; i++)
  2822. {
  2823. PDFViewer.RotatePage(ComPDFKitViewer.PageRotate.Rotate90, rightRotate, pageitems[i]);
  2824. items.Add(pageitems[i], pageitems[i]);
  2825. //重新拿图
  2826. if (PageEditItems.Count > pageitems[i])
  2827. {
  2828. PageEditItems[pageitems[i]].IsGetImage = false;
  2829. }
  2830. }
  2831. }
  2832. }
  2833. //旋转页面后取消选中
  2834. PDFViewer.ClearSelectPDFEdit(true);
  2835. //如果是undo,Redo里调用的方法,则不记录到栈里
  2836. if (!isFromUndoRedo)
  2837. {
  2838. //记录进Undo栈
  2839. PageEditHistory history = new PageEditHistory();
  2840. history.Type = rightRotate ? ActionType.RightRotate : ActionType.LeftRotate;
  2841. history.SourceTargetValue = items;
  2842. history.viewmodel = this;
  2843. PDFViewer.UndoManager.AddHistory(history);
  2844. }
  2845. //页面范围-横向页面,旋转90°后,输入框应当变成具体的页码,因为旋转后不是横向页面了(纵向页面同理)
  2846. if (items.Count > 0)
  2847. {
  2848. ReloadAfterOption(true, false, Tuple.Create(items.Keys.First(), items.Keys.Last()));
  2849. }
  2850. IsLoading = Visibility.Collapsed;
  2851. }
  2852. private Dictionary<int, int> SetRotateSelectedPagesEvent(bool rightRotate)
  2853. {
  2854. Dictionary<int, int> items = new Dictionary<int, int>();
  2855. for (int i = 0; i < PageEditItems.Count; i++)
  2856. {
  2857. if (PageEditItems[i].Selected)
  2858. {
  2859. //逆时针旋转传 - 1 顺时针旋转传1
  2860. //直接用PDFViewer的旋转接口,需要留意好,中间层不能有自动清空undo栈
  2861. PDFViewer.RotatePage(ComPDFKitViewer.PageRotate.Rotate90, rightRotate, i);
  2862. items.Add(i, i);
  2863. //重新拿图
  2864. PageEditItems[i].IsGetImage = false;
  2865. }
  2866. }
  2867. return items;
  2868. }
  2869. /// <summary>
  2870. /// 刷新页码
  2871. /// </summary>
  2872. private void RefreshPageNum()
  2873. {
  2874. for (int i = 0; i < PageEditItems.Count; i++)
  2875. {
  2876. PageEditItems[i].PageNumber = i + 1;
  2877. if (IsBOTAThumb == Visibility.Visible)
  2878. {
  2879. PageEditItems[i].FontSize = 12;
  2880. }
  2881. else
  2882. {
  2883. PageEditItems[i].FontSize = 14;
  2884. }
  2885. }
  2886. }
  2887. /// <summary>
  2888. /// 刷新页面范围下拉框
  2889. /// </summary>
  2890. private void RefreshPageRangeItem()
  2891. {
  2892. PageRangeItems.Clear();
  2893. PageRangeItems.Add(App.MainPageLoader.GetString("WritableComboBox_AllPage"));
  2894. PageRangeItems.Add(App.MainPageLoader.GetString("WritableComboBox_OddPage"));
  2895. PageRangeItems.Add(App.MainPageLoader.GetString("WritableComboBox_EvenPage"));
  2896. PageRangeItems.Add("Horizontal Page");
  2897. PageRangeItems.Add("Vertical Page");
  2898. PageRangeItems.Add(App.MainPageLoader.GetString("WritableComboBox_CustomRangeEge"));
  2899. }
  2900. /// <summary>
  2901. /// 选中目标页面范围的页面
  2902. /// </summary>
  2903. /// <param name="index">页面范围选中项索引</param>
  2904. private async void SuitPageRange(int index)
  2905. {
  2906. List<int> pageRange = new List<int>();
  2907. switch (index)
  2908. {
  2909. case 0:
  2910. for (int i = 0; i < PageEditItems.Count; i++)
  2911. {
  2912. pageRange.Add(i + 1);
  2913. }
  2914. break;
  2915. case 1:
  2916. //奇数页
  2917. //先清空所有选项
  2918. ListSelectedIndex = -1;
  2919. for (int i = 0; i < PageEditItems.Count; i++)
  2920. {
  2921. if (i % 2 == 0)
  2922. {
  2923. pageRange.Add(i + 1);
  2924. }
  2925. }
  2926. break;
  2927. case 2:
  2928. //偶数页
  2929. //先清空所有选项
  2930. ListSelectedIndex = -1;
  2931. for (int i = 0; i < PageEditItems.Count; i++)
  2932. {
  2933. if (i % 2 != 0)
  2934. {
  2935. pageRange.Add(i + 1);
  2936. }
  2937. }
  2938. break;
  2939. case 3://横向页面
  2940. //先清空所有选项
  2941. ListSelectedIndex = -1;
  2942. for (int i = 0; i < PageEditItems.Count; i++)
  2943. {
  2944. if (!PageEditItems[i].isVertical)
  2945. {
  2946. pageRange.Add(i + 1);
  2947. }
  2948. }
  2949. break;
  2950. case 4://纵向页面
  2951. for (int i = 0; i < PageEditItems.Count; i++)
  2952. {
  2953. if (PageEditItems[i].isVertical)
  2954. {
  2955. pageRange.Add(i + 1);
  2956. }
  2957. }
  2958. break;
  2959. case 5://自定义
  2960. TxtPageInputVisibility = Visibility.Visible;
  2961. await Task.Delay(30);
  2962. TbPageRange = "";
  2963. break;
  2964. default:
  2965. break;
  2966. }
  2967. if (pageRange.Count > 0)
  2968. {
  2969. //避免在下面的过程中频繁触发SelectionChanged事件
  2970. IsSelectFormSelf = true;
  2971. NotifyUIToRefresh(pageRange);
  2972. //为了避免重复选中,条件也符合而文本缺失的情况
  2973. TbPageRange = PageRangeItems[index];
  2974. RightSelectedIndexs.Clear();
  2975. foreach (var item in pageRange)
  2976. {
  2977. RightSelectedIndexs.Add(item - 1);
  2978. }
  2979. IsSelectFormSelf = false;
  2980. }
  2981. else
  2982. {
  2983. //选中了下拉框的选项
  2984. //增加条件,修复全选页面,但是ctrl点选取消后,选中状态不准确问题
  2985. if (index != -1)
  2986. {
  2987. //没有符合条件时 清空选中项
  2988. ListSelectedIndex = -1;
  2989. //20230714 没有符合条件的页面时候,控件内容不清空
  2990. //没有符合条件的页面时,空间内容清空
  2991. //await Task.Delay(30);
  2992. //TbPageRange = "";
  2993. }
  2994. }
  2995. }
  2996. /// <summary>
  2997. /// 是否显示页面尺寸
  2998. /// </summary>
  2999. /// <param name="show">是或否</param>
  3000. private void ShowPageSize()
  3001. {
  3002. if (ShowedPageSize == Visibility.Visible)
  3003. {
  3004. IsShowPageSize = true;
  3005. ShowedPageSize = Visibility.Collapsed;
  3006. HidePageSize = Visibility.Visible;
  3007. }
  3008. else
  3009. {
  3010. IsShowPageSize = false;
  3011. HidePageSize = Visibility.Collapsed;
  3012. ShowedPageSize = Visibility.Visible;
  3013. }
  3014. ListSelectedIndex = -1;
  3015. selectedPageList.Clear();
  3016. //显示显示当前范围的item的页面尺寸,其他的Item在刷新图片时,再显示尺寸
  3017. if (startRefreshedIndex <= endRefreshedIndex)
  3018. {
  3019. for (int i = startRefreshedIndex; i < endRefreshedIndex; i++)
  3020. {
  3021. if (i >= 0 && i <= PageEditItems.Count - 1)
  3022. {
  3023. PageEditItems[i].ShowPageSize = IsShowPageSize;
  3024. }
  3025. }
  3026. }
  3027. }
  3028. /// <summary>
  3029. /// 从PDFView获取所有Items集合
  3030. /// </summary>
  3031. public void GetSourceItems(bool isFirstLoad)
  3032. {
  3033. if (isFirstLoad || IsBOTAThumb == Visibility.Visible)
  3034. {
  3035. PageEditItems.Clear();
  3036. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  3037. {
  3038. var item = GetNewPageEditItem(i);
  3039. PageEditItems.Add(item);
  3040. }
  3041. }
  3042. //else
  3043. //{
  3044. // //后续进来仅刷新图片即可
  3045. // for (int i = 0; i < PageEditItems.Count; i++)
  3046. // {
  3047. // PageEditItems[i].IsGetImage = false;
  3048. // }
  3049. //}
  3050. }
  3051. /// <summary>
  3052. /// 刷新书签列表
  3053. /// </summary>
  3054. public void RefreshBookMarkList()
  3055. {
  3056. if (PDFViewer != null)
  3057. {
  3058. var booklist = PDFViewer.Document.GetBookmarkList();
  3059. if (booklist == null)
  3060. {
  3061. return;
  3062. }
  3063. //获取所有书签的Index集合
  3064. List<int> marks = new List<int>();
  3065. for (int i = 0; i < booklist.Count; i++)
  3066. {
  3067. marks.Add(booklist[i].PageIndex);
  3068. }
  3069. //不能只遍历marks集合,考虑书签集合元素删除的情况
  3070. for (int k = 0; k < PageEditItems.Count; k++)
  3071. {
  3072. if (marks.Contains(k))
  3073. PageEditItems[k].HaveBookMark = true;
  3074. else
  3075. PageEditItems[k].HaveBookMark = false;
  3076. }
  3077. }
  3078. }
  3079. /// <summary>
  3080. /// 刷新指定范围的图片
  3081. /// </summary>
  3082. /// <param name="startIndex"></param>
  3083. /// <param name="endIndex"></param>
  3084. private void RefreshItemImage(int startIndex, int endIndex)
  3085. {
  3086. startRefreshedIndex = startIndex;
  3087. endRefreshedIndex = endIndex;
  3088. if (PDFViewer == null || PDFViewer.Document == null)
  3089. {
  3090. return;
  3091. }
  3092. //一个拿图比例系数,减少拿图耗时,尽量确保缩放后图片清晰度不会很差
  3093. int rate = (int)ItemSizes[2].Height;
  3094. //230628 缩略图需要根据当前 的缩略值
  3095. //int rate = (int)ItemSizes[ZoomIndex + 1].Height;
  3096. for (int i = startIndex; i <= endIndex; i++)
  3097. {
  3098. if (i <= PageEditItems.Count - 1 && i >= 0 && PageEditItems.Count > 0 && PDFViewer.Document != null && rate > 0)
  3099. {
  3100. //先更新一次每个页面的尺寸 确保图的方向正确
  3101. PageEditItems[i].Size = PDFViewer.Document.GetPageSize(i);
  3102. if (PageEditItems[i].Size.Width <= 0 || PageEditItems[i].Size.Height <= 0)
  3103. {
  3104. //补充判空逻辑,避免崩溃
  3105. continue;
  3106. }
  3107. //不能一次性显示页面大小,页数较多时会导致界面卡死
  3108. //每次刷新图片时,根据右键菜单状态(是否显示页面大小)来控制每个Item的页面尺寸显示
  3109. PageEditItems[i].ShowPageSize = IsShowPageSize;
  3110. //宽高为样式中图片控件宽高
  3111. if (PageEditItems[i].isVertical)
  3112. {
  3113. //等比例缩小
  3114. if (PageEditItems[i].Size.Height > rate)
  3115. {
  3116. int index = Convert.ToInt32(PageEditItems[i].Size.Width / (PageEditItems[i].Size.Height / rate));
  3117. SetThumbnailImage(i, index, rate, true);
  3118. }
  3119. else
  3120. {
  3121. int index = Convert.ToInt32(PageEditItems[i].Size.Width * (rate / (int)PageEditItems[i].Size.Height));
  3122. //尺寸大小的图片 等比例放大 避免页面放大后内容模糊
  3123. SetThumbnailImage(i, index, rate, true);
  3124. }
  3125. }
  3126. else
  3127. {
  3128. if (PageEditItems[i].Size.Width > rate)
  3129. {
  3130. int index = Convert.ToInt32(PageEditItems[i].Size.Height / (PageEditItems[i].Size.Width / rate));
  3131. SetThumbnailImage(i, index, rate, false);
  3132. }
  3133. else
  3134. {
  3135. int index = Convert.ToInt32(PageEditItems[i].Size.Height * (rate / (int)(PageEditItems[i].Size.Width)));
  3136. SetThumbnailImage(i, index, rate, false);
  3137. }
  3138. }
  3139. }
  3140. }
  3141. }
  3142. private async void SetThumbnailImage(int i, int index, int rate, bool isVertical)
  3143. {
  3144. if (viewContentViewModel.IsHiddenAnnot)
  3145. {
  3146. if (isVertical)
  3147. {
  3148. //显示=1, 隐藏=0注释
  3149. await PDFViewer.GetThumbnail(i, index, rate, 0);
  3150. }
  3151. else
  3152. {
  3153. await PDFViewer.GetThumbnail(i, rate, index, 0);
  3154. }
  3155. }
  3156. else
  3157. {
  3158. if (isVertical)
  3159. {
  3160. await PDFViewer.GetThumbnail(i, index, rate, 1);
  3161. }
  3162. else
  3163. {
  3164. await PDFViewer.GetThumbnail(i, rate, index, 1);
  3165. }
  3166. }
  3167. }
  3168. /// <summary>
  3169. /// 拖拽排序功能
  3170. /// </summary>
  3171. /// <param name="targetIndex"></param>
  3172. /// <param name="pageIndexs"></param>
  3173. public void DragToSort(int targetIndex, List<int> pageIndexs, bool IsFromUndoRedo = false)
  3174. {
  3175. //拖拽排序后需要重新选中项的集合
  3176. List<int> selectedPages = new List<int>();
  3177. //记录原位置和目标位置的字典,用于Undo,Redo恢复
  3178. Dictionary<int, int> SourceTargetValue = new Dictionary<int, int>();
  3179. bool result = false;
  3180. //排序前,先对所有页码排序
  3181. pageIndexs.Sort();
  3182. //要考虑每一次交换都会导致局部页码发生改变
  3183. //每次整体往后移动时,先移动大页码;整体往前移动时,先移动小页码;往中间移动时,再按上述两种情况分别移动
  3184. if (targetIndex <= pageIndexs[0])
  3185. {
  3186. for (int i = 0; i < pageIndexs.Count; i++)
  3187. {
  3188. // list.Add(PageEditListBox.Items[pageIndexs[i]] as ListBoxItem);
  3189. //sourceindex.Add(pageIndexs[i]);
  3190. result = MoveItemToIndex(pageIndexs[i], targetIndex + i);
  3191. if (!result)
  3192. {
  3193. return;
  3194. }
  3195. SourceTargetValue.Add(pageIndexs[i], targetIndex + i);
  3196. selectedPages.Add(targetIndex + i + 1);
  3197. }
  3198. }
  3199. else if (targetIndex > pageIndexs[pageIndexs.Count - 1])
  3200. {
  3201. //目标位置在所有选中内容右边 整体后移
  3202. for (int i = 0; i < pageIndexs.Count; i++)
  3203. {
  3204. result = MoveItemToIndex(pageIndexs[pageIndexs.Count - 1 - i], targetIndex - 1 - i);
  3205. if (!result)
  3206. {
  3207. return;
  3208. }
  3209. SourceTargetValue.Add(pageIndexs[pageIndexs.Count - 1 - i], targetIndex - 1 - i);
  3210. selectedPages.Add(targetIndex - i);
  3211. }
  3212. }
  3213. else//目标位置在所有选中项中间
  3214. {
  3215. int i, j, k;
  3216. //找出PageEditListBox.Items中页码等于destpage的下标
  3217. for (k = 0; k < pageIndexs.Count - 1; k++)
  3218. {
  3219. //这里要算入K---即前面部分的页面个数
  3220. if (pageIndexs[k] < targetIndex && pageIndexs[k + 1] >= targetIndex)
  3221. {
  3222. break;
  3223. }
  3224. }
  3225. //局部往后移动
  3226. for (i = 0; i <= k; i++)
  3227. {
  3228. result = MoveItemToIndex(pageIndexs[k - i], targetIndex - 1 - i);
  3229. if (!result)
  3230. {
  3231. return;
  3232. }
  3233. SourceTargetValue.Add(pageIndexs[k - i], targetIndex - 1 - i);
  3234. selectedPages.Add(targetIndex - i);
  3235. }
  3236. int newIndex = targetIndex;
  3237. //局部往前移动
  3238. for (j = i; j < pageIndexs.Count; j++)
  3239. {
  3240. result = MoveItemToIndex(pageIndexs[j], newIndex);
  3241. if (!result)
  3242. {
  3243. return;
  3244. }
  3245. SourceTargetValue.Add(pageIndexs[j], newIndex);
  3246. selectedPages.Add(newIndex + 1);
  3247. newIndex++;
  3248. }
  3249. }
  3250. //刷新页码和图片
  3251. ReloadAfterOption(true, false, new Tuple<int, int>(0, PDFViewer.Document.PageCount));
  3252. //先刷新页码再选中item,顶部的选中页面更准确
  3253. //通知UI选中Items
  3254. NotifyUIToRefresh(selectedPages);
  3255. if (!IsFromUndoRedo)
  3256. {
  3257. //存入Undo栈
  3258. PageEditHistory history = new PageEditHistory();
  3259. history.Type = ActionType.DragSort;
  3260. history.SourceTargetValue = SourceTargetValue;
  3261. //用于Redo恢复
  3262. history.InsertIndex = targetIndex;
  3263. history.viewmodel = this;
  3264. PDFViewer.UndoManager.AddHistory(history);
  3265. }
  3266. }
  3267. /// <summary>
  3268. /// 移动某个Item到指定位置
  3269. /// </summary>
  3270. private bool MoveItemToIndex(int sourceindex, int targetindex)
  3271. {
  3272. if (targetindex == sourceindex || targetindex < 0)
  3273. {
  3274. return false;
  3275. }
  3276. //文档层移动
  3277. var result = PDFViewer.Document.MovePage(sourceindex, targetindex);
  3278. if (!result)
  3279. {
  3280. return false;
  3281. }
  3282. //UI层移动
  3283. var source = PageEditItems[sourceindex];
  3284. PageEditItems.RemoveAt(sourceindex);
  3285. PageEditItems.Insert(targetindex, source);
  3286. if (PDFViewer.CurrentIndex == sourceindex)
  3287. {
  3288. PDFViewer.GoToPage(targetindex);
  3289. }
  3290. return true;
  3291. }
  3292. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  3293. {
  3294. if (PDFViewer.Parent is ContentControl ui)
  3295. {
  3296. //判断鼠标焦点 是否在控件上
  3297. if (ui.IsMouseOver)
  3298. {
  3299. //缩略图 复制粘贴后会选择第一项的问题
  3300. if (e.Key == "PageNum" && !viewContentViewModel.isInPageEdit)
  3301. {
  3302. int index = (e.Value as ComPDFKitViewer.RenderData).PageIndex - 1;
  3303. //注释掉 该方法有许多联动,避免联动影响
  3304. //NotifyUIToRefresh(new List<int> { (e.Value as ComPDFKitViewer.RenderData).PageIndex });
  3305. eventor.GetEvent<PageEditSelectedItemEvent>().Publish(new PageEditSelectedItemEventArgs() { Unicode = unicode, SelectedItemIndex = index, IsSingle = true });
  3306. }
  3307. }
  3308. }
  3309. //不准确 有时候会触发两次,就无法判断
  3310. //if (IsAddPages == false)
  3311. //{
  3312. // //缩略图 复制粘贴后会选择第一项的问题
  3313. // if (e.Key == "PageNum" && !viewContentViewModel.isInPageEdit)
  3314. // {
  3315. // int index = (e.Value as ComPDFKitViewer.RenderData).PageIndex - 1;
  3316. // //注释掉 该方法有许多联动,避免联动影响
  3317. // //NotifyUIToRefresh(new List<int> { (e.Value as ComPDFKitViewer.RenderData).PageIndex });
  3318. // eventor.GetEvent<PageEditSelectedItemEvent>().Publish(new PageEditSelectedItemEventArgs() { Unicode = unicode, SelectedItemIndex = index, IsSingle = true });
  3319. // }
  3320. //}
  3321. //else
  3322. //{
  3323. // IsAddPages = false;
  3324. // return;
  3325. //}
  3326. }
  3327. /// <summary>
  3328. /// 从底层库获取对应页面的图片
  3329. /// </summary>
  3330. /// <param name="pageIndex"></param>
  3331. /// <param name="thumb"></param>
  3332. /// <param name="w"></param>
  3333. /// <param name="h"></param>
  3334. private void PDFViewer_OnThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
  3335. {
  3336. try
  3337. {
  3338. if (PageEditItems.Count >= pageIndex + 1)
  3339. {
  3340. PixelFormat fmt = PixelFormats.Bgra32;
  3341. BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
  3342. PageEditItems[pageIndex].Image = bps;
  3343. PageEditItems[pageIndex].IsGetImage = true;
  3344. }
  3345. }
  3346. catch
  3347. {
  3348. }
  3349. }
  3350. #endregion 方法
  3351. #region Navigate
  3352. public bool IsNavigationTarget(NavigationContext navigationContext)
  3353. {
  3354. return true;
  3355. }
  3356. public void OnNavigatedFrom(NavigationContext navigationContext)
  3357. {
  3358. }
  3359. /// <summary>
  3360. /// 从其他页面导航过来的时候会触发
  3361. /// 考虑只有在页面编辑的时候会进行页面的删减,可以尝试逻辑优化:仅第一次进入页面编辑时重新刷新页面
  3362. /// </summary>
  3363. /// <param name="navigationContext"></param>
  3364. public void OnNavigatedTo(NavigationContext navigationContext)
  3365. {
  3366. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  3367. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  3368. bool isBOTA = false;
  3369. navigationContext.Parameters.TryGetValue<bool>(ParameterNames.BOTAThumb, out isBOTA);
  3370. if (PDFViewer != null)
  3371. {
  3372. Initializing = true;
  3373. if (isFirstLoad)
  3374. {
  3375. PDFViewer.OnThumbnailGenerated -= PDFViewer_OnThumbnailGenerated;
  3376. PDFViewer.OnThumbnailGenerated += PDFViewer_OnThumbnailGenerated;
  3377. PDFViewer.InfoChanged -= PDFViewer_InfoChanged;
  3378. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  3379. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  3380. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  3381. ZoomIndex = 0;
  3382. }
  3383. //BOTA缩略图时,显示不同的菜单栏
  3384. if (isBOTA)
  3385. {
  3386. IsBOTAThumb = Visibility.Visible;
  3387. //ShowedPageSize = Visibility.Collapsed;
  3388. //HidePageSize = Visibility.Collapsed;
  3389. }
  3390. else
  3391. {
  3392. IsBOTAThumb = Visibility.Collapsed;
  3393. //进入页面编辑 滚动到选中页面
  3394. //NotifyUIToRefresh();
  3395. }
  3396. //每次进入页面编辑都刷新item个数,缩略图删减时,可以更新item
  3397. //需要留意大文档性能
  3398. GetSourceItems(true);
  3399. // 暂时 bota 和页面编辑不联动,切换后,是重新获取数据,不会获得之前的状态
  3400. IsShowPageSize = false;
  3401. ShowedPageSize = Visibility.Visible;
  3402. HidePageSize = Visibility.Collapsed;
  3403. //清空一下选中项,修复进入页面编辑后仍然选中多页的问题
  3404. ListSelectedIndex = -1;
  3405. ListSelectedIndex = PDFViewer.CurrentIndex;
  3406. //页面编辑取消显示书签Icon
  3407. //RefreshBookMarkList();
  3408. isFirstLoad = false;
  3409. Initializing = false;
  3410. if (PageEditItems.Count > 0)
  3411. {
  3412. if (PageEditItems.IndexOf(PageEditItems[PDFViewer.CurrentIndex]) > -1)
  3413. {
  3414. //记录 进页面编辑是这一页内容
  3415. CurrentPageEditItem = PageEditItems[PDFViewer.CurrentIndex];
  3416. }
  3417. }
  3418. }
  3419. }
  3420. private void PDFViewer_AnnotEditHandler(object sender, List<ComPDFKitViewer.AnnotEvent.AnnotEditEvent> e)
  3421. {
  3422. if (e != null && e.Count > 0 && IsBOTAThumb == Visibility.Visible)
  3423. {
  3424. //缩略图模式下 刷新页面
  3425. int pageIndex = e[0].PageIndex;
  3426. RefreshItemImage(pageIndex, pageIndex);
  3427. }
  3428. }
  3429. #endregion Navigate
  3430. }
  3431. }