KMMainViewController.swift 345 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898
  1. //
  2. // KMMainViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. let MIN_SIDE_PANE_WIDTH: NSNumber = 264 // 最小值
  10. let CPDFViewIsReadModeKey = "kKMPDFViewIsReadMode"
  11. let CPDFViewLeftSidePaneWidthKey = "CPDFOfficeLeftSidePaneWidthKey"
  12. struct KMNMWCFlags {
  13. var settingUpWindow: Bool = true
  14. }
  15. @objcMembers class KMMainViewController: KMNBaseViewController, NSTextFieldDelegate {
  16. @IBOutlet var contendBox: NSBox!
  17. @IBOutlet var toolbarBox: NSBox! //工具栏Box
  18. @IBOutlet var bottomContendBox: NSBox! //
  19. @IBOutlet var sidebarBox: NSBox! //左侧边栏Box
  20. @IBOutlet var infoContendSplitView: NSSplitView!
  21. @IBOutlet var infoSplitLeftView: NSView!
  22. @IBOutlet var infoSplitRightView: NSView!
  23. @IBOutlet var infoSplitCenterView: NSView!
  24. @IBOutlet var infoSplitCenterSubView: NSBox!
  25. @IBOutlet var pdfSplitView: NSSplitView!
  26. @IBOutlet var pdfSplitTopView: NSView!
  27. @IBOutlet var pdfSplitBottomView: NSView!
  28. @IBOutlet var toolbarBoxHeightConst: NSLayoutConstraint!
  29. @IBOutlet var infoSplitViewLeftConst: NSLayoutConstraint!
  30. @IBOutlet var infoSplitViewRightConst: NSLayoutConstraint!
  31. @IBOutlet var infoSplitViewTopConst: NSLayoutConstraint!
  32. var viewManager: KMPDFViewManager = KMPDFViewManager.init()
  33. var toolbarManager: KMPDFToolbarManager = KMPDFToolbarManager.init()
  34. var listView: CPDFListView = CPDFListView.init()
  35. var document: CPDFDocument?
  36. var myDocument: NSDocument?
  37. var isFirstOpen: Bool = true
  38. var insertDocuments: Set<CPDFDocument> = [] //插入新文档时,SDK会出现崩溃,临时记录
  39. //工具栏
  40. private var pdfToolbarController: KMPDFToolbarController?
  41. //BOTA SideBar
  42. private var sideBarController: KMPDFSideBarController?
  43. //页面编辑
  44. private var pageEditViewController: KMNPageEditViewController?
  45. //DisplaySetting
  46. private var displaySettingController: KMNDisplayViewController?
  47. //SPlitPDF分屏视图
  48. private var splitPDFController: KMSplitPDFViewController?
  49. private var pageNumberToolbar: KMPageNumberPromptView?
  50. //PPT操作界面
  51. var presentationTopViewController: KMPresentationTopViewController?
  52. //Edit
  53. var editToolbarView: KMEditToolbarView?
  54. //水印
  55. var watermarkViewController: KMWatermarkController?
  56. //背景
  57. var backgroundViewController: KMBackgroundController?
  58. //Header&Footer
  59. var headerFooterViewController: KMHeaderFooterController?
  60. //Bates
  61. var batesViewController: KMBatesController?
  62. //Crop
  63. var cropController: KMCropController?
  64. //左边
  65. var botaViewController: KMNLeftSideViewController?
  66. //右边
  67. var rightSideController: KMRightSideController?
  68. let alertTipViewController: KMNAlertTipViewController = KMNAlertTipViewController(nibName: "KMNAlertTipViewController", bundle: nil)
  69. //合并
  70. var mergeWindowController: KMMergeWindowController?
  71. //春季活动
  72. var recommondPopWindowVC: KMRecommondPopWindow?
  73. //数字签名状态
  74. var signaturestateVC: CDSignatureCertificateStateViewController = CDSignatureCertificateStateViewController.init()
  75. //PDFView右键菜单
  76. private var groupListMenuGroup: ComponentGroup? = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  77. var textFieldSheet: KMTextFieldSheetController = KMTextFieldSheetController.init(windowNibName: "KMTextFieldSheetController")
  78. @IBOutlet weak var leftView: NSView!
  79. var model = KMMainModel()
  80. //Search
  81. var searchIndex: Int = 0
  82. //对比
  83. var isCompareModel: Bool = false
  84. //密码弹窗
  85. var passwordWindow: KMPasswordInputWindow?
  86. var securityWindowController: KMSecurityWindowController?
  87. //压缩
  88. var compressWindowController: KMCompressWindowController?
  89. //引导
  90. var guideInfoWindowController: KMGuideInfoWindowController?
  91. //测量
  92. var distanceMeasureInfoWindowController: CDistanceMeasureInfoWindowController?
  93. var perimeterMeasureInfoWindowController: CPerimeterMeasureInfoWindowController?
  94. var areaMeasureInfoWindowController: CAreaMeasureInfoWindowController?
  95. //打印
  96. var bookletWindowController: KMPDFBookletWindowController?
  97. var multiplePrintWindowController: KMPDFMultiplePrintWindowController?
  98. var posterPrintWindowController: KMPDFPosterPrintWindowController?
  99. var removeAllAnnotationsStore = KMPDFViewRemoveAllAnnotationsStore()
  100. var componentMessageView: ComponentMessage = ComponentMessage()
  101. private var _needSave = false
  102. var needSave: Bool {
  103. set {
  104. _needSave = newValue
  105. if (_needSave == false) {
  106. self.clearIsPDFDocumentEdited()
  107. }
  108. }
  109. get {
  110. return _needSave
  111. }
  112. }
  113. var isPDFDocumentEdited: Bool {
  114. get {
  115. return self.model.isPDFDocumentEdited
  116. }
  117. }
  118. var newMwcFlags = KMNMWCFlags(settingUpWindow: true)
  119. var searchResults: [KMSearchMode] = []
  120. var mwcFlags: MwcFlags = MwcFlags()
  121. weak var browserWindowController: KMBrowserWindowController? //慎直接使用这个方法
  122. var currentWindowController: NSWindowController!
  123. var savedNormalSetup: NSMutableDictionary = NSMutableDictionary()
  124. let CPDFOfficeLeftSidePaneWidthKey = "CPDFOfficeLeftSidePaneWidthKey"
  125. let CPDFOfficeRightSidePaneWidthKey = "CPDFOfficeRightSidePaneWidthKey"
  126. var extract: KMExtractImageWindowController?
  127. var pageNumber: UInt?
  128. var autoSaveTimer: Timer?
  129. private var _documentFirstLoad: Bool = true
  130. var eventMonitor: Any?
  131. var keyEventMonitor: Any?
  132. var mouseRightMenuEvent: NSEvent?
  133. lazy private var homeVC: KMNHomeViewController? = {
  134. let vc = KMNHomeViewController()
  135. return vc
  136. }()
  137. fileprivate var _secureOptions: [CPDFDocumentWriteOption : Any]?
  138. var secureOptions: [CPDFDocumentWriteOption : Any]? {
  139. get {
  140. return self._secureOptions
  141. }
  142. }
  143. var documentAttribute: [CPDFDocumentAttribute : Any]?
  144. fileprivate var _removeSecureFlag = false
  145. var removeSecureFlag: Bool {
  146. get {
  147. return self._removeSecureFlag
  148. }
  149. }
  150. fileprivate var _saveWatermarkFlag = false
  151. var saveWatermarkFlag: Bool {
  152. get {
  153. return self._saveWatermarkFlag
  154. }
  155. }
  156. private var mainWindow_: NSWindow?
  157. var mainWindow: NSWindow? {
  158. get {
  159. return self.mainWindow_
  160. }
  161. set {
  162. self.mainWindow_ = newValue
  163. }
  164. }
  165. var setDocument: CPDFDocument? {
  166. get {
  167. return document
  168. }
  169. set {
  170. if document != newValue {
  171. document = newValue
  172. }
  173. listView.document = document
  174. botaViewController?.changeDocument(document: document)
  175. }
  176. }
  177. var setPageNumber: UInt {
  178. get {
  179. return pageNumber!
  180. }
  181. set {
  182. let pageCount = listView.document?.pageCount ?? 0
  183. var value = newValue
  184. if value > pageCount {
  185. value = pageCount
  186. }
  187. if value > 0 && listView.currentPage().pageIndex() != value-1 {
  188. listView.go(to: listView.document?.page(at: value-1))
  189. }
  190. if pageNumber != value {
  191. pageNumber = value
  192. }
  193. }
  194. }
  195. //MARK: - func
  196. deinit {
  197. NotificationCenter.default.removeObserver(self)
  198. self.listView.delegate = nil
  199. self.listView.document?.delegate = nil
  200. self.removeEventMonitor()
  201. }
  202. override func viewDidLoad() {
  203. super.viewDidLoad()
  204. // Do view setup here.
  205. setupData()
  206. setupUI()
  207. }
  208. override func viewDidAppear() {
  209. super.viewDidAppear()
  210. reloadPopUIWindow()
  211. addEventMonitor()
  212. addKeyEventMonitor()
  213. }
  214. override func viewDidDisappear() {
  215. super.viewDidDisappear()
  216. toggleClosePopUIWindow()
  217. }
  218. override func initContentView() {
  219. super.initContentView()
  220. }
  221. override func updateUIThemeColor() {
  222. super.updateUIThemeColor()
  223. }
  224. override func updateUILanguage() {
  225. super.updateUILanguage()
  226. }
  227. //MARK: - private
  228. func setupUI() {
  229. initPDFView()
  230. initToolbar()
  231. initSideBar()
  232. setUpSplitView()
  233. }
  234. func setupData() {
  235. toolbarManager.pdfViewManager = viewManager
  236. if (UserDefaults.standard.object(forKey: CPDFViewLeftSidePaneWidthKey) != nil) {
  237. UserDefaults.standard.set(MIN_SIDE_PANE_WIDTH, forKey: CPDFViewLeftSidePaneWidthKey)
  238. UserDefaults.standard.synchronize()
  239. }
  240. newMwcFlags.settingUpWindow = true
  241. }
  242. @objc func editFontColorItemPanelAction(_ sender: Any) {
  243. if let color = (sender as? NSColorPanel)?.color {
  244. listView.changeEditingTextarea_Color(color: color)
  245. }
  246. }
  247. //MARK: - document load成功
  248. private func documentLoadComplete() {
  249. initLeftSideController()
  250. addNotificationCenter()
  251. activityLoadMethod()
  252. let readModel = UserDefaults.standard.bool(forKey: CPDFViewIsReadModeKey)
  253. if readModel == true {
  254. self.openPDFReadMode()
  255. }
  256. reloadDigitalSigns()
  257. alertTipViewController.showInView(listView: listView, subView: infoSplitCenterView)
  258. alertTipViewController.formFieldHighlightCallback = { [weak self] in
  259. let value = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  260. CPDFAnnotation.updateHighlightFormFiled(self?.listView, highlightFormFiled: !value)
  261. self?.toolbarManager.refreshDefaultConfigItem()
  262. }
  263. alertTipViewController.enterPasswordCallback = { [weak self] in
  264. self?.removeOwnerPassword()
  265. }
  266. alertTipViewController.digitalDetailsCallback = { [weak self] in
  267. }
  268. alertTipViewController.redactApplyCallback = { [weak self] in
  269. self?.redactApplyAction()
  270. }
  271. alertTipViewController.heightCallback = { [weak self] height in
  272. self?.infoSplitViewTopConst.constant = height
  273. }
  274. newMwcFlags.settingUpWindow = false
  275. loadUserDefaultsData()
  276. }
  277. private func saveAsPath() {
  278. let saveAccessCtr = KMSavePanelAccessoryController()
  279. var fileName = listView.document.documentURL.deletingPathExtension().lastPathComponent
  280. fileName = String(format: "%@_%@.pdf", fileName, "Redact")
  281. let outputSavePanel = NSSavePanel()
  282. outputSavePanel.allowedFileTypes = ["pdf"]
  283. outputSavePanel.nameFieldStringValue = fileName
  284. outputSavePanel.accessoryView = saveAccessCtr.view
  285. outputSavePanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  286. if result == .OK {
  287. self.listView.document.applyRedactions()
  288. self.showProgressWindow(message: KMLocalizedString("Save") + "PDF")
  289. self.progressC?.maxValue = 3.0
  290. self.progressC?.increment(by: 1.0)
  291. let savePDFPath = outputSavePanel.url!.path
  292. // 执行进度 [假进度]
  293. self.progressC?.increment(by: 1.0)
  294. self.progressC?.increment(by: 1.0)
  295. let isSuccess = self.listView.document.write(toFile: savePDFPath)
  296. if (isSuccess) {
  297. if (saveAccessCtr.openAutomaticButton.state == .on) {
  298. NSDocumentController.shared.km_safe_openDocument(withContentsOf: outputSavePanel.url!, display: true) { _, _, _ in
  299. }
  300. } else {
  301. let url = URL(fileURLWithPath: savePDFPath)
  302. NSWorkspace.shared.activateFileViewerSelecting([url])
  303. }
  304. }
  305. self.hiddenProgressWindow()
  306. }
  307. }
  308. }
  309. private func reloadDigitalSigns() {
  310. let signatures = listView.document.signatures()
  311. for i in 0 ..< (signatures?.count ?? 0) {
  312. let signature:CPDFSignature = signatures?[i] ?? CPDFSignature()
  313. if signature.signers.count > 0 {
  314. signature.verifySignature(with: listView.document)//耗时,注意
  315. }
  316. }
  317. listView.signatures = signatures
  318. }
  319. //MARK: - 活动加载相关
  320. private func activityLoadMethod() {
  321. }
  322. private func addNotificationCenter() {
  323. NotificationCenter.default.addObserver(self, selector: #selector(annotationsAttributeHasChange), name: NSNotification.Name.CPDFListViewAnnotationsAttributeHasChange, object:nil)
  324. NotificationCenter.default.addObserver(self, selector: #selector(signatureStateChangeNoti), name: NSNotification.Name(rawValue: "CSignatureTrustCerDidChangeNotification"), object: nil)
  325. NotificationCenter.default.addObserver(self, selector: #selector(pdfViewScrollViewDidScroll), name: NSScrollView.didLiveScrollNotification, object: listView.documentView())
  326. NotificationCenter.default.addObserver(self, selector: #selector(pageCountChangedNotification), name: NSNotification.Name.CPDFDocumentPageCountChanged, object: listView.document)
  327. NotificationCenter.default.addObserver(self, selector: #selector(documentDidUnlockNotification), name: Notification.Name("CPDFDocumentDidUnlockNotification"), object: nil)
  328. NotificationCenter.default.addObserver(self, selector: #selector(applicationWillTerminateNotification), name: NSApplication.willTerminateNotification, object: nil)
  329. NotificationCenter.default.addObserver(self, selector: #selector(pdfViewHighlightFormFiledUpdateNoti), name: kPDFViewHighlightFormFiledUpdateNotiName, object: nil)
  330. }
  331. internal func removeNotifications() {
  332. NotificationCenter.default.removeObserver(self)
  333. }
  334. //MARK: - 初始化默认PDF数据
  335. private func loadUserDefaultsData() {
  336. applyLeftSideWidth(0, rightSideWidth: 0) //初始打开左边栏
  337. //初始化侧边栏打开内容
  338. if SettingsManager.sharedInstance.leftPanelType == .defaultOpen {
  339. if SettingsManager.sharedInstance.defaultOpen == .thumbnail {
  340. viewManager.pdfSideBarType = .thumbnail
  341. } else if SettingsManager.sharedInstance.defaultOpen == .outline {
  342. viewManager.pdfSideBarType = .outline
  343. } else if SettingsManager.sharedInstance.defaultOpen == .bookmark {
  344. viewManager.pdfSideBarType = .bookmark
  345. } else if SettingsManager.sharedInstance.defaultOpen == .annotation {
  346. viewManager.pdfSideBarType = .annotation
  347. }
  348. } else if SettingsManager.sharedInstance.leftPanelType == .sameAsLastOpen {
  349. if let value = UserDefaults.standard.value(forKey: "KMPDFSidebarTypeKey"), let data = value as? Int {
  350. let pdfSideBarType: KMPDFSidebarType = KMPDFSidebarType(rawValue: data) ?? .none
  351. viewManager.pdfSideBarType = pdfSideBarType
  352. }
  353. } else if SettingsManager.sharedInstance.leftPanelType == .hideLeftSide {
  354. viewManager.pdfSideBarType = .none
  355. } else if SettingsManager.sharedInstance.leftPanelType == .prioritizeOutline {
  356. if let rootOutline = self.listView.document.outlineRoot(), rootOutline.numberOfChildren > 0 {
  357. viewManager.pdfSideBarType = .outline
  358. }
  359. }
  360. if viewManager.pdfSideBarType == .none {
  361. toggleCloseLeftSide()
  362. } else {
  363. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.15) {
  364. self.toggleCloseLeftSide()
  365. self.toggleOpenLeftSide(pdfSideBarType: self.viewManager.pdfSideBarType)
  366. }
  367. }
  368. //Layout & Zoom
  369. let layoutType = SettingsManager.sharedInstance.layoutType
  370. if layoutType == .singlePage {
  371. self.updatePDFViewDisplayMode(viewMode: .singlePage)
  372. } else if layoutType == .singlePageContinue {
  373. self.updatePDFViewDisplayMode(viewMode: .singlePageContinuous)
  374. } else if layoutType == .twoPage {
  375. self.updatePDFViewDisplayMode(viewMode: .twoUp)
  376. } else if layoutType == .twoPageContinue {
  377. self.updatePDFViewDisplayMode(viewMode: .twoUpContinuous)
  378. } else if layoutType == .bookMode {
  379. self.updatePDFViewDisplayMode(isbookMode: true, direction: .horizontal)
  380. } else if layoutType == .bookModeContinue {
  381. self.updatePDFViewDisplayMode(isbookMode: true, direction: .vertical)
  382. }
  383. if self.listView.document != nil {
  384. if let pageIndex = KMPreferenceManager.shared.getPageNumber(forKey: self.listView.document.documentURL.path) {
  385. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.15) {
  386. self.listView.go(toPageIndex: pageIndex, animated: false)
  387. }
  388. }
  389. if let scale = KMPreferenceManager.shared.getPageScale(forKey: self.listView.document.documentURL.path) {
  390. self.listView.scaleFactor = CGFloat(scale)
  391. } else {
  392. let zoomType = SettingsManager.sharedInstance.zoomType
  393. self.refreshPDFViewZoomInfo(zoomType)
  394. }
  395. }
  396. }
  397. func refreshPDFViewZoomInfo(_ zoomType: zoomInfoType) {
  398. if zoomType == .adaptationWidth {
  399. listView.autoScales = true
  400. } else if zoomType == .adapPage {
  401. let pageHeight = listView.currentPage().size.height
  402. let pdfviewHeight = listView.bounds.size.height
  403. listView.scaleFactor = pdfviewHeight/pageHeight
  404. listView.autoScales = false
  405. } else if zoomType == .actualSize {
  406. if listView.scaleFactor != 1.0 {
  407. listView.scaleFactor = 1.0
  408. listView.autoScales = false
  409. }
  410. } else if zoomType == .percent_10 {
  411. listView.scaleFactor = 0.1
  412. } else if zoomType == .percent_25 {
  413. listView.scaleFactor = 0.25
  414. } else if zoomType == .percent_50 {
  415. listView.scaleFactor = 0.5
  416. } else if zoomType == .percent_75 {
  417. listView.scaleFactor = 0.75
  418. } else if zoomType == .percent_100 {
  419. listView.scaleFactor = 1.0
  420. } else if zoomType == .percent_150 {
  421. listView.scaleFactor = 1.5
  422. } else if zoomType == .percent_200 {
  423. listView.scaleFactor = 2.0
  424. } else if zoomType == .percent_400 {
  425. listView.scaleFactor = 4.0
  426. } else if zoomType == .percent_800 {
  427. listView.scaleFactor = 8.0
  428. }
  429. }
  430. func applyLeftSideWidth(_ leftSideWidth: CGFloat, rightSideWidth: CGFloat) -> Void {
  431. infoContendSplitView.setPosition(infoContendSplitView.maxPossiblePositionOfDivider(at: 1) - infoContendSplitView.dividerThickness - rightSideWidth, ofDividerAt: 1)
  432. infoContendSplitView.setPosition(leftSideWidth, ofDividerAt: 0)
  433. }
  434. //MARK: - PDFView
  435. func initPDFView() {
  436. listView.autoresizingMask = [.width, .height]
  437. listView.autoScales = true
  438. listView.delegate = self
  439. listView.pdfListViewDelegate = self
  440. listView.document = self.document
  441. listView.pageBreakMargins = NSEdgeInsetsMake(10, 0, 10, 10)
  442. listView.hideNotes = false
  443. if let _ = UserDefaults.standard.value(forKey: "kPDFViewShowFormFieldNameKey") {
  444. listView.showFormFieldName = UserDefaults.getDefaultBoolValue(forKey: "kPDFViewShowFormFieldNameKey")
  445. } else {
  446. listView.showFormFieldName = true
  447. }
  448. reloadPDFSplitInfo()
  449. }
  450. func updatePDFViewAnnotationMode() {
  451. let toolbarMode = viewManager.toolMode
  452. let subToolMode = viewManager.subToolMode
  453. listView.isHidden = false
  454. if toolbarMode == .None {
  455. listView.toolMode = .CTextToolMode
  456. listView.annotationType = .unkown
  457. } else if toolbarMode == .Markup {
  458. if subToolMode == .None {
  459. listView.toolMode = .CTextToolMode
  460. } else {
  461. listView.toolMode = .CNoteToolMode
  462. }
  463. //MARK: -Markup
  464. if subToolMode == .None {
  465. listView.annotationType = .unkown
  466. } else if subToolMode == .Highlight {
  467. listView.annotationType = .highlight
  468. } else if subToolMode == .Underline {
  469. listView.annotationType = .underline
  470. } else if subToolMode == .Waveline {
  471. listView.annotationType = .squiggly
  472. } else if subToolMode == .Strikethrough {
  473. listView.annotationType = .strikeOut
  474. } else if subToolMode == .Text {
  475. listView.annotationType = .freeText
  476. } else if subToolMode == .Note {
  477. listView.annotationType = .anchored
  478. } else if subToolMode == .Pen {
  479. listView.annotationType = .ink
  480. } else if subToolMode == .Eraser {
  481. listView.annotationType = .eraser
  482. } else if subToolMode == .Rectangle {
  483. listView.annotationType = .square
  484. } else if subToolMode == .Circle {
  485. listView.annotationType = .circle
  486. } else if subToolMode == .Arrow {
  487. listView.annotationType = .arrow
  488. } else if subToolMode == .Line {
  489. listView.annotationType = .line
  490. } else if subToolMode == .Measure {
  491. listView.annotationType = CPDFMeasureDefaultInfo.default_measureType()
  492. refreshMeasureInfo()
  493. } else if subToolMode == .Stamp {
  494. listView.annotationType = .stamp
  495. } else if subToolMode == .Sign {
  496. listView.annotationType = .signSignature
  497. }
  498. } else if toolbarMode == .Edit {
  499. //MARK: -编辑
  500. if subToolMode == .None {
  501. if listView.toolMode != .CEditPDFToolMode {
  502. listView.toolMode = .CEditPDFToolMode
  503. listView.configPDFEditingInfo()
  504. }
  505. let editingPDFLoadType: CEditingLoadType = listView.editingPDFLoadType()
  506. listView.setShouAddEdit([])
  507. listView.change([.text, .image])
  508. } else if subToolMode == .Edit_text {
  509. if listView.toolMode != .CEditPDFToolMode {
  510. listView.toolMode = .CEditPDFToolMode
  511. listView.configPDFEditingInfo()
  512. }
  513. listView.setShouAddEdit([.text])
  514. listView.change(.text)
  515. } else if subToolMode == .Edit_Image {
  516. if listView.toolMode != .CEditPDFToolMode {
  517. listView.toolMode = .CEditPDFToolMode
  518. listView.configPDFEditingInfo()
  519. }
  520. listView.setShouAddEdit([.image])
  521. listView.change(.image)
  522. } else if subToolMode == .Edit_Link {
  523. listView.toolMode = .CEditLinkToolMode
  524. listView.annotationType = .link
  525. } else if subToolMode == .Edit_Crop {
  526. listView.isHidden = true
  527. listView.toolMode = .CCropToolMode
  528. }
  529. if viewManager.editType == .watermark ||
  530. viewManager.editType == .background ||
  531. viewManager.editType == .header_Footer ||
  532. viewManager.editType == .bates {
  533. listView.isHidden = true
  534. listView.toolMode = .CTextToolMode
  535. }
  536. if subToolMode != .Edit_Crop {
  537. removeCropController()
  538. }
  539. self.showOCREditAlert()
  540. } else if toolbarMode == .Form {
  541. //MARK: -Form表单
  542. listView.toolMode = .CFormToolMode
  543. if subToolMode == .None {
  544. listView.annotationType = .unkown
  545. } else if subToolMode == .Form_text {
  546. listView.annotationType = .textField
  547. } else if subToolMode == .Form_checkbox {
  548. listView.annotationType = .checkBox
  549. } else if subToolMode == .Form_radio {
  550. listView.annotationType = .radioButton
  551. } else if subToolMode == .Form_list {
  552. listView.annotationType = .listMenu
  553. } else if subToolMode == .Form_dropdown {
  554. listView.annotationType = .comboBox
  555. } else if subToolMode == .Form_OK {
  556. listView.annotationType = .actionButton
  557. } else if subToolMode == .Form_digitalSign {
  558. listView.annotationType = .signature
  559. }
  560. } else if toolbarMode == .Fill {
  561. //MARK: -填充
  562. if subToolMode == .None {
  563. listView.toolMode = .CTextToolMode
  564. } else {
  565. listView.toolMode = .CNoteToolMode
  566. }
  567. if subToolMode == .None {
  568. } else if subToolMode == .Fill_tick {
  569. listView.annotationType = .signTure
  570. } else if subToolMode == .fill_fork {
  571. listView.annotationType = .signFalse
  572. } else if subToolMode == .fill_rectangle {
  573. listView.annotationType = .signCircle
  574. } else if subToolMode == .fill_line {
  575. listView.annotationType = .signLine
  576. } else if subToolMode == .fill_dot {
  577. listView.annotationType = .signDot
  578. } else if subToolMode == .fill_date {
  579. listView.annotationType = .signDate
  580. } else if subToolMode == .fill_sign {
  581. listView.annotationType = .signSignature
  582. }
  583. } else if toolbarMode == .Convert {
  584. //MARK: -转档
  585. listView.toolMode = .CTextToolMode
  586. } else if toolbarMode == .Protect {
  587. //MARK: -Protect
  588. listView.toolMode = .CTextToolMode
  589. if subToolMode == .Redact {
  590. //密文
  591. listView.annotationType = .redact
  592. listView.toolMode = .CRedactToolMode
  593. self.enterRedactAlert()
  594. } else if subToolMode == .Digital_Sign {
  595. //数字签名
  596. listView.annotationType = .digitalSign
  597. listView.toolMode = .CDigitalSignToolMode
  598. self.enterDigitalSignature()
  599. }
  600. } else if toolbarMode == .Tools {
  601. //MARK: -工具
  602. // if subToolMode == .Tool_OCR {
  603. // listView.toolMode = .COCRToolMode
  604. // } else {
  605. // listView.toolMode = .CTextToolMode
  606. // }
  607. }
  608. if (toolbarMode != .Edit) {
  609. self.closeOCREditAlert()
  610. }
  611. }
  612. func showOrHideNotes() {
  613. self.listView.hideNotes = !self.listView.hideNotes
  614. let items = [toolbarManager.highlightProperty, toolbarManager.UnderlineProperty,
  615. toolbarManager.wavelineProperty, toolbarManager.strikethroughProperty,
  616. toolbarManager.textProperty, toolbarManager.noteProperty,
  617. toolbarManager.penProperty, toolbarManager.eraserProperty,
  618. toolbarManager.rectangleProperty, toolbarManager.circleProperty,
  619. toolbarManager.arrowProperty, toolbarManager.lineProperty,
  620. toolbarManager.measureProperty, toolbarManager.stampProperty,
  621. toolbarManager.signProperty]
  622. for item in items {
  623. item.isDisabled = listView.hideNotes
  624. }
  625. pdfToolbarController?.resetSecondToolbar(forceRefresh: true)
  626. }
  627. //MARK: - SplitView
  628. func setUpSplitView() {
  629. infoContendSplitView.wantsLayer = true
  630. infoContendSplitView.layer?.backgroundColor = NSColor.clear.cgColor
  631. infoContendSplitView.delegate = self
  632. infoContendSplitView.layer?.masksToBounds = true
  633. }
  634. func setupSplitPDFController() {
  635. if splitPDFController == nil {
  636. splitPDFController = KMSplitPDFViewController.init()
  637. }
  638. splitPDFController?.view.frame = pdfSplitBottomView.bounds
  639. splitPDFController?.view.autoresizingMask = [.height, .width]
  640. splitPDFController?.viewManager = self.viewManager
  641. splitPDFController?.delegate = self
  642. splitPDFController?.pdfView?.pdfListViewDelegate = self
  643. splitPDFController?.reloadData()
  644. pdfSplitBottomView.addSubview(splitPDFController!.view)
  645. }
  646. //MARK: - 工具栏
  647. func initToolbar() {
  648. toolbarBox.borderWidth = 0
  649. if pdfToolbarController == nil {
  650. pdfToolbarController = KMPDFToolbarController.init()
  651. }
  652. pdfToolbarController?.view.frame = toolbarBox.bounds
  653. pdfToolbarController?.view.autoresizingMask = [.width, .height]
  654. pdfToolbarController?.delegate = self
  655. toolbarBox.contentView = pdfToolbarController?.view
  656. pdfToolbarController?.viewManager = viewManager
  657. pdfToolbarController?.toolbarManager = toolbarManager
  658. pdfToolbarController?.pdfView = listView
  659. pdfToolbarController?.setUpData()
  660. refreshToolbarViewHeightInfo()
  661. if listView.showFormFieldName {
  662. toolbarManager.form_ShowName_Property.righticon = NSImage(named: "tick_Green")
  663. } else {
  664. toolbarManager.form_ShowName_Property.righticon = nil
  665. }
  666. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.15) {
  667. self.pdfToolbarController?.clickWithIdentify(KMPDFToolbar_Markup_Identifier)
  668. }
  669. }
  670. func refreshToolbarViewHeightInfo() {
  671. let _viewManager = viewManager
  672. if viewManager.isPageEditMode {
  673. toolbarBoxHeightConst.constant = 80
  674. } else if viewManager.editType == .watermark ||
  675. viewManager.editType == .background ||
  676. viewManager.editType == .header_Footer ||
  677. viewManager.editType == .bates {
  678. toolbarBoxHeightConst.constant = 40
  679. } else if _viewManager.toolMode == .Markup ||
  680. _viewManager.toolMode == .Edit ||
  681. _viewManager.toolMode == .Form ||
  682. _viewManager.toolMode == .Fill ||
  683. _viewManager.toolMode == .Convert ||
  684. _viewManager.toolMode == .Protect ||
  685. _viewManager.toolMode == .Tools {
  686. toolbarBoxHeightConst.constant = 80
  687. if _viewManager.subToolMode == .Redact {
  688. toolbarBoxHeightConst.constant = 40
  689. }
  690. } else {
  691. toolbarBoxHeightConst.constant = 40
  692. }
  693. }
  694. func toolbarViewModeChanged() {
  695. updatePDFViewAnnotationMode()
  696. refreshToolbarRightViewInfo()
  697. if viewManager.toolMode != .Edit && viewManager.subToolMode != .Edit_Crop {
  698. removeCropController()
  699. }
  700. }
  701. func updatePDFViewToolsType(_ viewToolsType: KMPDFViewToolsType) {
  702. viewManager.viewToolsType = viewToolsType
  703. pdfToolbarController?.reloadToolsView()
  704. if let toolbarVC = pdfToolbarController {
  705. kmPDFToolbarControllerDidViewToolsChanged(toolbarVC)
  706. }
  707. }
  708. //MARK: - 右边属性栏
  709. func refreshToolbarRightViewInfo() {
  710. //统一刷新viewManager.showRightSide, 由这个参数控制右边属性边框的展开与隐藏
  711. if viewManager.showRightSide == true {
  712. toolbarManager.rightViewProperty.state = .pressed
  713. toggleOpenRightSide()
  714. rightSideController?.reloadDataWithPDFView(pdfView: listView)
  715. } else {
  716. toggleCloseRightSide()
  717. }
  718. if viewManager.showRightSide == true {
  719. toolbarManager.rightViewProperty.state = .pressed
  720. } else {
  721. toolbarManager.rightViewProperty.state = .normal
  722. }
  723. pdfToolbarController?.reloadRightToolsView()
  724. }
  725. //MARK: - 侧边工具栏
  726. func initSideBar() {
  727. sidebarBox.borderWidth = 0
  728. if sideBarController == nil {
  729. sideBarController = KMPDFSideBarController.init()
  730. }
  731. sideBarController?.view.frame = sidebarBox.bounds
  732. sideBarController?.view.autoresizingMask = [.width, .height]
  733. sidebarBox.contentView = sideBarController?.view
  734. sideBarController?.pdfView = listView
  735. sideBarController?.delegate = self
  736. sideBarController?.pdfViewManager = viewManager
  737. sideBarController?.reloadData()
  738. }
  739. func reloadSideBar() {
  740. sideBarController?.reloadData()
  741. if viewManager.editType == .none {
  742. sideBarController?.searchItem.isHidden = false
  743. sideBarController?.thumbnailItem.isHidden = false
  744. sideBarController?.outlineItem.isHidden = false
  745. sideBarController?.bookmarkItem.isHidden = false
  746. sideBarController?.annotationItem.isHidden = false
  747. sideBarController?.aiToolItem.isHidden = false
  748. } else {
  749. sideBarController?.searchItem.isHidden = true
  750. sideBarController?.thumbnailItem.isHidden = true
  751. sideBarController?.outlineItem.isHidden = true
  752. sideBarController?.bookmarkItem.isHidden = true
  753. sideBarController?.annotationItem.isHidden = true
  754. sideBarController?.aiToolItem.isHidden = true
  755. }
  756. }
  757. //MARK: - 左边侧边栏
  758. func initLeftSideController() {
  759. if botaViewController == nil {
  760. botaViewController = KMNLeftSideViewController(listView.document)
  761. }
  762. botaViewController?.leftSideViewDelegate = self
  763. botaViewController?.view.frame = infoSplitLeftView.bounds
  764. botaViewController?.view.autoresizingMask = [.width, .height]
  765. if botaViewController != nil {
  766. infoSplitLeftView?.addSubview(botaViewController!.view)
  767. }
  768. }
  769. private func leftSidePaneIsOpen() -> Bool {
  770. return !infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) //第一次点击时存在问题,待解决
  771. }
  772. func toggleOpenLeftSide(pdfSideBarType: KMPDFSidebarType) {
  773. if(leftSidePaneIsOpen() == false) {
  774. let leftWidthNumber = UserDefaults.standard.object(forKey: CPDFViewLeftSidePaneWidthKey) as? NSNumber ?? MIN_SIDE_PANE_WIDTH
  775. infoContendSplitView.setPosition(MIN_SIDE_PANE_WIDTH.doubleValue, ofDividerAt: 0) //暂时无法记录上一次打开的宽度
  776. }
  777. if pdfSideBarType == .search {
  778. botaViewController?.searchViewC.handdler.pdfView = listView
  779. botaViewController?.leftsideType = .search
  780. } else if pdfSideBarType == .thumbnail {
  781. botaViewController?.leftsideType = pdfSideBarType
  782. } else if pdfSideBarType == .outline {
  783. botaViewController?.outlineViewC.handdler.pdfView = listView
  784. botaViewController?.leftsideType = pdfSideBarType
  785. } else if pdfSideBarType == .bookmark {
  786. botaViewController?.bookmarkViewC.handdler.pdfView = listView
  787. botaViewController?.leftsideType = pdfSideBarType
  788. } else if pdfSideBarType == .annotation {
  789. botaViewController?.annoController.listView = listView
  790. botaViewController?.leftsideType = .annotation
  791. } else if pdfSideBarType == .aiTools {
  792. botaViewController?.leftsideType = .aiTools
  793. }
  794. }
  795. func toggleCloseLeftSide() {
  796. if(leftSidePaneIsOpen() == true) {
  797. infoContendSplitView.setPosition(0, ofDividerAt: 0)
  798. }
  799. }
  800. //MARK: - 右侧属性栏
  801. func initRightSideController() {
  802. if rightSideController == nil {
  803. rightSideController = KMRightSideController.init()
  804. rightSideController?.delegate = self
  805. }
  806. rightSideController?.view.frame = CGRectMake(0, 0, MIN_SIDE_PANE_WIDTH.doubleValue, 680)
  807. rightSideController?.view.autoresizingMask = [.height, .maxXMargin]
  808. }
  809. func removeRightSideController() {
  810. rightSideController?.view.removeFromSuperview()
  811. rightSideController = nil
  812. }
  813. @objc func toggleOpenRightSide() -> Void {
  814. if rightSideController != nil {
  815. return
  816. }
  817. initRightSideController()
  818. rightSideController?.view.frame = infoSplitRightView.bounds
  819. rightSideController?.view.autoresizingMask = [.width, .height]
  820. infoSplitRightView.addSubview(rightSideController!.view)
  821. infoContendSplitView.setPosition(CGRectGetWidth(view.frame)-MIN_SIDE_PANE_WIDTH.doubleValue, ofDividerAt: 1)
  822. rightSideController?.viewManager = self.viewManager
  823. rightSideController?.reloadDataWithPDFView(pdfView: listView)
  824. }
  825. @objc func toggleCloseRightSide() -> Void {
  826. removeRightSideController()
  827. infoContendSplitView.setPosition(CGRectGetWidth(view.frame), ofDividerAt: 1)
  828. viewManager.showRightSide = false
  829. pdfToolbarController?.reloadRightToolsView()
  830. }
  831. func refreshRightSide() -> Void {
  832. if let rightVC = rightSideController, let _ = rightSideController?.view.superview {
  833. rightVC.reloadDataWithPDFView(pdfView: listView)
  834. }
  835. }
  836. //MARK: - PDFDisplayView
  837. func updatePDFDisplaySettingView() {
  838. if viewManager.showDisplayView {
  839. infoSplitViewLeftConst.constant = 0
  840. infoContendSplitView.setPosition(MIN_SIDE_PANE_WIDTH.doubleValue, ofDividerAt: 0)
  841. } else {
  842. if viewManager.isPDFReadMode == false {
  843. infoSplitViewLeftConst.constant = 44
  844. if viewManager.pdfSideBarType == .none {
  845. toggleCloseLeftSide()
  846. } else {
  847. toggleOpenLeftSide(pdfSideBarType: viewManager.pdfSideBarType)
  848. }
  849. } else {
  850. toggleCloseLeftSide()
  851. }
  852. }
  853. if viewManager.showDisplayView {
  854. if displaySettingController == nil {
  855. displaySettingController = KMNDisplayViewController.init()
  856. }
  857. displaySettingController?.view.frame = CGRectMake(0, 0, MIN_SIDE_PANE_WIDTH.doubleValue, CGRectGetHeight(bottomContendBox.frame))
  858. displaySettingController?.view.autoresizingMask = [.height, .width]
  859. infoSplitLeftView.addSubview(displaySettingController!.view)
  860. displaySettingController?.pdfView = self.listView
  861. displaySettingController?.viewManager = self.viewManager
  862. displaySettingController?.delegate = self
  863. displaySettingController?.reloadData()
  864. } else {
  865. displaySettingController?.view.removeFromSuperview()
  866. displaySettingController = nil
  867. }
  868. }
  869. //MARK: - 页面编辑
  870. func enterPageEditMode() {
  871. pageEditViewController = KMNPageEditViewController(self.document)
  872. if(pageEditViewController != nil) {
  873. bottomContendBox.addSubview(pageEditViewController!.view)
  874. pageEditViewController?.view.frame = bottomContendBox.bounds
  875. pageEditViewController?.thumbnailBaseViewDelegate = self
  876. pageEditViewController?.selectionIndexPaths = [IndexPath(item: listView.currentPageIndex, section: 0)]
  877. pageEditViewController?.view.autoresizingMask = [.width,.height]
  878. pageEditViewController?.currentUndoManager = listView.undoManager
  879. toolbarManager.page_pageInfo_Property.text = String(listView.currentPageIndex + 1)
  880. pdfToolbarController?.refreshSecondToolbarItemsState()
  881. listView.isHidden = true
  882. }
  883. }
  884. func exitPageEditMode() {
  885. if pageEditViewController != nil {
  886. pageEditViewController?.view.removeFromSuperview()
  887. pageEditViewController = nil
  888. }
  889. listView.isHidden = false
  890. if listView.document?.isModified() == true {
  891. listView.layoutDocumentView()
  892. botaViewController?.reloadData()
  893. }
  894. }
  895. //MARK: - 阅读模式
  896. func openPDFReadMode() {
  897. if viewManager.showDisplayView {
  898. UserDefaults.setDefaultBoolValue(true, toKey: "ShowDisplayViewWhenExitPDFReadMode")
  899. viewManager.showDisplayView = false
  900. pdfToolbarController?.reloadLeftIconView()
  901. updatePDFDisplaySettingView()
  902. }
  903. if infoSplitViewLeftConst.constant != 0 {
  904. infoSplitViewLeftConst.constant = 0
  905. updatePDFDisplaySettingView()
  906. }
  907. if viewManager.pdfSideBarType != .none {
  908. UserDefaults.setDefaultIntValue(viewManager.pdfSideBarType.rawValue, toKey: "viewManagerPdfSideBarTypeRawValue")
  909. viewManager.pdfSideBarType = .none
  910. }
  911. toolbarBoxHeightConst.constant = 0
  912. view.window?.makeFirstResponder(listView)
  913. self.componentMessageView.properties = ComponentMessageProperty(messageType: .normal_custom, title: KMLocalizedString("Read Mode On"))
  914. self.componentMessageView.frame = CGRectMake((CGRectGetWidth(self.view.frame) - self.componentMessageView.properties.propertyInfo.viewWidth)/2,
  915. CGRectGetHeight(self.view.frame) - self.componentMessageView.properties.propertyInfo.viewHeight - 8,
  916. self.componentMessageView.properties.propertyInfo.viewWidth,
  917. self.componentMessageView.properties.propertyInfo.viewHeight)
  918. self.componentMessageView.reloadData()
  919. self.componentMessageView.show(inView: self.view, autoHideSeconde: 2)
  920. setUpPDFPageNumberToolbar()
  921. }
  922. func exitPDFReadMode() {
  923. viewManager.isPDFReadMode = false
  924. if UserDefaults.getDefaultBoolValue(forKey: "ShowDisplayViewWhenExitPDFReadMode") == true {
  925. if viewManager.showDisplayView == false {
  926. viewManager.showDisplayView = true
  927. pdfToolbarController?.reloadLeftIconView()
  928. }
  929. UserDefaults.setDefaultBoolValue(false, toKey: "ShowDisplayViewWhenExitPDFReadMode")
  930. }
  931. if let index = UserDefaults.getDefaultIntValue(forKey: "viewManagerPdfSideBarTypeRawValue") {
  932. let type = KMPDFSidebarType(rawValue: index) ?? .none
  933. viewManager.pdfSideBarType = type
  934. UserDefaults.standard.removeObject(forKey: "viewManagerPdfSideBarTypeRawValue")
  935. UserDefaults.standard.synchronize()
  936. }
  937. updatePDFDisplaySettingView()
  938. refreshToolbarViewHeightInfo()
  939. reloadPDFPageNumberToolbar()
  940. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
  941. self.componentMessageView.properties = ComponentMessageProperty(messageType: .normal_custom, title: KMLocalizedString("Read Mode Off"))
  942. self.componentMessageView.frame = CGRectMake((CGRectGetWidth(self.infoSplitCenterView.frame) - self.componentMessageView.properties.propertyInfo.viewWidth)/2,
  943. CGRectGetHeight(self.infoSplitCenterView.frame) - self.componentMessageView.properties.propertyInfo.viewHeight - 8,
  944. self.componentMessageView.properties.propertyInfo.viewWidth,
  945. self.componentMessageView.properties.propertyInfo.viewHeight)
  946. self.componentMessageView.reloadData()
  947. self.componentMessageView.show(inView: self.infoSplitCenterView, autoHideSeconde: 2)
  948. }
  949. }
  950. //MARK: - PPT
  951. func togglePresentation(_ sender: Any?) {
  952. if self.canExitPresentation() {
  953. exitFullScreen()
  954. } else if self.canEnterPresentation() {
  955. NotificationCenter.default.addObserver(self, selector: #selector(willEnterInteractionModeNotification), name: NSWindow.willEnterInteractionModeNotification, object: nil)
  956. NotificationCenter.default.addObserver(self, selector: #selector(didEnterInteractionModeNotification), name: NSWindow.didEnterInteractionModeNotification, object: nil)
  957. NotificationCenter.default.addObserver(self, selector: #selector(willShowFullScreenNotification), name: NSWindow.willShowFullScreenNotification, object: nil)
  958. NotificationCenter.default.addObserver(self, selector: #selector(didShowFullScreenNotification), name: NSWindow.didShowFullScreenNotification, object: nil)
  959. view.window?.enterPresentation(provider: self)
  960. }
  961. }
  962. func enterPresentationMode() {
  963. let scrollView = listView.documentView().enclosingScrollView
  964. savedNormalSetup.setValue(scrollView?.hasHorizontalScroller, forKey: KMMainModel.Key.kHasHorizontalScroller)
  965. savedNormalSetup.setValue(scrollView?.hasVerticalScroller, forKey: KMMainModel.Key.kHasVerticalsCroller)
  966. savedNormalSetup.setValue(scrollView?.autohidesScrollers, forKey: KMMainModel.Key.kAutoHidesScrollers)
  967. listView.backgroundColor = NSColor.clear
  968. listView.setDisplay(.singlePage)
  969. listView.autoScales = true
  970. listView.displayBox = .cropBox
  971. listView.displaysPageBreaks = false
  972. scrollView?.autohidesScrollers = true
  973. scrollView?.hasHorizontalScroller = false
  974. scrollView?.hasVerticalScroller = false
  975. listView.setCurrentSelection(nil, animate: true)
  976. }
  977. func exitPresentationMode() {
  978. NotificationCenter.default.removeObserver(self, name: NSWindow.willEnterInteractionModeNotification, object: nil)
  979. NotificationCenter.default.removeObserver(self, name: NSWindow.didEnterInteractionModeNotification, object: nil)
  980. NotificationCenter.default.removeObserver(self, name: NSWindow.willShowFullScreenNotification, object: nil)
  981. NotificationCenter.default.removeObserver(self, name: NSWindow.willShowFullScreenNotification, object: nil)
  982. }
  983. func exitFullScreen() {
  984. if self.canExitPresentation() == true {
  985. let mainDocument = self.myDocument as? KMMainDocument
  986. let browserWindowController = mainDocument?.browser?.windowController as? KMBrowserWindowController
  987. browserWindowController?.exitFullscreen()
  988. }
  989. }
  990. func exitFullscreenMode() {
  991. if self.interactionMode == .presentation {
  992. self.exitPresentationMode()
  993. }
  994. self.applyPDFSettings(self.savedNormalSetup)
  995. self.savedNormalSetup.removeAllObjects()
  996. listView.layoutDocumentView()
  997. listView.requiresDisplay()
  998. if let backgroundColor = UserDefaults.standard.color(forKey: KMBackgroundColorKey) {
  999. listView.backgroundColor = backgroundColor
  1000. }
  1001. reloadPDFSplitInfo()
  1002. }
  1003. func applyPDFSettings(_ setup: NSDictionary) {
  1004. if let data = setup.object(forKey: KMMainModel.Key.kAutoScales) as? NSNumber {
  1005. self.listView.autoScales = data.boolValue
  1006. }
  1007. if self.listView.autoScales == false {
  1008. if let data = setup.object(forKey: KMMainModel.Key.kScaleFactor) as? NSNumber {
  1009. self.listView.scaleFactor = data.floatValue.cgFloat
  1010. }
  1011. }
  1012. if let data = setup.object(forKey: KMMainModel.Key.kDisplayMode) as? NSNumber {
  1013. self.listView.setDisplay(CPDFDisplayViewMode(rawValue: data.intValue) ?? .singlePage)
  1014. }
  1015. if let data = setup.object(forKey: KMMainModel.Key.kDisplaysAsBook) as? NSNumber {
  1016. self.listView.displaysAsBook = data.boolValue
  1017. }
  1018. if let data = setup.object(forKey: KMMainModel.Key.kDisplaysPageBreaks) as? NSNumber {
  1019. self.listView.displaysPageBreaks = data.boolValue
  1020. }
  1021. if let data = setup.object(forKey: KMMainModel.Key.kDisplayBox) as? NSNumber {
  1022. }
  1023. self.listView.layoutDocumentView()
  1024. }
  1025. func currentPDFSettings() -> NSDictionary {
  1026. let setup = NSMutableDictionary()
  1027. setup[KMMainModel.Key.kDisplaysPageBreaks] = NSNumber(value: listView.displaysPageBreaks)
  1028. setup[KMMainModel.Key.kDisplaysAsBook] = NSNumber(value: listView.displaysAsBook)
  1029. setup[KMMainModel.Key.kDisplayBox] = NSNumber(value: listView.displayBox.rawValue)
  1030. setup[KMMainModel.Key.kScaleFactor] = NSNumber(value: listView.scaleFactor)
  1031. setup[KMMainModel.Key.kAutoScales] = NSNumber(value: listView.autoScales)
  1032. setup[KMMainModel.Key.kDisplayMode] = NSNumber(value: listView.fetchDisplayViewMode().rawValue)
  1033. return setup
  1034. }
  1035. func canEnterFullscreen() -> Bool {
  1036. if (mwcFlags.isSwitchingFullScreen != 0) {
  1037. return false
  1038. }
  1039. if useNativeFullScreen() {
  1040. return interactionMode == .normal || interactionMode == .presentation
  1041. } else {
  1042. return !self.listView.document.isLocked && (interactionMode == .normal || interactionMode == .presentation) && self.view.window?.tabbedWindows?.count ?? 0 < 2
  1043. }
  1044. }
  1045. override func canEnterPresentation() -> Bool {
  1046. let can = super.canEnterPresentation()
  1047. if can == false {
  1048. return false
  1049. }
  1050. guard let doc = self.listView.document, doc.isLocked == false else {
  1051. return false
  1052. }
  1053. return can
  1054. }
  1055. func fadeOutFullScreenWindow() {
  1056. guard let fullScreenWindow = self.view.window as? KMFullScreenWindow else {
  1057. NSSound.beep()
  1058. return
  1059. }
  1060. let mainWindow = fullScreenWindow.interactionParent
  1061. let collectionBehavior = mainWindow?.collectionBehavior
  1062. mainWindow?.alphaValue = 0
  1063. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  1064. mainWindow?.animationBehavior = .none
  1065. }
  1066. // trick to make sure the main window shows up in the same space as the fullscreen window
  1067. fullScreenWindow.addChildWindow(mainWindow!, ordered: .below)
  1068. fullScreenWindow.removeChildWindow(mainWindow!)
  1069. fullScreenWindow.level = .popUpMenu
  1070. // these can change due to the child window trick
  1071. mainWindow?.level = .normal
  1072. mainWindow?.alphaValue = 1.0
  1073. mainWindow?.collectionBehavior = collectionBehavior!
  1074. mainWindow?.display()
  1075. mainWindow?.makeFirstResponder(self.listView)
  1076. mainWindow?.recalculateKeyViewLoop()
  1077. // mainWindow?.delegate = self
  1078. mainWindow?.makeKey()
  1079. if let data = mainWindow?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  1080. mainWindow?.animationBehavior = .default
  1081. }
  1082. NSApp.removeWindowsItem(fullScreenWindow)
  1083. fullScreenWindow.fadeOut()
  1084. }
  1085. //MARK: - PDF分屏视图
  1086. func reloadPDFSplitInfo() {
  1087. if listView.viewSplitMode == .disable {
  1088. pdfSplitView.isHidden = true
  1089. listView.frame = infoSplitCenterSubView.bounds
  1090. infoSplitCenterSubView.addSubview(listView)
  1091. if splitPDFController != nil {
  1092. splitPDFController = nil
  1093. }
  1094. } else {
  1095. pdfSplitView.isHidden = false
  1096. listView.frame = pdfSplitTopView.bounds
  1097. pdfSplitTopView.addSubview(listView)
  1098. setUpPDFPageNumberToolbar()
  1099. setupSplitPDFController()
  1100. if listView.viewSplitMode == .horizontal {
  1101. pdfSplitView.isVertical = false
  1102. } else if listView.viewSplitMode == .vertical {
  1103. pdfSplitView.isVertical = true
  1104. }
  1105. }
  1106. }
  1107. func setUpPDFPageNumberToolbar() {
  1108. if pageNumberToolbar != nil {
  1109. pageNumberToolbar?.removeFromSuperview()
  1110. pageNumberToolbar = nil
  1111. }
  1112. if pageNumberToolbar == nil {
  1113. pageNumberToolbar = KMPageNumberPromptView.init()
  1114. }
  1115. pageNumberToolbar?.frame = CGRectMake(CGRectGetWidth(listView.frame)/2-144, 20, 288, 40)
  1116. pageNumberToolbar?.autoresizingMask = [.minXMargin, .maxXMargin, .maxYMargin]
  1117. pageNumberToolbar?.pdfView = self.listView
  1118. pageNumberToolbar?.reloadData()
  1119. pageNumberToolbar?.isHidden = true
  1120. listView.addSubview(pageNumberToolbar!)
  1121. reloadPDFPageNumberToolbar()
  1122. }
  1123. func reloadPDFPageNumberToolbar() {
  1124. if viewManager.isPDFReadMode == true ||
  1125. (viewManager.splitShowBottomBar && listView.viewSplitMode != .disable) {
  1126. pageNumberToolbar?.isHidden = false
  1127. pageNumberToolbar?.reloadData()
  1128. } else {
  1129. pageNumberToolbar?.isHidden = true
  1130. }
  1131. }
  1132. //MARK: - Edit模式
  1133. func showEditToolbarView() {
  1134. if editToolbarView == nil {
  1135. editToolbarView = KMEditToolbarView()
  1136. }
  1137. editToolbarView?.frame = toolbarBox.bounds
  1138. editToolbarView?.delegate = self
  1139. editToolbarView?.autoresizingMask = [.width, .height]
  1140. toolbarBox.contentView = editToolbarView
  1141. reloadSideBar()
  1142. }
  1143. func exitEditToolbarView() {
  1144. viewManager.editType = .none
  1145. viewManager.subToolMode = .None
  1146. editToolbarView?.removeFromSuperview()
  1147. editToolbarView = nil
  1148. watermarkViewController?.view.removeFromSuperview()
  1149. watermarkViewController = nil
  1150. backgroundViewController?.view.removeFromSuperview()
  1151. backgroundViewController = nil
  1152. headerFooterViewController?.view.removeFromSuperview()
  1153. headerFooterViewController = nil
  1154. batesViewController?.view.removeFromSuperview()
  1155. batesViewController = nil
  1156. refreshToolbarViewHeightInfo()
  1157. toolbarBox.contentView = pdfToolbarController?.view
  1158. updatePDFViewAnnotationMode()
  1159. reloadSideBar()
  1160. }
  1161. func updateEditModeDocumentWhenPageChanged() {
  1162. if viewManager.editType == .watermark {
  1163. updateWatermarkDocument()
  1164. } else if viewManager.editType == .background {
  1165. updateBackgroundDocument()
  1166. } else if viewManager.editType == .header_Footer {
  1167. updateHeaderFooterDocument()
  1168. } else if viewManager.editType == .bates {
  1169. updateBatesDocument()
  1170. } else if viewManager.subToolMode == .Edit_Crop {
  1171. updateCropDocument()
  1172. }
  1173. }
  1174. //MARK: - 数字签名
  1175. func writeSignatureToWidget(_ widget: CPDFSignatureWidgetAnnotation, _ path: String, _ password: String, _ config: CPDFSignatureConfig, _ isLock: Bool) ->() {
  1176. let fileName = listView.document.documentURL?.lastPathComponent
  1177. let fileNameWithoutExtension = URL(fileURLWithPath: fileName!).deletingPathExtension().lastPathComponent
  1178. let outputSavePanel = NSSavePanel()
  1179. outputSavePanel.directoryURL = listView.document.documentURL.deletingLastPathComponent()
  1180. outputSavePanel.title = KMLocalizedString("", comment: "Save as PDF")
  1181. outputSavePanel.allowedFileTypes = ["pdf"]
  1182. outputSavePanel.nameFieldStringValue = fileNameWithoutExtension + "_" + KMLocalizedString("Signed", comment: "")
  1183. let result = outputSavePanel.runModal()
  1184. if result == .OK {
  1185. let contentArr = NSMutableArray()
  1186. var locationStr = ""
  1187. var reasonStr = KMLocalizedString("none", comment: "")
  1188. for item in config.contents {
  1189. if item.key == KMLocalizedString("Reason", comment: "") {
  1190. if item.value == KMLocalizedString("<your signing reason here>", comment: "") {
  1191. item.value = " " + KMLocalizedString("none", comment: "")
  1192. }
  1193. reasonStr = item.value
  1194. } else if item.key == KMLocalizedString("Location", comment: "") {
  1195. if item.value == KMLocalizedString("<your signing location here>", comment: "") {
  1196. item.value = " "
  1197. }
  1198. locationStr = item.value
  1199. }
  1200. contentArr.add(item)
  1201. }
  1202. config.contents = contentArr as? [CPDFSignatureConfigItem]
  1203. widget.setFieldName(widget.getValidName(inPage: widget.page))
  1204. widget.signAppearanceConfig(config)
  1205. let success = listView.document.writeSignature(to: outputSavePanel.url, withWidget: widget, pkcs12Cert: path, password: password, location: locationStr, reason: reasonStr, permissions: .forbidChange)
  1206. widget.removeSignature()
  1207. if success {
  1208. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  1209. NSDocumentController.shared.openDocument(withContentsOf: outputSavePanel.url!, display: true) { document, documentWasAlreadyOpen, error in
  1210. if error != nil {
  1211. NSApp.presentError(error!)
  1212. return
  1213. }
  1214. }
  1215. }
  1216. } else {
  1217. let alert = NSAlert.init()
  1218. alert.messageText = KMLocalizedString("Save failed!", comment: "")
  1219. alert.addButton(withTitle: KMLocalizedString("OK", comment: ""))
  1220. alert.runModal()
  1221. }
  1222. widget.page.removeAnnotation(widget)
  1223. listView.setNeedsDisplayAnnotationViewFor(widget.page)
  1224. } else {
  1225. widget.page.removeAnnotation(widget)
  1226. listView.setNeedsDisplayAnnotationViewFor(widget.page)
  1227. }
  1228. }
  1229. func popUpSignatureWidgetState(_ signature: CPDFSignature, _ pdfListView: CPDFListView) ->(){
  1230. signaturestateVC.signature = signature
  1231. signaturestateVC.pdfListView = pdfListView
  1232. signaturestateVC.actionBlock = { [weak self, weak signaturestateVC] stateVCSelf, actionType in
  1233. guard let weakSelf = self, let stateVC = signaturestateVC else { return }
  1234. if actionType == .cancel {
  1235. stateVC.dismiss(stateVCSelf)
  1236. } else if actionType == .confirm {
  1237. if let signer = signature.signers.first, let data = signer.certificates {
  1238. let signatureDetail = DSignatureDetailsViewController.init()
  1239. signatureDetail.certificates = data
  1240. signatureDetail.signature = signature
  1241. signatureDetail.pdfListView = pdfListView
  1242. stateVCSelf.presentAsSheet(signatureDetail)
  1243. } else {
  1244. NSSound.beep()
  1245. }
  1246. }
  1247. }
  1248. self.presentAsSheet(signaturestateVC)
  1249. signaturestateVC.reloadData()
  1250. }
  1251. func enterDigitalSignature() {
  1252. if UserDefaults.standard.object(forKey: "kDigitalSignature") != nil {
  1253. return
  1254. }
  1255. let alert = NSAlert()
  1256. alert.alertStyle = .informational
  1257. alert.messageText = KMLocalizedString("Using your mouse, click and drag to draw the area where you would like the signature to appear. Once you finish dragging out the desired area, you will be taken to the next step of the signing process. ")
  1258. alert.addButton(withTitle: KMLocalizedString("OK"))
  1259. alert.showsSuppressionButton = true
  1260. let response = alert.runModal()
  1261. if response.rawValue == 1000 {
  1262. if alert.suppressionButton?.state == .on {
  1263. UserDefaults.standard.set("YES", forKey: "kDigitalSignature")
  1264. UserDefaults.standard.synchronize()
  1265. }
  1266. }
  1267. }
  1268. // MARK: - 显示合并窗口
  1269. public func showMergeWindow(url: URL? = nil, _ password: String?) {
  1270. DispatchQueue.main.async {
  1271. var documentURL = url
  1272. if documentURL == nil {
  1273. documentURL = self.listView.document?.documentURL
  1274. }
  1275. guard let _url = documentURL else { return }
  1276. guard let document = PDFDocument(url: _url) else { return }
  1277. self.mergeWindowController = KMMergeWindowController(document: document, password: password ?? "")
  1278. self.mergeWindowController!.password = self.listView.document.password ?? ""
  1279. self.mergeWindowController!.oriDucumentUrl = self.listView.document?.documentURL
  1280. self.mergeWindowController!.pageIndex = self.listView.currentPageIndex
  1281. self.mergeWindowController!.cancelAction = { [unowned self] controller in
  1282. self.view.window?.endSheet(mergeWindowController!.window!)
  1283. }
  1284. self.mergeWindowController!.mergeAction = { [unowned self] controller, filePath in
  1285. self.view.window?.endSheet(mergeWindowController!.window!)
  1286. }
  1287. self.view.window?.beginSheet(self.mergeWindowController!.window!)
  1288. }
  1289. }
  1290. //MARK: - Crop裁剪
  1291. func showCropController() {
  1292. if cropController == nil {
  1293. cropController = KMCropController.init()
  1294. cropController?.view.frame = infoSplitCenterView.bounds
  1295. cropController?.view.autoresizingMask = [.width, .height]
  1296. cropController?.delegate = self
  1297. infoSplitCenterView.addSubview(cropController!.view)
  1298. if viewManager.showRightSide == false {
  1299. viewManager.showRightSide = true
  1300. refreshToolbarRightViewInfo()
  1301. }
  1302. updateCropDocument()
  1303. }
  1304. }
  1305. func updateCropDocument() {
  1306. guard let controller = cropController else { return }
  1307. controller.pdfDocument = nil
  1308. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1309. let editDocument = CPDFDocument.init()
  1310. editDocument?.insertPageObject(page, at: 0)
  1311. if let editPage = editDocument?.page(at: 0) {
  1312. let mediaRect = editPage.bounds(for: .mediaBox)
  1313. editPage.setBounds(mediaRect, for: .cropBox)
  1314. controller.selectionRect = page?.bounds(for: .cropBox) ?? .zero
  1315. if let cropVC = rightSideController?.edit_cropController {
  1316. cropVC.pageCropBounds = page?.bounds(for: .cropBox) ?? .zero
  1317. cropVC.reloadData()
  1318. }
  1319. }
  1320. controller.pdfDocument = editDocument
  1321. controller.reloadData()
  1322. }
  1323. func removeCropController() {
  1324. if cropController != nil {
  1325. cropController?.view.removeFromSuperview()
  1326. cropController = nil
  1327. toolbarManager.edit_crop_Property.state = .normal
  1328. }
  1329. }
  1330. // MARK: - Secure 【安全】
  1331. public func hiddenSecureLimitTip() {
  1332. }
  1333. func savePageNumberIfNeed() {
  1334. let scaleFactor = self.listView.scaleFactor
  1335. if scaleFactor <= 0 {
  1336. return
  1337. }
  1338. if self.listView.document != nil {
  1339. KMPreferenceManager.shared.setPageNumber(self.listView.currentPageIndex, forKey: self.listView.document.documentURL.path)
  1340. KMPreferenceManager.shared.setPageScale(Float(scaleFactor), forKey: self.listView.document.documentURL.path)
  1341. }
  1342. }
  1343. // MARK: - 显示加密弹窗
  1344. public func showSecureWindow() {
  1345. guard let url = self.listView.document?.documentURL else {
  1346. return
  1347. }
  1348. self.securityWindowController = KMSecurityWindowController(windowNibName: "KMSecurityWindowController")
  1349. guard let securityWindowController = securityWindowController else { return }
  1350. securityWindowController.documentURL = self.listView.document?.documentURL
  1351. securityWindowController.batchAction = { [unowned self] controller, files in
  1352. self.view.window?.endSheet((securityWindowController.window)!)
  1353. // let batchWindowController = KMBatchOperateWindowController.sharedWindowController
  1354. // let batchOperateFile = KMBatchOperateFile(filePath: self.document?.documentURL.path ?? "", type: .AddPassword)
  1355. // batchWindowController.switchToOperateType(.AddPassword, files: [batchOperateFile])
  1356. // batchWindowController.window?.makeKeyAndOrderFront("")
  1357. self.showBatchWindow(type: .security, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  1358. }
  1359. securityWindowController.doneAction = { [unowned self] controller, options, attribute in
  1360. let openPanel = NSOpenPanel()
  1361. openPanel.canChooseFiles = false
  1362. openPanel.canChooseDirectories = true
  1363. openPanel.canCreateDirectories = true
  1364. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { (result) in
  1365. if result == NSApplication.ModalResponse.OK {
  1366. for fileURL in openPanel.urls {
  1367. let document = CPDFDocument(url: self.document?.documentURL)
  1368. if document != nil {
  1369. document!.setDocumentAttributes(attribute)
  1370. let path = fileURL.path.stringByAppendingPathComponent(url.deletingPathExtension().lastPathComponent) + "_SetPassword" + "." + url.pathExtension
  1371. let success = document!.write(to: NSURL(fileURLWithPath: path) as URL, withOptions: options)
  1372. if success {
  1373. self.view.window?.endSheet((securityWindowController.window)!)
  1374. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)])
  1375. }
  1376. }
  1377. }
  1378. }
  1379. }
  1380. }
  1381. securityWindowController.cancelAction = { [unowned self] controller in
  1382. self.view.window?.endSheet((securityWindowController.window)!)
  1383. }
  1384. NSWindow.currentWindow().beginSheet(securityWindowController.window!)
  1385. }
  1386. // MARK: - 移除文档密码
  1387. public func showRemoveSecureWindow() {
  1388. var isDocumentLocked: Bool = false
  1389. if self.document?.allowsCopying == false || self.document?.allowsPrinting == false {
  1390. isDocumentLocked = true
  1391. } else if (self.document?.password ?? "").count > 0 {
  1392. isDocumentLocked = true
  1393. }
  1394. if isDocumentLocked == false {
  1395. let alert = NSAlert()
  1396. alert.alertStyle = .warning
  1397. alert.messageText = KMLocalizedString("This document doesn’t contain any security settings and doesn‘t need to be removed.")
  1398. alert.addButton(withTitle: KMLocalizedString("OK"))
  1399. alert.beginSheetModal(for: NSWindow.currentWindow()) { returnCode in
  1400. }
  1401. } else {
  1402. let controller = KMRemovePasswordWindowController(windowNibName: "KMRemovePasswordWindowController")
  1403. controller.pdfDocument = self.document
  1404. self.currentWindowController = controller
  1405. controller.batchAction = { [unowned self] controller, files in
  1406. self.view.window?.endSheet((self.currentWindowController.window)!)
  1407. self.currentWindowController = nil
  1408. var array: [URL] = []
  1409. for item in files {
  1410. array.append(NSURL(fileURLWithPath: item.filePath) as URL)
  1411. }
  1412. self.showBatchWindow(type: .batchRemove, files: array)
  1413. }
  1414. controller.cancelAction = { [unowned self] controller in
  1415. self.view.window?.endSheet((self.currentWindowController.window)!)
  1416. self.currentWindowController = nil
  1417. }
  1418. controller.doneAction = { [unowned self] controller in
  1419. self.view.window?.endSheet((self.currentWindowController.window)!)
  1420. self.currentWindowController = nil
  1421. NSWindowController.checkPassword(url: self.document!.documentURL!, type: .owner, password: self.document?.password ?? "") { [unowned self] success, resultPassword in
  1422. if success {
  1423. let savePanel = NSSavePanel()
  1424. savePanel.nameFieldStringValue = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_RemovePassword"
  1425. savePanel.allowedFileTypes = ["pdf"]
  1426. savePanel.beginSheetModal(for: NSApp.mainWindow!) {[unowned self] result in
  1427. guard result == .OK else { return }
  1428. /// 删除安全性设置
  1429. if (!self.listView.document!.allowsCopying || !self.listView.document!.allowsPrinting) {
  1430. self.model.isSaveKeyChain = false
  1431. self.listView.document.unlock(withPassword: resultPassword)
  1432. }
  1433. let document = CPDFDocument.init(url: self.listView.document.documentURL)
  1434. guard let document = document else { return }
  1435. document.unlock(withPassword: resultPassword)
  1436. let success = document.writeDecrypt(to: savePanel.url)
  1437. if success {
  1438. self.hiddenSecureLimitTip()
  1439. NSWorkspace.shared.activateFileViewerSelecting([savePanel.url!])
  1440. } else {
  1441. self.hiddenSecureLimitTip()
  1442. }
  1443. }
  1444. }
  1445. }
  1446. }
  1447. NSWindow.currentWindow().beginSheet(controller.window!)
  1448. }
  1449. }
  1450. //MARK: - Unlock Document
  1451. func unlockPDFDocument() {
  1452. NSWindowController.checkPassword(url: self.document!.documentURL!, type: .owner, password: self.document?.password ?? "") { [unowned self] success, resultPassword in
  1453. self.listView.document.unlock(withPassword: resultPassword)
  1454. }
  1455. }
  1456. //MARK: - Watermark水印
  1457. func showWatermarkController() {
  1458. viewManager.editType = .watermark
  1459. showEditToolbarView()
  1460. editToolbarView?.editType = .watermark
  1461. if KMWatermarkManager.defaultManager.watermarks.count == 0 {
  1462. editToolbarView?.editSubType = .add
  1463. } else {
  1464. editToolbarView?.editSubType = .template
  1465. }
  1466. editToolbarView?.reloadData()
  1467. if watermarkViewController == nil {
  1468. watermarkViewController = KMWatermarkController.init()
  1469. }
  1470. watermarkViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1471. watermarkViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1472. watermarkViewController?.delegate = self
  1473. bottomContendBox.contentView?.addSubview(watermarkViewController!.view)
  1474. watermarkViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1475. watermarkViewController?.reloadData()
  1476. updateWatermarkDocument()
  1477. watermarkViewController?.loadData()
  1478. }
  1479. func updateWatermarkDocument() {
  1480. guard let controller = watermarkViewController else { return }
  1481. var editDocument = CPDFDocument.init()
  1482. if let vcDoc = controller.pdfDocument {
  1483. editDocument = vcDoc
  1484. }
  1485. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1486. editDocument?.insertPageObject(page, at: 0)
  1487. if editDocument?.pageCount == 2 {
  1488. let theIndex = IndexSet(integer: 1)
  1489. editDocument?.removePage(at: theIndex)
  1490. }
  1491. if watermarkViewController?.pdfDocument == nil {
  1492. watermarkViewController?.pdfDocument = editDocument
  1493. }
  1494. watermarkViewController?.resetUI()
  1495. watermarkViewController?.reloadData()
  1496. }
  1497. //移除文档水印
  1498. func removePDFWatermark() {
  1499. let watermarks = self.listView.document.watermarks()
  1500. if (watermarks == nil || watermarks!.count <= 0) {
  1501. let alert = NSAlert()
  1502. alert.alertStyle = .warning
  1503. alert.messageText = KMLocalizedString("Could not find a removable watermark in this document. If you see a watermark, it was not added with PDF Reader Pro and therefore cannot be detected.", comment: "")
  1504. alert.addButton(withTitle: KMLocalizedString("Confirm", comment: ""))
  1505. alert.runModal()
  1506. return
  1507. }
  1508. let alert = NSAlert()
  1509. alert.alertStyle = .warning
  1510. alert.messageText = KMLocalizedString("Are you sure you want to remove the watermark?", comment: "")
  1511. alert.addButton(withTitle: KMLocalizedString("Delete", comment: ""))
  1512. alert.addButton(withTitle: KMLocalizedString("Cancel", comment: ""))
  1513. let result = alert.runModal()
  1514. if (result == .alertFirstButtonReturn) {
  1515. for watermark in watermarks! {
  1516. listView.document.removeWatermark(watermark)
  1517. }
  1518. listView.layoutDocumentView()
  1519. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Watermark removed"),
  1520. type: .success,
  1521. fromView: bottomContendBox,
  1522. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1523. }
  1524. }
  1525. func batchAddWatermark() {
  1526. self.showBatchWindow(type: .watermark, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  1527. }
  1528. func batchRemoveWatermark() {
  1529. self.showBatchWindow(type: .batchRemove, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  1530. }
  1531. //MARK: - PopUI
  1532. func reloadPopUIWindow() {
  1533. if listView.toolMode == .CSelectToolMode {
  1534. let pageRect = listView.currentSelectionRect()
  1535. let page:CPDFPage? = listView.currentSelectionPage()
  1536. if listView.selectionRect != CGRectZero && page != nil {
  1537. let positioningRect = listView.convert(pageRect, from: page)
  1538. if (CGRectIntersectsRect(positioningRect, listView.frame)) {
  1539. reloadPopUIOperation()
  1540. return
  1541. }
  1542. }
  1543. toggleClosePopUIWindow()
  1544. } else if listView.toolMode == .COCRToolMode {
  1545. let pageRect = listView.currentSelectionRect()
  1546. let page:CPDFPage? = listView.currentSelectionPage()
  1547. if listView.selectionRect != CGRectZero && page != nil {
  1548. let positioningRect = listView.convert(pageRect, from: page)
  1549. if (CGRectIntersectsRect(positioningRect, listView.frame)) {
  1550. reloadPopUIOperation()
  1551. return
  1552. }
  1553. }
  1554. toggleClosePopUIWindow()
  1555. } else if(listView.isEditing() == false) {
  1556. let activeAnnotations:[CPDFAnnotation] = listView.activeAnnotations as! [CPDFAnnotation]
  1557. if(activeAnnotations.count > 0) {
  1558. if let page = activeAnnotations.first?.page {
  1559. let pageRect = listView.selectionMultipleBounds(with: activeAnnotations)
  1560. let positioningRect = listView.convert(pageRect, from: page)
  1561. if (CGRectIntersectsRect(positioningRect, listView.frame)) {
  1562. reloadPopUIActiveAnnotations(activeAnnotations: activeAnnotations)
  1563. } else {
  1564. toggleClosePopUIWindow()
  1565. }
  1566. } else {
  1567. toggleClosePopUIWindow()
  1568. }
  1569. } else {
  1570. toggleClosePopUIWindow()
  1571. }
  1572. if(listView.popOver?.isShown == true || (groupListMenuGroup?.superview) != nil) { //右键菜单弹出时,或者Pop编辑框弹出时不显示Pop
  1573. toggleClosePopUIWindow()
  1574. }
  1575. } else {
  1576. let editAreas:[CPDFEditArea] = listView.km_EditingAreas()
  1577. if(editAreas.count > 0) {
  1578. if let page = editAreas.first?.page {
  1579. let pageRect = listView.selectionMultipleBounds(withEditArea: editAreas)
  1580. let positioningRect = listView.convert(pageRect, from: page)
  1581. if (CGRectIntersectsRect(positioningRect, listView.frame)) {
  1582. reloadPopUIContentEdits(editAreas: editAreas)
  1583. } else {
  1584. toggleClosePopUIWindow()
  1585. }
  1586. } else {
  1587. toggleClosePopUIWindow()
  1588. }
  1589. } else {
  1590. toggleClosePopUIWindow()
  1591. }
  1592. if(listView.popOver?.isShown == true || (groupListMenuGroup?.superview) != nil) { //右键菜单弹出时,或者Pop编辑框弹出时不显示Pop
  1593. toggleClosePopUIWindow()
  1594. }
  1595. }
  1596. }
  1597. func toggleClosePopUIWindow() {
  1598. let popWindow = KMNAnnotationPopToolbarWindow.shared
  1599. if popWindow.isVisible == true {
  1600. closeAnnotationPopWindow()
  1601. }
  1602. let editPopWindow = KMNContentEditPopToolbarWindow.shared
  1603. if editPopWindow.isVisible == true {
  1604. closePopContentEditWindow()
  1605. }
  1606. let operationWindow = KMNOperationPopToolbarWindow.shared
  1607. if operationWindow.isVisible == true {
  1608. closePopOperationWindow()
  1609. }
  1610. }
  1611. func closeAnnotationPopWindow() {
  1612. KMNAnnotationPopToolbarWindow.shared.closeWindow(listView: listView)
  1613. }
  1614. func closePopContentEditWindow() {
  1615. KMNContentEditPopToolbarWindow.shared.closeWindow(listView: listView)
  1616. }
  1617. func closePopOperationWindow() {
  1618. KMNOperationPopToolbarWindow.shared.closeWindow(listView: listView)
  1619. }
  1620. func reloadPopUIOperation() {
  1621. if listView.selectionRect != CGRectZero {
  1622. let popWindow = KMNOperationPopToolbarWindow.shared
  1623. popWindow.show(relativeTo: CGRectZero, of: self.listView, preferredEdge: .maxY)
  1624. self.listView.window?.addChildWindow(popWindow, ordered: .above)
  1625. let operationViewController = KMNPopOperationViewController.shared
  1626. operationViewController.listView = listView
  1627. if listView.toolMode == .CSelectToolMode {
  1628. operationViewController.popType = .crop
  1629. operationViewController.cropCurrentCallback = {[weak self] in
  1630. let rect = self?.listView.currentSelectionRect() ?? CGRect.zero
  1631. let orgPage : CPDFPage = self?.listView.currentSelectionPage() ?? CPDFPage()
  1632. self?.cropPages(atIndexs: [orgPage.pageIndex()], to: [rect])
  1633. self?.closePopOperationWindow()
  1634. }
  1635. } else if listView.toolMode == .COCRToolMode {
  1636. operationViewController.popType = .ocr
  1637. operationViewController.OCRAction = { [weak self] in
  1638. self?.convertSelectionRectOCR(rect: self?.listView.currentSelectionRect() ?? CGRectZero)
  1639. }
  1640. }
  1641. operationViewController.updatePDFViewCallback = {[weak self] in
  1642. self?.closePopOperationWindow()
  1643. self?.listView.setNeedsDisplayForVisiblePages()
  1644. }
  1645. updatePopOperationPopWinodwFrame()
  1646. } else {
  1647. closePopOperationWindow()
  1648. }
  1649. }
  1650. func reloadPopUIActiveAnnotations(activeAnnotations:[CPDFAnnotation]) {
  1651. let annotationMode = KMNAnnotationPopMode(pdfAnnotations: activeAnnotations )
  1652. let popVC = KMNPopAnnotationViewController.shared
  1653. if annotationMode.popType == .popTypeNone || !SettingsManager.sharedInstance.showQuickActionBar {
  1654. closeAnnotationPopWindow()
  1655. } else {
  1656. let win = KMNAnnotationPopToolbarWindow.shared
  1657. win.annotationPopMode = annotationMode
  1658. win.show(relativeTo: CGRectZero, of: self.listView, preferredEdge: .maxY)
  1659. self.listView.window?.addChildWindow(win, ordered: .above)
  1660. popVC.listView = listView
  1661. popVC.annotationPopMode = annotationMode
  1662. popVC.isOpenPane = viewManager.showRightSide
  1663. updateAnnotationsPopWinodwFrame()
  1664. popVC.updatePDFViewCallback = {[weak self] in
  1665. self?.rightSideController?.reloadDataWithPDFView(pdfView: self?.listView ?? CPDFListView())
  1666. self?.listView.setNeedsDisplayMultiAnnotations(annotationMode.annotations)
  1667. NotificationCenter.default.post(name: toolbarImageColorChangedNotificationName, object: nil)
  1668. }
  1669. popVC.paneCallback = {[weak self] isOpen in
  1670. if isOpen == true {
  1671. self?.viewManager.showRightSide = true
  1672. } else {
  1673. self?.viewManager.showRightSide = false
  1674. }
  1675. self?.refreshToolbarRightViewInfo()
  1676. }
  1677. }
  1678. }
  1679. func reloadPopUIContentEdits(editAreas:[CPDFEditArea]) {
  1680. let editingAreas = listView.km_EditingAreas()
  1681. let editMode = KMNEditContentPopMode(currentEditAreas: editingAreas)
  1682. let popVC = KMNPopContentEditViewController.shared
  1683. if editMode.popType == .editNone || !SettingsManager.sharedInstance.showQuickActionBar {
  1684. closePopContentEditWindow()
  1685. } else {
  1686. let win = KMNContentEditPopToolbarWindow.shared
  1687. win.editContentPopMode = editMode
  1688. win.show(relativeTo: CGRectZero, of: self.listView, preferredEdge: .maxY)
  1689. self.listView.window?.addChildWindow(win, ordered: .above)
  1690. popVC.listView = listView
  1691. popVC.editContentPopMode = editMode
  1692. popVC.isOpenPane = viewManager.showRightSide
  1693. popVC.editContentPopMode = editMode
  1694. updateContentEditPopWinodwFrame()
  1695. popVC.paneCallback = {[weak self] isOpen in
  1696. if isOpen == true && self?.viewManager.showRightSide == false {
  1697. self?.viewManager.showRightSide = false
  1698. } else {
  1699. self?.viewManager.showRightSide = true
  1700. }
  1701. self?.refreshToolbarRightViewInfo()
  1702. }
  1703. popVC.updatePDFViewCallback = { [weak self] in
  1704. self?.rightSideController?.reloadEditingAreas()
  1705. }
  1706. }
  1707. }
  1708. func updateAnnotationsPopWinodwFrame() {
  1709. let popWindow = KMNAnnotationPopToolbarWindow.shared
  1710. popWindow.updateFrame(listView: listView)
  1711. }
  1712. func updateContentEditPopWinodwFrame() {
  1713. let popWindow = KMNContentEditPopToolbarWindow.shared
  1714. popWindow.updateFrame(listView: listView)
  1715. }
  1716. func updatePopOperationPopWinodwFrame() {
  1717. let popWindow = KMNOperationPopToolbarWindow.shared
  1718. popWindow.updateFrame(listView: listView,page: listView.currentSelectionPage())
  1719. }
  1720. //MARK: - 安全
  1721. func removeOwnerPassword() {
  1722. guard let doc = listView.document else {
  1723. NSSound.beep()
  1724. return
  1725. }
  1726. if doc.permissionsStatus != .user {
  1727. NSSound.beep()
  1728. return
  1729. }
  1730. _ = KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: doc.documentURL) { [weak self] result, password in
  1731. if result == .cancel { /// 关闭
  1732. return
  1733. }
  1734. self?.listView.document?.unlock(withPassword: password)
  1735. if doc.permissionsStatus == .owner {
  1736. self?.alertTipViewController.reloadSecureAlertUI()
  1737. self?.alertTipViewController.reloadAlertUIFrame()
  1738. }
  1739. }
  1740. }
  1741. //MARK: - Background背景
  1742. func showBackgroundController() {
  1743. viewManager.editType = .background
  1744. showEditToolbarView()
  1745. editToolbarView?.editType = .background
  1746. if KMBackgroundManager.defaultManager.datas.count == 0 {
  1747. editToolbarView?.editSubType = .add
  1748. } else {
  1749. editToolbarView?.editSubType = .template
  1750. }
  1751. editToolbarView?.reloadData()
  1752. if backgroundViewController == nil {
  1753. backgroundViewController = KMBackgroundController.init()
  1754. }
  1755. backgroundViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1756. backgroundViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1757. backgroundViewController?.delegate = self
  1758. bottomContendBox.contentView?.addSubview(backgroundViewController!.view)
  1759. backgroundViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1760. backgroundViewController?.resetUI()
  1761. updateBackgroundDocument()
  1762. }
  1763. func updateBackgroundDocument() {
  1764. guard let controller = backgroundViewController else { return }
  1765. controller.pdfDocument = nil
  1766. let editDocument = CPDFDocument.init()
  1767. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1768. editDocument?.insertPageObject(page, at: 0)
  1769. backgroundViewController?.pdfDocument = editDocument
  1770. backgroundViewController?.reloadData()
  1771. }
  1772. func removePDFBackground() {
  1773. let alert = NSAlert()
  1774. alert.alertStyle = .warning
  1775. alert.messageText = KMLocalizedString("Are you sure you want to remove the background?", comment: "")
  1776. alert.addButton(withTitle: KMLocalizedString("Delete", comment: ""))
  1777. alert.addButton(withTitle: KMLocalizedString("Cancel", comment: ""))
  1778. let result = alert.runModal()
  1779. if (result == .alertFirstButtonReturn) {
  1780. let background = listView.document.background()
  1781. background?.clear()
  1782. listView.document?.refreshPageData()
  1783. listView.layoutDocumentView()
  1784. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Background removed"),
  1785. type: .success,
  1786. fromView: bottomContendBox,
  1787. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1788. }
  1789. }
  1790. func batchAddBackground() {
  1791. self.showBatchWindow(type: .background, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  1792. }
  1793. func batchRemoveBackground() {
  1794. self.showBatchWindow(type: .batchRemove, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  1795. }
  1796. //MARK: - header&footer
  1797. func showHeaderFooterController() {
  1798. viewManager.editType = .header_Footer
  1799. showEditToolbarView()
  1800. editToolbarView?.editType = .header_Footer
  1801. if KMHeaderFooterManager.defaultManager.headFooterObjects.count == 0 {
  1802. editToolbarView?.editSubType = .add
  1803. } else {
  1804. editToolbarView?.editSubType = .template
  1805. }
  1806. editToolbarView?.reloadData()
  1807. if headerFooterViewController == nil {
  1808. headerFooterViewController = KMHeaderFooterController.init()
  1809. }
  1810. headerFooterViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1811. headerFooterViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1812. headerFooterViewController?.totalPDFCount = Int(listView.document.pageCount)
  1813. headerFooterViewController?.delegate = self
  1814. bottomContendBox.contentView?.addSubview(headerFooterViewController!.view)
  1815. headerFooterViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1816. updateHeaderFooterDocument()
  1817. }
  1818. func updateHeaderFooterDocument() {
  1819. guard let controller = headerFooterViewController else { return }
  1820. controller.pdfDocument = nil
  1821. let editDocument = CPDFDocument.init()
  1822. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1823. editDocument?.insertPageObject(page, at: 0)
  1824. headerFooterViewController?.totalPDFCount = Int(listView.document.pageCount)
  1825. headerFooterViewController?.pdfDocument = editDocument
  1826. headerFooterViewController?.resetUI()
  1827. headerFooterViewController?.reloadData()
  1828. }
  1829. func removeHeaderFooter() {
  1830. let alert = NSAlert()
  1831. alert.alertStyle = .warning
  1832. alert.messageText = KMLocalizedString("Are you sure you want to remove the Header & Footer?", comment: "")
  1833. alert.addButton(withTitle: KMLocalizedString("Delete", comment: ""))
  1834. alert.addButton(withTitle: KMLocalizedString("Cancel", comment: ""))
  1835. let result = alert.runModal()
  1836. if (result == .alertFirstButtonReturn) {
  1837. let headerFooter = listView.document.headerFooter()
  1838. headerFooter?.clear()
  1839. listView.document?.refreshPageData()
  1840. listView.layoutDocumentView()
  1841. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Header & Footer removed"),
  1842. type: .success,
  1843. fromView: bottomContendBox,
  1844. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1845. }
  1846. }
  1847. func batchAddHeaderFooter() {
  1848. self.showBatchWindow(type: .headerAndFooter, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  1849. }
  1850. func batchRemoveHeaderFooter() {
  1851. self.showBatchWindow(type: .batchRemove, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  1852. }
  1853. //MARK: - Bates
  1854. func showBatesController() {
  1855. viewManager.editType = .bates
  1856. showEditToolbarView()
  1857. editToolbarView?.editType = viewManager.editType
  1858. if KMBatesManager.defaultManager.datas.count == 0 {
  1859. editToolbarView?.editSubType = .add
  1860. } else {
  1861. editToolbarView?.editSubType = .template
  1862. }
  1863. editToolbarView?.reloadData()
  1864. if batesViewController == nil {
  1865. batesViewController = KMBatesController.init()
  1866. }
  1867. batesViewController?.view.frame = CGRectMake(44, 0, CGRectGetWidth(bottomContendBox.frame)-44, CGRectGetHeight(bottomContendBox.frame))
  1868. batesViewController?.view.autoresizingMask = [.maxXMargin, .width, .height]
  1869. batesViewController?.delegate = self
  1870. batesViewController?.totalPDFCount = Int(listView.document.pageCount)
  1871. bottomContendBox.contentView?.addSubview(batesViewController!.view)
  1872. batesViewController?.editSubType = editToolbarView?.editSubType ?? .template
  1873. updateBatesDocument()
  1874. batesViewController?.resetUI()
  1875. }
  1876. func updateBatesDocument() {
  1877. guard let controller = batesViewController else { return }
  1878. controller.pdfDocument = nil
  1879. let editDocument = CPDFDocument.init()
  1880. let page = listView.document.page(at: UInt(listView.currentPageIndex))
  1881. editDocument?.insertPageObject(page, at: 0)
  1882. batesViewController?.pdfDocument = editDocument
  1883. batesViewController?.reloadData()
  1884. }
  1885. func removePDFBates() {
  1886. let alert = NSAlert()
  1887. alert.alertStyle = .warning
  1888. alert.messageText = KMLocalizedString("Are you sure you want to remove the Bates?", comment: "")
  1889. alert.addButton(withTitle: KMLocalizedString("Delete", comment: ""))
  1890. alert.addButton(withTitle: KMLocalizedString("Cancel", comment: ""))
  1891. let result = alert.runModal()
  1892. if (result == .alertFirstButtonReturn) {
  1893. let bates = listView.document.bates()
  1894. bates?.clear()
  1895. listView.document?.refreshPageData()
  1896. listView.layoutDocumentView()
  1897. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Bates removed"),
  1898. type: .success,
  1899. fromView: bottomContendBox,
  1900. point:CGPointMake(CGRectGetWidth(bottomContendBox.frame)/2, CGRectGetHeight(bottomContendBox.frame)-28))
  1901. }
  1902. }
  1903. func batchAddBates() {
  1904. self.showBatchWindow(type: .batesNumber, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  1905. }
  1906. func batchRemoveBates() {
  1907. self.showBatchWindow(type: .batchRemove, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  1908. }
  1909. //MARK: - Crop Action
  1910. // 白边距,统一大小
  1911. @objc func auto_cropPagesWhiteMargin(_ pageIndexs: [UInt]) {
  1912. var size = NSZeroSize
  1913. for i in pageIndexs {
  1914. let page = self.listView.document.page(at: i)
  1915. let rect = KMCropTools.getPageForegroundBox(page!)
  1916. size.width = fmax(size.width, NSWidth(rect))
  1917. size.height = fmax(size.height, NSHeight(rect))
  1918. }
  1919. var rectArray: Array<NSRect> = []
  1920. for i in pageIndexs {
  1921. progressC?.increment(by: Double(i))
  1922. progressC?.doubleValue = Double(i)
  1923. let page = self.listView.document.page(at: i)
  1924. var rect = KMCropTools.getPageForegroundBox(page!)
  1925. let bounds: NSRect = (page?.bounds(for: .mediaBox))!
  1926. if (rect.minX - bounds.minX > bounds.maxX-rect.maxX) {
  1927. rect.origin.x = rect.maxX-size.width
  1928. }
  1929. rect.origin.y = rect.maxY-size.height
  1930. rect.size = size
  1931. if (NSWidth(rect) > NSWidth(bounds)) {
  1932. rect.size.width = NSWidth(bounds)
  1933. }
  1934. if (NSHeight(rect) > NSHeight(bounds)) {
  1935. rect.size.height = NSHeight(bounds)
  1936. }
  1937. if (NSMinX(rect) < NSMinX(bounds)) {
  1938. rect.origin.x = NSMinX(bounds)
  1939. } else if (NSMaxX(rect) > NSMaxX(bounds)) {
  1940. rect.origin.x = NSMaxX(bounds) - NSWidth(rect)
  1941. }
  1942. if (NSMinY(rect) < NSMinY(bounds)) {
  1943. rect.origin.y = NSMinY(bounds)
  1944. } else if (NSMaxY(rect) > NSMaxY(bounds)) {
  1945. rect.origin.y = NSMaxY(bounds) - NSHeight(rect)
  1946. }
  1947. rectArray.append(rect)
  1948. }
  1949. self.cropPages(atIndexs: pageIndexs, to: rectArray)
  1950. }
  1951. func cropPages(atIndexs pageIndexs: [UInt], to rects: Array<NSRect>) {
  1952. let currentPage = self.listView.currentPage()
  1953. let visibleRect: NSRect = self.listView.convert(self.listView.convert(self.listView.documentView().visibleRect, from: self.listView.documentView()), to: self.listView.currentPage())
  1954. var oldRectArray: Array<NSRect> = []
  1955. let rectCount = rects.count
  1956. for i in pageIndexs {
  1957. if let page = self.listView.document.page(at: i) {
  1958. let rect = NSIntersectionRect(rects[Int(i) % rectCount], (page.bounds(for: .mediaBox)))
  1959. let oldRect = page.bounds(for: .cropBox)
  1960. oldRectArray.append(oldRect)
  1961. page.setBounds(rect, for: .cropBox)
  1962. }
  1963. }
  1964. let undoManager = self.listView.undoManager
  1965. (undoManager?.prepare(withInvocationTarget: self) as AnyObject).cropPages(atIndexs: pageIndexs, to: oldRectArray)
  1966. /// 刷新预览视图
  1967. self.listView.layoutDocumentView()
  1968. self.listView.displayBox = .cropBox
  1969. self.listView.go(to: currentPage)
  1970. self.listView.go(to: visibleRect, on: currentPage)
  1971. }
  1972. //MARK: - 文件对比
  1973. func beginCompareAction(_ index: Int) {
  1974. let controller = KMCompareWindowController(windowNibName: "KMCompareWindowController")
  1975. self.currentWindowController = controller
  1976. controller.password = self.document?.password ?? ""
  1977. controller.filePath = (self.document?.documentURL.path)!
  1978. controller.cancelAction = { [unowned self] controller in
  1979. self.view.window?.endSheet((self.currentWindowController.window)!)
  1980. self.currentWindowController = nil
  1981. }
  1982. controller.contentComplete = { [unowned self] controller, pdfCompareContent, result, oldDocument, document in
  1983. self.view.window?.endSheet((self.currentWindowController.window)!)
  1984. self.currentWindowController = nil
  1985. self.openContentCompareVC(with: pdfCompareContent, results: result, oldDocument: oldDocument, document: document)
  1986. }
  1987. controller.coveringComplete = { [unowned self] controller, document in
  1988. self.view.window?.endSheet((self.currentWindowController.window)!)
  1989. self.currentWindowController = nil
  1990. self.openCoveringCompareVC(with: document)
  1991. }
  1992. if index == 1 {
  1993. controller.fileType = .content
  1994. } else {
  1995. controller.fileType = .coverting
  1996. }
  1997. NSWindow.currentWindow().beginSheet(controller.window!)
  1998. }
  1999. //文件对比
  2000. func openContentCompareVC(with pdfCompareContent: CPDFCompareContent?, results: [CPDFCompareResults], oldDocument: CPDFDocument, document: CPDFDocument) {
  2001. self.isCompareModel = true
  2002. let compareContentView = KMCompareContentView()
  2003. compareContentView.oldDocument = oldDocument
  2004. compareContentView.document = document
  2005. compareContentView.compareResults = results
  2006. compareContentView.saveHandle = { [unowned self] view in
  2007. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25) { [unowned self] in
  2008. let saveController = KMCompareSaveWindow(windowNibName: "KMCompareSaveWindow")
  2009. self.currentWindowController = saveController
  2010. saveController.cancelHandle = { [unowned self] controller in
  2011. self.view.window!.endSheet(controller.window!)
  2012. self.currentWindowController = nil
  2013. }
  2014. saveController.saveHandle = { [unowned self] controller, saveType in
  2015. let folderPath = controller.fileSaveFolderPath
  2016. if folderPath != nil {
  2017. if !FileManager.default.fileExists(atPath: folderPath) {
  2018. try? FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: true, attributes: nil)
  2019. }
  2020. #if VERSION_DMG
  2021. #else
  2022. let url = URL(fileURLWithPath: folderPath)
  2023. let fileAccess = AppSandboxFileAccess()
  2024. fileAccess?.persistPermissionURL(url)
  2025. if let bookmarkData = try?url.bookmarkData(options: [.withSecurityScope]) {
  2026. fileAccess?.bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, for: url)
  2027. let urlString = url.path
  2028. let _url = URL(fileURLWithPath: urlString)
  2029. fileAccess?.bookmarkPersistanceDelegate.setBookmarkData(bookmarkData, for: _url)
  2030. }
  2031. #endif
  2032. var savePath: String
  2033. switch saveType {
  2034. case 0:
  2035. let filePath = oldDocument.documentURL.path
  2036. let fileName = filePath.deletingPathExtension.lastPathComponent
  2037. savePath = "\(folderPath)/\(fileName)_compare\(filePath.extension)"
  2038. savePath = self.getValidFilePath(savePath)
  2039. oldDocument.write(to: URL(fileURLWithPath: savePath))
  2040. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: savePath)])
  2041. case 1:
  2042. let filePath = document.documentURL.path
  2043. let fileName = filePath.deletingPathExtension.lastPathComponent
  2044. savePath = "\(folderPath)/\(fileName)_compare\(filePath.extension)"
  2045. savePath = self.getValidFilePath(savePath)
  2046. document.write(to: URL(fileURLWithPath: savePath))
  2047. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: savePath)])
  2048. case 2:
  2049. let filePath = oldDocument.documentURL.path
  2050. let fileName = filePath.deletingPathExtension.lastPathComponent
  2051. savePath = "\(folderPath)/MergedCompareFile\(filePath.extension)"
  2052. savePath = self.getValidFilePath(savePath)
  2053. pdfCompareContent!.saveAsComparisonDocument(withFilePath: savePath)
  2054. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: savePath)])
  2055. default:
  2056. break
  2057. }
  2058. }
  2059. self.view.window!.endSheet(controller.window!)
  2060. self.currentWindowController = nil
  2061. }
  2062. NSWindow.currentWindow().beginSheet(saveController.window!)
  2063. }
  2064. }
  2065. compareContentView.closeHandle = { [unowned self] view in
  2066. self.isCompareModel = false
  2067. view.removeFromSuperview()
  2068. }
  2069. contendBox.addSubview(compareContentView)
  2070. compareContentView.frame = contendBox.bounds
  2071. compareContentView.autoresizingMask = [.width,.height]
  2072. }
  2073. func openCoveringCompareVC(with pdfDocument: CPDFDocument) {
  2074. self.isCompareModel = true
  2075. let coveringView = KMCompareCoveringView()
  2076. coveringView.pdfDocument = pdfDocument
  2077. coveringView.closeHandle = { [unowned self] view in
  2078. self.isCompareModel = false
  2079. view.removeFromSuperview()
  2080. }
  2081. coveringView.saveHandle = { [unowned self] view in
  2082. let savePanel = NSSavePanel()
  2083. savePanel.nameFieldStringValue = "untitled"
  2084. savePanel.allowedFileTypes = ["pdf"]
  2085. savePanel.beginSheetModal(for: NSWindow.currentWindow()) { result in
  2086. if result == .OK {
  2087. pdfDocument.write(to: savePanel.url!)
  2088. NSWorkspace.shared.activateFileViewerSelecting([savePanel.url!])
  2089. }
  2090. }
  2091. }
  2092. contendBox.addSubview(coveringView)
  2093. coveringView.frame = contendBox.bounds
  2094. coveringView.autoresizingMask = [.width,.height]
  2095. }
  2096. func getValidFilePath(_ oldPath: String) -> String {
  2097. let fileManager = FileManager.default
  2098. do {
  2099. let fileAttributes = try fileManager.attributesOfItem(atPath: oldPath)
  2100. guard let fileType = fileAttributes[FileAttributeKey.type] as? String else {
  2101. return oldPath
  2102. }
  2103. var i = 1
  2104. var newPath = oldPath
  2105. while fileManager.fileExists(atPath: newPath) {
  2106. if fileType == FileAttributeType.typeDirectory.rawValue {
  2107. newPath = oldPath + "(\(i))"
  2108. } else {
  2109. let fileExtension = (oldPath as NSString).pathExtension
  2110. newPath = ((oldPath as NSString).deletingPathExtension as NSString).appendingFormat("(\(i)).\(fileExtension)" as NSString) as String
  2111. }
  2112. i += 1
  2113. }
  2114. return newPath
  2115. } catch {
  2116. print("Error getting file attributes: \(error)")
  2117. return oldPath
  2118. }
  2119. }
  2120. //MARK: - TTS
  2121. @IBAction func startSpeaking(_ sender: Any?) {
  2122. self.showTTSWindow()
  2123. let ttsView = KMTTSWindowController.share
  2124. // ttsView.buttonItemClick_Play(ttsView.playButton)
  2125. }
  2126. @IBAction func stopSpeaking(_ sender: Any) {
  2127. let ttsWindowC = KMTTSWindowController.share
  2128. if ttsWindowC.pdfView?.document?.documentURL.path == self.listView.document?.documentURL.path {
  2129. if let data = ttsWindowC.window?.isVisible, data {
  2130. ttsWindowC.stopSpeaking()
  2131. ttsWindowC.close()
  2132. }
  2133. }
  2134. }
  2135. func showTTSWindow() {
  2136. var lastPDFView: CPDFView?
  2137. let ttsView = KMTTSWindowController.share
  2138. if (ttsView.window?.isVisible ?? false) {
  2139. lastPDFView = ttsView.pdfView
  2140. if lastPDFView?.document?.documentURL?.path == self.listView.document?.documentURL?.path {
  2141. lastPDFView = nil
  2142. ttsView.window?.orderOut(nil)
  2143. } else {
  2144. ttsView.pdfView = self.listView
  2145. ttsView.showWindow(nil)
  2146. }
  2147. } else {
  2148. ttsView.pdfView = self.listView
  2149. ttsView.showWindow(nil)
  2150. }
  2151. ttsView.closeWindowCallback = { (isCloseWindow: Bool) in
  2152. if isCloseWindow {
  2153. }
  2154. }
  2155. if let currentSelection = self.listView.currentSelection {
  2156. if let data = currentSelection.selectionsByLine, data.isEmpty == false {
  2157. ttsView.startSpeakingPDFSelection(currentSelection)
  2158. }
  2159. }
  2160. if let lastPDFView = lastPDFView {
  2161. lastPDFView.setHighlightedSelections([])
  2162. ttsView.stopSpeaking()
  2163. }
  2164. }
  2165. //MARK: 导出图片
  2166. func extractImageAction(num: Int) {
  2167. if !IAPProductsManager.default().isAvailableAllFunction(){
  2168. let winC = KMPurchaseCompareWindowController.sharedInstance()
  2169. winC?.kEventName = "Reading_ExtractImage_BuyNow"
  2170. winC?.showWindow(nil)
  2171. return
  2172. }
  2173. if !(self.listView.document.allowsPrinting || self.listView.document.allowsCopying) {
  2174. let alert = NSAlert()
  2175. alert.alertStyle = .critical
  2176. alert.messageText = KMLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
  2177. alert.runModal()
  2178. return
  2179. }
  2180. let document = self.listView.document
  2181. var fileURL = document?.documentURL
  2182. if num == 1 {
  2183. let pageCount = document?.pageCount ?? 0
  2184. let indeSet = NSMutableIndexSet()
  2185. indeSet.add(in: NSRange(location: 0, length: Int(pageCount)))
  2186. if indeSet.count == 0 {
  2187. return
  2188. }
  2189. let lastPathName = fileURL?.deletingPathExtension().lastPathComponent ?? ""
  2190. let tFileName = (String(format: "%@_Extract Images", lastPathName))
  2191. let outputSavePanel = NSSavePanel()
  2192. outputSavePanel.title = KMLocalizedString("Save as PDF", comment: "")
  2193. outputSavePanel.allowsOtherFileTypes = true
  2194. outputSavePanel.isExtensionHidden = true
  2195. outputSavePanel.canCreateDirectories = true
  2196. outputSavePanel.nameFieldStringValue = tFileName
  2197. outputSavePanel.beginSheetModal(for: self.view.window!, completionHandler: { (result) in
  2198. if result == NSApplication.ModalResponse.OK {
  2199. DispatchQueue.main.async {
  2200. self.beginProgressSheet(withMessage: KMLocalizedString("Extracting all pictures...", comment: "") + "...", maxValue: 0)
  2201. let tDestFile = outputSavePanel.url!.path
  2202. let uniquePath = KMExtractImageWindowController.createDestFolder(path: tDestFile, isUnique: false)
  2203. let pdfconverter = PDFConvertObject()
  2204. pdfconverter.extractResourcesFromPDF(at: fileURL?.path ?? "", pdfPassword: document?.password, selectIndexSet: indeSet as IndexSet, destDocPath: uniquePath, moreOptions: nil)
  2205. self.dismissProgressSheet()
  2206. let fileManager = FileManager.default
  2207. if fileManager.fileExists(atPath: tDestFile) {
  2208. let workspace = NSWorkspace.shared
  2209. let url = URL(fileURLWithPath: tDestFile)
  2210. workspace.activateFileViewerSelecting([url])
  2211. }
  2212. }
  2213. }
  2214. })
  2215. return
  2216. }
  2217. if fileURL != nil {
  2218. self.myDocument?.save(nil)
  2219. } else {
  2220. let myDocument = self.myDocument
  2221. let str = String(format: "%@.pdf", myDocument?.displayName ?? "")
  2222. let writeSuccess = document!.write(to: URL(fileURLWithPath: (kTempSavePath?.stringByAppendingPathComponent(str))!))
  2223. if writeSuccess {
  2224. var documentTemp = CPDFDocument(url: URL(fileURLWithPath: (kTempSavePath?.stringByAppendingPathComponent(str))!))
  2225. fileURL = document?.documentURL
  2226. } else {
  2227. NSSound.beep()
  2228. return
  2229. }
  2230. }
  2231. extract = KMExtractImageWindowController(windowNibName: "KMExtractImageWindowController")
  2232. extract?.docPath = fileURL?.path ?? ""
  2233. extract?.password = document?.password ?? ""
  2234. extract?.currentPage = self.listView.currentPageIndex
  2235. extract?.own_beginSheetModal(for: self.view.window, completionHandler: { result in
  2236. })
  2237. extract?.selectCurrentPageBtn()
  2238. }
  2239. func beginProgressSheet(withMessage message: String, maxValue: UInt) {
  2240. let progress = SKProgressController()
  2241. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  2242. progress.window?.contentView?.wantsLayer = true
  2243. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  2244. progress.progressField.textColor = NSColor.white
  2245. progress.message = KMLocalizedString("Converting...", comment: "")
  2246. progressC = progress
  2247. progressC?.message = message
  2248. if maxValue > 0 {
  2249. progressC?.indeterminate = false
  2250. progressC?.maxValue = Double(maxValue)
  2251. progressC?.progressBar.doubleValue = 0.3
  2252. } else {
  2253. progressC?.indeterminate = true
  2254. }
  2255. progressC?.own_beginSheetModal(for: self.view.window, completionHandler: { result in
  2256. })
  2257. }
  2258. func dismissProgressSheet() {
  2259. progressC?.stopAnimation()
  2260. progressC?.own_closeEndSheet()
  2261. progressC = nil
  2262. }
  2263. func converFilesToPath(files: Array<KMBatchOperateFile>) -> [String] {
  2264. let newArr = NSMutableArray()
  2265. for item in files {
  2266. newArr.add(item.filePath)
  2267. }
  2268. return newArr as! [String]
  2269. }
  2270. //MARK: - 打印
  2271. internal func showPrintWindow(pageRange: KMPrintPageRange = KMPrintPageRange(type: .allPage, selectPages: [])) {
  2272. self.saveDocument()
  2273. if (self.listView.document != nil && !self.listView.document.allowsPrinting) { // 有打印限制
  2274. KMPasswordInputWindow.openWindow(window: self.view.window!, type: .owner, url: self.listView.document.documentURL) { [weak self] result ,password in
  2275. if (result == .cancel) {
  2276. return
  2277. }
  2278. // 解除权限
  2279. self?.listView.document.unlock(withPassword: password)
  2280. // 隐藏提示
  2281. self?.hiddenSecureLimitTip()
  2282. // 去打印
  2283. KMPrintWindowController.openDocument(inputDocument: self?.listView.document, inputPageRange: pageRange)
  2284. }
  2285. return
  2286. }
  2287. KMPrintWindowController.openDocument(inputDocument: self.listView.document, inputPageRange: pageRange)
  2288. }
  2289. //Poster
  2290. func showPosterPrintWindow() {
  2291. guard let document = self.document else { return }
  2292. guard let pdfDocument = PDFDocument(url: document.documentURL) else { return }
  2293. if self.posterPrintWindowController == nil {
  2294. posterPrintWindowController = KMPDFPosterPrintWindowController.init(pdfDocument: pdfDocument)
  2295. }
  2296. if let window = posterPrintWindowController?.window {
  2297. NSApp.mainWindow?.beginSheet(window, completionHandler: { response in
  2298. })
  2299. }
  2300. }
  2301. //Multiple
  2302. func showMultiplePrintWindow() {
  2303. guard let document = self.document else { return }
  2304. guard let pdfDocument = PDFDocument(url: document.documentURL) else { return }
  2305. if self.multiplePrintWindowController == nil {
  2306. multiplePrintWindowController = KMPDFMultiplePrintWindowController.init(pdfDocument: pdfDocument)
  2307. }
  2308. if let window = multiplePrintWindowController?.window {
  2309. NSApp.mainWindow?.beginSheet(window, completionHandler: { response in
  2310. })
  2311. }
  2312. }
  2313. //Booklet
  2314. func showBookletPrintWindow() {
  2315. guard let document = self.document else { return }
  2316. guard let pdfDocument = PDFDocument(url: document.documentURL) else { return }
  2317. if self.bookletWindowController == nil {
  2318. bookletWindowController = KMPDFBookletWindowController.init(document: pdfDocument)
  2319. }
  2320. if let window = bookletWindowController?.window {
  2321. NSApp.mainWindow?.beginSheet(window, completionHandler: { response in
  2322. })
  2323. }
  2324. }
  2325. //MARK: - FileInfo
  2326. func showFileInfo() {
  2327. KMInfoWindowController.shared.showWindow(nil)
  2328. }
  2329. //MARK: - Share Action
  2330. @objc private func shareDocument(sender: NSView) {
  2331. let document = self.listView.document ?? CPDFDocument()
  2332. if document?.documentURL == nil {
  2333. return
  2334. }
  2335. var doucumentURL : URL = self.listView.document.documentURL
  2336. if doucumentURL.path.count > 0 {
  2337. let docDir = NSTemporaryDirectory()
  2338. let documentName : String = doucumentURL.path.lastPathComponent
  2339. let path = docDir.stringByAppendingPathComponent(documentName)
  2340. let writeSuccess = self.listView.document.write(to: URL(fileURLWithPath: path))
  2341. if writeSuccess == false {
  2342. __NSBeep()
  2343. return;
  2344. }
  2345. doucumentURL = URL(fileURLWithPath: path)
  2346. }
  2347. let array = [doucumentURL]
  2348. let picker = NSSharingServicePicker.init(items: array)
  2349. if sender.window != nil {
  2350. picker.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
  2351. } else {
  2352. picker.show(relativeTo: NSRect(x: (self.view.window?.contentView?.frame.size.width)!, y: (self.view.window?.contentView?.frame.size.height ?? 0)-8, width: 0, height: 0), of: self.view.window?.contentView ?? NSView(), preferredEdge: NSRectEdge.minY)
  2353. }
  2354. }
  2355. @objc private func shareFlatten(sender: NSView) {
  2356. let document = self.listView.document ?? CPDFDocument()
  2357. var path: String?
  2358. if document?.documentURL != nil {
  2359. path = document?.documentURL.path ?? ""
  2360. }
  2361. if path?.count ?? 0 > 0 {
  2362. let docDir = NSTemporaryDirectory()
  2363. let documentName : String = path?.lastPathComponent ?? ""
  2364. path = docDir.stringByAppendingPathComponent(documentName)
  2365. }
  2366. let pathFolder = path?.fileURL.deletingLastPathComponent().path
  2367. var tfileName = path?.deletingPathExtension.lastPathComponent
  2368. let tStdFileSuffix = "_flatten"
  2369. tfileName = (tfileName ?? "") + tStdFileSuffix + ".pdf"
  2370. path = (pathFolder ?? "") + "/" + (tfileName ?? "")
  2371. let success : Bool = document?.writeFlatten(to: URL(fileURLWithPath: path ?? "")) ?? false
  2372. if success {
  2373. let url = URL(fileURLWithPath: path ?? "")
  2374. let picker = NSSharingServicePicker.init(items: [url])
  2375. if sender.window != nil {
  2376. picker.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
  2377. } else {
  2378. picker.show(relativeTo: NSRect(x: (self.view.window?.contentView?.frame.size.width)!, y: (self.view.window?.contentView?.frame.size.height ?? 0)-8, width: 0, height: 0), of: self.view.window?.contentView ?? NSView(), preferredEdge: NSRectEdge.minY)
  2379. }
  2380. }
  2381. }
  2382. @objc private func shareOriginalPDF(sender: NSView) {
  2383. guard let pdfDoc = self.listView.document else {
  2384. NSSound.beep()
  2385. return
  2386. }
  2387. if !pdfDoc.allowsCopying || !pdfDoc.allowsPrinting {
  2388. let alert = NSAlert()
  2389. alert.alertStyle = .critical
  2390. alert.messageText = KMLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
  2391. alert.runModal()
  2392. return
  2393. }
  2394. let document = self.listView.document ?? CPDFDocument()
  2395. var path: String?
  2396. if document?.documentURL != nil {
  2397. path = document?.documentURL.path ?? ""
  2398. }
  2399. if path?.count ?? 0 > 0 {
  2400. let docDir = NSTemporaryDirectory()
  2401. let documentName : String = path?.lastPathComponent ?? ""
  2402. path = docDir.stringByAppendingPathComponent(documentName)
  2403. }
  2404. var writeSuccess = document?.write(to: URL(fileURLWithPath: path ?? ""))
  2405. if writeSuccess == false {
  2406. __NSBeep()
  2407. return;
  2408. }
  2409. let newDocument = CPDFDocument(url: URL(fileURLWithPath: path ?? ""))
  2410. let cnt = newDocument?.pageCount ?? 0
  2411. for i in 0 ..< cnt {
  2412. let page = newDocument!.page(at: i)
  2413. var annotations : [CPDFAnnotation] = []
  2414. for annotation in page!.annotations {
  2415. annotations.append(annotation)
  2416. }
  2417. for annotation in annotations {
  2418. annotation.page.removeAnnotation(annotation)
  2419. }
  2420. }
  2421. writeSuccess = newDocument?.write(to:URL(fileURLWithPath: path ?? ""))
  2422. if writeSuccess ?? false {
  2423. let url = URL(fileURLWithPath: path ?? "")
  2424. let picker = NSSharingServicePicker.init(items: [url])
  2425. if sender.window != nil {
  2426. picker.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
  2427. } else {
  2428. picker.show(relativeTo: NSRect(x: (self.view.window?.contentView?.frame.size.width)!, y: (self.view.window?.contentView?.frame.size.height ?? 0)-8, width: 0, height: 0), of: self.view.window?.contentView ?? NSView(), preferredEdge: NSRectEdge.minY)
  2429. }
  2430. }
  2431. }
  2432. @objc func shareFromService(sender: NSMenuItem) {
  2433. if ((NSApp.mainWindow?.windowController is KMBrowserWindowController) == false) {
  2434. return
  2435. }
  2436. var string = ""
  2437. if let freeTextAnnotation = listView.activeAnnotation as? CPDFFreeTextAnnotation {
  2438. string = freeTextAnnotation.contents ?? ""
  2439. } else if let markupAnnotation = listView.activeAnnotation as? CPDFMarkupAnnotation {
  2440. if let page = markupAnnotation.page {
  2441. if let selection = page.selection(for: markupAnnotation.bounds) {
  2442. string = selection.string() ?? ""
  2443. }
  2444. }
  2445. } else {
  2446. string = listView.currentSelection?.string() ?? ""
  2447. }
  2448. let windowControler = NSApp.mainWindow?.windowController as! KMBrowserWindowController
  2449. let model = windowControler.browser?.tabStripModel
  2450. if let cnt = model?.count(), cnt <= 0 {
  2451. return
  2452. }
  2453. if let data = model?.activeTabContents().isHome, data {
  2454. return
  2455. }
  2456. let document: KMMainDocument = model?.activeTabContents() as! KMMainDocument
  2457. if string.count > 0 {
  2458. let represent : NSSharingService = sender.representedObject as! NSSharingService
  2459. represent.perform(withItems: [string])
  2460. return
  2461. }
  2462. let represent = sender.representedObject as? NSSharingService
  2463. represent?.perform(withItems: [string])
  2464. }
  2465. //MARK: - PDFView Menu
  2466. func clickPresentationMenu(point:NSPoint)->KMNMenuStruct {
  2467. var viewHeight: CGFloat = 8
  2468. var menuItemArr: [ComponentMenuitemProperty] = []
  2469. var items: [(String, String)] = []
  2470. items.append(("Next Page", PDFViewMenuIdentifier_PageNext))
  2471. items.append(("Previous Page", PDFViewMenuIdentifier_PagePrevious))
  2472. items.append(("First", PDFViewMenuIdentifier_PageFirst))
  2473. items.append(("Last", PDFViewMenuIdentifier_PageLast))
  2474. items.append(("", ""))
  2475. items.append(("Laser pointer", PDFViewMenuIdentifier_Presentation_LaserPoint))
  2476. items.append(("Brush", PDFViewMenuIdentifier_Presentation_Brush))
  2477. items.append(("", ""))
  2478. items.append(("Exit Presentation", PDFViewMenuIdentifier_Presentation_Exit))
  2479. for (i, value) in items {
  2480. if value.count == 0 {
  2481. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  2482. menuItemArr.append(property)
  2483. viewHeight += 8
  2484. } else {
  2485. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  2486. itemSelected: false,
  2487. isDisabled: false,
  2488. keyEquivalent: nil,
  2489. text: KMLocalizedString(i),
  2490. identifier: value,representedObject: point)
  2491. if value == PDFViewMenuIdentifier_PageNext {
  2492. properties_Menuitem.keyEquivalent = "▶"
  2493. } else if value == PDFViewMenuIdentifier_PagePrevious {
  2494. properties_Menuitem.keyEquivalent = "◀"
  2495. } else if value == PDFViewMenuIdentifier_PageFirst {
  2496. properties_Menuitem.keyEquivalent = "⌘ ◀"
  2497. } else if value == PDFViewMenuIdentifier_PageLast {
  2498. properties_Menuitem.keyEquivalent = "⌘ ▶"
  2499. } else if value == PDFViewMenuIdentifier_Presentation_Exit {
  2500. properties_Menuitem.keyEquivalent = "ESC"
  2501. }
  2502. if(value == PDFViewMenuIdentifier_Presentation_LaserPoint) {
  2503. if((listView.presentationDrawView != nil) && listView.presentationDrawView.isHidden == false) {
  2504. } else {
  2505. properties_Menuitem.righticon = NSImage(named: "KMNImageNameMenuSelect")
  2506. }
  2507. } else if value == PDFViewMenuIdentifier_Presentation_Brush {
  2508. if((listView.presentationDrawView != nil) && listView.presentationDrawView.isHidden == false) {
  2509. properties_Menuitem.righticon = NSImage(named: "KMNImageNameMenuSelect")
  2510. } else {
  2511. }
  2512. }
  2513. menuItemArr.append(properties_Menuitem)
  2514. viewHeight += 36
  2515. }
  2516. }
  2517. let menuStruct = KMNMenuStruct(menuitems: menuItemArr, viewHeight: viewHeight)
  2518. return menuStruct
  2519. }
  2520. func gotoPage(_ sender: Any?) {
  2521. var pages : [String] = []
  2522. for i in 0 ..< self.listView.document.pageCount {
  2523. pages.append("\(i)")
  2524. }
  2525. self.textFieldSheet.callback = { [weak self] stringValue in
  2526. guard let index = Int(stringValue) else {
  2527. return
  2528. }
  2529. if (self?.listView == nil) {
  2530. return
  2531. }
  2532. if (index > 0 && index <= self!.listView.document.pageCount) {
  2533. self?.listView.go(toPageIndex: index-1, animated: true)
  2534. }
  2535. }
  2536. self.textFieldSheet.showWindow(nil)
  2537. self.textFieldSheet.pageBox.addItems(withObjectValues: pages)
  2538. self.textFieldSheet.stringValue = "\(self.listView.currentPageIndex+1)"
  2539. }
  2540. //MARK: - 添加书签
  2541. @objc func menuItemBookMarkClick_add(sender:NSMenuItem?) {
  2542. if self.listView.document?.bookmark(forPageIndex: UInt(self.listView.currentPageIndex)) == nil {
  2543. let index = self.listView.currentPageIndex
  2544. self.listView.document?.addBookmark("\(KMLocalizedString("Page", comment: "")) \(index+1)", forPageIndex: UInt(index))
  2545. self.listView.updateRender(true)
  2546. } else {
  2547. self.listView.document?.removeBookmark(forPageIndex: UInt(self.listView.currentPageIndex))
  2548. self.listView.updateRender(true)
  2549. }
  2550. botaViewController?.reloadData()
  2551. self.listView.undoManager?.setActionName("")//添加undo事件就可删除
  2552. }
  2553. //MARK: - 页面旋转
  2554. func rotateLeft(page:CPDFPage,listView:CPDFListView?) {
  2555. Task { @MainActor in
  2556. let rotation = page.rotation
  2557. page.leftRotate()
  2558. listView?.layoutDocumentView()
  2559. if(listView == self.listView) {
  2560. var pageIndexes = IndexSet()
  2561. pageIndexes.insert(Int(page.pageIndex()))
  2562. KMNThumbnailManager.reloadThumImage(document: self.listView.document, pageIndexs: pageIndexes)
  2563. botaViewController?.reloadData()
  2564. }
  2565. }
  2566. }
  2567. func rotateRight(page:CPDFPage,listView:CPDFListView?) {
  2568. Task { @MainActor in
  2569. let rotation = page.rotation
  2570. page.rightRotate()
  2571. listView?.layoutDocumentView()
  2572. if(listView == self.listView) {
  2573. var pageIndexes = IndexSet()
  2574. pageIndexes.insert(Int(page.pageIndex()))
  2575. KMNThumbnailManager.reloadThumImage(document: self.listView.document, pageIndexs: pageIndexes)
  2576. botaViewController?.reloadData()
  2577. }
  2578. }
  2579. }
  2580. //MARK: - 自动滚动
  2581. func isAutoFlowOn() -> Bool {
  2582. return listView.isAutoFlow()
  2583. }
  2584. func toggleAutoFlow(_ sender: Any?) {
  2585. if (listView.isAutoFlow()) {
  2586. listView.stopAutoFlow()
  2587. } else {
  2588. listView.startAutoFlow()
  2589. }
  2590. }
  2591. //MARK: - 高亮Form
  2592. func highlightFormFiled(_ sender: Any?) {
  2593. let enabled = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  2594. CPDFKitConfig.sharedInstance().setEnableFormFieldHighlight(!enabled)
  2595. listView.setNeedsDisplayForVisiblePages()
  2596. self.alertTipViewController.reloadFormAlertUI()
  2597. }
  2598. //MARK: - 高亮Link
  2599. func highlightLinks(_ sender: Any?) {
  2600. let enabled = CPDFKitConfig.sharedInstance().enableLinkFieldHighlight()
  2601. CPDFAnnotation.updateLinkFieldHighlight(listView, linkFieldHighlight: !enabled)
  2602. }
  2603. //MARK: - 重置Form
  2604. func resetForm(_ sender: Any?) {
  2605. listView.resetFormAnnotation()
  2606. }
  2607. //MARK: - 属性
  2608. @IBAction func menuItemAction_property(_ sender: Any?) {
  2609. showFileInfo()
  2610. }
  2611. //MARK: - 打印
  2612. @IBAction func menuItemAction_print(_ sender: Any?) {
  2613. self.showPrintWindow()
  2614. }
  2615. //MARK: - Flattened
  2616. func saveAsFlattenedPDFAction() {
  2617. DispatchQueue.main.async {
  2618. NSPanel.savePanel(self.view.window!, true) { panel in
  2619. panel.nameFieldStringValue = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_flatten" + ".pdf"
  2620. } completion: { response, url, isOpen in
  2621. if (response == .cancel) {
  2622. return
  2623. }
  2624. var result = self.listView.document.writeFlatten(to: url!)
  2625. if (!result) {
  2626. return
  2627. }
  2628. if (isOpen) {
  2629. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  2630. }
  2631. } else {
  2632. NSWorkspace.shared.activateFileViewerSelecting([url!])
  2633. }
  2634. }
  2635. }
  2636. }
  2637. func showInFinder() {
  2638. NSWorkspace.shared.activateFileViewerSelecting([self.listView.document.documentURL])
  2639. }
  2640. //MARK: - DisplayViewMode
  2641. func getPDFViewPageLayoutType() -> pageLayoutType {
  2642. if listView.displayMode() == .singlePage {
  2643. return .singlePage
  2644. } else if listView.displayMode() == .singlePageContinuous {
  2645. return .singlePageContinue
  2646. } else if listView.displayMode() == .twoUp {
  2647. if listView.displaysAsBook == true {
  2648. return .bookMode
  2649. }
  2650. return .twoPage
  2651. } else if listView.displayMode() == .twoUpContinuous {
  2652. if listView.displaysAsBook == true {
  2653. return .bookMode
  2654. }
  2655. return .twoPageContinue
  2656. }
  2657. return .singlePage
  2658. }
  2659. func updatePDFViewDisplayMode(viewMode mode: CPDFDisplayViewMode = .singlePage, isbookMode bookMode: Bool = false, direction directionValue: CPDFDisplayDirection? = nil) {
  2660. if bookMode == true {
  2661. //书本模式
  2662. listView.displaysAsBook = true
  2663. listView.displayTwoUp = true
  2664. if let value = directionValue {
  2665. listView.displayDirection = value
  2666. }
  2667. } else {
  2668. listView.setDisplay(mode)
  2669. }
  2670. listView.layoutDocumentView()
  2671. }
  2672. //MARK: - 新增大纲
  2673. @objc func addOutLineItemAction() {
  2674. botaViewController?.outlineViewC.addOutline()
  2675. }
  2676. //MARK: - 搜索
  2677. @objc func searchBaiduAction() {
  2678. let label = self.listView.currentSelection?.string() ?? ""
  2679. let query = label.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
  2680. if let url = URL(string: "https://www.baidu.com/s?wd=\(query)") {
  2681. NSWorkspace.shared.open(url)
  2682. }
  2683. }
  2684. //MARK: - 转档
  2685. func showConvertWindow(_ convertType: KMPDFConvertType) {
  2686. var winC: KMConvertBaseWindowController = KMConvertBaseWindowController()
  2687. if convertType == .word {
  2688. winC = KMConvertWordWindowController()
  2689. let model = KMDocumentModel(url: listView.document.documentURL)
  2690. winC.documentModel = model
  2691. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2692. } else if convertType == .ppt {
  2693. winC = KMConvertPPTsWindowController()
  2694. winC.subType = 1
  2695. let model = KMDocumentModel(url: listView.document.documentURL)
  2696. winC.documentModel = model
  2697. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2698. } else if convertType == .rtf {
  2699. winC = KMConvertPPTsWindowController()
  2700. winC.subType = 2
  2701. let model = KMDocumentModel(url: listView.document.documentURL)
  2702. winC.documentModel = model
  2703. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2704. } else if convertType == .text {
  2705. winC = KMConvertPPTsWindowController()
  2706. winC.subType = 4
  2707. let model = KMDocumentModel(url: listView.document.documentURL)
  2708. winC.documentModel = model
  2709. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2710. } else if convertType == .csv {
  2711. winC = KMConvertCSVWindowController()
  2712. winC.subType = 5
  2713. let model = KMDocumentModel(url: listView.document.documentURL)
  2714. winC.documentModel = model
  2715. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2716. } else if convertType == .excel {
  2717. winC = KMConvertExcelWindowController()
  2718. let model = KMDocumentModel(url: listView.document.documentURL)
  2719. winC.documentModel = model
  2720. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2721. } else if convertType == .html {
  2722. winC = KMConvertHtmlWindowController()
  2723. let model = KMDocumentModel(url: listView.document.documentURL)
  2724. winC.documentModel = model
  2725. winC.own_beginSheetModal(for: self.view.window, completionHandler: nil)
  2726. } else if convertType == .json {
  2727. winC = KMConvertJsonWindowController()
  2728. let model = KMDocumentModel(url: listView.document.documentURL)
  2729. winC.documentModel = model
  2730. winC.own_beginSheetModal(for: view.window, completionHandler: nil)
  2731. } else if convertType == .jpeg ||
  2732. convertType == .jpg ||
  2733. convertType == .png ||
  2734. convertType == .gif ||
  2735. convertType == .tiff ||
  2736. convertType == .tga ||
  2737. convertType == .bmp ||
  2738. convertType == .jp2{
  2739. winC = KMConvertImageWindowController()
  2740. let model = KMDocumentModel(url: listView.document.documentURL)
  2741. winC.documentModel = model
  2742. winC.own_beginSheetModal(for: view.window, completionHandler: nil)
  2743. if let settingView = winC.settingView as? KMConvertImageSettingView {
  2744. settingView.selectConvertType(convertType: convertType)
  2745. }
  2746. }
  2747. winC.batchAction = { [unowned self] in
  2748. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  2749. model.password = listView.document.password ?? ""
  2750. self.showBatchWindow(type: .convertPDF, subType: convertType.rawValue, files: [], fileModels: [model])
  2751. }
  2752. }
  2753. //MARK: - 选择Form注释
  2754. @objc func selectAllFormAnnotation () {
  2755. var formActiveAnnotations:[CPDFAnnotation] = []
  2756. for i in 0 ..< listView.document.pageCount {
  2757. let page = listView.document.page(at: i)
  2758. let annotations = page?.annotations
  2759. for j in 0 ..< (annotations?.count ?? 0) {
  2760. if let an = annotations?[j] as? CPDFAnnotation {
  2761. if (an.isKind(of: CPDFWidgetAnnotation.self) == true) {
  2762. formActiveAnnotations.append(an)
  2763. }
  2764. }
  2765. }
  2766. }
  2767. listView.updateActiveAnnotations(formActiveAnnotations)
  2768. }
  2769. @objc func selectAllNomerAnnotation () {
  2770. var normalActiveAnnotations:[CPDFAnnotation] = []
  2771. for i in 0 ..< listView.document.pageCount {
  2772. let page = listView.document.page(at: i)
  2773. let annotations = page?.annotations
  2774. for j in 0 ..< (annotations?.count ?? 0) {
  2775. if let an = annotations?[j] as? CPDFAnnotation {
  2776. if an.isKind(of: CPDFWidgetAnnotation.self) == true ||
  2777. an.isKind(of: CPDFLinkAnnotation.self) == true ||
  2778. an.isKind(of: CPDFRedactAnnotation.self) == true {
  2779. } else {
  2780. normalActiveAnnotations.append(an)
  2781. }
  2782. }
  2783. }
  2784. }
  2785. listView.updateActiveAnnotations(normalActiveAnnotations)
  2786. }
  2787. //MARK: 搜索 & 替换
  2788. func showSearchPopWindow(type: KMNBotaSearchType, keyborad: String?, replaceText: String?, results: [KMSearchMode]) {
  2789. let toolMode = self.listView.toolMode
  2790. let isEditing = self.listView.isEditing()
  2791. var winH: CGFloat = 112
  2792. if type == .replace {
  2793. if IAPProductsManager.default().isAvailableAllFunction() == false {
  2794. let winC = KMPurchaseCompareWindowController.sharedInstance()
  2795. winC?.kEventName = "Reading_ReplaceText_BuyNow"
  2796. winC?.showWindow(nil)
  2797. return
  2798. }
  2799. if toolMode == .CEditPDFToolMode && isEditing {
  2800. } else { // 进入内容编辑模式
  2801. viewManager.toolMode = .Edit
  2802. updatePDFViewAnnotationMode()
  2803. }
  2804. winH = 208
  2805. }
  2806. self.view.window?.makeFirstResponder(nil)
  2807. let winC = KMSearchReplaceWindowController(with: listView, type: type)
  2808. self.currentWindowController = winC
  2809. winC.replaceCallback = { [weak self] in
  2810. let toolMode = self?.listView.toolMode ?? .none
  2811. let isEditing = self?.listView.isEditing() ?? false
  2812. if toolMode == .CEditPDFToolMode && isEditing {
  2813. } else { // 进入内容编辑模式
  2814. self?.viewManager.toolMode = .Edit
  2815. self?.updatePDFViewAnnotationMode()
  2816. }
  2817. }
  2818. winC.itemClick = { [weak self] idx, params in
  2819. if idx == 1 {
  2820. self?.toggleOpenLeftSide(pdfSideBarType: .search)
  2821. guard let handdler = params.first as? KMNSearchHanddler else {
  2822. return
  2823. }
  2824. let viewC = self?.botaViewController?.searchViewC
  2825. viewC?.update(keyborad: handdler.searchKey, replaceKey: handdler.replaceKey, results: handdler.searchSectionResults)
  2826. }
  2827. }
  2828. let targetView = self.pdfToolbarController?.leftViewButton
  2829. let point = targetView?.convert(targetView?.frame.origin ?? .zero, to: nil) ?? .zero
  2830. // 200 248
  2831. let x = point.x + (self.view.window?.frame.origin.x ?? 0) - 32
  2832. let y = point.y + (self.view.window?.frame.origin.y ?? 0) - winH - 32
  2833. let winFramePoint = NSPoint(x: x, y: y)
  2834. winC.window?.setFrameOrigin(winFramePoint)
  2835. winC.update(keyborad: keyborad, replaceKey: replaceText, results: results)
  2836. self.view.window?.addChildWindow(winC.window!, ordered: .above)
  2837. }
  2838. // MARK: -显示加密弹窗
  2839. //MARK: - Redact密文
  2840. func showRedactProperty(readactAnnotation: CPDFRedactAnnotation?) {
  2841. let properties = KMRedactPropertiesWindowController()
  2842. properties.readactAnnotation = readactAnnotation
  2843. properties.own_beginSheetModal(for: self.view.window) { result in
  2844. }
  2845. }
  2846. //MARK: - 测量
  2847. func refreshMeasureInfo() {
  2848. if distanceMeasureInfoWindowController == nil {
  2849. distanceMeasureInfoWindowController = CDistanceMeasureInfoWindowController()
  2850. }
  2851. if perimeterMeasureInfoWindowController == nil {
  2852. perimeterMeasureInfoWindowController = CPerimeterMeasureInfoWindowController()
  2853. }
  2854. if areaMeasureInfoWindowController == nil {
  2855. areaMeasureInfoWindowController = CAreaMeasureInfoWindowController()
  2856. }
  2857. showMeasureFloatingWindowsIfNeed()
  2858. }
  2859. @objc func cancelMeasureType() {
  2860. self.hideMeasureFloatingWindows()
  2861. }
  2862. func hideMeasureFloatingWindows() {
  2863. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  2864. distanceMeasureInfoWindowController?.hideFloatingWindow()
  2865. } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  2866. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  2867. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  2868. areaMeasureInfoWindowController?.hideFloatingWindow()
  2869. }
  2870. }
  2871. func showMeasureFloatingWindowsIfNeed() {
  2872. cancelMeasureType()
  2873. let toolMode = self.listView.toolMode
  2874. if toolMode != .CNoteToolMode {
  2875. return
  2876. }
  2877. if let data = self.listView.activeAnnotation as? CPDFPolylineAnnotation {
  2878. } else if let data = self.listView.activeAnnotation as? CPDFPolygonAnnotation {
  2879. } else if let data = self.listView.activeAnnotation as? CPDFLineAnnotation, data.isMeasure {
  2880. } else {
  2881. let type = self.listView.annotationType
  2882. if type == .measureLine {
  2883. distanceMeasureInfoWindowController?.measureInfo = listView.distanceMeasureInfo
  2884. distanceMeasureInfoWindowController?.window?.orderFront(nil)
  2885. } else if type == .measurePolyLine {
  2886. perimeterMeasureInfoWindowController?.measureInfo = listView.perimeterMeasureInfo
  2887. perimeterMeasureInfoWindowController?.window?.orderFront(nil)
  2888. } else if type == .measurePolyGon {
  2889. areaMeasureInfoWindowController?.measureInfo = listView.polygonAreaMeasureInfo
  2890. areaMeasureInfoWindowController?.window?.orderFront(nil)
  2891. } else if type == .measureSquare {
  2892. areaMeasureInfoWindowController?.measureInfo = listView.squareAreaMeasureInfo
  2893. areaMeasureInfoWindowController?.window?.orderFront(nil)
  2894. } else {
  2895. cancelMeasureType()
  2896. }
  2897. }
  2898. }
  2899. func showMeasureSettingWindow() {
  2900. let distanceSettingWC = CDistanceSettingWindowController(measureInfo: self.listView.distanceMeasureInfo)
  2901. self.distanceMeasureInfoWindowController?.hideFloatingWindow()
  2902. distanceSettingWC.delegate = self
  2903. distanceSettingWC.own_beginSheetModal(for: self.view.window) { string in
  2904. }
  2905. }
  2906. }
  2907. //MARK: Compress
  2908. extension KMMainViewController {
  2909. func showCompressController(_ url: URL) {
  2910. KMPasswordInputWindow.openWindow(window: NSWindow.currentWindow(), type: .owner, url: url) { [unowned self] result, password in
  2911. if (result == .success) {
  2912. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  2913. model.password = password ?? ""
  2914. model.isLock = false
  2915. self.showCompressController(model: model)
  2916. }
  2917. }
  2918. }
  2919. func showCompressController(model: KMBatchProcessingTableViewModel) {
  2920. let document = CPDFDocument(url: model.filePath.fileURL)
  2921. document?.unlock(withPassword: model.password)
  2922. if !document!.allowsPrinting && !document!.allowsCopying {
  2923. KMPasswordInputWindow.openWindow(window: NSWindow.currentWindow(), type: .owner, url: document!.documentURL) { [unowned self] result, password in
  2924. if (result == .success) {
  2925. // let model = KMBatchProcessingTableViewModel.initWithFilePath(url: model.filePath.fileURL)
  2926. model.password = password ?? ""
  2927. model.isLock = false
  2928. self.showCompress(model: model)
  2929. }
  2930. }
  2931. } else {
  2932. self.showCompress(model: model)
  2933. }
  2934. }
  2935. func showCompress(model: KMBatchProcessingTableViewModel) {
  2936. self.compressWindowController = KMCompressWindowController(windowNibName: "KMCompressWindowController")
  2937. self.compressWindowController?.password = model.password
  2938. self.compressWindowController?.documentURL = model.filePath.fileURL
  2939. let controllerWindow = self.view.window
  2940. controllerWindow?.beginSheet(self.compressWindowController!.window!)
  2941. self.compressWindowController?.itemClick = { [unowned self] in
  2942. controllerWindow?.endSheet((self.compressWindowController?.window)!)
  2943. }
  2944. self.compressWindowController?.batchAction = { [unowned self] view, filePaths in
  2945. controllerWindow?.endSheet((self.compressWindowController?.window)!)
  2946. self.showBatchWindow(type: .compress, files: filePaths)
  2947. }
  2948. self.compressWindowController?.resultCallback = { [unowned self] result, openDocument, fileURL, error in
  2949. controllerWindow?.endSheet((self.compressWindowController?.window)!)
  2950. if (result) {
  2951. if (openDocument) {
  2952. NSDocumentController.shared.openDocument(withContentsOf: fileURL, display: true) { document, result, error in }
  2953. } else {
  2954. NSWorkspace.shared.activateFileViewerSelecting([fileURL])
  2955. }
  2956. } else {
  2957. let alert = NSAlert()
  2958. alert.messageText = KMLocalizedString("Compress Faild", comment: "")
  2959. alert.runModal()
  2960. }
  2961. }
  2962. }
  2963. }
  2964. //MARK: - OCR
  2965. extension KMMainViewController {
  2966. //window
  2967. func showOCRWindow() {
  2968. if !IAPProductsManager.default().isAvailableAllFunction(){
  2969. let winC = KMPurchaseCompareWindowController.sharedInstance()
  2970. winC?.showWindow(nil)
  2971. return
  2972. }
  2973. let window = KMOCRSettingWindowController(windowNibName: "KMOCRSettingWindowController")
  2974. window.OCRAction = {[unowned self] controller, model in
  2975. self.convertOCRScanFile(window: window, document: self.listView.document, model: model)
  2976. }
  2977. window.cancelAction = {[unowned self] controller in
  2978. self.view.window?.endSheet(window.window ?? NSWindow())
  2979. }
  2980. self.view.window?.beginSheet(window.window ?? NSWindow())
  2981. // self.km_beginSheet(windowC: window)
  2982. }
  2983. func showOCREditAlert() {
  2984. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { [unowned self] in
  2985. alertTipViewController.showInView(listView: listView, subView: infoSplitCenterView)
  2986. alertTipViewController.reloadOCRAlertUI()
  2987. alertTipViewController.reloadAlertUIFrame()
  2988. alertTipViewController.OCRApplyCallback = { [unowned self] in
  2989. self.showOCRWindow()
  2990. }
  2991. }
  2992. }
  2993. func closeOCREditAlert() {
  2994. guard let view = alertTipViewController.OCRComponentAlert else { return }
  2995. alertTipViewController.reloadOCRAlertUI()
  2996. alertTipViewController.reloadAlertUIFrame()
  2997. }
  2998. func showOCRToolAlert(_ string: String) {
  2999. let alertView = ComponentMessage()
  3000. alertView.properties = ComponentMessageProperty(messageType: .normal_custom, title: KMLocalizedString(string))
  3001. alertView.frame = CGRectMake((CGRectGetWidth(self.listView.frame) - alertView.properties.propertyInfo.viewWidth) / 2,
  3002. CGRectGetHeight(self.listView.frame) - alertView.properties.propertyInfo.viewHeight - 8,
  3003. alertView.properties.propertyInfo.viewWidth,
  3004. alertView.properties.propertyInfo.viewHeight)
  3005. alertView.reloadData()
  3006. self.listView.addSubview(alertView)
  3007. // 自动移除视图
  3008. DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
  3009. self.removeOCRToolAlert(alertView)
  3010. }
  3011. }
  3012. private func removeOCRToolAlert(_ alertView: NSView) {
  3013. // 淡出动画
  3014. NSAnimationContext.runAnimationGroup({ context in
  3015. context.duration = 0.3
  3016. alertView.animator().alphaValue = 0
  3017. }, completionHandler: {
  3018. alertView.removeFromSuperview()
  3019. })
  3020. }
  3021. //OCR
  3022. func convertOCRScanFile(window: KMOCRSettingWindowController, document: CPDFDocument, model: KMOCRModel) {
  3023. // window.beginLoading()
  3024. KMCompressManager.shared.showLoadingWindow(window: window.window)
  3025. KMCompressManager.shared.cancelAction = {
  3026. KMOCRManager.manager.cancelRecognition()
  3027. }
  3028. //当前页面需要提前设置
  3029. if model.pageRangeType == .current {
  3030. model.pageRange = [self.listView.currentPageIndex]
  3031. }
  3032. KMOCRManager.manager.convertScanFile(document: document, model: model, progress: { progress in
  3033. KMCompressManager.shared.updateLoadingProgress(value: progress)
  3034. }) { [weak self] document, text, error in
  3035. KMCompressManager.shared.dismissLoadiingWindow(window: window.window)
  3036. window.endLoading()
  3037. window.km_quick_endSheet()
  3038. if !model.saveAsPDF {
  3039. self?.listView.layoutDocumentView()
  3040. }
  3041. }
  3042. }
  3043. func convertOCR(document: CPDFDocument, model: KMOCRModel) {
  3044. // self.view.window?.windowController.beginLoading()
  3045. //当前页面需要提前设置
  3046. if model.pageRangeType == .current {
  3047. model.pageRange = [self.listView.currentPageIndex]
  3048. }
  3049. self.showProgressWindow(message: "正在转换中....")
  3050. KMOCRManager.manager.convertOCR(document: document, model: model, progress: { [weak self] progress in
  3051. self?.progressC?.message = "正在转换中...."
  3052. self?.progressC?.doubleValue = Double(progress * 100)
  3053. }) { [weak self] document, text, error in
  3054. // self?.view.window?.windowController.endLoading()
  3055. // window.km_quick_endSheet()
  3056. if !model.saveAsPDF {
  3057. self?.listView.layoutDocumentView()
  3058. }
  3059. self?.hiddenProgressWindow()
  3060. }
  3061. self.progressC?.closeBlock = {
  3062. print("手动取消中")
  3063. KMOCRManager.manager.cancelRecognition()
  3064. }
  3065. }
  3066. func convertOCRSaveAsTXT(text: String) {
  3067. NSPanel.savePanel(NSWindow.currentWindow()) { panel in
  3068. let url: URL = self.listView.document.documentURL
  3069. panel.nameFieldStringValue = ""+url.deletingPathExtension().lastPathComponent+"_OCR"
  3070. panel.allowedFileTypes = ["txt"]
  3071. } completion: { [unowned self] response, url in
  3072. if (response == .cancel) {
  3073. return
  3074. }
  3075. let saveAsPDFFilePath = url?.path ?? ""
  3076. let outputURL = URL(fileURLWithPath: saveAsPDFFilePath)
  3077. try? text.write(to: outputURL, atomically: true, encoding: .utf8)
  3078. }
  3079. }
  3080. func convertSelectionRectOCR(rect: NSRect) {
  3081. let rect = NSIntegralRect(rect)
  3082. let orgPage : CPDFPage = listView.currentSelectionPage() ?? CPDFPage()
  3083. if let page : CPDFPage = orgPage.copy() as? CPDFPage {
  3084. page.setBounds(rect, for: .cropBox)
  3085. let image = page.thumbnail(of: rect.size) ?? NSImage()
  3086. guard let model = self.rightSideController?.tool_OCRController?.model else { return }
  3087. model.pageRange = [Int(orgPage.pageIndex())]
  3088. KMOCRManager.manager.convertOCR(images: [image], model: model, progress: { progress in
  3089. }) { [weak self] document, text, error in
  3090. self?.rightSideController?.tool_OCRController?.model.text = text ?? ""
  3091. self?.rightSideController?.tool_OCRController?.reloadData()
  3092. //关闭窗口
  3093. self?.listView.selectionRect = NSZeroRect
  3094. self?.listView.selectionPageIndex = UInt(NSNotFound)
  3095. self?.closePopOperationWindow()
  3096. self?.listView.setNeedsDisplayForVisiblePages()
  3097. }
  3098. }
  3099. }
  3100. }
  3101. //MARK: Batch
  3102. extension KMMainViewController {
  3103. func showBatchWindow(type: KMBatchCollectionViewType, fileModels: [KMBatchProcessingTableViewModel]?) {
  3104. self.showBatchWindow(type: type, subType: 0, files: [], fileModels: fileModels)
  3105. }
  3106. func showBatchWindow(type: KMBatchCollectionViewType, files: [URL]?) {
  3107. self.showBatchWindow(type: type, subType: 0, files: files, fileModels: [])
  3108. }
  3109. func showBatchWindow(type: KMBatchCollectionViewType, subType: Int = 0, files: [URL]?, fileModels: [KMBatchProcessingTableViewModel]?) {
  3110. let batchWindowController = KMBatchWindowController.manager
  3111. batchWindowController.window?.makeKeyAndOrderFront("")
  3112. if files?.count != 0 {
  3113. batchWindowController.inputData = files ?? []
  3114. }
  3115. if fileModels?.count != 0 {
  3116. batchWindowController.inputDataModel = fileModels ?? []
  3117. }
  3118. batchWindowController.type = type
  3119. batchWindowController.inputSubType = subType
  3120. }
  3121. }
  3122. //MARK: - 代理方法
  3123. //MARK: - NSSplitViewDelegate
  3124. extension KMMainViewController: NSSplitViewDelegate {
  3125. func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
  3126. if splitView == infoContendSplitView {
  3127. return subview.isEqual(to: infoSplitCenterView) == false
  3128. }
  3129. return false
  3130. }
  3131. func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool {
  3132. if splitView == infoContendSplitView {
  3133. if(subview.isEqual(to: infoSplitLeftView)) {
  3134. } else if(subview.isEqual(to: infoSplitRightView)) {
  3135. }
  3136. }
  3137. return false
  3138. }
  3139. func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
  3140. if splitView == infoContendSplitView {
  3141. return splitView == infoContendSplitView
  3142. } else if splitView == pdfSplitView {
  3143. return splitView == pdfSplitView
  3144. }
  3145. return false
  3146. }
  3147. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  3148. if splitView == infoContendSplitView {
  3149. if dividerIndex == 0 {
  3150. if viewManager.showDisplayView {
  3151. return MIN_SIDE_PANE_WIDTH.doubleValue
  3152. }
  3153. return infoContendSplitView.bounds.width/2
  3154. } else if (dividerIndex == 1) {
  3155. return proposedMaximumPosition - MIN_SIDE_PANE_WIDTH.doubleValue
  3156. }
  3157. }
  3158. return proposedMaximumPosition
  3159. }
  3160. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  3161. if(splitView == infoContendSplitView && dividerIndex == 0) {
  3162. return proposedMinimumPosition + MIN_SIDE_PANE_WIDTH.doubleValue
  3163. }
  3164. return proposedMinimumPosition
  3165. }
  3166. func splitViewDidResizeSubviews(_ notification: Notification) {
  3167. let splitView = notification.object as? NSSplitView
  3168. if(splitView == infoContendSplitView) {
  3169. leftSplitViewResizeFinish()
  3170. if(newMwcFlags.settingUpWindow == false && self.view.window?.frameAutosaveName != nil) {
  3171. let leftWidth = infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) ? 0.0 : infoSplitLeftView.frame.width
  3172. UserDefaults.standard.set(leftWidth, forKey: CPDFViewLeftSidePaneWidthKey)
  3173. UserDefaults.standard.synchronize()
  3174. }
  3175. if listView.isEditing() == false {
  3176. updateAnnotationsPopWinodwFrame()
  3177. } else {
  3178. updateContentEditPopWinodwFrame()
  3179. }
  3180. }
  3181. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(splitViewResizeFinish), object: nil)
  3182. self.perform(#selector(splitViewResizeFinish), with: nil, afterDelay: 0.15)
  3183. }
  3184. @objc func leftSplitViewResizeFinish() {
  3185. botaViewController?.changeLeftSideBounds()
  3186. }
  3187. @objc func splitViewResizeFinish() {
  3188. if infoContendSplitView.isSubviewCollapsed(infoSplitLeftView) {
  3189. if viewManager.pdfSideBarType != .none {
  3190. viewManager.pdfSideBarType = .none
  3191. sideBarController?.reloadBOTAData()
  3192. }
  3193. }
  3194. }
  3195. }
  3196. //MARK: - KMPDFSideBarControllerDelegate 左侧Sidebar代理
  3197. extension KMMainViewController: KMPDFSideBarControllerDelegate {
  3198. func kmPDFSideBarControllerDidSidebarTypeUpdated(_ view: KMPDFSideBarController) {
  3199. if viewManager.pdfSideBarType == .none {
  3200. toggleCloseLeftSide()
  3201. } else {
  3202. toggleOpenLeftSide(pdfSideBarType: viewManager.pdfSideBarType)
  3203. }
  3204. }
  3205. func kmPDFSideBarControllerDidGotoPage(_ view: KMPDFSideBarController, _ pageIndex: Int) {
  3206. listView.go(toPageIndex: pageIndex, animated: true)
  3207. }
  3208. }
  3209. //MARK: - KMPDFToolbarControllerDelegate 工具栏代理
  3210. extension KMMainViewController: KMPDFToolbarControllerDelegate {
  3211. //MARK: -ViewTools发生变化时调用
  3212. func kmPDFToolbarControllerDidViewToolsChanged(_ controller: KMPDFToolbarController) {
  3213. let viewToolsType = viewManager.viewToolsType
  3214. if viewToolsType == .Select {
  3215. listView.toolMode = .CNoteToolMode
  3216. } else if viewToolsType == .Scroll {
  3217. listView.toolMode = .CMoveToolMode
  3218. } else if viewToolsType == .Content_Selection {
  3219. listView.toolMode = .CSelectToolMode
  3220. } else if viewToolsType == .Magnify {
  3221. listView.toolMode = .CMagnifyToolMode
  3222. } else if viewToolsType == .AreaZoom {
  3223. listView.toolMode = .CSelectZoomToolMode
  3224. }
  3225. refreshToolbarViewHeightInfo()
  3226. }
  3227. //MARK: -一级工具栏状态发生变化时调用
  3228. func kmPDFToolbarControllerDidToolModeChanged(_ controller: KMPDFToolbarController) {
  3229. refreshToolbarViewHeightInfo()
  3230. toolbarViewModeChanged()
  3231. }
  3232. //MARK: -点击工具栏按钮
  3233. func kmPDFToolbarControllerDidToolbarItemClicked(_ controller: KMPDFToolbarController, _ itemIdentifier: String) {
  3234. print("toolbar点击", itemIdentifier)
  3235. if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_PageEdit_Identifier).contains(itemIdentifier) {
  3236. //MARK: -页面编辑
  3237. if itemIdentifier == KMPDFToolbar_PageEdit_InsertFile_Identifier {
  3238. pageEditViewController?.insertFromPDFAction()
  3239. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertBlank_Identifier {
  3240. pageEditViewController?.insertFromBlankAction()
  3241. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertClip_Identifier {
  3242. pageEditViewController?.insertFromClipboardAction()
  3243. } else if itemIdentifier == KMPDFToolbar_PageEdit_InsertScanner_Identifier {
  3244. pageEditViewController?.insertFromScannerAction()
  3245. } else if itemIdentifier == KMPDFToolbar_PageEdit_Extract_Identifier {
  3246. pageEditViewController?.extractPDFAction()
  3247. } else if itemIdentifier == KMPDFToolbar_PageEdit_Replace_Identifier {
  3248. pageEditViewController?.replacePDFAction()
  3249. } else if itemIdentifier == KMPDFToolbar_PageEdit_Split_Identifier {
  3250. pageEditViewController?.splitPDFAction()
  3251. } else if itemIdentifier == KMPDFToolbar_PageEdit_Reverse_Identifier {
  3252. pageEditViewController?.reversePDFAction()
  3253. } else if itemIdentifier == KMPDFToolbar_PageEdit_LeftRotate_Identifier {
  3254. pageEditViewController?.rotatePageLeftAction()
  3255. } else if itemIdentifier == KMPDFToolbar_PageEdit_RightRotate_Identifier{
  3256. pageEditViewController?.rotatePageRightAction()
  3257. } else if itemIdentifier == KMPDFToolbar_PageEdit_Delete_Identifier {
  3258. pageEditViewController?.deletePageAction()
  3259. } else if itemIdentifier == KMPDFToolbar_PageEdit_Reduce_Identifier {
  3260. pageEditViewController?.zoomOutPageAction()
  3261. if(pageEditViewController?.canZoomInPageSize() == false) {
  3262. toolbarManager.page_Increase_Property.isDisabled = true
  3263. } else {
  3264. toolbarManager.page_Increase_Property.isDisabled = false
  3265. }
  3266. if(pageEditViewController?.canZoomOutPageSize() == false) {
  3267. toolbarManager.page_Reduce_Property.isDisabled = true
  3268. } else {
  3269. toolbarManager.page_Reduce_Property.isDisabled = false
  3270. }
  3271. controller.refreshSecondToolbarItemsState()
  3272. } else if itemIdentifier == KMPDFToolbar_PageEdit_Increase_Identifier {
  3273. pageEditViewController?.zoomInPageAction()
  3274. if(pageEditViewController?.canZoomInPageSize() == false) {
  3275. toolbarManager.page_Increase_Property.isDisabled = true
  3276. } else {
  3277. toolbarManager.page_Increase_Property.isDisabled = false
  3278. }
  3279. if(pageEditViewController?.canZoomOutPageSize() == false) {
  3280. toolbarManager.page_Reduce_Property.isDisabled = true
  3281. } else {
  3282. toolbarManager.page_Reduce_Property.isDisabled = false
  3283. }
  3284. controller.refreshSecondToolbarItemsState()
  3285. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_oddPage_Identifier {
  3286. pageEditViewController?.thumbnailChoosePageStyle = .odd
  3287. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  3288. toolbarManager.page_pageInfo_Property.creatable = false
  3289. controller.refreshSecondToolbarItemsState()
  3290. }
  3291. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_EvenPage_Identifier {
  3292. pageEditViewController?.thumbnailChoosePageStyle = .even
  3293. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  3294. toolbarManager.page_pageInfo_Property.creatable = false
  3295. controller.refreshSecondToolbarItemsState()
  3296. }
  3297. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_PortraitPage_Identifier {
  3298. pageEditViewController?.thumbnailChoosePageStyle = .horizontal
  3299. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  3300. toolbarManager.page_pageInfo_Property.creatable = false
  3301. controller.refreshSecondToolbarItemsState()
  3302. }
  3303. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_LandscapePage_Identifier {
  3304. pageEditViewController?.thumbnailChoosePageStyle = .vertical
  3305. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  3306. toolbarManager.page_pageInfo_Property.creatable = false
  3307. controller.refreshSecondToolbarItemsState()
  3308. }
  3309. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_AllPage_Identifier {
  3310. pageEditViewController?.thumbnailChoosePageStyle = .allPage
  3311. if(toolbarManager.page_pageInfo_Property.creatable == true) {
  3312. toolbarManager.page_pageInfo_Property.creatable = false
  3313. controller.refreshSecondToolbarItemsState()
  3314. }
  3315. } else if itemIdentifier == KMPDFToolbar_PageEdit_page_CustomPage_Identifier {
  3316. pageEditViewController?.thumbnailChoosePageStyle = .custom
  3317. toolbarManager.page_pageInfo_Property.text = nil
  3318. if(toolbarManager.page_pageInfo_Property.creatable == false) {
  3319. toolbarManager.page_pageInfo_Property.creatable = true
  3320. }
  3321. controller.refreshSecondToolbarItemsState()
  3322. }
  3323. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Markup_Identifier).contains(itemIdentifier) {
  3324. //MARK: -Markup
  3325. if itemIdentifier == KMPDFToolbar_eye_Identifier {
  3326. self.showOrHideNotes()
  3327. }
  3328. if viewManager.subToolMode == .None {
  3329. viewManager.showRightSide = false
  3330. } else if SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  3331. viewManager.showRightSide = true
  3332. } else if viewManager.subToolMode == .Stamp || viewManager.subToolMode == .Sign {
  3333. viewManager.showRightSide = true
  3334. }
  3335. self.refreshToolbarRightViewInfo()
  3336. if let currentSelect = listView.currentSelection {
  3337. if(currentSelect.selectionType() == .text) {
  3338. let annotationType: CAnnotationType = KMPDFViewManager.getValidPDFAnnotationType(viewManager.subToolMode)
  3339. if annotationType != .unkown {
  3340. listView.addAnnotation(with: annotationType, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  3341. }
  3342. }
  3343. }
  3344. toolbarViewModeChanged()
  3345. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Edit_Identifier).contains(itemIdentifier) {
  3346. //MARK: -编辑
  3347. if itemIdentifier == KMPDFToolbar_edit_addWatermark_Identifier {
  3348. showWatermarkController()
  3349. } else if itemIdentifier == KMPDFToolbar_edit_removeWatermark_Identifier {
  3350. removePDFWatermark()
  3351. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddWatermark_Identifier {
  3352. batchAddWatermark()
  3353. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveWatermark_Identifier {
  3354. batchRemoveWatermark()
  3355. } else if itemIdentifier == KMPDFToolbar_edit_addBG_Identifier {
  3356. showBackgroundController()
  3357. } else if itemIdentifier == KMPDFToolbar_edit_removeBG_Identifier {
  3358. removePDFBackground()
  3359. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddBG_Identifier {
  3360. batchAddBackground()
  3361. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveBG_Identifier {
  3362. batchRemoveBackground()
  3363. } else if itemIdentifier == KMPDFToolbar_edit_addHF_Identifier {
  3364. showHeaderFooterController()
  3365. } else if itemIdentifier == KMPDFToolbar_edit_removeHF_Identifier {
  3366. removeHeaderFooter()
  3367. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddHF_Identifier {
  3368. batchAddHeaderFooter()
  3369. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveHF_Identifier {
  3370. batchRemoveHeaderFooter()
  3371. } else if itemIdentifier == KMPDFToolbar_edit_addBates_Identifier {
  3372. showBatesController()
  3373. } else if itemIdentifier == KMPDFToolbar_edit_removeBates_Identifier {
  3374. removePDFBates()
  3375. } else if itemIdentifier == KMPDFToolbar_edit_batch_AddBates_Identifier {
  3376. batchAddBates()
  3377. } else if itemIdentifier == KMPDFToolbar_edit_batchRemoveBates_Identifier {
  3378. batchRemoveBates()
  3379. } else if itemIdentifier == KMPDFToolbar_edit_crop_Identifier {
  3380. showCropController()
  3381. }
  3382. if viewManager.subToolMode == .None {
  3383. viewManager.showRightSide = false
  3384. } else if SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  3385. viewManager.showRightSide = true
  3386. }
  3387. self.refreshToolbarRightViewInfo()
  3388. toolbarViewModeChanged()
  3389. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Form_Identifier).contains(itemIdentifier) {
  3390. //MARK: -Form表单
  3391. if itemIdentifier == KMPDFToolbar_form_HighlightFields_Identifier {
  3392. let value = CPDFKitConfig.sharedInstance().enableFormFieldHighlight()
  3393. CPDFAnnotation.updateHighlightFormFiled(listView, highlightFormFiled: !value)
  3394. self.toolbarManager.refreshDefaultConfigItem()
  3395. self.alertTipViewController.reloadFormAlertUI()
  3396. } else if itemIdentifier == KMPDFToolbar_form_ShowName_Identifier {
  3397. listView.showFormFieldName = !listView.showFormFieldName
  3398. if listView.showFormFieldName {
  3399. toolbarManager.form_ShowName_Property.righticon = NSImage(named: "tick_Green")
  3400. } else {
  3401. toolbarManager.form_ShowName_Property.righticon = nil
  3402. }
  3403. listView.setNeedsDisplayForVisiblePages()
  3404. } else if itemIdentifier == KMPDFToolbar_form_ClearForm_Identifier {
  3405. listView.resetFormAnnotation()
  3406. } else if itemIdentifier == KMPDFToolbar_form_Align_Left_Identifier {
  3407. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .left)
  3408. listView.setNeedsDisplayForVisiblePages()
  3409. } else if itemIdentifier == KMPDFToolbar_form_Align_Hori_Identifier {
  3410. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .horizontally)
  3411. listView.setNeedsDisplayForVisiblePages()
  3412. } else if itemIdentifier == KMPDFToolbar_form_Align_Right_Identifier {
  3413. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .right)
  3414. listView.setNeedsDisplayForVisiblePages()
  3415. } else if itemIdentifier == KMPDFToolbar_form_Align_Top_Identifier {
  3416. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .top)
  3417. listView.setNeedsDisplayForVisiblePages()
  3418. } else if itemIdentifier == KMPDFToolbar_form_Align_Vert_Identifier {
  3419. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .vertical)
  3420. listView.setNeedsDisplayForVisiblePages()
  3421. } else if itemIdentifier == KMPDFToolbar_form_Align_Bottom_Identifier {
  3422. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .bottom)
  3423. listView.setNeedsDisplayForVisiblePages()
  3424. } else if itemIdentifier == KMPDFToolbar_form_Distribute_Vert_Identifier {
  3425. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .disVertical)
  3426. listView.setNeedsDisplayForVisiblePages()
  3427. } else if itemIdentifier == KMPDFToolbar_form_Distribute_Hori_Identifier {
  3428. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .disHorizontally)
  3429. listView.setNeedsDisplayForVisiblePages()
  3430. }
  3431. if itemIdentifier == KMPDFToolbar_form_HighlightFields_Identifier ||
  3432. itemIdentifier == KMPDFToolbar_form_ShowName_Identifier ||
  3433. itemIdentifier == KMPDFToolbar_form_ClearForm_Identifier ||
  3434. itemIdentifier == KMPDFToolbar_form_Align_Left_Identifier ||
  3435. itemIdentifier == KMPDFToolbar_form_Align_Hori_Identifier ||
  3436. itemIdentifier == KMPDFToolbar_form_Align_Right_Identifier ||
  3437. itemIdentifier == KMPDFToolbar_form_Align_Top_Identifier ||
  3438. itemIdentifier == KMPDFToolbar_form_Align_Vert_Identifier ||
  3439. itemIdentifier == KMPDFToolbar_form_Align_Bottom_Identifier ||
  3440. itemIdentifier == KMPDFToolbar_form_Distribute_Vert_Identifier ||
  3441. itemIdentifier == KMPDFToolbar_form_Distribute_Hori_Identifier {
  3442. } else {
  3443. toolbarViewModeChanged()
  3444. }
  3445. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Fill_Identifier).contains(itemIdentifier) {
  3446. //MARK: -填充
  3447. if viewManager.subToolMode == .None {
  3448. viewManager.showRightSide = false
  3449. } else if SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  3450. viewManager.showRightSide = true
  3451. }
  3452. self.refreshToolbarRightViewInfo()
  3453. toolbarViewModeChanged()
  3454. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Convert_Identifier).contains(itemIdentifier) {
  3455. //MARK: -转档
  3456. if itemIdentifier == KMPDFToolbar_convert_word_Identifier {
  3457. self.showConvertWindow(.word)
  3458. } else if itemIdentifier == KMPDFToolbar_convert_ppt_Identifier {
  3459. self.showConvertWindow(.ppt)
  3460. } else if itemIdentifier == KMPDFToolbar_convert_RTF_Identifier {
  3461. self.showConvertWindow(.rtf)
  3462. } else if itemIdentifier == KMPDFToolbar_convert_Text_Identifier {
  3463. self.showConvertWindow(.text)
  3464. } else if itemIdentifier == KMPDFToolbar_convert_CSV_Identifier {
  3465. self.showConvertWindow(.csv)
  3466. } else if itemIdentifier == KMPDFToolbar_convert_excel_Identifier {
  3467. self.showConvertWindow(.excel)
  3468. } else if itemIdentifier == KMPDFToolbar_convert_HTML_Identifier {
  3469. self.showConvertWindow(.html)
  3470. } else if itemIdentifier == KMPDFToolbar_convert_Json_Identifier {
  3471. self.showConvertWindow(.json)
  3472. } else if itemIdentifier == KMPDFToolbar_convert_image_Identifier {
  3473. self.showConvertWindow(.jpeg)
  3474. } else if itemIdentifier == KMPDFToolbar_convert_imageToPDF_Identifier {
  3475. NSApplication.ShowImageToPDFWindow()
  3476. }
  3477. self.refreshToolbarRightViewInfo()
  3478. toolbarViewModeChanged()
  3479. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Protect_Identifier).contains(itemIdentifier) {
  3480. //MARK: -Protect
  3481. if itemIdentifier == KMPDFToolbar_protect_redact_Identifier {
  3482. toolbarViewModeChanged()
  3483. if viewManager.showRightSide == true {
  3484. viewManager.showRightSide = false
  3485. pdfToolbarController?.reloadRightToolsView()
  3486. toggleCloseRightSide()
  3487. }
  3488. } else if itemIdentifier == KMPDFToolbar_protect_redact_Property_Identifier {
  3489. self.showRedactProperty(readactAnnotation: nil)
  3490. toolbarViewModeChanged()
  3491. } else if itemIdentifier == KMPDFToolbar_protect_redact_Apply_Identifier {
  3492. toolbarViewModeChanged()
  3493. } else if itemIdentifier == KMPDFToolbar_protect_redact_Exit_Identifier {
  3494. toolbarViewModeChanged()
  3495. } else if itemIdentifier == KMPDFToolbar_protect_security_Identifier {
  3496. self.showSecureWindow()
  3497. } else if itemIdentifier == KMPDFToolbar_protect_removeSecurity_Identifier {
  3498. self.showRemoveSecureWindow()
  3499. } else if itemIdentifier == KMPDFToolbar_protect_digitalSign_Identifier {
  3500. self.toolbarViewModeChanged()
  3501. }
  3502. alertTipViewController.reloadAlertUIFrame()
  3503. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Tools_Identifier).contains(itemIdentifier) {
  3504. //MARK: -工具
  3505. if itemIdentifier == KMPDFToolbar_tools_compress_Identifier {
  3506. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  3507. model.password = listView.document.password ?? ""
  3508. self.showCompressController(model: model)
  3509. } else if itemIdentifier == KMPDFToolbar_tools_batch_compress_Identifier {
  3510. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  3511. model.password = listView.document.password ?? ""
  3512. self.showBatchWindow(type: .compress, files: [], fileModels: [model])
  3513. } else if itemIdentifier == KMPDFToolbar_tools_OCR_Identifier {
  3514. viewManager.showRightSide = !viewManager.showRightSide
  3515. self.refreshToolbarRightViewInfo()
  3516. } else if itemIdentifier == KMPDFToolbar_tools_merge_Identifier {
  3517. self.showMergeWindow(self.listView.document.password)
  3518. } else if itemIdentifier == KMPDFToolbar_tools_TTS_Identifier {
  3519. self.showTTSWindow()
  3520. } else if itemIdentifier == KMPDFToolbar_tools_extractImage_Identifier {
  3521. self.extractImageAction(num: 2)
  3522. } else if itemIdentifier == KMPDFToolbar_tools_AITools_Identifier {
  3523. self.viewManager.pdfSideBarType = .aiTools
  3524. self.sideBarController?.reloadData()
  3525. if let sideVC = self.sideBarController {
  3526. self.kmPDFSideBarControllerDidSidebarTypeUpdated(sideVC)
  3527. }
  3528. } else if itemIdentifier == KMPDFToolbar_tools_compare_side_Identifier {
  3529. self.beginCompareAction(1)
  3530. } else if itemIdentifier == KMPDFToolbar_tools_compare_Overlay_Identifier {
  3531. self.beginCompareAction(2)
  3532. } else if itemIdentifier == KMPDFToolbar_tools_batch_Identifier {
  3533. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  3534. model.password = listView.document.password ?? ""
  3535. self.showBatchWindow(type: .convertPDF, files: [], fileModels: [model])
  3536. }
  3537. if viewManager.subToolMode == .None {
  3538. viewManager.showRightSide = false
  3539. } else if SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  3540. viewManager.showRightSide = true
  3541. }
  3542. self.refreshToolbarRightViewInfo()
  3543. toolbarViewModeChanged()
  3544. } else if itemIdentifier == KMPDFToolbar_ViewDisplay_Identifier {
  3545. //MARK: -Display
  3546. updatePDFDisplaySettingView()
  3547. } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Right_Identifiers).contains(itemIdentifier) {
  3548. if(itemIdentifier == KMPDFToolbar_undo_Identifier) {
  3549. //MARK: -Undo
  3550. listView.undoManager?.undo()
  3551. } else if(itemIdentifier == KMPDFToolbar_redo_Identifier) {
  3552. //MARK: -Redo
  3553. listView.undoManager?.redo()
  3554. } else if(itemIdentifier == KMPDFToolbar_fileInfo_Identifier) {
  3555. self.showFileInfo()
  3556. } else if(itemIdentifier == KMPDFToolbar_share_PDF_Identifier ||
  3557. itemIdentifier == KMPDFToolbar_share_Flattened_Identifier ||
  3558. itemIdentifier == KMPDFToolbar_share_Original_Identifier) {
  3559. //MARK: -Share
  3560. if(itemIdentifier == KMPDFToolbar_share_PDF_Identifier) {
  3561. if let view = controller.findViewWith(KMPDFToolbar_share_Identifier) {
  3562. shareDocument(sender: view)
  3563. }
  3564. } else if(itemIdentifier == KMPDFToolbar_share_Flattened_Identifier) {
  3565. if let view = controller.findViewWith(KMPDFToolbar_share_Identifier) {
  3566. shareFlatten(sender: view)
  3567. }
  3568. } else if(itemIdentifier == KMPDFToolbar_share_Original_Identifier) {
  3569. if let view = controller.findViewWith(KMPDFToolbar_share_Identifier) {
  3570. shareOriginalPDF(sender: view)
  3571. }
  3572. }
  3573. } else if itemIdentifier == KMPDFToolbar_save_Identifier {
  3574. myDocument?.save(nil)
  3575. } else if itemIdentifier == KMPDFToolbar_print_Identifier {
  3576. self.showPrintWindow()
  3577. } else if itemIdentifier == KMPDFToolbar_tts_Identifier {
  3578. self.showTTSWindow()
  3579. } else if itemIdentifier == KMPDFToolbar_ppt_Identifier {
  3580. self.togglePresentation(nil)
  3581. } else if itemIdentifier == KMPDFToolbar_batch_Identifier {
  3582. let model = KMBatchProcessingTableViewModel.initWithFilePath(url: listView.document.documentURL)
  3583. model.password = listView.document.password ?? ""
  3584. self.showBatchWindow(type: .convertPDF, files: [], fileModels: [model])
  3585. }
  3586. } else if itemIdentifier == KMPDFToolbar_PageEdit_Identifier {
  3587. //MARK: -页面编辑
  3588. if viewManager.isPageEditMode == true {
  3589. enterPageEditMode()
  3590. } else {
  3591. exitPageEditMode()
  3592. }
  3593. } else if itemIdentifier == KMPDFToolbar_rightView_Identifier {
  3594. //MARK: -属性栏
  3595. self.refreshToolbarRightViewInfo()
  3596. } else {
  3597. print("click else")
  3598. }
  3599. refreshToolbarViewHeightInfo()
  3600. refreshRightSide()
  3601. }
  3602. func kmPDFToolbarControllerDidSelectTextDidBeginEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  3603. }
  3604. func kmPDFToolbarControllerDidSelectTextDidChange(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  3605. }
  3606. func kmPDFToolbarControllerDidSelectTextDidEndEditing(_ controller: KMPDFToolbarController, _ view: ComponentSelect) {
  3607. if view.properties == toolbarManager.page_pageInfo_Property {
  3608. if viewManager.isPageEditMode == true {
  3609. let fileAttribute = KMNFileAttribute()
  3610. fileAttribute.password = listView.document?.password ?? ""
  3611. fileAttribute.pdfDocument = listView.document
  3612. fileAttribute.filePath = listView.document?.documentURL.path ?? ""
  3613. fileAttribute.bAllPage = false
  3614. fileAttribute.pagesType = .PagesString
  3615. fileAttribute.pagesString = view.properties.text ?? ""
  3616. let fetchSelectPages = fileAttribute.fetchSelectPages()
  3617. if (fetchSelectPages.isEmpty) {
  3618. let alert = NSAlert()
  3619. alert.alertStyle = .critical
  3620. alert.messageText = String(format: "%@ %@", fileAttribute.filePath.lastPathComponent, KMLocalizedString("Invalid page range or the page number is out of range. Please try again."))
  3621. alert.runModal()
  3622. toolbarManager.page_pageInfo_Property.text = ""
  3623. controller.refreshSecondToolbarItemsState()
  3624. } else {
  3625. var tIndexPaths: Set<IndexPath> = []
  3626. for i in 0 ..< fetchSelectPages.count {
  3627. tIndexPaths.insert(IndexPath(item: (fetchSelectPages[i] - 1), section: 0))
  3628. }
  3629. pageEditViewController?.selectionIndexPaths = tIndexPaths
  3630. }
  3631. }
  3632. }
  3633. }
  3634. func kmPDFToolbarControllerDidExitPageEditMode(_ controller: KMPDFToolbarController) {
  3635. exitPageEditMode()
  3636. }
  3637. }
  3638. //MARK: - KMRightSideControllerDelegate右边属性栏代理
  3639. extension KMMainViewController: KMRightSideControllerDelegate {
  3640. func kmRightSideControllerRotateLeft(_ annotations: [CPDFStampAnnotation], withPDFView pdfView: CPDFListView?) {
  3641. CPDFStampAnnotation.rotateLeft(annotations, withPDFView: pdfView)
  3642. (undoManager?.prepare(withInvocationTarget: self) as AnyObject).kmRightSideControllerRotateRight(annotations, withPDFView: pdfView)
  3643. }
  3644. func kmRightSideControllerRotateRight(_ annotations: [CPDFStampAnnotation], withPDFView pdfView: CPDFListView?) {
  3645. CPDFStampAnnotation.rotateRight(annotations, withPDFView: pdfView)
  3646. (undoManager?.prepare(withInvocationTarget: self) as AnyObject).kmRightSideControllerRotateLeft(annotations, withPDFView: pdfView)
  3647. }
  3648. func kmRightSideControllerDidContendVCUpdated(_ controller: KMRightSideController) {
  3649. //MARK: -Crop
  3650. if (controller.contentViewController is KMCropPropertyController) {
  3651. if let cropVC = rightSideController?.edit_cropController {
  3652. cropVC.pdfView = controller.pdfView
  3653. if cropVC.delegate == nil {
  3654. cropVC.delegate = cropController
  3655. }
  3656. cropVC.reloadData()
  3657. }
  3658. }
  3659. reloadPopUIWindow()
  3660. }
  3661. func kmRightSideControllerOCRShowTypeChange(_ controller: KMRightSideController, _ type: KMOCRShowType) {
  3662. if type == .area {
  3663. self.listView.toolMode = .COCRToolMode
  3664. self.showOCRToolAlert(KMLocalizedString("Please select the area that needs OCR and click the “OCR” button to start"))
  3665. } else {
  3666. self.listView.toolMode = .CTextToolMode
  3667. }
  3668. }
  3669. func kmRightSideControllerOCRDoneAction(_ controller: KMRightSideController, _ model: KMOCRModel) {
  3670. if model.showType == .area {
  3671. self.convertOCRSaveAsTXT(text: model.text)
  3672. } else {
  3673. if model.saveAsPDF {
  3674. if model.saveType == .PDF {
  3675. } else {
  3676. }
  3677. } else {
  3678. }
  3679. self.convertOCR(document: self.listView.document, model: model)
  3680. }
  3681. }
  3682. //测量设置界面
  3683. func kmRightSideControllerShowMeasureSetting(_ controller: KMRightSideController) {
  3684. showMeasureSettingWindow()
  3685. }
  3686. //裁剪模块
  3687. func kmRightSideControllerDidRevertCropInfo(_ controller: KMRightSideController) {
  3688. if let cropVC = cropController {
  3689. cropVC.reloadSelectionRect()
  3690. self.kmCropControllerDidChangedSelectionOrMagnification(cropVC)
  3691. }
  3692. }
  3693. }
  3694. //MARK: - KMNDisplayViewControllerDelegate代理
  3695. extension KMMainViewController: KMNDisplayViewControllerDelegate {
  3696. //Display Mode
  3697. func displayViewControllerDidDisplayModeChanged(_ controller: KMNDisplayViewController) {
  3698. listView.layoutDocumentView()
  3699. }
  3700. //阅读模式
  3701. func displayViewControllerDidReadModeUpdated(_ controller: KMNDisplayViewController) {
  3702. if viewManager.isPDFReadMode {
  3703. openPDFReadMode()
  3704. } else {
  3705. exitPDFReadMode()
  3706. }
  3707. }
  3708. //PPT
  3709. func displayViewControllerDidGotoSlideShow(_ controller: KMNDisplayViewController) {
  3710. togglePresentation(nil)
  3711. }
  3712. //SplitView
  3713. func displayViewControllerDidSplitModeChanged(_ controller: KMNDisplayViewController) {
  3714. reloadPDFSplitInfo()
  3715. }
  3716. func displayViewControllerDidSplitFileChanged(_ controller: KMNDisplayViewController) {
  3717. splitPDFController?.reloadData()
  3718. }
  3719. func displayViewControllerDidToolbarStateChanged(_ controller: KMNDisplayViewController) {
  3720. splitPDFController?.refreshToolbarState()
  3721. reloadPDFPageNumberToolbar()
  3722. }
  3723. }
  3724. //MARK: - PPT
  3725. extension KMMainViewController: KMInteractionProviderProtocol {
  3726. func providerContentView(fullScreenWindow: NSWindow, inset: CGFloat) -> NSView? {
  3727. if(interactionMode == .presentation) {
  3728. if listView.presentationDrawView == nil {
  3729. listView.createPresentationDraw()
  3730. }
  3731. presentationTopViewController = KMPresentationTopViewController.init(nibName: "KMPresentationTopViewController", bundle: nil)
  3732. presentationTopViewController?.pdfView = listView
  3733. presentationTopViewController?.delegate = self
  3734. presentationTopViewController?.isSelectionPre = true
  3735. listView.isPresentationMode = true
  3736. presentationTopViewController?.view.frame = CGRect(x: 0, y: (fullScreenWindow.contentView?.bounds.height ?? 0) - 42, width: fullScreenWindow.contentView?.bounds.width ?? 0, height: 42)
  3737. if((presentationTopViewController) != nil) {
  3738. fullScreenWindow.contentView?.addSubview(presentationTopViewController!.view)
  3739. }
  3740. } else {
  3741. listView.frame = NSInsetRect(fullScreenWindow.contentView?.bounds ?? .zero, 0, 0)
  3742. }
  3743. fullScreenWindow.contentView?.addSubview(listView)
  3744. if(interactionMode == .presentation) {
  3745. let frame = fullScreenWindow.frame
  3746. listView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height-42)
  3747. listView.autoresizingMask = [.width, .maxYMargin]
  3748. }
  3749. return view
  3750. }
  3751. @objc func willEnterInteractionModeNotification(_ sender: Notification) {
  3752. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  3753. return
  3754. }
  3755. let interactionMode = sender.userInfo?[NSWindow.UserInfo.interactionModeKey] as? KMInteractionMode
  3756. if interactionMode == .presentation {
  3757. let backgroundColor = NSColor.black
  3758. let level = UserDefaults.standard.bool(forKey: "SKUseNormalLevelForPresentationKey") ? NSWindow.Level.normal : NSWindow.Level.popUpMenu
  3759. let wasInteractionMode = self.interactionMode
  3760. if wasInteractionMode == .normal {
  3761. self.savedNormalSetup.setDictionary(self.currentPDFSettings() as! [AnyHashable : Any])
  3762. }
  3763. if wasInteractionMode == .legacyFullScreen {
  3764. self.enterPresentationMode()
  3765. self.listView.frame = (self.view.window?.contentView?.bounds)!
  3766. self.view.window?.contentView?.addSubview(listView)
  3767. // self.view.window?.backgroundColor = backgroundColor
  3768. self.view.window?.level = level
  3769. self.listView.layoutDocumentView()
  3770. self.listView.requiresDisplay()
  3771. }
  3772. } else {
  3773. KMPrint("2")
  3774. }
  3775. }
  3776. @objc func didEnterInteractionModeNotification(_ sender: Notification) {
  3777. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  3778. return
  3779. }
  3780. if self.interactionMode == .presentation {
  3781. self.listView.layoutDocumentView()
  3782. self.listView.requiresDisplay()
  3783. }
  3784. }
  3785. @objc func willShowFullScreenNotification(_ sender: Notification) {
  3786. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  3787. return
  3788. }
  3789. if self.interactionMode == .presentation {
  3790. let view = self.view.window?.firstResponder as? NSView
  3791. if let data = view?.isDescendant(of: self.pdfSplitView), data {
  3792. self.view.window?.makeFirstResponder(nil)
  3793. }
  3794. }
  3795. }
  3796. @objc func didShowFullScreenNotification(_ sender: Notification) {
  3797. guard let win = sender.object as? NSWindow, win.isEqual(to: self.view.window) else {
  3798. return
  3799. }
  3800. if self.interactionMode == .presentation {
  3801. self.enterPresentationMode()
  3802. }
  3803. }
  3804. }
  3805. // MARK: -KMPresentationTopViewControllerDelegate (幻灯片)
  3806. extension KMMainViewController: KMPresentationTopViewControllerDelegate {
  3807. func presentationTopViewExit(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  3808. self.exitFullScreen()
  3809. }
  3810. func presentationTopViewClear(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  3811. listView.presentationDrawView?.clear()
  3812. }
  3813. func presentationTopViewUndo(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton) {
  3814. let presentationDrawView = listView.presentationDrawView
  3815. if presentationDrawView?.canUndo() == true {
  3816. presentationDrawView?.undo()
  3817. }
  3818. }
  3819. func presentationTopViewType(_ presentationTopViewController: KMPresentationTopViewController, withButton: NSButton, isSeletion: Bool) {
  3820. listView.isPresentationMode = isSeletion
  3821. if listView.isEnterPresentationDrawMode() == true {
  3822. listView.exitPresentationDrawMode()
  3823. }
  3824. }
  3825. func presentationTopViewDrawColor(_ presentationTopViewController: KMPresentationTopViewController, withView: NSView,color:NSColor?) {
  3826. if color == nil{
  3827. listView.exitPresentationDrawMode()
  3828. } else {
  3829. if listView.isEnterPresentationDrawMode() == false {
  3830. listView.enterPresentationDrawMode()
  3831. }
  3832. listView.changePresentationDrawModelColor(color)
  3833. }
  3834. }
  3835. }
  3836. //MARK: - KMSplitPDFViewControllerDelegate SplitPDFView分屏视图
  3837. extension KMMainViewController: KMSplitPDFViewControllerDelegate {
  3838. func splitPDFViewControllerDidUpdateFilePath(_ controller: KMSplitPDFViewController) {
  3839. displaySettingController?.reloadData()
  3840. }
  3841. func splitPDFViewControllerDidUpdatePDFScale(_ controller: KMSplitPDFViewController) {
  3842. if let scaleFactor = controller.pdfView?.scaleFactor {
  3843. listView.scaleFactor = scaleFactor
  3844. }
  3845. }
  3846. func splitPDFViewControllerDidUpdatePDFPageIndex(_ controller: KMSplitPDFViewController) {
  3847. if let pageIndex = controller.pdfView?.currentPageIndex {
  3848. listView.go(toPageIndex: pageIndex, animated: false)
  3849. }
  3850. }
  3851. }
  3852. //MARK: - Edit编辑相关代理
  3853. extension KMMainViewController: KMEditToolbarViewDelegate {
  3854. func kmEditToolbarViewDidUpdateMode(_ view: KMEditToolbarView) {
  3855. if view.editType == .watermark {
  3856. watermarkViewController?.editSubType = editToolbarView?.editSubType ?? .template
  3857. watermarkViewController?.reloadData()
  3858. watermarkViewController?.loadData()
  3859. } else if view.editType == .background {
  3860. backgroundViewController?.editSubType = editToolbarView?.editSubType ?? .template
  3861. backgroundViewController?.resetUI()
  3862. backgroundViewController?.reloadData()
  3863. } else if view.editType == .header_Footer {
  3864. headerFooterViewController?.editSubType = editToolbarView?.editSubType ?? .template
  3865. headerFooterViewController?.resetUI()
  3866. headerFooterViewController?.reloadData()
  3867. } else if view.editType == .bates {
  3868. batesViewController?.editSubType = editToolbarView?.editSubType ?? .template
  3869. batesViewController?.resetUI()
  3870. batesViewController?.reloadData()
  3871. }
  3872. }
  3873. func kmEditToolbarViewDidChooseBatch(_ view: KMEditToolbarView) {
  3874. if view.editType == .watermark {
  3875. self.showBatchWindow(type: .watermark, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  3876. } else if view.editType == .background {
  3877. self.showBatchWindow(type: .background, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  3878. } else if view.editType == .header_Footer {
  3879. self.showBatchWindow(type: .headerAndFooter, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  3880. } else if view.editType == .bates {
  3881. self.showBatchWindow(type: .batesNumber, files: [URL(fileURLWithPath: self.document?.documentURL.path ?? "")])
  3882. }
  3883. }
  3884. func kmEditToolbarViewDidChooseApply(_ view: KMEditToolbarView) {
  3885. let pageInfo = view.pageRangeSelectView.getSelectedPageIndex(listView.document)
  3886. let pageIndex = pageInfo.0
  3887. let isCurrentPage = pageInfo.1
  3888. if pageIndex.isEmpty {
  3889. let alert = NSAlert()
  3890. alert.alertStyle = .critical
  3891. alert.messageText = KMLocalizedString("Invalid page range or the page number is out of range. Please try again.")
  3892. alert.runModal()
  3893. return
  3894. }
  3895. var pageString = view.pageRangeSelectView.getSelectedPageString(listView.document, pageIndex)
  3896. if isCurrentPage {
  3897. pageString = String(format: "%ld", listView.currentPageIndex)
  3898. }
  3899. if view.editType == .watermark {
  3900. if let model = watermarkViewController?.currentWatermarkData {
  3901. let watermark = KMWatermarkModel.returnWaterMarkWith(model, listView.document)
  3902. watermark.pageString = pageString
  3903. listView.document.addWatermark(watermark)
  3904. listView.layoutDocumentView()
  3905. }
  3906. exitEditToolbarView()
  3907. } else if view.editType == .background {
  3908. if let model = backgroundViewController?.backgroundModel {
  3909. if let background = listView.document.background() {
  3910. KMBackgroundManager.defaultManager.updateBackground(background, withModel: model)
  3911. background.pageString = pageString
  3912. background.update()
  3913. listView.document?.refreshPageData()
  3914. listView.layoutDocumentView()
  3915. }
  3916. }
  3917. exitEditToolbarView()
  3918. } else if view.editType == .header_Footer {
  3919. if let model = headerFooterViewController?.headerFooterModel {
  3920. if let headerFooter = listView.document.headerFooter() {
  3921. KMHeaderFooterManager.defaultManager.updateCPDFHeaderFooter(headerFooter, withModel: model, Int(listView.document.pageCount))
  3922. headerFooter.pageString = pageString
  3923. headerFooter.update()
  3924. listView.document?.refreshPageData()
  3925. listView.layoutDocumentView()
  3926. }
  3927. }
  3928. exitEditToolbarView()
  3929. } else if view.editType == .bates {
  3930. if let model = batesViewController?.batesModel {
  3931. if let bates = listView.document.bates() {
  3932. KMBatesManager.defaultManager.updateCPDFBates(bates, withModel: model, Int(listView.document.pageCount))
  3933. bates.pageString = pageString
  3934. bates.update()
  3935. listView.document?.refreshPageData()
  3936. listView.layoutDocumentView()
  3937. }
  3938. }
  3939. exitEditToolbarView()
  3940. }
  3941. }
  3942. func kmEditToolbarViewDidChooseExit(_ view: KMEditToolbarView) {
  3943. if view.applyEnable {
  3944. let alert = NSAlert()
  3945. if view.editType == .watermark {
  3946. alert.messageText = KMLocalizedString("There are unapplied Watermark settings, do you want to apply them?", comment: "")
  3947. } else if view.editType == .background {
  3948. alert.messageText = KMLocalizedString("There are unapplied Background settings, do you want to apply them?", comment: "")
  3949. } else if view.editType == .header_Footer {
  3950. alert.messageText = KMLocalizedString("There are unapplied Header & Footer settings, do you want to apply them?", comment: "")
  3951. } else if view.editType == .bates {
  3952. alert.messageText = KMLocalizedString("There are unapplied Bates settings, do you want to apply them?", comment: "")
  3953. }
  3954. alert.informativeText = KMLocalizedString("If not, the changes will be lost.", comment: "")
  3955. alert.addButton(withTitle: KMLocalizedString("Apply", comment: ""))
  3956. alert.addButton(withTitle: KMLocalizedString("Don't Apply", comment: ""))
  3957. alert.addButton(withTitle: KMLocalizedString("Cancel", comment: ""))
  3958. let result = alert.runModal()
  3959. if (result == .alertFirstButtonReturn) {
  3960. self.kmEditToolbarViewDidChooseApply(view)
  3961. } else if (result == .alertSecondButtonReturn) {
  3962. //"Don't Apply"
  3963. exitEditToolbarView()
  3964. } else if (result == .alertThirdButtonReturn) {
  3965. //Cancel
  3966. }
  3967. } else {
  3968. exitEditToolbarView()
  3969. }
  3970. }
  3971. }
  3972. //MARK: - KMCropControllerDelegate 裁剪相关代理
  3973. extension KMMainViewController: KMCropControllerDelegate {
  3974. func kmCropControllerDidCrop(_ controller: KMCropController, _ cropRect: CGRect, _ view: KMPageRangeSelectView) {
  3975. let rangeSelectResult = view.getSelectedPageIndex(listView.document)
  3976. let indexs = rangeSelectResult.0
  3977. let isCurrentPage = rangeSelectResult.1
  3978. var uIndexs: [UInt] = []
  3979. for index in indexs {
  3980. if index > 0 {
  3981. uIndexs.append(UInt(index-1))
  3982. }
  3983. }
  3984. if isCurrentPage {
  3985. uIndexs = [UInt(listView.currentPageIndex)]
  3986. }
  3987. cropPages(atIndexs: uIndexs, to: [cropRect])
  3988. removeCropController()
  3989. viewManager.subToolMode = .None
  3990. if let toolbarVC = self.pdfToolbarController {
  3991. toolbarVC.refreshSecondToolbarItemsState()
  3992. self.kmPDFToolbarControllerDidToolbarItemClicked(toolbarVC, KMPDFToolbar_edit_crop_Identifier)
  3993. }
  3994. }
  3995. func kmCropControllerDidCropSeparate(_ controller: KMCropController, _ view: KMPageRangeSelectView) {
  3996. let rangeSelectResult = view.getSelectedPageIndex(listView.document)
  3997. let indexs = rangeSelectResult.0
  3998. let isCurrentPage = rangeSelectResult.1
  3999. var rectArray: Array<NSRect> = []
  4000. var uIndexs: [UInt] = []
  4001. for index in indexs {
  4002. if index > 0 {
  4003. uIndexs.append(UInt(index-1))
  4004. let page = self.listView.document.page(at: UInt(index-1))
  4005. let rect = KMCropTools.getPageForegroundBox(page!)
  4006. rectArray.append(rect)
  4007. }
  4008. }
  4009. if isCurrentPage {
  4010. uIndexs = [UInt(listView.currentPageIndex)]
  4011. }
  4012. cropPages(atIndexs: uIndexs, to: rectArray)
  4013. removeCropController()
  4014. viewManager.subToolMode = .None
  4015. if let toolbarVC = self.pdfToolbarController {
  4016. toolbarVC.refreshSecondToolbarItemsState()
  4017. self.kmPDFToolbarControllerDidToolbarItemClicked(toolbarVC, KMPDFToolbar_edit_crop_Identifier)
  4018. }
  4019. }
  4020. func kmCropControllerDidCropAuto(_ controller: KMCropController, _ view: KMPageRangeSelectView) {
  4021. let rangeSelectResult = view.getSelectedPageIndex(listView.document)
  4022. let indexs = rangeSelectResult.0
  4023. let isCurrentPage = rangeSelectResult.1
  4024. var uIndexs: [UInt] = []
  4025. for index in indexs {
  4026. if index > 0 {
  4027. uIndexs.append(UInt(index-1))
  4028. }
  4029. }
  4030. if isCurrentPage {
  4031. uIndexs = [UInt(listView.currentPageIndex)]
  4032. }
  4033. auto_cropPagesWhiteMargin(uIndexs)
  4034. removeCropController()
  4035. viewManager.subToolMode = .None
  4036. if let toolbarVC = self.pdfToolbarController {
  4037. toolbarVC.refreshSecondToolbarItemsState()
  4038. self.kmPDFToolbarControllerDidToolbarItemClicked(toolbarVC, KMPDFToolbar_edit_crop_Identifier)
  4039. }
  4040. }
  4041. func kmCropControllerDidChangedSelectionOrMagnification(_ controller: KMCropController) {
  4042. if let cropVC = rightSideController?.edit_cropController {
  4043. if cropVC.pdfView != controller.pdfView {
  4044. cropVC.pdfView = controller.pdfView
  4045. }
  4046. cropVC.cropSeparateOn = false
  4047. cropVC.cropAutoOn = false
  4048. cropVC.reloadData()
  4049. }
  4050. }
  4051. }
  4052. //MARK: - KMWatermarkControllerDelegate 水印相关代理
  4053. extension KMMainViewController: KMWatermarkControllerDelegate {
  4054. func kmWatermarkControllerDidUpdateMode(_ view: KMWatermarkController) {
  4055. editToolbarView?.editSubType = view.editSubType
  4056. editToolbarView?.reloadData()
  4057. }
  4058. func kmWatermarkControllerDidWatermarkUpdated(_ view: KMWatermarkController) {
  4059. var applyEnable = true
  4060. if let model = view.currentWatermarkData {
  4061. if model.watermarkType == .text {
  4062. if model.text == nil || model.text?.count == 0 {
  4063. applyEnable = false
  4064. }
  4065. } else if model.watermarkType == .image {
  4066. if model.imagePath == nil {
  4067. applyEnable = false
  4068. }
  4069. }
  4070. } else {
  4071. applyEnable = false
  4072. }
  4073. editToolbarView?.applyEnable = applyEnable
  4074. editToolbarView?.reloadData()
  4075. }
  4076. }
  4077. //MARK: - KMBackgroundControllerDelegate 背景代理
  4078. extension KMMainViewController: KMBackgroundControllerDelegate {
  4079. func kmBackgroundControllerDidUpdateMode(_ view: KMBackgroundController) {
  4080. editToolbarView?.editSubType = view.editSubType
  4081. }
  4082. func kmBackgroundControllerDidBGDataUpdated(_ view: KMBackgroundController) {
  4083. var applyEnable = true
  4084. if let model = view.backgroundModel {
  4085. if model.type == .image {
  4086. if model.imagePath == nil {
  4087. applyEnable = false
  4088. }
  4089. }
  4090. } else {
  4091. applyEnable = false
  4092. }
  4093. editToolbarView?.applyEnable = applyEnable
  4094. editToolbarView?.reloadData()
  4095. }
  4096. }
  4097. //MARK: - KMHeaderFooterControllerDelegate 页眉页脚代理
  4098. extension KMMainViewController: KMHeaderFooterControllerDelegate {
  4099. func kmHeaderFooterControllerDidUpdateModeType(_ view: KMHeaderFooterController) {
  4100. editToolbarView?.editSubType = view.editSubType
  4101. }
  4102. func kmHeaderFooterControllerDidModelDataUpdated(_ view: KMHeaderFooterController) {
  4103. var applyEnable = true
  4104. if let model = view.headerFooterModel {
  4105. if model.topLeftString.count == 0 && model.topCenterString.count == 0 && model.topRightString.count == 0 &&
  4106. model.bottomLeftString.count == 0 && model.bottomCenterString.count == 0 && model.bottomRightString.count == 0 {
  4107. applyEnable = false
  4108. }
  4109. } else {
  4110. applyEnable = false
  4111. }
  4112. editToolbarView?.applyEnable = applyEnable
  4113. editToolbarView?.reloadData()
  4114. }
  4115. }
  4116. //MARK: - KMBatesControllerDelegate Bates贝茨码代理
  4117. extension KMMainViewController: KMBatesControllerDelegate {
  4118. func kmBatesControllerDidUpdateMode(_ view: KMBatesController) {
  4119. editToolbarView?.editSubType = view.editSubType
  4120. editToolbarView?.reloadData()
  4121. }
  4122. func kmBatesControllerDidModelDataUpdated(_ view: KMBatesController) {
  4123. var applyEnable = true
  4124. if let model = view.batesModel {
  4125. if model.topLeftString.count == 0 && model.topCenterString.count == 0 && model.topRightString.count == 0 &&
  4126. model.bottomLeftString.count == 0 && model.bottomCenterString.count == 0 && model.bottomRightString.count == 0 {
  4127. applyEnable = false
  4128. }
  4129. } else {
  4130. applyEnable = false
  4131. }
  4132. editToolbarView?.applyEnable = applyEnable
  4133. editToolbarView?.reloadData()
  4134. }
  4135. }
  4136. //MARK: - CPDFViewDelegate PDFView代理
  4137. extension KMMainViewController: CPDFViewDelegate,CPDFListViewDelegate {
  4138. func pdfViewDocumentDidLoaded(_ pdfView: CPDFView!) {
  4139. sideBarController?.reloadPageTurnerData()
  4140. documentLoadComplete()
  4141. }
  4142. func pdfViewCurrentPageDidChanged(_ pdfView: CPDFView!) {
  4143. sideBarController?.reloadPageTurnerData()
  4144. botaViewController?.currentPageDidChangedAction(listView: listView)
  4145. //分屏视图
  4146. reloadPDFPageNumberToolbar()
  4147. if viewManager.splitSyncScroll {
  4148. if splitPDFController?.inPDFFirst == false && splitPDFController?.outPDFFirst == false {
  4149. splitPDFController?.outPDFFirst = true
  4150. if let splitPDFView = splitPDFController?.pdfView, let document = splitPDFView.document {
  4151. var index = pdfView.currentPageIndex
  4152. if index > document.pageCount {
  4153. index = Int(splitPDFView.document.pageCount - 1)
  4154. }
  4155. splitPDFView.go(toPageIndex: index, animated: false)
  4156. }
  4157. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  4158. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  4159. } else if splitPDFController?.outPDFFirst == true {
  4160. if let splitPDFView = splitPDFController?.pdfView, let document = splitPDFView.document {
  4161. var index = pdfView.currentPageIndex
  4162. if index > document.pageCount {
  4163. index = Int(splitPDFView.document.pageCount - 1)
  4164. }
  4165. splitPDFView.go(toPageIndex: index, animated: false)
  4166. }
  4167. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  4168. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  4169. }
  4170. }
  4171. //水印
  4172. updateEditModeDocumentWhenPageChanged()
  4173. //
  4174. }
  4175. func pdfViewScaleDidChanged(_ pdfView: CPDFView!) {
  4176. pdfToolbarController?.reloadSelectZoomView()
  4177. reloadPDFPageNumberToolbar()
  4178. reloadPopUIWindow()
  4179. //分屏视图
  4180. if viewManager.splitSyncScroll {
  4181. if splitPDFController?.inPDFFirst == false && splitPDFController?.outPDFFirst == false {
  4182. splitPDFController?.outPDFFirst = true
  4183. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  4184. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  4185. } else if splitPDFController?.outPDFFirst == true {
  4186. if let splitPDFView = splitPDFController?.pdfView {
  4187. splitPDFView.scaleFactor = pdfView.scaleFactor
  4188. }
  4189. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(pdfUpdatedFinish), object: nil)
  4190. self.perform(#selector(pdfUpdatedFinish), with: nil, afterDelay: 0.35)
  4191. }
  4192. }
  4193. }
  4194. func pdfViewDidClick(onLink pdfView: CPDFView!, withURL url: String!) {
  4195. if let urlString = url, urlString == Store_Link {
  4196. //跳转订阅比较表
  4197. return
  4198. }
  4199. if url.hasPrefix("smb://") {
  4200. NSWorkspace.shared.openFile(url)
  4201. } else {
  4202. KMTools.openURL(urlString: url)
  4203. }
  4204. }
  4205. func pdfViewPerformURL(_ pdfView: CPDFView!, withContent content: String!) {
  4206. KMPrint("pdfViewPerformURL")
  4207. if content != nil {
  4208. NSWorkspace.shared.open(URL(string: content)!)
  4209. } else {
  4210. let alert = NSAlert()
  4211. alert.alertStyle = .critical
  4212. alert.informativeText = KMLocalizedString("The hyperlink is invalid.", comment: "")
  4213. alert.messageText = ""
  4214. alert.addButton(withTitle: KMLocalizedString("OK", comment: ""))
  4215. alert.runModal()
  4216. return
  4217. }
  4218. }
  4219. func pdfViewPerformPrint(_ pdfView: CPDFView!) {
  4220. KMPrint("pdfViewPerformPrint")
  4221. self.showPrintWindow()
  4222. }
  4223. func pdfViewPerformGo(toPage pdfView: CPDFView!) {
  4224. KMPrint("pdfViewPerformGo")
  4225. }
  4226. func pdfViewOpenPDF(_ pdfView: CPDFView!, forRemoteGoTo action: CPDFAction!) {
  4227. KMPrint("pdfViewOpenPDF")
  4228. }
  4229. func pdfViewPerformReset(_ pdfView: CPDFView!) {
  4230. KMPrint("pdfViewPerformReset")
  4231. pdfView.document?.resetForm()
  4232. }
  4233. func pdfViewEditingBlockDidChanged(_ pdfView: CPDFView!) {
  4234. KMPrint("pdfViewEditingBlockDidChanged")
  4235. }
  4236. func pdfViewAsBookBookmark() -> NSImage! {
  4237. return NSImage(named: "KMImageNameBookmarkIcon")!
  4238. }
  4239. //MARK: -编辑模块
  4240. func pdfViewEditingSelectionDidChanged(_ pdfView: CPDFView!) {
  4241. // 文本区块 选中文本已经变化
  4242. reloadPopUIWindow()
  4243. }
  4244. func pdfViewPDFSelectionAttributeDidChanged(_ pdfView: CPDFView!) {
  4245. reloadPopUIWindow()
  4246. }
  4247. func pdfViewEditingAreaDidChanged(_ pdfView: CPDFView!) {
  4248. //编辑模块变化
  4249. rightSideController?.reloadDataWithPDFView(pdfView: (pdfView as! CPDFListView))
  4250. if pdfView is CPDFListView {
  4251. (pdfView as! CPDFListView).isEditImage = false
  4252. }
  4253. if let areas = pdfView.editingAreas(), areas.count > 0 {
  4254. if viewManager.showRightSide == false && SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  4255. viewManager.showRightSide = true
  4256. self.refreshToolbarRightViewInfo()
  4257. }
  4258. } else {
  4259. if viewManager.subToolMode == .None {
  4260. viewManager.showRightSide = false
  4261. self.refreshToolbarRightViewInfo()
  4262. }
  4263. }
  4264. reloadPopUIWindow()
  4265. }
  4266. func pdfViewEditingCropBoundsDidChanged(_ pdfView: CPDFView!, editing editArea: CPDFEditArea!) {
  4267. if editArea != nil && (editArea is CPDFEditImageArea){
  4268. self.listView.cropAreas = editArea as? CPDFEditImageArea
  4269. }
  4270. reloadPopUIWindow()
  4271. }
  4272. //编辑PDF 创建图片区域回调
  4273. func pdfViewEditingAddImageArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  4274. if (pdfView as! CPDFListView).isEditImage {
  4275. return
  4276. }
  4277. if listView.editingAreas().count > 0 {
  4278. pdfView.updateEditing([])
  4279. return
  4280. }
  4281. let panel = NSOpenPanel()
  4282. panel.allowsMultipleSelection = false
  4283. panel.allowedFileTypes = ["png","jpg"]
  4284. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  4285. if response == .OK {
  4286. var filePath = panel.url?.path
  4287. let image = NSImage.init(contentsOf: panel.url!)
  4288. //图片自适应范围
  4289. if image != nil {
  4290. var imageRect = rect
  4291. let imageSize = image!.size
  4292. var previewSize = rect.size
  4293. var isChangeSize = false
  4294. if previewSize.width == 0 && previewSize.height == 0 {
  4295. previewSize = CGSize(width: 500, height: 500)
  4296. isChangeSize = true
  4297. }
  4298. var scale = min(previewSize.width / imageSize.width, previewSize.height / imageSize.height)
  4299. if scale < 1 { // 大于 500
  4300. } else {
  4301. let wh = max(imageSize.width, imageSize.height)
  4302. if wh >= 72 {
  4303. scale = min(scale, 1)
  4304. } else {
  4305. scale = min(72 / imageSize.width, 72 / imageSize.height)
  4306. }
  4307. }
  4308. let newSize = CGSize(width: imageSize.width * scale, height: imageSize.height * scale)
  4309. if isChangeSize {
  4310. imageRect.origin.x = imageRect.origin.x - newSize.width / 2
  4311. imageRect.origin.y = imageRect.origin.y - newSize.height / 2
  4312. } else {
  4313. imageRect.origin.x = imageRect.origin.x + imageRect.width / 2 - newSize.width / 2
  4314. imageRect.origin.y = imageRect.origin.y + imageRect.height / 2 - newSize.height / 2
  4315. }
  4316. imageRect.size = newSize
  4317. let limitWidth = 1920.0
  4318. if imageSize.width > limitWidth || imageSize.height > limitWidth {
  4319. filePath = KMImageOptimization.needCompressImageLosslessly(image: image!,
  4320. targetSize: CGSize(width: limitWidth, height: limitWidth),
  4321. maxSizeInBytes: 1024 * 1024 * 5,
  4322. targetCompression: 1.0)
  4323. }
  4324. //自适应page
  4325. let pageRect = self.listView.currentPage().bounds ?? .zero
  4326. if imageRect.width > pageRect.width ||
  4327. imageRect.height > pageRect.height {
  4328. let pageScale = min(pageRect.width / imageSize.width, pageRect.height / imageSize.height)
  4329. imageRect = CGRect(x: imageRect.origin.x,
  4330. y: imageRect.origin.y,
  4331. width: imageRect.width * pageScale,
  4332. height: imageRect.height * pageScale)
  4333. }
  4334. if imageRect.origin.x < 0 {
  4335. imageRect.origin.x = 5
  4336. }
  4337. if imageRect.origin.y < 0 {
  4338. imageRect.origin.y = 5
  4339. }
  4340. if imageRect.origin.x + imageRect.width > pageRect.width ||
  4341. imageRect.origin.y + imageRect.height > pageRect.height {
  4342. let offsetX = imageRect.origin.x + imageRect.width - pageRect.width
  4343. let offsetY = imageRect.origin.y + imageRect.height - pageRect.height
  4344. imageRect.origin.x = imageRect.origin.x - offsetX - 5
  4345. imageRect.origin.y = imageRect.origin.y - offsetY - 5
  4346. }
  4347. DispatchQueue.main.async {
  4348. self.listView.createImagePath(filePath, rect: imageRect, page: pdfView.currentPage())
  4349. }
  4350. }
  4351. }
  4352. }
  4353. }
  4354. func pdfViewEditingAddTextArea(_ pdfView: CPDFView!, add page: CPDFPage!, add rect: CGRect) {
  4355. let areas = self.listView.km_EditingAreas()
  4356. if let area = areas.last {
  4357. if let data = area as? CPDFEditTextArea {
  4358. if let str = data.editTextAreaString(), str.isEmpty {
  4359. self.listView.remove(with: [area])
  4360. } else {
  4361. self.listView.updateEditing([])
  4362. return
  4363. }
  4364. }
  4365. }
  4366. var newRect = rect
  4367. if rect.size.equalTo(.zero) {
  4368. newRect = CGRect(x: rect.origin.x, y: rect.origin.y - 12, width: 20, height: 12)
  4369. } else {
  4370. newRect = CGRect(x: rect.origin.x, y: rect.origin.y + rect.size.height - 12, width: rect.size.width, height: 12)
  4371. }
  4372. let fontSize = CPDFEditTextArea.defaultFontSize()
  4373. let fontColor = CPDFEditTextArea.defaultColor()
  4374. let fontAlign = CPDFEditTextArea.defaultFontAlignment()
  4375. NSColorPanel.shared.color = fontColor
  4376. let attri = CEditAttributes()
  4377. attri.cFont = CPDFFont(familyName: CPDFEditTextArea.defaultFontName(), fontStyle: CPDFEditTextArea.defaultFontStyle())
  4378. attri.fontColor = fontColor
  4379. attri.alignment = fontAlign
  4380. attri.fontSize = fontSize
  4381. attri.isBold = CPDFEditTextArea.defaultIsBold()
  4382. attri.isItalic = CPDFEditTextArea.defaultIsItality()
  4383. self.listView.createStringBounds(newRect, with: attri, page: page)
  4384. }
  4385. func pdfViewMobileEditingBegan(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  4386. rightSideController?.reloadEditingAreas()
  4387. toggleClosePopUIWindow()
  4388. }
  4389. func pdfViewMobileEditingMove(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  4390. rightSideController?.reloadEditingAreas()
  4391. toggleClosePopUIWindow()
  4392. }
  4393. func pdfViewMobileEditingEnd(_ point: CGPoint, for pdfView: CPDFView!, forEditing editingAreas: [CPDFEditArea]!) {
  4394. rightSideController?.reloadEditingAreas()
  4395. reloadPopUIWindow()
  4396. }
  4397. func pdfViewEditingSelectCharDidChanged(_ pdfView: CPDFView!) {
  4398. rightSideController?.reloadEditingAreas()
  4399. reloadPopUIWindow()
  4400. }
  4401. func pdfViewEditingExitCropMode(_ pdfView: CPDFView!, forEditing editingArea: CPDFEditImageArea!) {
  4402. rightSideController?.reloadEditingAreas()
  4403. toggleClosePopUIWindow()
  4404. }
  4405. func pdfViewEditingOperationDidChanged(_ pdfView: CPDFView!) {
  4406. let areas = self.listView.km_editingImageAreas()
  4407. if areas.count == 1 {
  4408. if let data = areas.first as? CPDFEditImageArea {
  4409. let updating = self.listView.editAreaBoundUpdating
  4410. if updating {
  4411. self.listView.editAreaBoundUpdating = false
  4412. }
  4413. }
  4414. }
  4415. }
  4416. func pdfViewEditingDoubleClick(_ pdfView: CPDFView!, imageArea editArea: CPDFEditArea!) {
  4417. if(editArea.isImageArea()) {
  4418. listView.cropAction()
  4419. rightSideController?.reloadEditingAreas()
  4420. }
  4421. }
  4422. //MARK: - 键盘事件
  4423. func pdfListViewKeyDownIsContinue(_ pdfListView: CPDFListView!, theEvent: NSEvent!) -> Bool {
  4424. let command = theEvent.modifierFlags.contains(.command)
  4425. let control = theEvent.modifierFlags.contains(.control)
  4426. let shift = theEvent.modifierFlags.contains(.shift)
  4427. let option = theEvent.modifierFlags.contains(.option)
  4428. KMPrint(theEvent.keyCode)
  4429. if self.listView.isEditing() == true {
  4430. if control && theEvent.keyCode == 11 { // ctr + b
  4431. self.listView.setEditingTextarea_Bold()
  4432. rightSideController?.reloadEditingAreas()
  4433. return false
  4434. } else if control && theEvent.keyCode == 34 { // ctr +i
  4435. self.listView.setEditingTextarea_Italic()
  4436. rightSideController?.reloadEditingAreas()
  4437. return false
  4438. } else if theEvent.keyCode == 36 { // enter
  4439. if self.listView.isCropMode {
  4440. self.listView.cropComfirmAction()
  4441. rightSideController?.reloadEditingAreas()
  4442. return false
  4443. }
  4444. } else if theEvent.keyCode == 53 { // Cancel
  4445. if self.listView.isCropMode {
  4446. self.listView.cropCancelAction()
  4447. rightSideController?.reloadEditingAreas()
  4448. return false
  4449. }
  4450. }
  4451. }
  4452. if (theEvent.keyCode == 11 && command) { // command + B [添加书签]
  4453. self.menuItemBookMarkClick_add(sender: NSMenuItem())
  4454. return false
  4455. } else if (command && control && theEvent.keyCode == 14) { // command + control + E [注释 橡皮擦]
  4456. return false
  4457. } else if (theEvent.keyCode == 123) { // 向左
  4458. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  4459. return false
  4460. } else {
  4461. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToPreviousPage()) {
  4462. self.listView.goToPreviousPage(nil)
  4463. return false
  4464. }
  4465. }
  4466. } else if (theEvent.keyCode == 126) { // 向上
  4467. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  4468. return false
  4469. } else {
  4470. if (self.listView.isContinousScroll()) {
  4471. return true
  4472. }
  4473. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToPreviousPage()) {
  4474. self.listView.goToPreviousPage(nil)
  4475. return false
  4476. }
  4477. }
  4478. } else if (theEvent.keyCode == 124) { // 向右
  4479. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  4480. return false
  4481. } else {
  4482. if (self.pdfViewCanHorizontalScroll() == false && self.listView.canGoToNextPage()) {
  4483. self.listView.goToNextPage(nil)
  4484. return false
  4485. }
  4486. }
  4487. } else if (theEvent.keyCode == 125) { // 向下
  4488. if(self.listView.isEditing() && !self.listView.isSelecteditAreaNotEdit()) {
  4489. return false
  4490. } else {
  4491. if (self.listView.isContinousScroll()) {
  4492. return true
  4493. }
  4494. if (self.pdfViewCanVerticalScroll() == false && self.listView.canGoToNextPage()) {
  4495. self.listView.goToNextPage(nil)
  4496. return false
  4497. }
  4498. }
  4499. } else if (theEvent.keyCode == 36) {
  4500. if self.listView.shouAddEditAreaType() == .image || self.listView.shouAddEditAreaType() == .text {
  4501. if self.listView.isEditImage {
  4502. self.menuItemEditingClick_CropImage(sender: NSMenuItem())
  4503. }
  4504. }
  4505. } else if option && theEvent.keyCode == 18 {
  4506. //option + 1
  4507. updatePDFViewToolsType(.Select)
  4508. } else if option && theEvent.keyCode == 19 {
  4509. //option + 2
  4510. updatePDFViewToolsType(.Scroll)
  4511. } else if option && theEvent.keyCode == 20 {
  4512. //option + 3
  4513. updatePDFViewToolsType(.Content_Selection)
  4514. } else if option && theEvent.keyCode == 21 {
  4515. //option + 4
  4516. updatePDFViewToolsType(.Magnify)
  4517. } else if option && theEvent.keyCode == 23 {
  4518. //option + 5
  4519. updatePDFViewToolsType(.AreaZoom)
  4520. } else if theEvent.keyCode == 53 {
  4521. //ESC
  4522. self.exitFullScreen()
  4523. if viewManager.isPDFReadMode {
  4524. self.exitPDFReadMode()
  4525. }
  4526. self.leftSideViewCancelSelect()
  4527. }
  4528. return true
  4529. }
  4530. func pdfListViewMenuValidate(_ pdfListView: CPDFListView!, menuItem: NSMenuItem!, isTakesEffect: UnsafeMutablePointer<ObjCBool>!) -> Bool {
  4531. guard let action = menuItem.action else {
  4532. isTakesEffect.pointee = false
  4533. return false
  4534. }
  4535. isTakesEffect.pointee = false
  4536. return false
  4537. }
  4538. //MARK: - CPDFListViewDelegate
  4539. func pdfListViewChangedToolMode(_ pdfListView: CPDFListView!, for toolMode: CToolMode) {
  4540. reloadPopUIWindow()
  4541. }
  4542. func pdfListViewChangedAnnotationType(_ pdfListView: CPDFListView!, for annotationType: CAnnotationType) {
  4543. if(annotationType == .unkown) {
  4544. toggleCloseRightSide()
  4545. if viewManager.subToolMode != .None {
  4546. viewManager.subToolMode = .None
  4547. pdfToolbarController?.cancelSelectedSecondToolbarItems(viewManager.toolMode)
  4548. pdfToolbarController?.refreshSecondToolbarItemsState()
  4549. }
  4550. cancelMeasureType()
  4551. } else if annotationType == .measureLine ||
  4552. annotationType == .measurePolyLine ||
  4553. annotationType == .measurePolyGon ||
  4554. annotationType == .measureSquare {
  4555. refreshMeasureInfo()
  4556. } else {
  4557. cancelMeasureType()
  4558. }
  4559. }
  4560. func pdfListViewChangeatioActiveAnnotations(_ pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!, isRightMenu: Bool) {
  4561. self.view.window?.makeFirstResponder(self.listView)
  4562. reloadPopUIWindow()
  4563. if isRightMenu {
  4564. } else if annotations.count > 0 {
  4565. if self.viewManager.isPDFReadMode {
  4566. toggleCloseRightSide()
  4567. } else {
  4568. let isMultiAnnotations = pdfListView.isMultiAnnotation(annotations)
  4569. if isMultiAnnotations == true {
  4570. viewManager.showRightSide = false
  4571. } else {
  4572. if SettingsManager.sharedInstance.autoExpandPropertyPanel == true {
  4573. viewManager.showRightSide = true
  4574. }
  4575. }
  4576. refreshToolbarRightViewInfo()
  4577. }
  4578. } else if (annotations.count == 0){
  4579. if pdfListView.annotationType == .unkown {
  4580. viewManager.showRightSide = false
  4581. } else {
  4582. if self.viewManager.isPDFReadMode {
  4583. viewManager.showRightSide = false
  4584. } else {
  4585. }
  4586. }
  4587. refreshToolbarRightViewInfo()
  4588. }
  4589. self.refreshMeasureInfo()
  4590. //
  4591. pdfToolbarController?.pdfViewActiveAnnotationsChanged()
  4592. }
  4593. func pdfListViewMenu(forEvent pdfListView: CPDFListView!, for theEvent: NSEvent!, click menu: AutoreleasingUnsafeMutablePointer<NSMenu?>!, isMoveSelectAnno: Bool) {
  4594. toggleClosePopUIWindow()
  4595. self.view.window?.orderFront(nil)
  4596. var pagePoint = CGPoint.zero
  4597. let point = self.view.convert(theEvent.locationInWindow, from: nil)
  4598. let isShowPopUI:Bool = !SettingsManager.sharedInstance.showQuickActionBar
  4599. if let page = pdfListView.pageAndPoint(&pagePoint, for: theEvent, nearest: false) {
  4600. if view.window?.interactionMode == .presentation {
  4601. let menuStruct = clickPresentationMenu(point: pagePoint)
  4602. groupListMenuGroup?.groupDelegate = self
  4603. groupListMenuGroup?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  4604. groupListMenuGroup?.updateGroupInfo(menuStruct.menuitems)
  4605. groupListMenuGroup?.showWithPoint(CGPoint(x: point.x, y: point.y - menuStruct.viewHeight), relativeTo: nil)
  4606. } else {
  4607. var menuStringArr: [String] = []
  4608. let activeAnnotations = pdfListView.activeAnnotations as? [CPDFAnnotation]
  4609. if activeAnnotations?.count ?? 0 > 1 {
  4610. var isSameAnnotation = true
  4611. let firstAnnotation = activeAnnotations?.first
  4612. for i in 1..<(activeAnnotations?.count ?? 1) {
  4613. if firstAnnotation?.type != activeAnnotations?[i].type {
  4614. isSameAnnotation = false
  4615. break
  4616. }
  4617. }
  4618. if isSameAnnotation {
  4619. if firstAnnotation?.isKind(of: CPDFMarkupAnnotation.self) == true {
  4620. menuStringArr.append(PDFViewMenuIdentifier_Normal_CopyText)
  4621. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4622. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4623. } else {
  4624. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4625. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4626. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4627. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4628. }
  4629. if(isShowPopUI){
  4630. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4631. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4632. }
  4633. menuStringArr.append(PDFViewMenuIdentifier_Normal_SortAnnotation)
  4634. if firstAnnotation?.isKind(of: CPDFLinkAnnotation.self) == true || firstAnnotation?.isKind(of: CPDFWidgetAnnotation.self) == true {
  4635. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4636. menuStringArr.append(PDFViewMenuIdentifier_Normal_Aligning)
  4637. }
  4638. } else {
  4639. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4640. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4641. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4642. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4643. menuStringArr.append(PDFViewMenuIdentifier_Normal_SortAnnotation)
  4644. if firstAnnotation?.isKind(of: CPDFWidgetAnnotation.self) == true {
  4645. menuStringArr.append(PDFViewMenuIdentifier_Normal_Aligning)
  4646. }
  4647. }
  4648. } else if activeAnnotations?.count == 1 {
  4649. if pdfListView.activeAnnotation.isKind(of: CPDFMarkupAnnotation.self) {
  4650. menuStringArr.append(PDFViewMenuIdentifier_Normal_CopyText)
  4651. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4652. if(isShowPopUI){
  4653. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4654. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4655. }
  4656. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4657. menuStringArr.append(PDFViewMenuIdentifier_Normal_SortAnnotation)
  4658. } else if pdfListView.activeAnnotation.isKind(of: CPDFRedactAnnotation.self) {
  4659. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4660. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4661. menuStringArr.append(PDFViewMenuIdentifier_Redact_Apply)
  4662. menuStringArr.append(PDFViewMenuIdentifier_Redact_Multipage)
  4663. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4664. menuStringArr.append(PDFViewMenuIdentifier_Normal_RedactProperties)
  4665. menuStringArr.append(PDFViewMenuIdentifier_Redact_Default)
  4666. } else if listView.activeAnnotation.isKind(of: CPDFLinkAnnotation.self) {
  4667. let link = listView.activeAnnotation as? CPDFLinkAnnotation
  4668. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4669. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4670. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4671. if(isShowPopUI){
  4672. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4673. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4674. }
  4675. } else if pdfListView.activeAnnotation.isKind(of: CPDFWidgetAnnotation.self) {
  4676. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4677. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4678. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4679. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4680. if(isShowPopUI){
  4681. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4682. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4683. }
  4684. menuStringArr.append(PDFViewMenuIdentifier_Normal_SortAnnotation)
  4685. } else {
  4686. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4687. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4688. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4689. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4690. if pdfListView.activeAnnotation.isKind(of: CPDFTextAnnotation.self) ||
  4691. pdfListView.activeAnnotation.isKind(of: CPDFFreeTextAnnotation.self) {
  4692. } else {
  4693. menuStringArr.append(PDFViewMenuIdentifier_Normal_Content)
  4694. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4695. }
  4696. if(isShowPopUI){
  4697. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4698. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4699. }
  4700. menuStringArr.append(PDFViewMenuIdentifier_Normal_SortAnnotation)
  4701. }
  4702. } else {
  4703. let currentSelection = pdfListView.currentSelection
  4704. if currentSelection != nil {
  4705. if currentSelection?.selectionType() == .text {
  4706. menuStringArr.append(PDFViewMenuIdentifier_Normal_AITool)
  4707. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4708. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4709. if pdfListView.canPaste() {
  4710. menuStringArr.append(PDFViewMenuIdentifier_Normal_Past)
  4711. }
  4712. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4713. menuStringArr.append(PDFViewMenuIdentifier_Normal_Hight)
  4714. menuStringArr.append(PDFViewMenuIdentifier_Normal_Underline)
  4715. menuStringArr.append(PDFViewMenuIdentifier_Normal_StrikeOut)
  4716. menuStringArr.append(PDFViewMenuIdentifier_Normal_Squiggly)
  4717. menuStringArr.append(PDFViewMenuIdentifier_Normal_Square)
  4718. menuStringArr.append(PDFViewMenuIdentifier_Normal_Circle)
  4719. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4720. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddOutLine)
  4721. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddBook)
  4722. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4723. menuStringArr.append(PDFViewMenuIdentifier_Normal_TTS)
  4724. menuStringArr.append(PDFViewMenuIdentifier_Normal_SearchText)
  4725. } else if currentSelection?.selectionType() == .image {
  4726. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4727. if pdfListView.canPaste() {
  4728. menuStringArr.append(PDFViewMenuIdentifier_Normal_Past)
  4729. }
  4730. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4731. menuStringArr.append(PDFViewMenuIdentifier_Normal_Export)
  4732. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4733. menuStringArr.append(PDFViewMenuIdentifier_Normal_Square)
  4734. menuStringArr.append(PDFViewMenuIdentifier_Normal_Circle)
  4735. }
  4736. } else {
  4737. if pdfListView.toolMode == .CFormToolMode {
  4738. if pdfListView.canPaste() {
  4739. menuStringArr.append(PDFViewMenuIdentifier_Normal_Past)
  4740. }
  4741. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4742. menuStringArr.append(PDFViewMenuIdentifier_Normal_SelectAllForm)
  4743. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4744. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightForm)
  4745. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowFormName)
  4746. menuStringArr.append(PDFViewMenuIdentifier_Normal_RestForm)
  4747. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4748. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddBook)
  4749. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4750. menuStringArr.append(PDFViewMenuIdentifier_Normal_ViewTools)
  4751. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4752. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageView)
  4753. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4754. menuStringArr.append(PDFViewMenuIdentifier_Normal_Scale)
  4755. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4756. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageRotate)
  4757. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4758. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageNum)
  4759. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4760. menuStringArr.append(PDFViewMenuIdentifier_Normal_Search)
  4761. menuStringArr.append(PDFViewMenuIdentifier_Normal_Print)
  4762. menuStringArr.append(PDFViewMenuIdentifier_Normal_Properties)
  4763. } else if pdfListView.toolMode == .CRedactToolMode {
  4764. menuStringArr.append(PDFViewMenuIdentifier_Normal_SelectAllText)
  4765. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4766. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddBook)
  4767. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4768. menuStringArr.append(PDFViewMenuIdentifier_Normal_ViewTools)
  4769. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4770. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageView)
  4771. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4772. menuStringArr.append(PDFViewMenuIdentifier_Normal_Scale)
  4773. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4774. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageRotate)
  4775. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4776. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageNum)
  4777. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4778. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightLink)
  4779. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightForm)
  4780. menuStringArr.append(PDFViewMenuIdentifier_Normal_RestForm)
  4781. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4782. menuStringArr.append(PDFViewMenuIdentifier_Normal_Search)
  4783. menuStringArr.append(PDFViewMenuIdentifier_Normal_Print)
  4784. menuStringArr.append(PDFViewMenuIdentifier_Normal_Properties)
  4785. } else {
  4786. if(listView.viewSplitMode != .disable) {
  4787. menuStringArr.append(PDFViewMenuIdentifier_Split_ViewMode)
  4788. menuStringArr.append(PDFViewMenuIdentifier_Split_Sync)
  4789. menuStringArr.append(PDFViewMenuIdentifier_Split_ShowBar)
  4790. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4791. } else {
  4792. menuStringArr.append(PDFViewMenuIdentifier_Normal_AITool)
  4793. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4794. if pdfListView == listView {
  4795. if pdfListView.canPaste() {
  4796. menuStringArr.append(PDFViewMenuIdentifier_Normal_Past)
  4797. }
  4798. menuStringArr.append(PDFViewMenuIdentifier_Normal_SelectAllText)
  4799. menuStringArr.append(PDFViewMenuIdentifier_Normal_SelectAllAnnotation)
  4800. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4801. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowAnnotation)
  4802. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4803. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddBook)
  4804. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4805. menuStringArr.append(PDFViewMenuIdentifier_Normal_ViewTools)
  4806. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4807. }
  4808. }
  4809. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageView)
  4810. if(listView.viewSplitMode != .disable) {
  4811. } else {
  4812. menuStringArr.append(PDFViewMenuIdentifier_Normal_ReadMode)
  4813. }
  4814. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4815. menuStringArr.append(PDFViewMenuIdentifier_Normal_Scale)
  4816. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4817. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageRotate)
  4818. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4819. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageNum)
  4820. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4821. menuStringArr.append(PDFViewMenuIdentifier_Normal_AutoScroll)
  4822. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4823. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightLink)
  4824. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightForm)
  4825. menuStringArr.append(PDFViewMenuIdentifier_Normal_RestForm)
  4826. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4827. menuStringArr.append(PDFViewMenuIdentifier_Normal_Search)
  4828. menuStringArr.append(PDFViewMenuIdentifier_Normal_Print)
  4829. menuStringArr.append(PDFViewMenuIdentifier_Normal_Properties)
  4830. }
  4831. }
  4832. }
  4833. let menuStruct = KMPDFMenuConfig.clickMenuUI(items: menuStringArr, theEvent: theEvent, listView: pdfListView)
  4834. groupListMenuGroup?.groupDelegate = self
  4835. groupListMenuGroup?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  4836. groupListMenuGroup?.updateGroupInfo(menuStruct.menuitems)
  4837. groupListMenuGroup?.showWithPoint(CGPoint(x: point.x, y: point.y - menuStruct.viewHeight), relativeTo: nil, withWindow: self.view.window)
  4838. }
  4839. } else {
  4840. var menuStringArr: [String] = []
  4841. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageView)
  4842. menuStringArr.append(PDFViewMenuIdentifier_Normal_ReadMode)
  4843. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4844. menuStringArr.append(PDFViewMenuIdentifier_Normal_Scale)
  4845. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4846. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightLink)
  4847. menuStringArr.append(PDFViewMenuIdentifier_Normal_HightForm)
  4848. menuStringArr.append(PDFViewMenuIdentifier_Normal_RestForm)
  4849. let menuStruct = KMPDFMenuConfig.clickMenuUI(items: menuStringArr, theEvent: theEvent, listView: pdfListView)
  4850. groupListMenuGroup?.groupDelegate = self
  4851. groupListMenuGroup?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  4852. groupListMenuGroup?.updateGroupInfo(menuStruct.menuitems)
  4853. groupListMenuGroup?.showWithPoint(CGPoint(x: point.x, y: point.y - menuStruct.viewHeight), relativeTo: nil, withWindow: self.view.window)
  4854. }
  4855. }
  4856. func pdfListViewMenuItemsEditing(at point: CGPoint, for page: CPDFPage!, menuItems: [NSMenuItem]!) -> [NSMenuItem]! {
  4857. toggleClosePopUIWindow()
  4858. var windowPoint = listView.convert(point, from: page)
  4859. let view: NSView? = nil
  4860. windowPoint = listView.convert(windowPoint, to: view)
  4861. if self.view.window?.interactionMode == .presentation {
  4862. let menuStruct = clickPresentationMenu(point: point)
  4863. groupListMenuGroup?.groupDelegate = self
  4864. groupListMenuGroup?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  4865. groupListMenuGroup?.updateGroupInfo(menuStruct.menuitems)
  4866. groupListMenuGroup?.showWithPoint(CGPoint(x: windowPoint.x, y: windowPoint.y - menuStruct.viewHeight), relativeTo: nil)
  4867. } else {
  4868. let isShowPopUI:Bool = !SettingsManager.sharedInstance.showQuickActionBar
  4869. var menuStringArr: [String] = []
  4870. let editingAreas = listView.km_EditingAreas()
  4871. let firstEditingArea = editingAreas.first
  4872. if editingAreas.count > 1 {
  4873. var isSameEditingArea = true
  4874. if(firstEditingArea?.isTextArea() == true) {
  4875. for i in 1..<(editingAreas.count) {
  4876. if !editingAreas[i].isTextArea() {
  4877. isSameEditingArea = false
  4878. break
  4879. }
  4880. }
  4881. } else if (firstEditingArea?.isImageArea() == true) {
  4882. for i in 1..<(editingAreas.count) {
  4883. if !editingAreas[i].isImageArea() {
  4884. isSameEditingArea = false
  4885. break
  4886. }
  4887. }
  4888. }
  4889. if(isSameEditingArea) {
  4890. if (firstEditingArea?.isTextArea() == true) {
  4891. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4892. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4893. if listView.isSupportPastMatchStyle() {
  4894. menuStringArr.append(PDFViewMenuIdentifier_Edit_Paste)
  4895. }
  4896. if listView.isSupportPast() {
  4897. menuStringArr.append(PDFViewMenuIdentifier_Edit_NoStylePaste)
  4898. }
  4899. menuStringArr.append(PDFViewMenuIdentifier_Edit_SelectAll)
  4900. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4901. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4902. menuStringArr.append(PDFViewMenuIdentifier_Edit_Font)
  4903. menuStringArr.append(PDFViewMenuIdentifier_Edit_FontName)
  4904. menuStringArr.append(PDFViewMenuIdentifier_Edit_FontAlight)
  4905. if(isShowPopUI){
  4906. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4907. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4908. }
  4909. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4910. menuStringArr.append(PDFViewMenuIdentifier_Normal_Aligning)
  4911. } else if (firstEditingArea?.isImageArea() == true) {
  4912. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4913. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4914. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4915. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4916. menuStringArr.append(PDFViewMenuIdentifier_Edit_RotateLeft)
  4917. menuStringArr.append(PDFViewMenuIdentifier_Edit_RotateRight)
  4918. menuStringArr.append(PDFViewMenuIdentifier_Edit_HorizontalMirror)
  4919. menuStringArr.append(PDFViewMenuIdentifier_Edit_VerticalMirror)
  4920. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4921. menuStringArr.append(PDFViewMenuIdentifier_Normal_Export)
  4922. if(isShowPopUI){
  4923. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4924. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4925. }
  4926. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4927. menuStringArr.append(PDFViewMenuIdentifier_Normal_Aligning)
  4928. }
  4929. } else {
  4930. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4931. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4932. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4933. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4934. menuStringArr.append(PDFViewMenuIdentifier_Normal_Aligning)
  4935. }
  4936. } else if editingAreas.count == 1 {
  4937. if firstEditingArea?.isTextArea() == true {
  4938. if !listView.isSelecteditAreaNotEdit() { //光标输入状态
  4939. if listView.isSupportPast() {
  4940. menuStringArr.append(PDFViewMenuIdentifier_Edit_NoStylePaste)
  4941. }
  4942. menuStringArr.append(PDFViewMenuIdentifier_Edit_SelectAll)
  4943. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4944. } else {
  4945. let editState = listView.editStatus()
  4946. if (editState == .editSelectText) {// 选择文本
  4947. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4948. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4949. if listView.isSupportPast() {
  4950. menuStringArr.append(PDFViewMenuIdentifier_Edit_NoStylePaste)
  4951. }
  4952. menuStringArr.append(PDFViewMenuIdentifier_Edit_SelectAll)
  4953. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4954. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4955. } else {
  4956. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4957. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4958. if listView.isSupportPastMatchStyle() {
  4959. menuStringArr.append(PDFViewMenuIdentifier_Edit_Paste)
  4960. }
  4961. if listView.isSupportPast() {
  4962. menuStringArr.append(PDFViewMenuIdentifier_Edit_NoStylePaste)
  4963. }
  4964. menuStringArr.append(PDFViewMenuIdentifier_Edit_SelectAll)
  4965. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4966. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4967. }
  4968. menuStringArr.append(PDFViewMenuIdentifier_Edit_Font)
  4969. menuStringArr.append(PDFViewMenuIdentifier_Edit_FontName)
  4970. menuStringArr.append(PDFViewMenuIdentifier_Edit_FontAlight)
  4971. if(isShowPopUI){
  4972. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4973. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4974. }
  4975. }
  4976. } else if firstEditingArea?.isImageArea() == true {
  4977. menuStringArr.append(PDFViewMenuIdentifier_Normal_Copy)
  4978. menuStringArr.append(PDFViewMenuIdentifier_Normal_Cut)
  4979. menuStringArr.append(PDFViewMenuIdentifier_Normal_Delete)
  4980. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4981. menuStringArr.append(PDFViewMenuIdentifier_Edit_RotateLeft)
  4982. menuStringArr.append(PDFViewMenuIdentifier_Edit_RotateRight)
  4983. menuStringArr.append(PDFViewMenuIdentifier_Edit_HorizontalMirror)
  4984. menuStringArr.append(PDFViewMenuIdentifier_Edit_VerticalMirror)
  4985. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4986. menuStringArr.append(PDFViewMenuIdentifier_Edit_Crop)
  4987. menuStringArr.append(PDFViewMenuIdentifier_Edit_Replace)
  4988. menuStringArr.append(PDFViewMenuIdentifier_Normal_Export)
  4989. if(isShowPopUI){
  4990. menuStringArr.append(PDFViewMenuIdentifier_Space)
  4991. menuStringArr.append(PDFViewMenuIdentifier_Normal_ShowPopUI)
  4992. }
  4993. }
  4994. } else {
  4995. if(listView.isSupportPastMatchStyle()) {
  4996. menuStringArr.append(PDFViewMenuIdentifier_Edit_Paste)
  4997. }
  4998. if(listView.isSupportPast()) {
  4999. menuStringArr.append(PDFViewMenuIdentifier_Edit_NoStylePaste)
  5000. }
  5001. menuStringArr.append(PDFViewMenuIdentifier_Edit_SelectAll)
  5002. menuStringArr.append(PDFViewMenuIdentifier_Space)
  5003. menuStringArr.append(PDFViewMenuIdentifier_Edit_AddText)
  5004. menuStringArr.append(PDFViewMenuIdentifier_Edit_AddImage)
  5005. menuStringArr.append(PDFViewMenuIdentifier_Edit_AddLink)
  5006. menuStringArr.append(PDFViewMenuIdentifier_Space)
  5007. menuStringArr.append(PDFViewMenuIdentifier_Normal_AddBook)
  5008. menuStringArr.append(PDFViewMenuIdentifier_Space)
  5009. menuStringArr.append(PDFViewMenuIdentifier_Normal_ViewTools)
  5010. menuStringArr.append(PDFViewMenuIdentifier_Space)
  5011. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageView)
  5012. menuStringArr.append(PDFViewMenuIdentifier_Space)
  5013. menuStringArr.append(PDFViewMenuIdentifier_Normal_Scale)
  5014. menuStringArr.append(PDFViewMenuIdentifier_Space)
  5015. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageRotate)
  5016. menuStringArr.append(PDFViewMenuIdentifier_Space)
  5017. menuStringArr.append(PDFViewMenuIdentifier_Normal_PageNum)
  5018. menuStringArr.append(PDFViewMenuIdentifier_Space)
  5019. menuStringArr.append(PDFViewMenuIdentifier_Normal_Search)
  5020. menuStringArr.append(PDFViewMenuIdentifier_Normal_Print)
  5021. menuStringArr.append(PDFViewMenuIdentifier_Normal_Properties)
  5022. }
  5023. let menuStruct = KMPDFMenuConfig.clickMenuUI(items: menuStringArr, theEvent: nil, listView: listView)
  5024. groupListMenuGroup?.groupDelegate = self
  5025. groupListMenuGroup?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  5026. groupListMenuGroup?.updateGroupInfo(menuStruct.menuitems)
  5027. groupListMenuGroup?.showWithPoint(CGPoint(x: windowPoint.x, y: windowPoint.y - menuStruct.viewHeight), relativeTo: nil)
  5028. }
  5029. return []
  5030. }
  5031. func pdfListViewMobileAnnotationBegan(_ point: CGPoint, for pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!) {
  5032. toggleClosePopUIWindow()
  5033. }
  5034. func pdfListViewMobileAnnotationMove(_ point: CGPoint, for pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!) {
  5035. toggleClosePopUIWindow()
  5036. }
  5037. func pdfListViewMobileAnnotationEnd(_ point: CGPoint, for pdfListView: CPDFListView!, forActiveAnnotations annotations: [CPDFAnnotation]!) {
  5038. reloadPopUIWindow()
  5039. }
  5040. func pdfListViewAddAnnotations(_ pdfListView: CPDFListView!, forAdd annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  5041. var pageIndexes = IndexSet()
  5042. pageIndexes.insert(Int(pdfPage.pageIndex()))
  5043. KMNThumbnailManager.reloadThumImage(document: self.listView.document, pageIndexs: pageIndexes)
  5044. botaViewController?.reloadData()
  5045. alertTipViewController.reloadFormAlertUI()
  5046. alertTipViewController.reloadAlertUIFrame()
  5047. }
  5048. func pdfListViewRemoveAnnotations(_ pdfListView: CPDFListView!, forRemove annotations: [CPDFAnnotation]!, in pdfPage: CPDFPage!) {
  5049. var pageIndexes = IndexSet()
  5050. pageIndexes.insert(Int(pdfPage.pageIndex()))
  5051. KMNThumbnailManager.reloadThumImage(document: self.listView.document, pageIndexs: pageIndexes)
  5052. botaViewController?.reloadData()
  5053. alertTipViewController.reloadFormAlertUI()
  5054. alertTipViewController.reloadAlertUIFrame()
  5055. }
  5056. func pdfListViewEditAnnotation(_ pdfListView: CPDFListView!, for anotation: CPDFAnnotation!) {
  5057. if anotation.isKind(of: CPDFSignatureWidgetAnnotation.self) {
  5058. if let signatureWidgetAnnotation = anotation as? CPDFSignatureWidgetAnnotation {
  5059. let signature = signatureWidgetAnnotation.signature()
  5060. if ((signature) != nil) {
  5061. popUpSignatureWidgetState(signature!, listView)
  5062. } else {
  5063. let widget = CPDFSignatureWidgetAnnotation.init(PDFListViewNoteWith: listView.document)
  5064. widget.bounds = NSMakeRect(-1000, -1000, 545, 178);
  5065. anotation.page.addAnnotation(widget)
  5066. let configWindowVC = DSignatureConfigWindowController.init(windowNibName: "DSignatureConfigWindowController")
  5067. configWindowVC.viewType = .fileList;
  5068. configWindowVC.appearanceWidget = widget;
  5069. configWindowVC.isCreatDS = false
  5070. configWindowVC.complentionHandle = {[weak self] isSign, dic, config, isLock in
  5071. widget.page.removeAnnotation(widget)
  5072. if isSign {
  5073. if (dic.object(forKey: SAVEFILEPATH_KEY) != nil) {
  5074. let p12Path = dic.object(forKey: SAVEFILEPATH_KEY) as! String
  5075. let password = dic.object(forKey: PASSWORD_KEY)
  5076. if p12Path.count > 0 {
  5077. self?.writeSignatureToWidget(signatureWidgetAnnotation, p12Path, password as! String, config, isLock)
  5078. }
  5079. }
  5080. } else {
  5081. if signatureWidgetAnnotation.isSignSignatureAdd() == true {
  5082. self?.listView.remove(anotation)
  5083. }
  5084. }
  5085. }
  5086. configWindowVC.actionBlock = {[weak self] controller, type in
  5087. if (type == .cancel) {
  5088. NSApplication.shared.stopModal()
  5089. controller.window?.orderOut(nil)
  5090. controller.window?.close()
  5091. widget.page.removeAnnotation(widget)
  5092. if signatureWidgetAnnotation.isSignSignatureAdd() == true {
  5093. self?.listView.remove(anotation)
  5094. } else {
  5095. self?.listView.setNeedsDisplayAnnotationViewFor(anotation.page)
  5096. }
  5097. } else if (type == .confirm) {
  5098. NSApplication.shared.stopModal()
  5099. controller.window?.orderOut(nil)
  5100. controller.window?.close()
  5101. }
  5102. }
  5103. configWindowVC.window?.center()
  5104. NSApplication.shared.runModal(for: configWindowVC.window!)
  5105. }
  5106. }
  5107. } else if anotation.isKind(of: CPDFRedactAnnotation.self) {
  5108. if let redactAnnotation = anotation as? CPDFRedactAnnotation {
  5109. let properties = KMRedactPropertiesWindowController()
  5110. properties.readactAnnotation = redactAnnotation
  5111. properties.own_beginSheetModal(for: self.view.window) { result in
  5112. }
  5113. properties.callback = { [weak self] annotation in
  5114. if let page = annotation?.page {
  5115. self?.listView.setNeedsDisplayAnnotationViewFor(page)
  5116. }
  5117. }
  5118. }
  5119. }
  5120. }
  5121. func pdfListViewSplitModeShowBar() -> Bool {
  5122. return viewManager.splitShowBottomBar
  5123. }
  5124. func pdfListViewEndEditMode(_ pdfListView: CPDFListView!) {
  5125. let document = listView.document
  5126. if(document != nil) {
  5127. for i in 0..<document!.pageCount {
  5128. let thumbnail = KMNThumbnail.init(document: document!, currentPageIndex: Int(i))
  5129. thumbnail.removeCacheImage()
  5130. }
  5131. }
  5132. botaViewController?.reloadData()
  5133. }
  5134. func pdfListViewAddEditAreaTypeChanged(_ pdfListView: CPDFListView!) {
  5135. if self.listView.shouAddEditAreaType() != .text && self.listView.shouAddEditAreaType() != .image {
  5136. if viewManager.subToolMode != .None {
  5137. viewManager.subToolMode = .None
  5138. pdfToolbarController?.cancelSelectedSecondToolbarItems(viewManager.toolMode)
  5139. pdfToolbarController?.refreshSecondToolbarItemsState()
  5140. }
  5141. }
  5142. }
  5143. //MARK: -Crop
  5144. func pdfListViewChangedSelectionOrMagnification(_ pdfListView: CPDFListView!) {
  5145. reloadPopUIWindow()
  5146. }
  5147. func pdfListViewDidSelectionEnd(_ pdfListView: CPDFListView!) {
  5148. if (!self.listView.isEqual(to: pdfListView)) {
  5149. return
  5150. }
  5151. if (self.listView.toolMode != .CSelectToolMode) {
  5152. return
  5153. }
  5154. reloadPopUIWindow()
  5155. }
  5156. func pdfListViewHaveDocumentAttribute() -> Bool {
  5157. if(!self.listView.document.allowsCopying) {
  5158. self.removeOwnerPassword()
  5159. return false
  5160. }
  5161. return true
  5162. }
  5163. func pdfListViewShowTipView() -> Bool {
  5164. let popWindow = KMNAnnotationPopToolbarWindow.shared
  5165. if popWindow.isVisible == true {
  5166. return true
  5167. }
  5168. return false
  5169. }
  5170. func pdfListViewAnnotationEditModeChange(_ pdfListView: CPDFListView!, for anotation: CPDFAnnotation!) {
  5171. reloadPopUIWindow()
  5172. }
  5173. func pdfListViewAnnotationChange(_ color: NSColor!, forActiveAnnotation annotation: CPDFAnnotation!) {
  5174. rightSideController?.reloadData()
  5175. NotificationCenter.default.post(name: toolbarImageColorChangedNotificationName, object: nil)
  5176. }
  5177. //MARK: -Measure
  5178. func pdfListViewAnnotationMeasureInfoChange(_ pdfListView: CPDFListView!, with annotation: CPDFAnnotation!) {
  5179. guard let data = annotation else {
  5180. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  5181. distanceMeasureInfoWindowController?.clearData()
  5182. }
  5183. return
  5184. }
  5185. if let lineAnnotation = annotation as? CPDFLineAnnotation {
  5186. handleLineAnnotation(lineAnnotation)
  5187. } else if let polylineAnnotation = annotation as? CPDFPolylineAnnotation {
  5188. handlePolylineAnnotation(polylineAnnotation)
  5189. } else if let polygonAnnotation = annotation as? CPDFPolygonAnnotation {
  5190. handlePolygonAnnotation(polygonAnnotation)
  5191. }
  5192. }
  5193. func pdfListViewMeasureCancel(_ pdfListView: CPDFListView!) {
  5194. cancelMeasureType()
  5195. }
  5196. private func handleLineAnnotation(_ annotation: CPDFLineAnnotation) {
  5197. if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  5198. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  5199. distanceMeasureInfoWindowController?.showWindow(self)
  5200. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  5201. areaMeasureInfoWindowController?.hideFloatingWindow()
  5202. distanceMeasureInfoWindowController?.showWindow(self)
  5203. } else if distanceMeasureInfoWindowController?.window?.isVisible == false {
  5204. distanceMeasureInfoWindowController?.showWindow(self)
  5205. }
  5206. let measureInfo = annotation.measureInfo
  5207. let startPoint = annotation.startPoint
  5208. let endPoint = annotation.endPoint
  5209. let angle = atan2(endPoint.y - startPoint.y, endPoint.x - startPoint.x) * (180.0 / .pi)
  5210. distanceMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  5211. distanceMeasureInfoWindowController?.xLabel.stringValue = String(format: "%.0f", abs(endPoint.x - startPoint.x))
  5212. distanceMeasureInfoWindowController?.yLabel.stringValue = String(format: "%.0f", abs(endPoint.y - startPoint.y))
  5213. distanceMeasureInfoWindowController?.reloadData(with: measureInfo!)
  5214. }
  5215. private func handlePolylineAnnotation(_ annotation: CPDFPolylineAnnotation) {
  5216. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  5217. distanceMeasureInfoWindowController?.hideFloatingWindow()
  5218. perimeterMeasureInfoWindowController?.showWindow(self)
  5219. } else if areaMeasureInfoWindowController?.window?.isVisible == true {
  5220. areaMeasureInfoWindowController?.hideFloatingWindow()
  5221. perimeterMeasureInfoWindowController?.showWindow(self)
  5222. } else if perimeterMeasureInfoWindowController?.window?.isVisible == false {
  5223. perimeterMeasureInfoWindowController?.showWindow(self)
  5224. }
  5225. let measureInfo = annotation.measureInfo
  5226. let savePoints = annotation.savePoints()
  5227. var angle: CGFloat = 0
  5228. if savePoints.count >= 3 {
  5229. let count = savePoints.count
  5230. let startPoint = savePoints[count - 3].pointValue
  5231. let midPoint = savePoints[count - 2].pointValue
  5232. let endPoint = savePoints.last!.pointValue
  5233. angle = angleBetweenPoints(startPoint, midPoint, endPoint)
  5234. }
  5235. angle = 180 - angle
  5236. perimeterMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  5237. perimeterMeasureInfoWindowController?.reloadData(with: measureInfo!)
  5238. }
  5239. private func handlePolygonAnnotation(_ annotation: CPDFPolygonAnnotation) {
  5240. if distanceMeasureInfoWindowController?.window?.isVisible == true {
  5241. distanceMeasureInfoWindowController?.hideFloatingWindow()
  5242. areaMeasureInfoWindowController?.showWindow(self)
  5243. } else if perimeterMeasureInfoWindowController?.window?.isVisible == true {
  5244. perimeterMeasureInfoWindowController?.hideFloatingWindow()
  5245. areaMeasureInfoWindowController?.showWindow(self)
  5246. } else if areaMeasureInfoWindowController?.window?.isVisible == false {
  5247. areaMeasureInfoWindowController?.showWindow(self)
  5248. }
  5249. let measureInfo = annotation.measureInfo
  5250. let savePoints = annotation.savePoints
  5251. var angle: CGFloat = 0
  5252. if savePoints.count >= 3 {
  5253. let count = savePoints.count
  5254. let startPoint = (savePoints[count - 3] as AnyObject).pointValue
  5255. let midPoint = (savePoints[count - 2] as AnyObject).pointValue
  5256. let endPoint = (savePoints.lastObject as AnyObject).pointValue
  5257. angle = angleBetweenPoints(startPoint!, midPoint!, endPoint!)
  5258. }
  5259. angle = 180 - angle
  5260. areaMeasureInfoWindowController?.angleLabel.stringValue = String(format: "%.2f°", abs(angle))
  5261. areaMeasureInfoWindowController?.reloadData(measureInfo!)
  5262. }
  5263. private func angleBetweenPoints(_ startPoint: CGPoint, _ midPoint: CGPoint, _ endPoint: CGPoint) -> CGFloat {
  5264. let vector1 = CGPoint(x: midPoint.x - startPoint.x, y: midPoint.y - startPoint.y)
  5265. let vector2 = CGPoint(x: endPoint.x - midPoint.x, y: endPoint.y - midPoint.y)
  5266. let dotProduct = vector1.x * vector2.x + vector1.y * vector2.y
  5267. let magnitude1 = sqrt(vector1.x * vector1.x + vector1.y * vector1.y)
  5268. let magnitude2 = sqrt(vector2.x * vector2.x + vector2.y * vector2.y)
  5269. return acos(dotProduct / (magnitude1 * magnitude2)) * (180.0 / .pi)
  5270. }
  5271. @objc func pdfUpdatedFinish() {
  5272. splitPDFController?.inPDFFirst = false
  5273. splitPDFController?.outPDFFirst = false
  5274. }
  5275. //MARK: - Notification
  5276. @objc private func pdfViewScrollViewDidScroll(_ noti: Notification) {
  5277. reloadPopUIWindow()
  5278. if listView.popOver?.isShown == true {
  5279. listView.popOver?.close()
  5280. }
  5281. }
  5282. func pageCountChangedNotification(_ sender: Notification) {
  5283. guard let document = sender.object as? CPDFDocument else {
  5284. return
  5285. }
  5286. botaViewController?.pageCountChangedAction(document: document)
  5287. }
  5288. func annotationsAttributeHasChange(_ sender: Notification) {
  5289. guard let dict = sender.object as? [String : Any] else {
  5290. return
  5291. }
  5292. if let anno = dict["object"] as? CPDFAnnotation {
  5293. let value = dict["keyPath"] as? String ?? ""
  5294. let didEnd = dict["didEnd"] as? Bool ?? false
  5295. if didEnd {
  5296. if value == CPDFAnnotationBoundsKey {
  5297. if anno is CPDFSquareAnnotation || anno is CPDFCircleAnnotation {
  5298. anno.contents = anno.page?.string(for: anno.bounds) ?? ""
  5299. }
  5300. }
  5301. if anno.km_isMeasure() && anno.contents == nil {
  5302. anno.contents = anno.string() ?? ""
  5303. }
  5304. var pageIndexes = IndexSet()
  5305. pageIndexes.insert(Int(anno.page.pageIndex()))
  5306. reloadIfThumData(pageIndexs: pageIndexes)
  5307. reloadPopUIWindow()
  5308. } else {
  5309. if value != CPDFAnnotationBoundsKey && value != CPDFAnnotationStartPointKey && value != CPDFAnnotationEndPointKey && value != CPDFAnnotationPathsKey { // 改变bounds(箭头、直线注释 开始点和结束点, 手绘注释的paths)的操作会卡顿,比如移动
  5310. var pageIndexes = IndexSet()
  5311. pageIndexes.insert(Int(anno.page.pageIndex()))
  5312. reloadIfThumData(pageIndexs: pageIndexes)
  5313. reloadPopUIWindow()
  5314. }
  5315. }
  5316. }
  5317. }
  5318. func reloadThumData(pageIndexs: IndexSet) {
  5319. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(reloadIfThumData), object: pageIndexs)
  5320. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  5321. self.reloadIfThumData(pageIndexs: pageIndexs)
  5322. }
  5323. }
  5324. func reloadIfThumData(pageIndexs: IndexSet) {
  5325. KMNThumbnailManager.reloadThumImage(document: self.listView.document, pageIndexs: pageIndexs)
  5326. botaViewController?.reloadData()
  5327. }
  5328. @objc fileprivate func signatureStateChangeNoti(_ sender: Notification) {
  5329. guard let objecListView = sender.object as? CPDFListView else {
  5330. return
  5331. }
  5332. if listView == objecListView {
  5333. reloadDigitalSigns()
  5334. alertTipViewController.reloadDigitalAlertUI()
  5335. alertTipViewController.reloadAlertUIFrame()
  5336. let signatureWidget = listView.editAnnotation
  5337. if let signatureWidgetAnnotation = signatureWidget as? CPDFSignatureWidgetAnnotation {
  5338. let signature = signatureWidgetAnnotation.signature()
  5339. if signature == nil {
  5340. return
  5341. }
  5342. signaturestateVC.signature = signature
  5343. signaturestateVC.reloadData()
  5344. }
  5345. }
  5346. }
  5347. }
  5348. //MARK: - KMNThumbnailBaseViewDelegate
  5349. extension KMMainViewController: KMNThumbnailBaseViewDelegate {
  5350. func clickThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,currentIndex:Int) {
  5351. exitPageEditMode()
  5352. if listView.currentPageIndex != currentIndex {
  5353. listView.go(toPageIndex: currentIndex, animated: true)
  5354. }
  5355. viewManager.isPageEditMode = false
  5356. pdfToolbarController?.clickWithIdentify(KMPDFToolbar_PageEdit_Identifier)
  5357. }
  5358. func insertPDFThumbnailViewControlle(pageEditVC: KMNThumbnailBaseViewController?, pdfDocment: CPDFDocument?) {
  5359. if(pdfDocment != nil) {
  5360. insertDocuments.insert(pdfDocment!)
  5361. }
  5362. }
  5363. func changeIndexPathsThumbnailViewControlle(pageEditVC: KMNThumbnailBaseViewController?, selectionIndexPaths: Set<IndexPath>, selectionStrings: String) {
  5364. toolbarManager.page_pageInfo_Property.text = selectionStrings
  5365. if(toolbarManager.page_pageInfo_Property.creatable == false) {
  5366. toolbarManager.page_pageInfo_Property.creatable = true
  5367. }
  5368. pdfToolbarController?.refreshSecondToolbarItemsState()
  5369. }
  5370. }
  5371. //MARK: - KMNLeftSideViewControllerDelegate
  5372. extension KMMainViewController: KMNLeftSideViewControllerDelegate {
  5373. func enterPageEditLeftSideViewController(leftSideViewController: KMNLeftSideViewController) {
  5374. if viewManager.isPageEditMode == false {
  5375. viewManager.isPageEditMode = true
  5376. if(pdfToolbarController != nil) {
  5377. kmPDFToolbarControllerDidToolbarItemClicked(pdfToolbarController!, KMPDFToolbar_PageEdit_Identifier)
  5378. pdfToolbarController?.resetSecondToolbar()
  5379. }
  5380. }
  5381. }
  5382. func changeSelectePageLeftSideViewController(leftSideViewController: KMNLeftSideViewController, pageIndex: Int) {
  5383. if(listView.currentPageIndex != pageIndex) {
  5384. listView.go(toPageIndex: pageIndex, animated: true)
  5385. }
  5386. }
  5387. func addBookmarkForLeftC(controller: KMNLeftSideViewController, bookmark: CPDFBookmark?, info: [String : Any]?) {
  5388. if let result = info?["result"] as? Bool {
  5389. if result == false {
  5390. if let targetV = listView.superview {
  5391. _ = KMNCustomAlertView.alertView(message: KMLocalizedString("This page has been bookmarked"), type: .normal_custom, fromView: targetV, point:CGPointMake(targetV.frame.size.width/2, targetV.bounds.size.height-24))
  5392. }
  5393. }
  5394. }
  5395. }
  5396. func switchSearchPopWindow(controller: KMNLeftSideViewController) {
  5397. if(viewManager.pdfSideBarType == .search) {
  5398. toggleCloseLeftSide()
  5399. }
  5400. let handdler = controller.searchViewC.handdler
  5401. showSearchPopWindow(type: handdler.type, keyborad: handdler.searchKey, replaceText: handdler.replaceKey, results: handdler.searchResults)
  5402. }
  5403. func searchTypeDidChange(controller: KMNLeftSideViewController) {
  5404. let handdler = controller.searchViewC.handdler
  5405. if handdler.type == .replace {
  5406. viewManager.toolMode = .Edit
  5407. updatePDFViewAnnotationMode()
  5408. }
  5409. }
  5410. }
  5411. //MARK: - ComponentGroupDelegate
  5412. extension KMMainViewController: ComponentGroupDelegate {
  5413. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  5414. if menuItemProperty?.identifier == PDFViewMenuIdentifier_PageNext {
  5415. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5416. if objectListView.canGoToNextPage() {
  5417. objectListView.goToNextPage(nil)
  5418. }
  5419. }
  5420. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_PagePrevious) {
  5421. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5422. if objectListView.canGoToPreviousPage() {
  5423. objectListView.goToPreviousPage(nil)
  5424. }
  5425. }
  5426. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_PageFirst) {
  5427. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5428. if objectListView.canGoToFirstPage() {
  5429. objectListView.goToFirstPage(nil)
  5430. }
  5431. }
  5432. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_PageLast) {
  5433. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5434. if objectListView.canGoToLastPage() {
  5435. objectListView.goToLastPage(nil)
  5436. }
  5437. }
  5438. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Presentation_LaserPoint) {
  5439. listView.exitPresentationDrawMode()
  5440. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Presentation_Brush) {
  5441. listView.enterPresentationDrawMode()
  5442. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Presentation_Exit) {
  5443. self.exitFullScreen()
  5444. } else if menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AIRewrite {
  5445. viewManager.pdfSideBarType = .aiTools
  5446. sideBarController?.reloadData()
  5447. if let sideVC = sideBarController {
  5448. kmPDFSideBarControllerDidSidebarTypeUpdated(sideVC)
  5449. }
  5450. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AIProofread) {
  5451. viewManager.pdfSideBarType = .aiTools
  5452. sideBarController?.reloadData()
  5453. if let sideVC = sideBarController {
  5454. kmPDFSideBarControllerDidSidebarTypeUpdated(sideVC)
  5455. }
  5456. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AITranslate) {
  5457. viewManager.pdfSideBarType = .aiTools
  5458. sideBarController?.reloadData()
  5459. if let sideVC = sideBarController {
  5460. kmPDFSideBarControllerDidSidebarTypeUpdated(sideVC)
  5461. }
  5462. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Past) {
  5463. let theEvent = menuItemProperty?.representedObject
  5464. if let currentEvent = theEvent as? NSEvent {
  5465. var pagePoint = CGPoint.zero
  5466. if let page = listView.pageAndPoint(&pagePoint, for: currentEvent, nearest: false) {
  5467. listView.menuPointPaste(pagePoint, page: page, isRightPaste: true)
  5468. }
  5469. }
  5470. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SelectAllText) {
  5471. listView.selectAll(nil)
  5472. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AddBook) {
  5473. menuItemBookMarkClick_add(sender: nil)
  5474. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_TextTool) {
  5475. listView.toolMode = .CTextToolMode
  5476. listView.annotationType = .unkown
  5477. viewManager.viewToolsType = .Select
  5478. pdfToolbarController?.reloadToolsView()
  5479. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_MoveTool) {
  5480. listView.toolMode = .CMoveToolMode
  5481. listView.annotationType = .unkown
  5482. viewManager.viewToolsType = .Scroll
  5483. pdfToolbarController?.reloadToolsView()
  5484. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SelectTool) {
  5485. listView.toolMode = .CSelectToolMode
  5486. listView.annotationType = .unkown
  5487. viewManager.viewToolsType = .Content_Selection
  5488. pdfToolbarController?.reloadToolsView()
  5489. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_MagnifyTool) {
  5490. listView.toolMode = .CMagnifyToolMode
  5491. listView.annotationType = .unkown
  5492. viewManager.viewToolsType = .Magnify
  5493. pdfToolbarController?.reloadToolsView()
  5494. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SelectZoomTool) {
  5495. listView.toolMode = .CSelectZoomToolMode
  5496. listView.annotationType = .unkown
  5497. viewManager.viewToolsType = .AreaZoom
  5498. pdfToolbarController?.reloadToolsView()
  5499. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Single) {
  5500. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5501. objectListView.menuItemClick_SinglePage(nil)
  5502. }
  5503. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SingleContinuous) {
  5504. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5505. objectListView.menuItemClick_SinglePagesContinuous(nil)
  5506. }
  5507. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_TwoPages) {
  5508. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5509. objectListView.menuItemClick_TwoPages(nil)
  5510. }
  5511. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_TwoPagesContinuous) {
  5512. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5513. objectListView.menuItemClick_TwoPagesContinuous(nil)
  5514. }
  5515. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_BookMode) {
  5516. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5517. objectListView.menuItemClick_BookMode(nil)
  5518. }
  5519. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ReadMode) {
  5520. viewManager.isPDFReadMode = !viewManager.isPDFReadMode
  5521. if viewManager.isPDFReadMode {
  5522. openPDFReadMode()
  5523. } else {
  5524. exitPDFReadMode()
  5525. }
  5526. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ScaleWidth) {
  5527. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5528. objectListView.autoScales = true
  5529. }
  5530. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ScalePage) {
  5531. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5532. let pageHeight = objectListView.currentPage()!.size.height
  5533. let pdfviewHeight = objectListView.bounds.size.height
  5534. objectListView.scaleFactor = pdfviewHeight/pageHeight
  5535. objectListView.autoScales = false
  5536. }
  5537. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ScaleOrg) {
  5538. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5539. if objectListView.scaleFactor != 1.0 {
  5540. objectListView.scaleFactor = 1.0
  5541. objectListView.autoScales = false
  5542. }
  5543. }
  5544. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ScaleZoomIn) {
  5545. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5546. var scale = objectListView.scaleFactor
  5547. switch scale {
  5548. case 0...0.25:
  5549. scale += 0.25
  5550. case 0.25...3:
  5551. scale += 0.25
  5552. case 3.1...10:
  5553. scale += 0.4
  5554. case 10.1...100:
  5555. scale += 1
  5556. default:
  5557. scale += 1
  5558. }
  5559. objectListView.scaleFactor = scale
  5560. }
  5561. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ScaleZoomOut) {
  5562. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5563. var scale = objectListView.scaleFactor
  5564. switch scale {
  5565. case 0...0.25:
  5566. scale = 0
  5567. case 0.25...3:
  5568. scale -= 0.25
  5569. case 3.1...10:
  5570. scale -= 0.4
  5571. case 10.1...100:
  5572. scale -= 1
  5573. default:
  5574. scale -= 1
  5575. }
  5576. objectListView.scaleFactor = scale
  5577. }
  5578. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_RotateLeft) {
  5579. if let dic = menuItemProperty?.representedObject as? NSDictionary {
  5580. let objectListView = dic["Object"] as? CPDFListView
  5581. let theEvent = dic["theEvent"] as? NSEvent
  5582. if(objectListView != nil && theEvent != nil) {
  5583. var pagePoint = CGPoint.zero
  5584. if let page = objectListView?.pageAndPoint(&pagePoint, for: theEvent, nearest: false) {
  5585. rotateLeft(page: page, listView: objectListView)
  5586. }
  5587. }
  5588. }
  5589. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_RotateRight) {
  5590. if let dic = menuItemProperty?.representedObject as? NSDictionary {
  5591. let objectListView = dic["Object"] as? CPDFListView
  5592. let theEvent = dic["theEvent"] as? NSEvent
  5593. if(objectListView != nil && theEvent != nil) {
  5594. var pagePoint = CGPoint.zero
  5595. if let page = objectListView?.pageAndPoint(&pagePoint, for: theEvent, nearest: false) {
  5596. rotateRight(page: page, listView: objectListView)
  5597. }
  5598. }
  5599. }
  5600. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_EnterPageNum) {
  5601. sideBarController?.beginEditing()
  5602. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_PageBack) {
  5603. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5604. if(objectListView.canGoBack() == true) {
  5605. objectListView.goBack(nil)
  5606. }
  5607. }
  5608. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_PageForward) {
  5609. if let objectListView = menuItemProperty?.representedObject as? CPDFListView {
  5610. if(objectListView.canGoForward() == true) {
  5611. objectListView.goForward(nil)
  5612. }
  5613. }
  5614. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AutoScroll) {
  5615. toggleAutoFlow(nil)
  5616. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_HightForm) {
  5617. highlightFormFiled(nil)
  5618. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_HightLink) {
  5619. highlightLinks(nil)
  5620. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_RestForm) {
  5621. resetForm(nil)
  5622. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Search) {
  5623. if botaViewController != nil {
  5624. switchSearchPopWindow(controller: botaViewController!)
  5625. }
  5626. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Print) {
  5627. menuItemAction_print(nil)
  5628. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Properties) {
  5629. menuItemAction_property(nil)
  5630. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Copy) {
  5631. if(listView.isEditing() == true) {
  5632. listView.copyEditAreaAction()
  5633. } else {
  5634. listView.copy(nil)
  5635. }
  5636. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Hight) {
  5637. if let currentSelect = listView.currentSelection {
  5638. if(currentSelect.selectionType() == .text) {
  5639. listView.addAnnotation(with: .highlight, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  5640. listView.currentSelection = nil;
  5641. }
  5642. }
  5643. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Underline) {
  5644. if let currentSelect = listView.currentSelection {
  5645. if(currentSelect.selectionType() == .text) {
  5646. listView.addAnnotation(with: .underline, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  5647. listView.currentSelection = nil;
  5648. }
  5649. }
  5650. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Squiggly) {
  5651. if let currentSelect = listView.currentSelection {
  5652. if(currentSelect.selectionType() == .text) {
  5653. listView.addAnnotation(with: .squiggly, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  5654. listView.currentSelection = nil;
  5655. }
  5656. }
  5657. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_StrikeOut) {
  5658. if let currentSelect = listView.currentSelection {
  5659. if(currentSelect.selectionType() == .text) {
  5660. listView.addAnnotation(with: .strikeOut, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  5661. listView.currentSelection = nil;
  5662. }
  5663. }
  5664. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Square) {
  5665. if let currentSelect = listView.currentSelection {
  5666. listView.addAnnotation(with: .square, selection: currentSelect, page: currentSelect.page, bounds: currentSelect.bounds)
  5667. listView.currentSelection = nil;
  5668. }
  5669. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Circle) {
  5670. if let currentSelect = listView.currentSelection {
  5671. listView.addAnnotation(with: .circle, selection: currentSelect, page: currentSelect.page, bounds: listView.currentSelection.bounds)
  5672. listView.currentSelection = nil;
  5673. }
  5674. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_AddOutLine) {
  5675. addOutLineItemAction()
  5676. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_TTS) {
  5677. startSpeaking(nil)
  5678. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SearchText) {
  5679. searchBaiduAction()
  5680. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SelectAllForm) {
  5681. selectAllFormAnnotation()
  5682. listView.setNeedsDisplayForVisiblePages()
  5683. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ShowFormName) {
  5684. listView.showFormFieldName = true
  5685. listView.setNeedsDisplayForVisiblePages()
  5686. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SelectAllAnnotation) {
  5687. selectAllNomerAnnotation()
  5688. listView.setNeedsDisplayForVisiblePages()
  5689. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ShowAnnotation) {
  5690. let isHiden = listView.hideNotes
  5691. listView.hideNotes = !isHiden
  5692. listView.setNeedsDisplayForVisiblePages()
  5693. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_CopyText) {
  5694. var copyText:String = ""
  5695. let annotations = listView.activeAnnotations
  5696. for i in 0 ..< (annotations?.count ?? 0){
  5697. if let an = annotations?[i] as? CPDFMarkupAnnotation {
  5698. let markupContent = an.markupContent()
  5699. if markupContent.isEmpty == false {
  5700. if copyText.isEmpty == true {
  5701. copyText = markupContent
  5702. } else {
  5703. copyText = copyText + "\n" + markupContent
  5704. }
  5705. }
  5706. }
  5707. }
  5708. let pboard = NSPasteboard.general
  5709. if copyText.isEmpty == false {
  5710. pboard.clearContents()
  5711. pboard.writeObjects([copyText as NSPasteboardWriting])
  5712. }
  5713. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Cut) {
  5714. if(listView.isEditing() == true) {
  5715. listView.cutEditAreaAction()
  5716. } else {
  5717. listView.cut(nil)
  5718. }
  5719. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Delete) {
  5720. if(listView.isEditing() == true) {
  5721. listView.remove(with: listView.km_EditingAreas())
  5722. } else {
  5723. listView.delete(nil)
  5724. }
  5725. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ShowPopUI) {
  5726. UserDefaults.standard.setValue(true, forKey: settingsShowQuickActionBarKey)
  5727. SettingsManager.sharedInstance.showQuickActionBar = true
  5728. reloadPopUIWindow()
  5729. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_LinkReade) {
  5730. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SortFirstAnnotation) {
  5731. listView.menuItemClick_BringFront(nil)
  5732. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SortTopAnnotation) {
  5733. listView.menuItemClick_BringForward(nil)
  5734. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SortBottomAnnotation) {
  5735. listView.menuItemClick_SendBackward(nil)
  5736. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_SortLastAnnotation) {
  5737. listView.menuItemClick_SendBack(nil)
  5738. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_Content) {
  5739. if let activeAnnotation = listView.activeAnnotation {
  5740. listView.edit(activeAnnotation)
  5741. }
  5742. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Redact_Apply) {
  5743. redactApplyAction()
  5744. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Redact_Multipage) {
  5745. if let redactAnnotation = listView.activeAnnotation as? CPDFRedactAnnotation {
  5746. redactMultipageAction(redactAnnotation: redactAnnotation)
  5747. }
  5748. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Redact_Default) {
  5749. if let redactAnnotation = listView.activeAnnotation as? CPDFRedactAnnotation {
  5750. setPropertiesDefault(annotation: redactAnnotation)
  5751. }
  5752. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Split_ViewSingleMode) {
  5753. listView.viewSplitMode = .disable
  5754. reloadPDFSplitInfo()
  5755. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Split_ViewVerticalMode) {
  5756. listView.viewSplitMode = .vertical
  5757. reloadPDFSplitInfo()
  5758. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Split_ViewHorizontalMode) {
  5759. listView.viewSplitMode = .horizontal
  5760. reloadPDFSplitInfo()
  5761. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Split_Sync) {
  5762. viewManager.splitSyncScroll = !viewManager.splitSyncScroll
  5763. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Split_ShowBar) {
  5764. viewManager.splitShowBottomBar = !viewManager.splitShowBottomBar
  5765. splitPDFController?.refreshToolbarState()
  5766. reloadPDFPageNumberToolbar()
  5767. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_LeftAlight) {
  5768. if(listView.isEditing()) {
  5769. listView.changeEditingAreas(.left)
  5770. } else {
  5771. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .left)
  5772. }
  5773. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_VerticallyAlight) {
  5774. if(listView.isEditing()) {
  5775. listView.changeEditingAreas(.vertical)
  5776. } else {
  5777. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .vertical)
  5778. }
  5779. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_RightAlight) {
  5780. if(listView.isEditing()) {
  5781. listView.changeEditingAreas(.right)
  5782. } else {
  5783. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .right)
  5784. }
  5785. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_TopAlight) {
  5786. if(listView.isEditing()) {
  5787. listView.changeEditingAreas(.top)
  5788. } else {
  5789. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .top)
  5790. }
  5791. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_HorizontallyAlight) {
  5792. if(listView.isEditing()) {
  5793. listView.changeEditingAreas(.horizontally)
  5794. } else {
  5795. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .horizontally)
  5796. }
  5797. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_BottomAlight) {
  5798. if(listView.isEditing()) {
  5799. listView.changeEditingAreas(.bottom)
  5800. } else {
  5801. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .bottom)
  5802. }
  5803. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_DistributeHorizontally) {
  5804. if(listView.isEditing()) {
  5805. listView.changeEditingAreas(.disHorizontally)
  5806. } else {
  5807. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .disHorizontally)
  5808. }
  5809. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_DistributeVertically) {
  5810. if(listView.isEditing()) {
  5811. listView.changeEditingAreas(.disVertical)
  5812. } else {
  5813. listView.change(listView.activeAnnotations as? [CPDFAnnotation], alignmentType: .disVertical)
  5814. }
  5815. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ExportJPG) {
  5816. if(listView.isEditing() == true) {
  5817. listView.exportEditingImageAreasAction(format: "jpg")
  5818. } else {
  5819. if let currentSelection = listView.currentSelection {
  5820. if(currentSelection.selectionType() == .image) {
  5821. listView.exprotSelection(currentSelection, type: 1)
  5822. }
  5823. }
  5824. }
  5825. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ExportPNG) {
  5826. if(listView.isEditing() == true) {
  5827. listView.exportEditingImageAreasAction(format: "png")
  5828. } else {
  5829. if let currentSelection = listView.currentSelection {
  5830. if(currentSelection.selectionType() == .image) {
  5831. listView.exprotSelection(currentSelection, type: 0)
  5832. }
  5833. }
  5834. }
  5835. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_ExportPDF) {
  5836. if(listView.isEditing() == true) {
  5837. listView.exportEditingImageAreasAction(format: "pdf")
  5838. } else {
  5839. if let currentSelection = listView.currentSelection {
  5840. if(currentSelection.selectionType() == .image) {
  5841. listView.exprotSelection(currentSelection, type: 2)
  5842. }
  5843. }
  5844. }
  5845. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_Paste) {
  5846. listView.pasteEditAreaMatchStyleAction()
  5847. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_NoStylePaste) {
  5848. listView.pasteEditAreaAction()
  5849. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_SelectAll) {
  5850. if listView.km_EditingAreas().count <= 0 {
  5851. listView.selectAllAreaAction()
  5852. } else {
  5853. listView.selectAllAction(with: listView.km_EditingAreas().first)
  5854. }
  5855. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_AddLink) {
  5856. pdfToolbarController?.clickWithIdentify(KMPDFToolbar_edit_link_Identifier)
  5857. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_AddText) {
  5858. pdfToolbarController?.clickWithIdentify(KMPDFToolbar_edit_text_Identifier)
  5859. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_AddImage) {
  5860. pdfToolbarController?.clickWithIdentify(KMPDFToolbar_edit_image_Identifier)
  5861. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontBold) {
  5862. listView.setEditingTextarea_Bold()
  5863. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontItalic) {
  5864. listView.setEditingTextarea_Italic()
  5865. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontUnderline) {
  5866. listView.setEditingTextarea_Under()
  5867. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontStrikeout) {
  5868. listView.setEditingTextarea_Strikeout()
  5869. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontZoomIn) {
  5870. listView.zoomInEditTextFontSize()
  5871. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontZoomOut) {
  5872. listView.zoomInEditTextFontSize()
  5873. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontColor) {
  5874. let colorPanel = NSColorPanel.shared
  5875. colorPanel.setTarget(self)
  5876. colorPanel.setAction(#selector(editFontColorItemPanelAction(_:)))
  5877. colorPanel.orderFront(nil)
  5878. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontH1Name) {
  5879. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .h1)
  5880. self.updateEditPDFTextFontModel(model)
  5881. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontH2Name) {
  5882. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .h2)
  5883. self.updateEditPDFTextFontModel(model)
  5884. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontH3Name) {
  5885. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .h3)
  5886. self.updateEditPDFTextFontModel(model)
  5887. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontB1Name) {
  5888. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .b1)
  5889. self.updateEditPDFTextFontModel(model)
  5890. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontB2Name) {
  5891. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .b2)
  5892. self.updateEditPDFTextFontModel(model)
  5893. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontB3Name) {
  5894. let model = KMEditPDFTextManager.manager.fetchUserDefaultData(type: .b3)
  5895. self.updateEditPDFTextFontModel(model)
  5896. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontLeftAlight) {
  5897. listView.setEditingTextarea_Alignment(align:.left)
  5898. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontCenterAlight) {
  5899. listView.setEditingTextarea_Alignment(align:.center)
  5900. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontRightAlight) {
  5901. listView.setEditingTextarea_Alignment(align:.right)
  5902. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_FontJustAlight) {
  5903. listView.setEditingTextarea_Alignment(align:.justified)
  5904. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_RotateLeft) {
  5905. listView.rotateEditingAreas(90)
  5906. rightSideController?.reloadEditingAreas()
  5907. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_RotateRight) {
  5908. listView.rotateEditingAreas(-90)
  5909. rightSideController?.reloadEditingAreas()
  5910. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_HorizontalMirror) {
  5911. listView.reverseYAction()
  5912. rightSideController?.reloadEditingAreas()
  5913. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_VerticalMirror) {
  5914. listView.reverseXAction()
  5915. rightSideController?.reloadEditingAreas()
  5916. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_Crop) {
  5917. listView.cropAction()
  5918. rightSideController?.reloadEditingAreas()
  5919. } else if(menuItemProperty?.identifier == PDFViewMenuIdentifier_Edit_Replace) {
  5920. listView.replaceImageEdit()
  5921. rightSideController?.reloadEditingAreas()
  5922. } else if (menuItemProperty?.identifier == PDFViewMenuIdentifier_Normal_RedactProperties) {
  5923. if let redactAnnotation = listView.activeAnnotation as? CPDFRedactAnnotation{
  5924. showRedactProperty(readactAnnotation: redactAnnotation)
  5925. }
  5926. }
  5927. }
  5928. func componentGroupDidDismiss(group: ComponentGroup?) {
  5929. }
  5930. func updateEditPDFTextFontModel(_ model: KMEditPDFTextFontModel) {
  5931. let fontName = model.fontName
  5932. let fontStyle = model.fontStyle
  5933. let fontSize = model.fontSize
  5934. let bold = model.bold
  5935. let italic = model.italic
  5936. let alignment = model.alignment
  5937. let font = CPDFFont(familyName: fontName, fontStyle: fontStyle)
  5938. listView.setEditingTextarea_font(font: font)
  5939. listView.setEditingTextarea_FontSize(size: fontSize)
  5940. listView.setEditingTextarea(isBold: bold)
  5941. listView.setEditingTextarea(isItalic: italic)
  5942. listView.setEditingTextarea_Alignment(align: alignment)
  5943. }
  5944. }
  5945. // MARK: - KMSnapshotWindowControllerDelegate
  5946. extension KMMainViewController: KMSnapshotWindowControllerDelegate {
  5947. func snapshotControllerWillClose(_ controller: KMSnapshotWindowController) {
  5948. }
  5949. func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect {
  5950. return CGRectZero
  5951. }
  5952. func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController) {
  5953. }
  5954. }
  5955. // MARK: - CPDFDocumentDelegate
  5956. extension KMMainViewController: CPDFDocumentDelegate {
  5957. func documentDidBeginDocumentFind(_ document: CPDFDocument!) {
  5958. }
  5959. func documentDidEndDocumentFind(_ document: CPDFDocument!) {
  5960. }
  5961. }
  5962. // MARK: - 测量代理CDistanceSettingWindowControllerDelegate
  5963. extension KMMainViewController : CDistanceSettingWindowControllerDelegate {
  5964. func distanceSettingWindowController(_ distanceSettingWindowController: CDistanceSettingWindowController, updateMeasureInfo measureInfo: CPDFMeasureInfo?) {
  5965. if measureInfo != nil {
  5966. if self.listView.activeAnnotations.count > 0 {
  5967. if self.listView.activeAnnotation.isKind(of: CPDFPolylineAnnotation.self) {
  5968. self.updateMeasureInfo((self.listView.activeAnnotation as! CPDFPolylineAnnotation).measureInfo, withNewMeasure: measureInfo)
  5969. } else if self.listView.activeAnnotation.isKind(of: CPDFPolygonAnnotation.self) {
  5970. self.updateMeasureInfo((self.listView.activeAnnotation as! CPDFPolygonAnnotation).measureInfo, withNewMeasure: measureInfo)
  5971. } else if let data = self.listView.activeAnnotation as? CPDFLineAnnotation, data.isMeasure {
  5972. self.updateMeasureInfo(data.measureInfo, withNewMeasure: measureInfo)
  5973. }
  5974. self.listView.setNeedsDisplayAnnotationViewFor(self.listView.activeAnnotation.page)
  5975. }
  5976. self.updateMeasureInfo(self.listView.distanceMeasureInfo, withNewMeasure: measureInfo)
  5977. self.updateMeasureInfo(self.listView.perimeterMeasureInfo, withNewMeasure: measureInfo)
  5978. self.updateMeasureInfo(self.listView.polygonAreaMeasureInfo, withNewMeasure: measureInfo)
  5979. self.updateMeasureInfo(self.listView.squareAreaMeasureInfo, withNewMeasure: measureInfo)
  5980. }
  5981. }
  5982. func updateMeasureInfo(_ measureInfo: CPDFMeasureInfo?, withNewMeasure newMeasure: CPDFMeasureInfo?) {
  5983. guard let measureInfo = measureInfo else { return }
  5984. guard let newMeasure = newMeasure else { return }
  5985. measureInfo.rulerBase = newMeasure.rulerBase
  5986. measureInfo.rulerTranslate = newMeasure.rulerTranslate
  5987. measureInfo.rulerBaseUnit = newMeasure.rulerBaseUnit
  5988. measureInfo.rulerTranslateUnit = newMeasure.rulerTranslateUnit
  5989. measureInfo.factor = newMeasure.factor
  5990. measureInfo.precision = newMeasure.precision
  5991. }
  5992. }
  5993. //MARK: - extension
  5994. extension KMMainViewController {
  5995. func removeAllAnnotations() {
  5996. let alert = NSAlert()
  5997. alert.messageText = KMLocalizedString("This will permanently remove all annotations. Are you sure to continue?", comment: "")
  5998. alert.addButton(withTitle: KMLocalizedString("Yes", comment:""))
  5999. alert.addButton(withTitle: KMLocalizedString("No", comment:""))
  6000. if (alert.runModal() != .alertFirstButtonReturn) {
  6001. return
  6002. }
  6003. DispatchQueue.main.async {
  6004. self.removeAllAnnotationsStore.store(t: self.listView)
  6005. }
  6006. }
  6007. //MARK: - AI
  6008. func showAITypeChooseView(aiConfigType: AIConfigType) -> Void {
  6009. if (self.document != nil) {
  6010. AIChatInfoManager.defaultManager.currentFilePath = (self.document?.documentURL.path)!
  6011. } else {
  6012. AIChatInfoManager.defaultManager.currentFilePath = ""
  6013. }
  6014. let windowVC: AINewConfigWindowController = AINewConfigWindowController.currentWC()
  6015. windowVC.chooseCurFileHandle = {[unowned self] windowVC in
  6016. if AIChatInfoManager.defaultManager.currentFilePath.isEmpty == false {
  6017. let documentArray = NSDocumentController.shared.documents
  6018. var didFileEdit: Bool = false
  6019. var curDoc: KMMainDocument!
  6020. for document in documentArray {
  6021. if document.fileURL?.path == AIChatInfoManager.defaultManager.currentFilePath {
  6022. didFileEdit = document.isDocumentEdited
  6023. curDoc = document as! KMMainDocument
  6024. break
  6025. }
  6026. }
  6027. if didFileEdit {
  6028. let tempFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(AIChatInfoManager.defaultManager.currentFilePath.lastPathComponent)
  6029. if FileManager.default.fileExists(atPath: tempFileURL.path) {
  6030. do {
  6031. try FileManager.default.removeItem(at: tempFileURL)
  6032. } catch {
  6033. }
  6034. }
  6035. if curDoc != nil {
  6036. curDoc.mainViewController?.SaveTempPDFDocumentToURLPath(tempPath: tempFileURL.path)
  6037. }
  6038. }
  6039. windowVC.window?.becomeMain()
  6040. }
  6041. }
  6042. if windowVC.window?.isVisible == true && windowVC.didSetOriginFrame == true {
  6043. } else {
  6044. var windowRect = windowVC.window?.frame
  6045. windowRect!.origin.x = NSMaxX(self.view.window!.frame) - (windowRect?.size.width)!
  6046. windowRect!.origin.y = NSMaxY(self.view.window!.frame) - (windowRect?.size.height)! - 64
  6047. windowVC.window?.setFrame(windowRect!, display: true)
  6048. windowVC.didSetOriginFrame = true
  6049. }
  6050. windowVC.eventLabel = "AITools_Tbr"
  6051. windowVC.showWindow(nil)
  6052. if (aiConfigType != .none) {
  6053. windowVC.eventLabel = "AITools_Start"
  6054. if self.listView.currentSelection?.string()?.isEmpty == false {
  6055. windowVC.setCurrentPDFSelection(self.listView.currentSelection.string())
  6056. }
  6057. windowVC.chooseAIFunctionWithType(aiConfigType)
  6058. }
  6059. }
  6060. @objc func aiTipIconViewShowStateChangeNoti() {
  6061. }
  6062. //MARK: - 引导
  6063. func loadFunctionGuide() -> Void {
  6064. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  6065. if self.view.window != nil {
  6066. self.loadOpenFileFunctionGuide(.openFileNormal)
  6067. }
  6068. }
  6069. }
  6070. func loadOpenFileFunctionGuide(_ showType: KMGuideInfoType) -> Void {
  6071. if showType == .openFileNormal && KMGuideInfoWindowController.availableShow(.openFileNormal) {
  6072. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  6073. guard let guideWC = self.guideInfoWindowController else { return }
  6074. guideWC.type = .openFileNormal
  6075. // guideWC.openPanelRect = (self.view.window?.contentView?.convert(leftPanelItem.frame, from: leftPanelItem.superview)) ?? .zero
  6076. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  6077. guideWC.normalGuideFinishHandle = { [weak self] windowVC in
  6078. }
  6079. guideWC.finishHandle = { [weak self] windowVC, type in
  6080. if type == .windowNewFinish ||
  6081. type == . windowDigitalFinish {
  6082. self?.checkFirstTrialController()
  6083. }
  6084. }
  6085. guideWC.openFileToggleHandle = { [weak self] windowVC, type in
  6086. self?.checkFirstTrialController()
  6087. }
  6088. var rect = self.view.window!.frame
  6089. rect.size.height -= 20
  6090. guideWC.window?.setFrame(rect, display: false)
  6091. guideWC.window?.minSize = rect.size
  6092. guideWC.window?.maxSize = rect.size
  6093. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  6094. guideWC.show()
  6095. } else if showType == .digitalSignGuide && KMGuideInfoWindowController.availableShow(.digitalSignGuide) {
  6096. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  6097. guard let guideWC = self.guideInfoWindowController else { return }
  6098. guideWC.type = .digitalSignGuide
  6099. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  6100. guideWC.finishHandle = { [weak self] windowVC, type in
  6101. self?.checkFirstTrialController()
  6102. }
  6103. var rect = self.view.window!.frame
  6104. rect.size.height -= 20
  6105. guideWC.window?.setFrame(rect, display: false)
  6106. guideWC.window?.minSize = rect.size
  6107. guideWC.window?.maxSize = rect.size
  6108. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  6109. guideWC.show()
  6110. } else if showType == .pdfCompareGuide && KMGuideInfoWindowController.availableShow(.pdfCompareGuide) {
  6111. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
  6112. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  6113. guard let guideWC = self.guideInfoWindowController else { return }
  6114. guideWC.type = .pdfCompareGuide
  6115. guard let win = self.view.window else {
  6116. return
  6117. }
  6118. guideWC.finishHandle = { [weak self] winC, type in
  6119. if type == .windowNewFinish {
  6120. DispatchQueue.main.async {
  6121. self?.loadOpenFileFunctionGuide(.measureGuide)
  6122. }
  6123. return
  6124. }
  6125. }
  6126. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  6127. var rect = self.view.window!.frame
  6128. rect.size.height -= 20
  6129. guideWC.window?.setFrame(rect, display: false)
  6130. guideWC.window?.minSize = rect.size
  6131. guideWC.window?.maxSize = rect.size
  6132. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  6133. guideWC.show()
  6134. }
  6135. } else if showType == .measureGuide && KMGuideInfoWindowController.availableShow(.measureGuide) {
  6136. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
  6137. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  6138. guard let guideWC = self.guideInfoWindowController else { return }
  6139. guard let _ = self.view.window else {
  6140. return
  6141. }
  6142. guideWC.type = .measureGuide
  6143. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  6144. var rect = self.view.window?.frame ?? .zero
  6145. rect.size.height -= 20
  6146. guideWC.window?.setFrame(rect, display: false)
  6147. guideWC.window?.minSize = rect.size
  6148. guideWC.window?.maxSize = rect.size
  6149. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  6150. guideWC.show()
  6151. }
  6152. } else if showType == .convertGuide && KMGuideInfoWindowController.availableShow(.convertGuide) {
  6153. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  6154. self.guideInfoWindowController = KMGuideInfoWindowController.currentWC()
  6155. guard let guideWC = self.guideInfoWindowController else { return }
  6156. guideWC.type = .convertGuide
  6157. guard let win = self.view.window else {
  6158. return
  6159. }
  6160. guideWC.purchaseHandle = { [weak self] windowVC in
  6161. #if VERSION_DMG
  6162. if IAPProductsManager.default().isAvailableAllFunction() {
  6163. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  6164. //Convert:
  6165. } else {
  6166. let limitWC = KMPurchaseLimitWindowController.currentLimitWC()
  6167. limitWC.continueBlock = { windowController in
  6168. }
  6169. limitWC.window?.center()
  6170. limitWC.showWindow(nil)
  6171. }
  6172. } else {
  6173. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  6174. }
  6175. #else
  6176. if IAPProductsManager.default().isAvailableAllFunction() {
  6177. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() {
  6178. //Convert:
  6179. } else {
  6180. var vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  6181. vc.showWindow(nil)
  6182. }
  6183. } else {
  6184. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  6185. }
  6186. #endif
  6187. }
  6188. guideWC.window?.collectionBehavior = [.canJoinAllSpaces]
  6189. var rect = self.view.window?.frame ?? .zero
  6190. rect.size.height -= 20
  6191. guideWC.window?.setFrame(rect, display: false)
  6192. guideWC.window?.minSize = rect.size
  6193. guideWC.window?.maxSize = rect.size
  6194. self.view.window?.addChildWindow(guideWC.window!, ordered: .above)
  6195. guideWC.show()
  6196. }
  6197. } else {
  6198. }
  6199. }
  6200. func checkFirstTrialController() -> Void {
  6201. #if VERSION_DMG
  6202. //打开文档后引导相关
  6203. if VerificationManager.default().status == .none {
  6204. let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  6205. let lastVersion = UserDefaults.standard.object(forKey: "SKLastTrialVersionMainDocumentLaunchedKey") as? String ?? ""
  6206. if lastVersion.isEmpty || lastVersion != appVersion {
  6207. UserDefaults.standard.setValue(appVersion, forKey: "SKLastTrialVersionMainDocumentLaunchedKey")
  6208. UserDefaults.standard.synchronize()
  6209. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  6210. }
  6211. }
  6212. #endif
  6213. }
  6214. // MARK: - Private Methods
  6215. func isPDFPageCountExceedsLimit(filePath: String) -> Bool {
  6216. let url = URL(fileURLWithPath: filePath)
  6217. guard let document = PDFDocument(url: url) else {
  6218. return false
  6219. }
  6220. let pageCount = document.pageCount
  6221. return pageCount > 30
  6222. }
  6223. // MARK: - Redact 【标记密文】
  6224. func redactApplyAction() {
  6225. let returnCode = KMAlertTool.runModelForMainThread_r(message: "", informative: KMLocalizedString("This will permanently remove the redacted information from this document. Once you save this document, you won’t be able to retrieve the redacted information."), buttons: [KMLocalizedString("Apply"), KMLocalizedString("Cancel")])
  6226. if returnCode == .alertFirstButtonReturn {
  6227. DispatchQueue.main.async {
  6228. self.saveAsPath()
  6229. }
  6230. }
  6231. }
  6232. func redactMultipageAction(redactAnnotation:CPDFRedactAnnotation) {
  6233. let pagesWindowController = KMRedactSelectPagesWindowController(document: listView.document)
  6234. pagesWindowController.own_beginSheetModal(for: self.view.window) { result in
  6235. }
  6236. pagesWindowController.callback = { [weak self] pages in
  6237. if pages.count > 0 {
  6238. self?.listView.redactAddAnnotationPages(pages, redactAnnotation: redactAnnotation)
  6239. }
  6240. }
  6241. }
  6242. func setPropertiesDefault(annotation:CPDFRedactAnnotation) {
  6243. CPDFRedactAnnotation.update_defaultOutlineColor(annotation.borderColor())
  6244. CPDFRedactAnnotation.update_defaultFillColor(annotation.interiorColor())
  6245. CPDFRedactAnnotation.update_defaultTextColor(annotation.fontColor())
  6246. CPDFRedactAnnotation.update_defaultFontSize(annotation.fontSize)
  6247. CPDFRedactAnnotation.update_defaultFontAlignment(annotation.alignment())
  6248. CPDFRedactAnnotation.update_defaultOverlayText(annotation.overlayText())
  6249. }
  6250. func exeRedactConfirm(_ type: KMRedactConfirmType, callback: @escaping () -> ()?) {
  6251. let windowController = KMRedactConfirmWindowController(type)
  6252. self.currentWindowController = windowController
  6253. self.view.window?.beginSheet(windowController.window!)
  6254. windowController.itemClick = { [weak self] index in
  6255. if (index == 2) { /// 取消
  6256. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  6257. self?.currentWindowController = nil
  6258. callback()
  6259. return
  6260. }
  6261. self?.view.window?.endSheet((self?.currentWindowController.window)!)
  6262. self?.currentWindowController = nil
  6263. let panel = NSSavePanel()
  6264. panel.nameFieldStringValue = "[新文件]"+((self?.listView.document?.documentURL.lastPathComponent) ?? "")
  6265. let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
  6266. button.state = .on
  6267. panel.accessoryView = button
  6268. panel.isExtensionHidden = true
  6269. panel.beginSheetModal(for: (self?.view.window!)!) { response in
  6270. if response != .OK {
  6271. callback()
  6272. return
  6273. }
  6274. if (type == .redactOne) {
  6275. let anno = self!.listView.activeAnnotation
  6276. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  6277. callback()
  6278. return
  6279. }
  6280. (anno as! CPDFRedactAnnotation).applyRedaction()
  6281. } else if (type == .redactAll) {
  6282. self?.listView.document?.applyRedactions()
  6283. } else if (type == .eraserOne) {
  6284. let anno = self!.listView.activeAnnotation
  6285. if (anno == nil || (anno?.isKind(of: CPDFRedactAnnotation.self)) == false) {
  6286. callback()
  6287. return
  6288. }
  6289. anno?.page.erasureRedact(from: anno!.bounds)
  6290. } else if (type == .eraserAll) {
  6291. KMRedactTools.eraserDocument((self?.listView.document)!) { result, errorAnno in
  6292. if (result == false) {
  6293. callback()
  6294. return
  6295. }
  6296. }
  6297. }
  6298. self!.listView.document?.write(to: panel.url)
  6299. if (button.state == .on) {
  6300. NSDocumentController.shared.openDocument(withContentsOf: panel.url!, display: true) { document, alreadyOpen, error in
  6301. }
  6302. } else {
  6303. NSWorkspace.shared.activateFileViewerSelecting([panel.url!])
  6304. }
  6305. callback()
  6306. }
  6307. }
  6308. }
  6309. func enterRedactAlert() {
  6310. if UserDefaults.standard.object(forKey: "kRedact") != nil {
  6311. return
  6312. }
  6313. let alert = NSAlert()
  6314. alert.alertStyle = .informational
  6315. alert.informativeText = KMLocalizedString("Redaction allows you to permanently mask and remove sensitive content.")
  6316. alert.messageText = KMLocalizedString("""
  6317. Redaction requires two steps:
  6318. 1. Mark for Redaction
  6319. 2. Apply Redactions
  6320. Note: Redactions are not applied permanently until you select Apply Redactions.
  6321. """)
  6322. alert.addButton(withTitle: KMLocalizedString("OK"))
  6323. alert.showsSuppressionButton = true
  6324. let response = alert.runModal()
  6325. if response.rawValue == 1000 {
  6326. if alert.suppressionButton?.state == .on {
  6327. UserDefaults.standard.set("YES", forKey: "kRedact")
  6328. UserDefaults.standard.synchronize()
  6329. }
  6330. }
  6331. }
  6332. // MARK: - 保存文档
  6333. internal func needSaveDocument() -> Bool {
  6334. if (self.isPDFDocumentEdited) {
  6335. return self.isPDFDocumentEdited
  6336. }
  6337. if (self.needSave) {
  6338. return self.needSave
  6339. }
  6340. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  6341. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  6342. return false
  6343. }
  6344. return true
  6345. }
  6346. internal func saveDocument(overlookDocumentIfEdited overlook: Bool = false) {
  6347. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  6348. if (overlook) {
  6349. document?.save(nil)
  6350. return
  6351. }
  6352. if (self.isPDFDocumentEdited) {
  6353. self.clearIsPDFDocumentEdited()
  6354. self.needSave = false
  6355. document?.save(nil)
  6356. return
  6357. }
  6358. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  6359. return
  6360. }
  6361. document?.save(nil)
  6362. }
  6363. internal func asyncSaveDocument(overlookDocumentIfEdited overlook: Bool = false, callback:@escaping KMCommonBlock) {
  6364. let document: KMMainDocument? = self.myDocument as? KMMainDocument
  6365. if (overlook) {
  6366. DispatchQueue.main.async {
  6367. document?.save(nil)
  6368. callback()
  6369. }
  6370. return
  6371. }
  6372. if (self.isPDFDocumentEdited) {
  6373. self.clearIsPDFDocumentEdited()
  6374. self.needSave = false
  6375. DispatchQueue.main.async {
  6376. document?.save(nil)
  6377. callback()
  6378. }
  6379. return
  6380. }
  6381. if (document?.isDocumentEdited == nil || document?.isDocumentEdited == false) {
  6382. callback()
  6383. return
  6384. }
  6385. DispatchQueue.main.async {
  6386. document?.save(nil)
  6387. callback()
  6388. }
  6389. }
  6390. internal func saveDocumentWithProgressAlert(callback:@escaping KMCommonBlock) {
  6391. // 显示进度
  6392. AutoSaveManager.manager.isSaving = true
  6393. self.showProgressWindow(message: KMLocalizedString("Save", comment: "") + "PDF")
  6394. self.progressC?.maxValue = 3.0
  6395. self.progressC?.increment(by: 1.0)
  6396. // 保存文档
  6397. self.asyncSaveDocument { [weak self] params in
  6398. // 执行进度 [假进度]
  6399. self?.progressC?.increment(by: 1.0)
  6400. self?.progressC?.increment(by: 1.0)
  6401. DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
  6402. // 隐藏进度
  6403. self?.hiddenProgressWindow()
  6404. DispatchQueue.main.asyncAfter(deadline: .now()+1) {
  6405. AutoSaveManager.manager.isSaving = false
  6406. }
  6407. // 回调
  6408. callback()
  6409. }
  6410. }
  6411. }
  6412. func SaveTempPDFDocumentToURLPath(tempPath: String) {
  6413. self.document?.write(toFile: tempPath)
  6414. }
  6415. // MARK: - 定时保存
  6416. func addAutoSaveEvent() {
  6417. if (self.autoSaveTimer != nil) {
  6418. self.autoSaveTimer?.invalidate()
  6419. self.autoSaveTimer = nil
  6420. }
  6421. if self.document != nil {
  6422. self.autoSaveTimer = Timer.scheduledTimer(withTimeInterval: AutoSaveManager.manager.timeInterval * 60, repeats: true, block: { [weak self] timer in
  6423. self?.autoSaveTimerAction(timer)
  6424. })
  6425. }
  6426. self.checkAutoSaveInfo()
  6427. }
  6428. func checkAutoSaveInfo() {
  6429. guard let cnt = AutoSaveManager.manager.autoSavePaths?.count, cnt > 0 else {
  6430. return
  6431. }
  6432. if AutoSaveManager.manager.autoSaveAlertShow {
  6433. return
  6434. }
  6435. AutoSaveManager.manager.autoSaveDidEndAction = false
  6436. AutoSaveManager.manager.autoSaveAlertShow = true
  6437. let blockSaveWindow = AutoSavePopController()
  6438. blockSaveWindow.cancelHandle = { [weak self] windowController in
  6439. AutoSaveManager.manager.autoSaveDidEndAction = true
  6440. AutoSaveManager.manager.clearCache()
  6441. self?.km_quick_endSheet()
  6442. }
  6443. blockSaveWindow.confirmHandle = { [weak self] windowController in
  6444. self?.km_quick_endSheet()
  6445. self?.saveAutoSaveInfo()
  6446. }
  6447. self.km_beginSheet(windowC: blockSaveWindow)
  6448. }
  6449. func saveAutoSaveInfo() {
  6450. let openPanel = NSOpenPanel()
  6451. openPanel.canChooseDirectories = true
  6452. openPanel.canChooseFiles = false
  6453. openPanel.allowsMultipleSelection = false
  6454. let win = NSApp.keyWindow != nil ? NSApp.keyWindow : self.view.window
  6455. openPanel.beginSheetModal(for: win!) { result in
  6456. if (result == .OK) {
  6457. let folderPath = openPanel.url?.path ?? openPanel.url?.absoluteString
  6458. for path in AutoSaveManager.manager.opendPaths ?? [] {
  6459. let _path = path as? String
  6460. var newPath = "\(folderPath ?? "")/\(_path?.lastPathComponent ?? "")"
  6461. newPath = self.getValidFilePath(newPath)
  6462. do {
  6463. try FileManager.default.moveItem(atPath: _path ?? "", toPath: newPath)
  6464. } catch {
  6465. NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: newPath)])
  6466. }
  6467. }
  6468. AutoSaveManager.manager.clearCache()
  6469. }
  6470. AutoSaveManager.manager.autoSaveDidEndAction = true
  6471. }
  6472. }
  6473. func autoSaveTimerAction(_ timer: Timer) {
  6474. if (self.document == nil || self.listView.document?.documentURL.path == nil) {
  6475. return
  6476. }
  6477. if AutoSaveManager.manager.autoSaveDidEndAction == false {
  6478. //防止提示弹窗出现后,未进行任何操作又进入自动保存的机制
  6479. return
  6480. }
  6481. if let data = self.document?.isLocked, data {
  6482. return
  6483. }
  6484. if AutoSaveManager.manager.autoSaveEnabled == false {
  6485. return
  6486. }
  6487. let documentArray = NSDocumentController.shared.documents
  6488. var didFileEdit = false
  6489. for doc in documentArray {
  6490. if doc.fileURL?.path == self.document?.documentURL.path {
  6491. didFileEdit = doc.isDocumentEdited
  6492. break
  6493. }
  6494. }
  6495. if (didFileEdit == false) {
  6496. return
  6497. }
  6498. AutoSaveManager.manager.isSaving = true
  6499. let savePath = AutoSaveManager.manager.autoSaveWithPath(self.listView.document?.documentURL.path ?? "")
  6500. if (!self.document!.isLocked) {
  6501. self.document?.write(to: URL(fileURLWithPath: savePath))
  6502. }
  6503. DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
  6504. AutoSaveManager.manager.isSaving = false
  6505. }
  6506. }
  6507. func removeAutoSaveInfo() {
  6508. if self.autoSaveTimer != nil {
  6509. self.autoSaveTimer?.invalidate()
  6510. self.autoSaveTimer = nil
  6511. }
  6512. if AutoSaveManager.manager.autoSaveDidEndAction == false {
  6513. //防止提示弹窗出现后,未进行任何操作又进入自动保存的机制
  6514. return
  6515. }
  6516. if AutoSaveManager.manager.autoSaveEnabled == false {
  6517. return
  6518. }
  6519. if self.document == nil || self.listView.document?.documentURL.path == nil {
  6520. return
  6521. }
  6522. AutoSaveManager.manager.removeAutoSavePath(self.listView.document?.documentURL.path ?? "")
  6523. }
  6524. // MARK: - 选择缩放模式
  6525. @objc public func selectZoom(_ type: KMPDFZoomType) {
  6526. switch type {
  6527. case .width:
  6528. self.listView.autoScales = true
  6529. break
  6530. case .fit:
  6531. if let pageHeight = self.listView.currentPage()?.size.height, pageHeight > 0 {
  6532. let pdfviewHeight = self.listView.bounds.size.height
  6533. self.listView.scaleFactor = pdfviewHeight/pageHeight
  6534. self.listView.autoScales = false
  6535. }
  6536. break
  6537. case .actualSize:
  6538. if self.listView.scaleFactor != 1.0 {
  6539. self.listView.scaleFactor = 1.0
  6540. self.listView.autoScales = false
  6541. }
  6542. break
  6543. case .zoom_In:
  6544. self.doZoomIn(nil)
  6545. break
  6546. case .zoom_Out:
  6547. self.doZoomOut(nil)
  6548. break
  6549. }
  6550. }
  6551. func doZoomIn(_ sender: Any?) {
  6552. if (self.listView.canZoomIn) {
  6553. self.listView.zoomIn(nil)
  6554. }
  6555. }
  6556. func doZoomOut(_ sender: Any?) {
  6557. if (self.listView.canZoomOut) {
  6558. self.listView.zoomOut(nil)
  6559. }
  6560. }
  6561. // MARK: - Event 监听
  6562. private func addEventMonitor() {
  6563. if (self.eventMonitor != nil) {
  6564. self.removeEventMonitor()
  6565. }
  6566. self.eventMonitor = NSEvent.addLocalMonitorForEvents(matching: [.scrollWheel, .leftMouseDown, .leftMouseUp]) { [weak self] event in
  6567. if (event.type == .scrollWheel && event.modifierFlags.contains(.option)) { // Alt + 鼠标滚轮
  6568. self?.listView.magnifyWheel(event)
  6569. return nil
  6570. } else if event.type == .leftMouseDown {
  6571. let point = event.locationInView(self?.listView ?? NSView())
  6572. let presentationDrawView = self?.listView.presentationDrawView
  6573. if let data = self?.interactionMode, data == .presentation,CGRectContainsPoint(self?.listView.frame ?? .zero, point),presentationDrawView?.isHidden == true { // 幻灯片模式下
  6574. if point.x >= (self?.listView.frame.maxX ?? 0) / 2 {
  6575. let can = self?.listView.canGoToNextPage() ?? false
  6576. if can {
  6577. self?.listView.goToNextPage(nil)
  6578. }
  6579. } else {
  6580. let can = self?.listView.canGoToPreviousPage() ?? false
  6581. if can {
  6582. self?.listView.goToPreviousPage(nil)
  6583. }
  6584. }
  6585. return nil
  6586. }
  6587. }
  6588. return event
  6589. }
  6590. }
  6591. private func removeEventMonitor() {
  6592. if (self.eventMonitor != nil) {
  6593. KMPrint("已移除事件监听")
  6594. NSEvent.removeMonitor(self.eventMonitor as Any)
  6595. self.eventMonitor = nil
  6596. }
  6597. }
  6598. func addKeyEventMonitor() {
  6599. if (self.keyEventMonitor != nil) {
  6600. self.removeKeyEventMonitor()
  6601. }
  6602. keyEventMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
  6603. if event.keyCode == 53 {
  6604. if self?.listView.annotationType == .freeText {
  6605. self?.listView.keyDown(with: event)
  6606. }
  6607. } else {
  6608. }
  6609. return event
  6610. }
  6611. }
  6612. func removeKeyEventMonitor() {
  6613. if (self.keyEventMonitor != nil) {
  6614. KMPrint("removeKeyEventMonitor 已移除事件监听")
  6615. NSEvent.removeMonitor(self.keyEventMonitor as Any)
  6616. self.keyEventMonitor = nil
  6617. }
  6618. }
  6619. //MARK: - Mouse Event
  6620. override func mouseMoved(with event: NSEvent) {
  6621. self.view.window?.mouseMoved(with: event)
  6622. }
  6623. override func keyDown(with event: NSEvent) {
  6624. listView.keyDown(with: event)
  6625. }
  6626. // MARK: - Tools
  6627. func pdfViewCanHorizontalScroll() -> Bool {
  6628. let scroll = self.listView.scroll()
  6629. if (scroll == nil) {
  6630. return false
  6631. }
  6632. return scroll?.horizontalScroller?.isHidden == nil ? false : !(scroll!.horizontalScroller!.isHidden)
  6633. }
  6634. func pdfViewCanVerticalScroll() -> Bool {
  6635. let scroll = self.listView.scroll()
  6636. if (scroll == nil) {
  6637. return false
  6638. }
  6639. return scroll?.verticalScroller?.isHidden == nil ? false : !(scroll!.verticalScroller!.isHidden)
  6640. }
  6641. // MARK: - Public Methods
  6642. // 清理数据 [eg. 通知]
  6643. public func clearData() {
  6644. KMThumbnailCache.shared.clearCache()
  6645. self.removeNotifications()
  6646. if (self.listView.spellingTag() > 0) {
  6647. NSSpellChecker.shared.closeSpellDocument(withTag: self.listView.spellingTag())
  6648. }
  6649. self.removeAutoSaveInfo()
  6650. self.myDocument = nil
  6651. if textFieldSheet.window?.isVisible == true {
  6652. textFieldSheet.close()
  6653. }
  6654. if NSColorPanel.shared.isVisible == true {
  6655. NSColorPanel.shared.close()
  6656. }
  6657. if KMInfoWindowController.shared.window?.isVisible == true {
  6658. KMInfoWindowController.shared.close()
  6659. }
  6660. }
  6661. public func clearSecureOptions() {
  6662. self._secureOptions = nil
  6663. self.documentAttribute = nil
  6664. }
  6665. public func clearRemoveSecureFlag() {
  6666. self._removeSecureFlag = false
  6667. }
  6668. public func recordIsPDFDocumentEdited(type: KMSubscribeWaterMarkType = .none) {
  6669. km_synchronized(self) {
  6670. self.model.isPDFDocumentEdited = true
  6671. if type == .editText || type == .editImage {
  6672. }
  6673. if let _document = self.myDocument {
  6674. KMTools.setDocumentEditedState(document: _document)
  6675. }
  6676. }
  6677. }
  6678. public func clearIsPDFDocumentEdited() {
  6679. km_synchronized(self) {
  6680. self.model.isPDFDocumentEdited = false
  6681. }
  6682. }
  6683. func showSnapshots(setups: NSArray?) {
  6684. if self.listView.document != nil {
  6685. for setup in setups ?? [] {
  6686. let swc = KMSnapshotWindowController()
  6687. swc.delegate = self
  6688. swc.setPdfDocument(self.listView.document, setup: setup as? NSDictionary)
  6689. swc.setForceOnTop(self.interactionMode != .normal)
  6690. self.myDocument?.addWindowController(swc)
  6691. }
  6692. }
  6693. }
  6694. // MARK: - Noti Actions
  6695. internal func documentDidUnlockNotification(_ sender: Notification) {
  6696. if (self.listView.document != nil && self.listView.document.isEqual(to: sender.object)) {
  6697. if (self.myDocument == nil) {
  6698. return
  6699. }
  6700. if (self.listView.document.allowsPrinting && self.listView.document.allowsCopying) {
  6701. self.hiddenSecureLimitTip()
  6702. }
  6703. let isUnlockFromKeychain = (self.myDocument as? KMMainDocument)?.isUnlockFromKeychain ?? false
  6704. if (isUnlockFromKeychain || self.model.isSaveKeyChain == false) {
  6705. return
  6706. }
  6707. let type = SettingsManager.sharedInstance.keychainType
  6708. if (type == .never) {
  6709. return
  6710. }
  6711. if (type == .always) {
  6712. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  6713. return
  6714. }
  6715. // 保存到钥匙串
  6716. let alert = NSAlert()
  6717. alert.messageText = KMLocalizedString("Remember Password?", comment: "")
  6718. alert.informativeText = KMLocalizedString("Do you want to save this password in your Keychain?", comment: "")
  6719. alert.addButton(withTitle: KMLocalizedString("Yes", comment: ""))
  6720. alert.addButton(withTitle: KMLocalizedString("No", comment: ""))
  6721. if (alert.runModal() == .alertFirstButtonReturn) { // 保存密码
  6722. self.myDocument?.savePasswordInKeychain(self.listView.document.password, self.listView.document)
  6723. return
  6724. }
  6725. }
  6726. }
  6727. internal func applicationWillTerminateNotification(_ sender: Notification) {
  6728. self.savePageNumberIfNeed()
  6729. self.saveDocument()
  6730. }
  6731. internal func pdfViewHighlightFormFiledUpdateNoti(_ sender: Notification) {
  6732. self.alertTipViewController.reloadFormAlertUI()
  6733. }
  6734. }