ComparisonSettingDialog.xaml.cs 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. using System.Windows;
  2. using ComPDFKit.Compare;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. using System.Windows.Controls;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.IO;
  17. using System.Windows.Forms;
  18. using System.Windows.Interop;
  19. using System.Runtime.InteropServices;
  20. using ComPDFKit.Controls.Common;
  21. using ComPDFKit.Controls.Helper;
  22. using ComPDFKit.Controls.PDFControl;
  23. using ComPDFKit.Controls.Properties;
  24. using Color = System.Windows.Media.Color;
  25. using ColorConverter = System.Windows.Media.ColorConverter;
  26. using ComboBox = System.Windows.Forms.ComboBox;
  27. using Image = System.Windows.Controls.Image;
  28. namespace ComPDFKit.Controls.Comparison
  29. {
  30. /// <summary>
  31. /// ConvertPage.xaml 的交互逻辑
  32. /// </summary>
  33. ///
  34. public enum CompareType
  35. {
  36. ContentCompare = 1,
  37. OverwriteCompare = 2
  38. }
  39. public partial class ComparisonSettingDialog : Window
  40. {
  41. [DllImport("gdi32.dll")]
  42. public static extern bool DeleteObject(IntPtr hObject);
  43. /// <summary>
  44. /// 0 Unknow 1 Content Compare 2 Overwrite Compare
  45. /// </summary>
  46. private CompareType CompareType { get; set; } = CompareType.ContentCompare;
  47. public CPDFDocument OldDocument { get; private set; }
  48. public CPDFDocument NewDocument { get; private set; }
  49. private List<string> NewFilePathList { get; set; } = new List<string>();
  50. private List<string> OldFilePathList { get; set; } = new List<string>();
  51. public Dictionary<string, string> FileAndPassword = new Dictionary<string, string>();
  52. //private List<int> OldRange { get; set; } = new List<int>();
  53. //private List<int> NewRange { get; set; } = new List<int>();
  54. private CPDFCompareType ObjectCompareType { get; set; } = CPDFCompareType.CPDFCompareTypeAll;
  55. private Color ReplaceColor { get; set; } = Color.FromRgb(255, 214, 102);
  56. private Color InsertColor { get; set; } = Color.FromRgb(51, 135, 255);
  57. private Color DeleteColor { get; set; } = Color.FromRgb(255, 51, 51);
  58. private double OldOpacity { get; set; } = 1;
  59. private double NewOpacity { get; set; } = 1;
  60. private Color OldMarkColor { get; set; } = Color.FromRgb(255, 51, 51);
  61. private Color NewMarkColor { get; set; } = Color.FromRgb(51, 135, 255);
  62. private CPDFBlendMode MixMode { get; set; } = CPDFBlendMode.CPDFBlendModeDarken;
  63. private PDFViewControl viewCtrl = null;
  64. private List<CPDFBlendMode> MixModeList { get; set; } = new List<CPDFBlendMode>()
  65. {
  66. CPDFBlendMode.CPDFBlendModeNormal,
  67. CPDFBlendMode.CPDFBlendModeMultiply,
  68. CPDFBlendMode.CPDFBlendModeScreen,
  69. CPDFBlendMode.CPDFBlendModeOverlay,
  70. CPDFBlendMode.CPDFBlendModeDarken,
  71. CPDFBlendMode.CPDFBlendModeLighten,
  72. CPDFBlendMode.CPDFBlendModeColorDodge,
  73. CPDFBlendMode.CPDFBlendModeColorBurn,
  74. CPDFBlendMode.CPDFBlendModeHardLight,
  75. CPDFBlendMode.CPDFBlendModeSoftLight,
  76. CPDFBlendMode.CPDFBlendModeDifference,
  77. CPDFBlendMode.CPDFBlendModeExclusion,
  78. CPDFBlendMode.CPDFBlendModeHue,
  79. CPDFBlendMode.CPDFBlendModeSaturation,
  80. CPDFBlendMode.CPDFBlendModeColor,
  81. CPDFBlendMode.CPDFBlendModeLuminosity
  82. };
  83. private bool IsFillWhite { get; set; } = false;
  84. public event EventHandler<UIElement> OnCompareStatusChanged;
  85. public ComparisonSettingDialog(PDFViewControl viewCtrl)
  86. {
  87. InitializeComponent();
  88. this.viewCtrl = viewCtrl;
  89. SwapeImage.IsEnabled = false;
  90. }
  91. public string ConvertBrushToHex(Color color)
  92. {
  93. return $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}";
  94. }
  95. public static Color ConvertHexToColor(string hex)
  96. {
  97. return (Color)ColorConverter.ConvertFromString(hex);
  98. }
  99. private int AddNewFileList(string filename, string filepath)
  100. {
  101. if (!NewFilePathList.Contains(filepath))
  102. {
  103. NewFileComboBox.Items.Insert(0, filename);
  104. NewFilePathList.Insert(0, filepath);
  105. NewFileComboBox.IsEnabled = true;
  106. if (NewFilePathList.Count > 5 && NewFileComboBox.Items.Count > 5)
  107. {
  108. NewFilePathList.RemoveAt(NewFilePathList.Count - 1);
  109. NewFileComboBox.Items.RemoveAt(NewFileComboBox.Items.Count - 1);
  110. }
  111. return 0;
  112. }
  113. else
  114. {
  115. return NewFilePathList.IndexOf(filepath);
  116. }
  117. }
  118. private int AddOldFileList(string filename, string filepath)
  119. {
  120. if (!OldFilePathList.Contains(filepath))
  121. {
  122. OldFileComboBox.Items.Insert(0, filename);
  123. OldFilePathList.Insert(0, filepath);
  124. if (OldFilePathList.Count > 5 && OldFileComboBox.Items.Count > 5)
  125. {
  126. OldFilePathList.RemoveAt(OldFilePathList.Count - 1);
  127. OldFileComboBox.Items.RemoveAt(OldFileComboBox.Items.Count - 1);
  128. }
  129. return 0;
  130. }
  131. else
  132. {
  133. return OldFilePathList.IndexOf(filepath);
  134. }
  135. }
  136. private void CompareType_MouseLeftDown(object sender, MouseButtonEventArgs e)
  137. {
  138. }
  139. public void SetCompareType(CompareType compareType)
  140. {
  141. if (compareType == CompareType.ContentCompare)
  142. {
  143. CompareTypeTab.SelectedIndex = 0;
  144. }
  145. else
  146. {
  147. CompareTypeTab.SelectedIndex = 1;
  148. }
  149. }
  150. private void CompareTypeTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
  151. {
  152. if (CompareTypeTab != null)
  153. {
  154. if (CompareTypeTab.SelectedIndex == 0)
  155. {
  156. CompareType = CompareType.ContentCompare;
  157. ContentSettingBox.Visibility = Visibility.Visible;
  158. OverlaySettingBox.Visibility = Visibility.Collapsed;
  159. }
  160. if (CompareTypeTab.SelectedIndex == 1)
  161. {
  162. CompareType = CompareType.OverwriteCompare;
  163. ContentSettingBox.Visibility = Visibility.Collapsed;
  164. OverlaySettingBox.Visibility = Visibility.Visible;
  165. }
  166. }
  167. }
  168. private void Close_MouseLeftDown(object sender, MouseButtonEventArgs e)
  169. {
  170. //MainWindow parentWnd = Window.GetWindow(this) as MainWindow;
  171. //if (parentWnd == null)
  172. //{
  173. // return;
  174. //}
  175. //parentWnd.ContentGrid.Children.Clear();
  176. //if (parentWnd.PrevElement != null)
  177. //{
  178. // parentWnd.ContentGrid.Children.Add(parentWnd.PrevElement);
  179. //}
  180. }
  181. public void UpdateDocCover(bool isOld)
  182. {
  183. if (isOld)
  184. {
  185. InitOldDocument();
  186. int select = AddOldFileList(OldDocument.FileName, OldDocument.FilePath);
  187. OldFileComboBox.SelectedIndex = select;
  188. }
  189. else
  190. {
  191. InitNewDocument();
  192. int select = AddNewFileList(NewDocument.FileName, NewDocument.FilePath);
  193. NewFileComboBox.SelectedIndex = select;
  194. }
  195. }
  196. public void SetDocument(CPDFDocument doc, bool isOld)
  197. {
  198. if (isOld)
  199. {
  200. OldDocument = doc;
  201. if (OldRange != null && OldRange.Count > 0)
  202. {
  203. List<int> rangeList = new List<int>();
  204. for (int i = OldRange.Min(); i <= OldRange.Max(); i++)
  205. {
  206. if (i >= doc.PageCount)
  207. {
  208. break;
  209. }
  210. rangeList.Add(i);
  211. }
  212. OldRange = rangeList;
  213. }
  214. }
  215. else
  216. {
  217. NewDocument = doc;
  218. if (NewRange != null && NewRange.Count > 0)
  219. {
  220. List<int> rangeList = new List<int>();
  221. for (int i = NewRange.Min(); i <= NewRange.Max(); i++)
  222. {
  223. if (i >= doc.PageCount)
  224. {
  225. break;
  226. }
  227. rangeList.Add(i);
  228. }
  229. NewRange = rangeList;
  230. }
  231. }
  232. }
  233. private void UpdateCover(CPDFDocument doc, Image imageControl, int pageIndex)
  234. {
  235. imageControl.Source = null;
  236. if (doc != null && doc.PageCount > 0 && doc.PageCount > pageIndex)
  237. {
  238. CPDFPage pdfPage = doc.PageAtIndex(pageIndex, true);
  239. int width = (int)pdfPage.PageSize.width;
  240. int height = (int)pdfPage.PageSize.height;
  241. Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  242. BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
  243. pdfPage.RenderPageBitmap(0, 0, width, height, uint.MaxValue, bitmapData.Scan0, 1 , true);
  244. bitmap.UnlockBits(bitmapData);
  245. IntPtr bitmapHandle = bitmap.GetHbitmap();
  246. BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmapHandle, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
  247. imageControl.Source = bitmapSource;
  248. DeleteObject(bitmapHandle);
  249. }
  250. }
  251. private string GetChoosePdf()
  252. {
  253. Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
  254. openFileDialog.Filter = "PDF Files(*.pdf;)|*.pdf;";
  255. if (openFileDialog.ShowDialog() == true)
  256. {
  257. return openFileDialog.FileName;
  258. }
  259. return string.Empty;
  260. }
  261. private void CheckDocPassword(CPDFDocument checkDoc)
  262. {
  263. if (checkDoc == null)
  264. {
  265. return;
  266. }
  267. if (checkDoc.IsLocked)
  268. {
  269. checkDoc.UnlockWithPassword(FileAndPassword[checkDoc.FilePath]);
  270. if (checkDoc.IsLocked)
  271. {
  272. PasswordDialog dialog = new PasswordDialog();
  273. dialog.Confirmed += (sender, s) =>
  274. {
  275. if (checkDoc.UnlockWithPassword(s))
  276. {
  277. FileAndPassword.Add(checkDoc.FilePath, s);
  278. }
  279. };
  280. }
  281. }
  282. }
  283. private void InitOldDocument()
  284. {
  285. TxtOldPage.Text = "1";
  286. CurrentOldPageIndex = 0;
  287. TxtOldPageCount.Text = "/" + OldDocument.PageCount;
  288. OldRange.Clear();
  289. for (int i = 0; i < OldDocument.PageCount; i++)
  290. {
  291. if (i < OldDocument.PageCount)
  292. {
  293. OldRange.Add(i);
  294. }
  295. }
  296. CmbOldPageRange.SelectedIndex = "0";
  297. if (OldFileComboBox.SelectedIndex != OldFilePathList.IndexOf(OldDocument.FilePath))
  298. {
  299. OldFileComboBox.SelectedIndex = OldFilePathList.IndexOf(OldDocument.FilePath);
  300. }
  301. UpdateCover(OldDocument, OldImageControl, CurrentOldPageIndex);
  302. CheckOldPageBtnState();
  303. UpdateOldPageIndex();
  304. }
  305. private void InitNewDocument()
  306. {
  307. TxtNewPage.Text = "1";
  308. CurrentNewPageIndex = 0;
  309. TxtNewPageCount.Text = "/" + NewDocument.PageCount;
  310. NewRange.Clear();
  311. for (int i = 0; i < NewDocument.PageCount; i++)
  312. {
  313. if (i < NewDocument.PageCount)
  314. {
  315. NewRange.Add(i);
  316. }
  317. }
  318. CmbNewPageRange.SelectedIndex = "0";
  319. if (NewFileComboBox.SelectedIndex != NewFilePathList.IndexOf(NewDocument.FilePath))
  320. {
  321. NewFileComboBox.SelectedIndex = NewFilePathList.IndexOf(NewDocument.FilePath);
  322. }
  323. UpdateCover(NewDocument, NewImageControl, CurrentNewPageIndex);
  324. CheckNewPageBtnState();
  325. UpdateNewPageIndex();
  326. }
  327. private void OldFileComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  328. {
  329. if (OldFilePathList != null && OldFilePathList.Count > OldFileComboBox.SelectedIndex && OldFileComboBox.SelectedIndex > -1)
  330. {
  331. string pdfFile = OldFilePathList[OldFileComboBox.SelectedIndex];
  332. if (File.Exists(pdfFile))
  333. {
  334. if (NewFilePathList != null && NewFileComboBox.SelectedIndex > -1 && !swapeImage)
  335. {
  336. if (pdfFile == NewFilePathList[NewFileComboBox.SelectedIndex])
  337. {
  338. OldFileComboBox.SelectedIndex = 0;
  339. // // MessageBoxEx.Show(App.MainPageLoader.GetString("FileCompare_DifferentFiles"));
  340. return;
  341. }
  342. }
  343. if (OldDocument != null)
  344. {
  345. if (OldDocument.FilePath != pdfFile)
  346. {
  347. CPDFDocument oldDocument = CPDFDocument.InitWithFilePath(pdfFile);
  348. CheckDocPassword(oldDocument);
  349. if (oldDocument.IsLocked == false)
  350. {
  351. OldDocument = oldDocument;
  352. }
  353. }
  354. }
  355. else
  356. {
  357. CPDFDocument oldDocument = CPDFDocument.InitWithFilePath(pdfFile);
  358. CheckDocPassword(oldDocument);
  359. if (oldDocument.IsLocked == false)
  360. {
  361. NewDocument = oldDocument;
  362. }
  363. }
  364. if (OldDocument != null && OldDocument.IsLocked == false)
  365. {
  366. if (OldFilePathList.IndexOf(OldDocument.FilePath) != 0)
  367. {
  368. OldFileComboBox.Items.RemoveAt(OldFilePathList.IndexOf(OldDocument.FilePath));
  369. OldFileComboBox.Items.Insert(0, OldDocument.FileName);
  370. OldFilePathList.Remove(OldDocument.FilePath);
  371. OldFilePathList.Insert(0, OldDocument.FilePath);
  372. OldFileComboBox.SelectedIndex = 0;
  373. }
  374. InitOldDocument();
  375. }
  376. else
  377. {
  378. NewFileComboBox.SelectedIndex = -1;
  379. }
  380. }
  381. else
  382. {
  383. OldFilePathList.RemoveAt(OldFileComboBox.SelectedIndex);
  384. NewFileComboBox.Items.RemoveAt(OldFileComboBox.SelectedIndex);
  385. if (OldFileComboBox.Items.Count > 0)
  386. {
  387. OldFileComboBox.SelectedIndex = 0;
  388. }
  389. else
  390. {
  391. OldFileComboBox.SelectedIndex = -1;
  392. }
  393. // // MessageBoxEx.Show(App.MainPageLoader.GetString("FileCompare_FileRemoved"));
  394. return;
  395. }
  396. }
  397. }
  398. private void NewFileComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  399. {
  400. if (NewFilePathList != null && NewFilePathList.Count > NewFileComboBox.SelectedIndex && NewFileComboBox.SelectedIndex > -1)
  401. {
  402. string pdfFile = NewFilePathList[NewFileComboBox.SelectedIndex];
  403. if (File.Exists(pdfFile))
  404. {
  405. if (OldFilePathList != null && OldFileComboBox.SelectedIndex > -1 && !swapeImage)
  406. {
  407. if (pdfFile == OldFilePathList[OldFileComboBox.SelectedIndex])
  408. {
  409. if (ADDFileBorder.Visibility != Visibility.Visible)
  410. {
  411. NewFileComboBox.SelectedIndex = 0;
  412. }
  413. else
  414. {
  415. NewFileComboBox.SelectedIndex = -1;
  416. }
  417. // // MessageBoxEx.Show(App.MainPageLoader.GetString("FileCompare_DifferentFiles"));
  418. return;
  419. }
  420. }
  421. if (NewDocument != null)
  422. {
  423. if (NewDocument.FilePath != pdfFile)
  424. {
  425. CPDFDocument newDocument = CPDFDocument.InitWithFilePath(pdfFile);
  426. CheckDocPassword(newDocument);
  427. if (newDocument.IsLocked == false)
  428. {
  429. NewDocument = newDocument;
  430. }
  431. }
  432. }
  433. else
  434. {
  435. CPDFDocument newDocument = CPDFDocument.InitWithFilePath(pdfFile);
  436. CheckDocPassword(newDocument);
  437. if (newDocument.IsLocked == false)
  438. {
  439. NewDocument = newDocument;
  440. }
  441. }
  442. if (NewDocument != null && NewDocument.IsLocked == false)
  443. {
  444. if (ADDFileBorder.Visibility == Visibility.Visible)
  445. {
  446. ADDFileBorder.Visibility = Visibility.Collapsed;
  447. }
  448. if (NewFilePathList.IndexOf(NewDocument.FilePath) != 0)
  449. {
  450. NewFileComboBox.Items.RemoveAt(NewFilePathList.IndexOf(NewDocument.FilePath));
  451. NewFileComboBox.Items.Insert(0, NewDocument.FileName);
  452. NewFilePathList.Remove(NewDocument.FilePath);
  453. NewFilePathList.Insert(0, NewDocument.FilePath);
  454. NewFileComboBox.SelectedIndex = 0;
  455. }
  456. SwapeImage.IsEnabled = true;
  457. InitNewDocument();
  458. }
  459. else
  460. {
  461. NewFileComboBox.SelectedIndex = -1;
  462. }
  463. }
  464. else
  465. {
  466. NewFilePathList.RemoveAt(NewFileComboBox.SelectedIndex);
  467. NewFileComboBox.Items.RemoveAt(NewFileComboBox.SelectedIndex);
  468. if (NewFileComboBox.Items.Count > 0)
  469. {
  470. NewFileComboBox.SelectedIndex = 0;
  471. }
  472. else
  473. {
  474. NewFileComboBox.SelectedIndex = -1;
  475. }
  476. // MessageBoxEx.Show(App.MainPageLoader.GetString("FileCompare_FileRemoved"));
  477. return;
  478. }
  479. }
  480. }
  481. private void BrowseOldBtn_Click(object sender, RoutedEventArgs e)
  482. {
  483. string pdfFile = GetChoosePdf();
  484. if (File.Exists(pdfFile))
  485. {
  486. if (NewFilePathList != null && NewFileComboBox.SelectedIndex > -1)
  487. {
  488. if (pdfFile == NewFilePathList[NewFileComboBox.SelectedIndex])
  489. {
  490. // MessageBoxEx.Show(App.MainPageLoader.GetString("FileCompare_DifferentFiles"));
  491. return;
  492. }
  493. }
  494. OldDocument = CPDFDocument.InitWithFilePath(pdfFile);
  495. CheckDocPassword(OldDocument);
  496. if (OldDocument != null && OldDocument.IsLocked == false)
  497. {
  498. int select = AddOldFileList(OldDocument.FileName, pdfFile);
  499. OldFileComboBox.SelectedIndex = select;
  500. InitOldDocument();
  501. }
  502. }
  503. }
  504. private void BrowseNewBtn_Click(object sender, RoutedEventArgs e)
  505. {
  506. string pdfFile = GetChoosePdf();
  507. if (File.Exists(pdfFile))
  508. {
  509. if (OldFilePathList != null && OldFileComboBox.SelectedIndex > -1)
  510. {
  511. if (pdfFile == OldFilePathList[OldFileComboBox.SelectedIndex])
  512. {
  513. // MessageBoxEx.Show(App.MainPageLoader.GetString("FileCompare_DifferentFiles"));
  514. return;
  515. }
  516. }
  517. NewDocument = CPDFDocument.InitWithFilePath(pdfFile);
  518. CheckDocPassword(NewDocument);
  519. if (NewDocument != null && NewDocument.IsLocked == false)
  520. {
  521. int select = AddNewFileList(NewDocument.FileName, pdfFile);
  522. NewFileComboBox.SelectedIndex = select;
  523. InitNewDocument();
  524. }
  525. }
  526. }
  527. public bool swapeImage = false;
  528. private void SwapeImage_MouseLeftDown(object sender, RoutedEventArgs e)
  529. {
  530. try
  531. {
  532. if (NewDocument != null && OldDocument != null)
  533. {
  534. swapeImage = true;
  535. UpdateCover(NewDocument, OldImageControl, CurrentOldPageIndex);
  536. UpdateCover(OldDocument, NewImageControl, CurrentNewPageIndex);
  537. CPDFDocument tempDoc = NewDocument;
  538. NewDocument = OldDocument;
  539. OldDocument = tempDoc;
  540. int oldselect = AddOldFileList(OldDocument.FileName, OldDocument.FilePath);
  541. OldFileComboBox.SelectedIndex = oldselect;
  542. int newselect = AddNewFileList(NewDocument.FileName, NewDocument.FilePath);
  543. NewFileComboBox.SelectedIndex = newselect;
  544. InitOldDocument();
  545. InitNewDocument();
  546. swapeImage = false;
  547. }
  548. else
  549. {
  550. swapeImage = false;
  551. // MessageBoxEx.Show(App.MainPageLoader.GetString("Main_NoSelectedFilesWarning"));
  552. }
  553. }
  554. catch
  555. {
  556. swapeImage = false;
  557. }
  558. }
  559. private void SavePageRanges(System.Windows.Controls.TextBox textBox, CPDFDocument doc, List<int> rangeList)
  560. {
  561. rangeList.Clear();
  562. if (textBox.Text != string.Empty && doc != null)
  563. {
  564. string[] checkTextArray = textBox.Text.Split(',');
  565. foreach (string checkText in checkTextArray)
  566. {
  567. if (Regex.IsMatch(checkText, "[0-9]+") && Regex.IsMatch(checkText, "[^0-9]") == false)
  568. {
  569. int range = int.Parse(checkText) - 1;
  570. if (range < 0)
  571. {
  572. continue;
  573. }
  574. if (rangeList.Contains(range) == false)
  575. {
  576. rangeList.Add(range);
  577. }
  578. continue;
  579. }
  580. if (Regex.IsMatch(checkText, "[1-9]+[0-9]*(-)[0-9]+") && Regex.IsMatch(checkText, "[^0-9\\-]") == false)
  581. {
  582. if (Regex.Matches(checkText, "[-]+").Count == 1)
  583. {
  584. string[] pagesArray = checkText.Split('-');
  585. int rangeLeft = int.Parse(pagesArray[0]);
  586. int rangeRight = int.Parse(pagesArray[1]);
  587. for (int i = Math.Min(rangeLeft, rangeRight) - 1; i < Math.Max(rangeLeft, rangeRight); i++)
  588. {
  589. if (i >= doc.PageCount)
  590. {
  591. break;
  592. }
  593. if (rangeList.Contains(i) == false)
  594. {
  595. rangeList.Add(i);
  596. }
  597. }
  598. }
  599. }
  600. }
  601. }
  602. }
  603. private void SetCoverGrid()
  604. {
  605. ReplaceColorRect.Fill = new SolidColorBrush(ReplaceColor);
  606. InsertColorRect.Fill = new SolidColorBrush(InsertColor);
  607. DeleteColorRect.Fill = new SolidColorBrush(DeleteColor);
  608. }
  609. private void ShowSelectFileBorder(bool isShow)
  610. {
  611. SelectFileBorder.Visibility = isShow ? Visibility.Visible : Visibility.Collapsed;
  612. }
  613. private void SaveCoverGrid()
  614. {
  615. if (ReplaceColorRect.Fill != null && ((SolidColorBrush)ReplaceColorRect.Fill).Color != ReplaceColor)
  616. {
  617. ReplaceColor = ((SolidColorBrush)ReplaceColorRect.Fill).Color;
  618. }
  619. if (InsertColorRect.Fill != null && ((SolidColorBrush)InsertColorRect.Fill).Color != InsertColor)
  620. {
  621. InsertColor = ((SolidColorBrush)InsertColorRect.Fill).Color;
  622. }
  623. if (DeleteColorRect.Fill != null && ((SolidColorBrush)DeleteColorRect.Fill).Color != DeleteColor)
  624. {
  625. DeleteColor = ((SolidColorBrush)DeleteColorRect.Fill).Color;
  626. }
  627. }
  628. private void CompareDocBtn_Click(object sender, RoutedEventArgs e)
  629. {
  630. try
  631. {
  632. if(OldFileComboBox.Text == "" || NewFileComboBox.Text == "")
  633. {
  634. ShowSelectFileBorder(true);
  635. return;
  636. }
  637. if (NewDocument != null && OldDocument != null && NewDocument.IsLocked == false && OldDocument.IsLocked == false && viewCtrl != null)
  638. {
  639. if (Math.Min(OldRange.Count, NewRange.Count) == 0 || (CmbOldPageRange.SelectedIndex == "3" && CmbOldPageRange.Text == "") || (CmbNewPageRange.SelectedIndex == "3" && CmbNewPageRange.Text == ""))
  640. {
  641. PageRangeBorder.Visibility = Visibility.Visible;
  642. return;
  643. }
  644. this.Close();
  645. if (CompareType == CompareType.ContentCompare)
  646. {
  647. ObjectCompareType = CPDFCompareType.CPDFCompareTypeAll;
  648. if (TextCheckBox.IsChecked == true && ImageCheckBox.IsChecked == true)
  649. {
  650. ObjectCompareType = CPDFCompareType.CPDFCompareTypeAll;
  651. }
  652. else
  653. {
  654. if (TextCheckBox.IsChecked == true)
  655. {
  656. ObjectCompareType = CPDFCompareType.CPDFCompareTypeText;
  657. }
  658. if (ImageCheckBox.IsChecked == true)
  659. {
  660. ObjectCompareType = CPDFCompareType.CPDFCompareTypeImage;
  661. }
  662. }
  663. Task.Factory.StartNew(() =>
  664. {
  665. CPDFCompareContent CPdfContent = new CPDFCompareContent(OldDocument, NewDocument);
  666. CPdfContent.SetInsertColor(new byte[] { InsertColor.R, InsertColor.G, InsertColor.B });
  667. CPdfContent.SetReplaceColor(new byte[] { ReplaceColor.R, ReplaceColor.G, ReplaceColor.B });
  668. CPdfContent.SetDeleteColor(new byte[] { DeleteColor.R, DeleteColor.G, DeleteColor.B });
  669. CPdfContent.SetReplaceTransparency(ReplaceColor.A);
  670. CPdfContent.SetDeleteTransparency(DeleteColor.A);
  671. CPdfContent.SetInsertTransparency(InsertColor.A);
  672. List<CPDFCompareResults> resultList = new List<CPDFCompareResults>();
  673. int minLength = Math.Min(OldRange.Count, NewRange.Count);
  674. List<int> oldTemp = new List<int>(OldRange);
  675. List<int> newTemp = new List<int>(NewRange);
  676. if (minLength == 0)
  677. {
  678. int maxCount = Math.Max(OldDocument.PageCount, NewDocument.PageCount);
  679. for (int i = 0; i < maxCount; i++)
  680. {
  681. if (i < OldDocument.PageCount)
  682. {
  683. oldTemp.Add(i);
  684. }
  685. if (i >= OldDocument.PageCount)
  686. {
  687. oldTemp.Add(OldDocument.PageCount);
  688. }
  689. if (i < NewDocument.PageCount)
  690. {
  691. newTemp.Add(i);
  692. }
  693. if (i >= NewDocument.PageCount)
  694. {
  695. newTemp.Add(OldDocument.PageCount);
  696. }
  697. }
  698. minLength = oldTemp.Count;
  699. }
  700. else
  701. {
  702. if (oldTemp.Count != newTemp.Count)
  703. {
  704. if (oldTemp.Count < newTemp.Count)
  705. {
  706. for (int i = oldTemp.Count; i < newTemp.Count; i++)
  707. {
  708. oldTemp.Add(OldDocument.PageCount);
  709. }
  710. }
  711. if (oldTemp.Count > newTemp.Count)
  712. {
  713. for (int i = newTemp.Count; i < oldTemp.Count; i++)
  714. {
  715. newTemp.Add(NewDocument.PageCount);
  716. }
  717. }
  718. minLength = oldTemp.Count;
  719. }
  720. }
  721. for (int i = 0; i < minLength; i++)
  722. {
  723. CPDFCompareResults result = CPdfContent.Compare(oldTemp[i], newTemp[i], ObjectCompareType, true);
  724. if (result != null && (result.TextResults.Count > 0 || result.ImageResults.Count > 0))
  725. {
  726. // if (viewCtrl.ParentPage.loadingConceal.Visibility == Visibility.Collapsed)
  727. // {
  728. // return;
  729. // }
  730. resultList.Add(result);
  731. // Dispatcher.Invoke(() =>
  732. // {
  733. // if (minLength != 0)
  734. // {
  735. // viewCtrl.ParentPage.loadingConcealBar.Value = (0.3 + ((double)i / (double)minLength) / (double)2);
  736. // }
  737. // });
  738. }
  739. }
  740. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString() + ".pdf");
  741. CPdfContent.SaveAsComparisonDocument(tempPath);
  742. CPDFDocument combineDoc = CPDFDocument.InitWithFilePath(tempPath);
  743. Dispatcher.Invoke(() =>
  744. {
  745. if (viewCtrl != null)
  746. {
  747. FileCompareGrid.Visibility = Visibility.Collapsed;
  748. CompareContentResultControl resultPage = new CompareContentResultControl();
  749. resultPage.SetCompareColor(new SolidColorBrush(DeleteColor), new SolidColorBrush(ReplaceColor), new SolidColorBrush(InsertColor));
  750. resultPage.SetCompareName(OldDocument.FileName, NewDocument.FileName);
  751. resultPage.pdfViewerCtrl = viewCtrl;
  752. resultPage.LoadComparePdf(combineDoc, OldDocument, NewDocument);
  753. resultPage.SetCompareResult(resultList);
  754. resultPage.ExitCompareEvent += (s, r) =>
  755. {
  756. OnCompareStatusChanged?.Invoke(this, null);
  757. };
  758. OnCompareStatusChanged?.Invoke(this, resultPage);
  759. }
  760. });
  761. });
  762. }
  763. if (CompareType == CompareType.OverwriteCompare)
  764. {
  765. List<int> oldTemp = new List<int>(OldRange);
  766. List<int> newTemp = new List<int>(NewRange);
  767. string oldRnage = "";
  768. string newRnage = "";
  769. foreach (var odlrange in oldTemp)
  770. {
  771. if (oldRnage != "")
  772. {
  773. oldRnage += ",";
  774. }
  775. oldRnage += (odlrange + 1);
  776. }
  777. foreach (var newrange in newTemp)
  778. {
  779. if (newRnage != "")
  780. {
  781. newRnage += ",";
  782. }
  783. newRnage += (newrange + 1);
  784. }
  785. Task.Factory.StartNew(() =>
  786. {
  787. CPDFCompareOverlay CPdfOverlay = null;
  788. if (string.IsNullOrEmpty(oldRnage) || string.IsNullOrEmpty(newRnage))
  789. {
  790. CPdfOverlay = new CPDFCompareOverlay(OldDocument, NewDocument);
  791. }
  792. else
  793. {
  794. CPdfOverlay = new CPDFCompareOverlay(OldDocument, oldRnage, NewDocument, newRnage);
  795. }
  796. CPdfOverlay?.SetBlendMode(MixMode);
  797. CPdfOverlay?.SetNoFill(IsFillWhite);
  798. CPdfOverlay?.SetNewDocumentStrokeAlpha((float)NewOpacity);
  799. CPdfOverlay?.SetOldDocumentStrokeAlpha((float)OldOpacity);
  800. CPdfOverlay?.SetNewDocumentFillAlpha((float)NewOpacity);
  801. CPdfOverlay?.SetOldDocumentFillAlpha((float)OldOpacity);
  802. CPdfOverlay?.SetNewDocumentStrokeColor(new byte[] { NewMarkColor.R, NewMarkColor.G, NewMarkColor.B });
  803. CPdfOverlay?.SetOldDocumentStrokeColor(new byte[] { OldMarkColor.R, OldMarkColor.G, OldMarkColor.B });
  804. CPdfOverlay?.Compare();
  805. CPDFDocument resultDoc = CPdfOverlay?.ComparisonDocument();
  806. Dispatcher.Invoke(() =>
  807. {
  808. if (viewCtrl != null)
  809. {
  810. // if (viewCtrl.ParentPage.loadingConceal.Visibility == Visibility.Collapsed)
  811. // {
  812. // return;
  813. // }
  814. // viewCtrl.ParentPage.loadingConcealBar.Value = 0.6;
  815. FileCompareGrid.Visibility = Visibility.Collapsed;
  816. CompareOverwriteResultControl resultPage = new CompareOverwriteResultControl();
  817. resultPage.SetCompareColor(new SolidColorBrush(NewMarkColor), new SolidColorBrush(OldMarkColor));
  818. resultPage.pdfViewerCtrl = viewCtrl;
  819. // resultPage.pdfViewerCtrl.ParentPage.SetCompareModel(false);
  820. // viewCtrl.ParentPage.loadingConcealClose.Visibility = Visibility.Visible;
  821. // viewCtrl.ParentPage.loadingConceal.Visibility = Visibility.Collapsed;
  822. resultPage.LoadComparePdf(resultDoc);
  823. resultPage.LeftDoc = OldDocument;
  824. resultPage.RightDoc = NewDocument;
  825. resultPage.ExitCompareEvent += (s, r) =>
  826. {
  827. OnCompareStatusChanged?.Invoke(this, null);
  828. };
  829. OnCompareStatusChanged?.Invoke(this, resultPage);
  830. }
  831. });
  832. });
  833. }
  834. }
  835. else
  836. {
  837. // MessageBoxEx.Show(App.MainPageLoader.GetString("FileCompare_PleaseSelect"));
  838. }
  839. }
  840. catch
  841. {
  842. return;
  843. }
  844. }
  845. private void TextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  846. {
  847. List<Key> allowKeyStroke = new List<Key>()
  848. {
  849. Key.D0, Key.D1, Key.D2, Key.D3, Key.D4, Key.D5,
  850. Key.D6,Key.D7,Key.D8,Key.D9,Key.NumPad0,Key.NumPad1,
  851. Key.NumPad2,Key.NumPad3,Key.NumPad4,Key.NumPad5,
  852. Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9,
  853. Key.Delete,Key.Back
  854. };
  855. if (allowKeyStroke.Contains(e.Key) == false)
  856. {
  857. e.Handled = true;
  858. }
  859. }
  860. private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  861. {
  862. e.CanExecute = false;
  863. e.Handled = true;
  864. }
  865. private void PageRangeCheck_LostFocus(object sender, RoutedEventArgs e)
  866. {
  867. System.Windows.Controls.TextBox textBox = sender as System.Windows.Controls.TextBox;
  868. if (textBox != null && string.IsNullOrEmpty(textBox.Text) == false)
  869. {
  870. bool isMatch = true;
  871. string[] checkTextArray = textBox.Text.Split(',');
  872. foreach (string checkText in checkTextArray)
  873. {
  874. if (Regex.IsMatch(checkText, "[0-9]+") && Regex.IsMatch(checkText, "[^0-9]") == false)
  875. {
  876. continue;
  877. }
  878. if (Regex.IsMatch(checkText, "[1-9]+[0-9]*(-)[0-9]+") && Regex.IsMatch(checkText, "[^0-9\\-]") == false)
  879. {
  880. if (Regex.Matches(checkText, "[-]+").Count == 1)
  881. {
  882. continue;
  883. }
  884. }
  885. isMatch = false;
  886. }
  887. if (isMatch == false)
  888. {
  889. textBox.Text = string.Empty;
  890. }
  891. }
  892. }
  893. int CurrentOldPageIndex = 0;
  894. private List<int> OldRange = new List<int>();
  895. private void UpdateOldPageIndex()
  896. {
  897. TxtOldPage.Text = (CurrentOldPageIndex + 1).ToString();
  898. TxtOldPageCount.Text = "/" + OldRange.Count;
  899. }
  900. private void CheckOldPageBtnState()
  901. {
  902. if (CurrentOldPageIndex + 1 >= OldRange.Count)
  903. {
  904. BtnOldNext.IsEnabled = false;
  905. }
  906. else
  907. {
  908. BtnOldNext.IsEnabled = true;
  909. }
  910. if (CurrentOldPageIndex + 1 <= 1)
  911. {
  912. BtnOldPre.IsEnabled = false;
  913. }
  914. else
  915. {
  916. BtnOldPre.IsEnabled = true;
  917. }
  918. }
  919. private void btnOldPre_Click(object sender, RoutedEventArgs e)
  920. {
  921. if (OldDocument == null)
  922. {
  923. return;
  924. }
  925. CurrentOldPageIndex--;
  926. if (CurrentOldPageIndex < 0 || CurrentOldPageIndex >= OldRange.Count)
  927. {
  928. CheckOldPageBtnState();
  929. TxtOldPage.Focus();
  930. return;
  931. }
  932. UpdateCover(OldDocument, OldImageControl, OldRange[CurrentOldPageIndex]);
  933. //RefreshPicture(pageOldIndexLists[CurrentPageIndex]);
  934. CheckOldPageBtnState();
  935. UpdateOldPageIndex();
  936. }
  937. private void txtOldPage_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  938. {
  939. if (OldDocument == null)
  940. {
  941. return;
  942. }
  943. if (e == null)
  944. {
  945. return;
  946. }
  947. //限制文本框输入内容
  948. List<Key> NumberKeys = new List<Key>() { Key.D0, Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3, Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9, Key.Delete, Key.Back, Key.Enter, Key.Right, Key.Left };
  949. if (!NumberKeys.Contains(e.Key))
  950. {
  951. e.Handled = true;
  952. }
  953. if (e.Key == Key.Enter)
  954. {
  955. int value = 0;
  956. bool result = int.TryParse(TxtOldPage.Text, out value);
  957. if (!result || value <= 0 || value > OldRange.Count)
  958. {
  959. TxtOldPage.Text = "1";
  960. CurrentOldPageIndex = 0;
  961. }
  962. else
  963. {
  964. CurrentOldPageIndex = value - 1;
  965. UpdateCover(OldDocument, OldImageControl, OldRange[CurrentOldPageIndex]);
  966. }
  967. CheckOldPageBtnState();
  968. }
  969. }
  970. private void txtOldPage_LostFocus(object sender, RoutedEventArgs e)
  971. {
  972. if (OldDocument == null)
  973. {
  974. return;
  975. }
  976. int value = 0;
  977. bool result = int.TryParse(TxtOldPage.Text, out value);
  978. if (!result || value < 0 || value > OldRange.Count)
  979. {
  980. TxtOldPage.Text = "1";
  981. CurrentOldPageIndex = 0;
  982. }
  983. else
  984. {
  985. CurrentOldPageIndex = value - 1;
  986. if(CurrentOldPageIndex < 0 || CurrentOldPageIndex >= OldRange.Count)
  987. {
  988. CurrentOldPageIndex = 0;
  989. }
  990. UpdateCover(OldDocument, OldImageControl, OldRange[CurrentOldPageIndex]);
  991. }
  992. CheckOldPageBtnState();
  993. }
  994. private void btnOldNext_Click(object sender, RoutedEventArgs e)
  995. {
  996. if (OldDocument == null)
  997. {
  998. return;
  999. }
  1000. CurrentOldPageIndex++;
  1001. if (CurrentOldPageIndex < 0 || CurrentOldPageIndex >= OldRange.Count)
  1002. {
  1003. CheckOldPageBtnState();
  1004. TxtOldPage.Focus();
  1005. return;
  1006. }
  1007. UpdateCover(OldDocument, OldImageControl, OldRange[CurrentOldPageIndex]);
  1008. CheckOldPageBtnState();
  1009. UpdateOldPageIndex();
  1010. }
  1011. int CurrentNewPageIndex = 0;
  1012. private List<int> NewRange = new List<int>();
  1013. private void UpdateNewPageIndex()
  1014. {
  1015. TxtNewPage.Text = (CurrentNewPageIndex + 1).ToString();
  1016. TxtNewPageCount.Text = "/" + NewRange.Count;
  1017. }
  1018. private void CheckNewPageBtnState()
  1019. {
  1020. if (CurrentNewPageIndex + 1 >= NewRange.Count)
  1021. {
  1022. BtnNewNext.IsEnabled = false;
  1023. }
  1024. else
  1025. {
  1026. BtnNewNext.IsEnabled = true;
  1027. }
  1028. if (CurrentNewPageIndex + 1 <= 1)
  1029. {
  1030. BtnNewPre.IsEnabled = false;
  1031. }
  1032. else
  1033. {
  1034. BtnNewPre.IsEnabled = true;
  1035. }
  1036. }
  1037. private void btnNewPre_Click(object sender, RoutedEventArgs e)
  1038. {
  1039. if (NewDocument == null)
  1040. {
  1041. return;
  1042. }
  1043. CurrentNewPageIndex--;
  1044. if (CurrentNewPageIndex < 0 || CurrentNewPageIndex >= NewRange.Count)
  1045. {
  1046. CheckNewPageBtnState();
  1047. TxtNewPage.Focus();
  1048. return;
  1049. }
  1050. UpdateCover(NewDocument, NewImageControl, NewRange[CurrentNewPageIndex]);
  1051. CheckNewPageBtnState();
  1052. UpdateNewPageIndex();
  1053. }
  1054. private void txtNewPage_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  1055. {
  1056. if (NewDocument == null)
  1057. {
  1058. return;
  1059. }
  1060. if (e == null)
  1061. {
  1062. return;
  1063. }
  1064. //限制文本框输入内容
  1065. List<Key> NumberKeys = new List<Key>() { Key.D0, Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3, Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9, Key.Delete, Key.Back, Key.Enter, Key.Right, Key.Left };
  1066. if (!NumberKeys.Contains(e.Key))
  1067. {
  1068. e.Handled = true;
  1069. }
  1070. if (e.Key == Key.Enter)
  1071. {
  1072. int value = 0;
  1073. bool result = int.TryParse(TxtNewPage.Text, out value);
  1074. if (!result || value <= 0 || value > NewRange.Count)
  1075. {
  1076. TxtNewPage.Text = "1";
  1077. CurrentNewPageIndex = 0;
  1078. }
  1079. else
  1080. {
  1081. CurrentNewPageIndex = value - 1;
  1082. UpdateCover(NewDocument, NewImageControl, NewRange[CurrentNewPageIndex]);
  1083. }
  1084. CheckNewPageBtnState();
  1085. }
  1086. }
  1087. private void txtNewPage_LostFocus(object sender, RoutedEventArgs e)
  1088. {
  1089. if (NewDocument == null)
  1090. {
  1091. return;
  1092. }
  1093. int value = 0;
  1094. bool result = int.TryParse(TxtNewPage.Text, out value);
  1095. if (!result || value < 0 || value > NewRange.Count)
  1096. {
  1097. TxtNewPage.Text = "1";
  1098. CurrentNewPageIndex = 0;
  1099. }
  1100. else
  1101. {
  1102. CurrentNewPageIndex = value - 1;
  1103. UpdateCover(NewDocument, NewImageControl, NewRange[CurrentNewPageIndex]);
  1104. }
  1105. CheckNewPageBtnState();
  1106. }
  1107. private void btnNewNext_Click(object sender, RoutedEventArgs e)
  1108. {
  1109. if (NewDocument == null)
  1110. {
  1111. return;
  1112. }
  1113. CurrentNewPageIndex++;
  1114. if (CurrentNewPageIndex < 0 || CurrentNewPageIndex >= NewRange.Count)
  1115. {
  1116. CheckNewPageBtnState();
  1117. TxtNewPage.Focus();
  1118. return;
  1119. }
  1120. UpdateCover(NewDocument, NewImageControl, NewRange[CurrentNewPageIndex]);
  1121. CheckNewPageBtnState();
  1122. UpdateNewPageIndex();
  1123. }
  1124. private void CmbOldPageRange_SelectionChanged(object sender, RoutedEventArgs e)
  1125. {
  1126. if (OldDocument != null)
  1127. {
  1128. switch (int.Parse(CmbOldPageRange.SelectedIndex))
  1129. {
  1130. case 0:
  1131. OldRange.Clear();
  1132. for (int i = 0; i < OldDocument.PageCount; i++)
  1133. {
  1134. if (i < OldDocument.PageCount)
  1135. {
  1136. OldRange.Add(i);
  1137. }
  1138. }
  1139. break;
  1140. case 1:
  1141. var numType1 = int.Parse(CmbOldPageRange.SelectedIndex) == 1 ? 1 : 0;
  1142. int[] page1 = new int[(OldDocument.PageCount + numType1) / 2];
  1143. for (int i = 0; i < page1.Length; i++)
  1144. {
  1145. page1[i] = i * 2 + 1 - numType1;
  1146. }
  1147. OldRange = page1.ToList();
  1148. break;
  1149. case 2:
  1150. var numType2 = int.Parse(CmbOldPageRange.SelectedIndex) == 1 ? 1 : 0;
  1151. int[] page2 = new int[(OldDocument.PageCount + numType2) / 2];
  1152. for (int i = 0; i < page2.Length; i++)
  1153. {
  1154. page2[i] = i * 2 + 1 - numType2;
  1155. }
  1156. OldRange = page2.ToList();
  1157. break;
  1158. case 3:
  1159. CmbOldPageRange.MaxPageRange = OldRange.Count;
  1160. if (CmbOldPageRange.PageIndexList != null && CmbOldPageRange.PageIndexList.Count > 0)
  1161. {
  1162. OldRange = CmbOldPageRange.PageIndexList;
  1163. }
  1164. break;
  1165. }
  1166. if (CurrentOldPageIndex > OldRange.Count)
  1167. {
  1168. CurrentOldPageIndex = OldRange.Count - 1;
  1169. }
  1170. if (OldRange.Count != 0)
  1171. {
  1172. UpdateCover(OldDocument, OldImageControl, OldRange[CurrentOldPageIndex]);
  1173. CheckOldPageBtnState();
  1174. UpdateOldPageIndex();
  1175. }
  1176. else
  1177. {
  1178. CmbOldPageRange.SelectedIndex = "0";
  1179. }
  1180. }
  1181. }
  1182. private void CmbOldPageRange_TextChanged(object sender, RoutedEventArgs e)
  1183. {
  1184. if (OldDocument != null)
  1185. {
  1186. List<int> TargetPages = new List<int>();
  1187. if (CommonHelper.GetPagesInRange(ref TargetPages, CmbOldPageRange.Text, OldDocument.PageCount, new char[] { ',' }, new char[] { '-' }))
  1188. {
  1189. OldRange = TargetPages;
  1190. if (CurrentOldPageIndex > OldRange.Count)
  1191. {
  1192. CurrentOldPageIndex = OldRange.Count - 1;
  1193. }
  1194. UpdateCover(OldDocument, OldImageControl, OldRange[CurrentOldPageIndex]);
  1195. CheckOldPageBtnState();
  1196. UpdateOldPageIndex();
  1197. }
  1198. }
  1199. }
  1200. private void CmbNewPageRange_SelectionChanged(object sender, RoutedEventArgs e)
  1201. {
  1202. if (NewDocument != null)
  1203. {
  1204. switch (int.Parse(CmbNewPageRange.SelectedIndex))
  1205. {
  1206. case 0:
  1207. NewRange.Clear();
  1208. for (int i = 0; i < NewDocument.PageCount; i++)
  1209. {
  1210. if (i < NewDocument.PageCount)
  1211. {
  1212. NewRange.Add(i);
  1213. }
  1214. }
  1215. break;
  1216. case 1:
  1217. var numType1 = int.Parse(CmbNewPageRange.SelectedIndex) == 1 ? 1 : 0;
  1218. int[] page1 = new int[(NewDocument.PageCount + numType1) / 2];
  1219. for (int i = 0; i < page1.Length; i++)
  1220. {
  1221. page1[i] = i * 2 + 1 - numType1;
  1222. }
  1223. NewRange = page1.ToList();
  1224. break;
  1225. case 2:
  1226. var numType2 = int.Parse(CmbNewPageRange.SelectedIndex) == 1 ? 1 : 0;
  1227. int[] page2 = new int[(NewDocument.PageCount + numType2) / 2];
  1228. for (int i = 0; i < page2.Length; i++)
  1229. {
  1230. page2[i] = i * 2 + 1 - numType2;
  1231. }
  1232. NewRange = page2.ToList();
  1233. break;
  1234. case 3:
  1235. CmbNewPageRange.MaxPageRange = NewRange.Count;
  1236. if (CmbNewPageRange.PageIndexList != null && CmbNewPageRange.PageIndexList.Count > 0)
  1237. {
  1238. NewRange = CmbNewPageRange.PageIndexList;
  1239. }
  1240. break;
  1241. }
  1242. if (CurrentNewPageIndex > NewRange.Count)
  1243. {
  1244. CurrentNewPageIndex = NewRange.Count - 1;
  1245. }
  1246. if (NewRange.Count != 0)
  1247. {
  1248. UpdateCover(NewDocument, NewImageControl, NewRange[CurrentNewPageIndex]);
  1249. CheckNewPageBtnState();
  1250. UpdateNewPageIndex();
  1251. }
  1252. else
  1253. {
  1254. CmbNewPageRange.SelectedIndex = "0";
  1255. }
  1256. }
  1257. }
  1258. private void CmbNewPageRange_TextChanged(object sender, RoutedEventArgs e)
  1259. {
  1260. if (NewDocument != null)
  1261. {
  1262. List<int> TargetPages = new List<int>();
  1263. if (CommonHelper.GetPagesInRange(ref TargetPages, CmbNewPageRange.Text, NewDocument.PageCount, new char[] { ',' }, new char[] { '-' }))
  1264. {
  1265. NewRange = TargetPages;
  1266. if (CurrentNewPageIndex > NewRange.Count)
  1267. {
  1268. CurrentNewPageIndex = NewRange.Count - 1;
  1269. }
  1270. UpdateCover(NewDocument, NewImageControl, NewRange[CurrentNewPageIndex]);
  1271. CheckNewPageBtnState();
  1272. UpdateNewPageIndex();
  1273. }
  1274. }
  1275. }
  1276. public System.Drawing.Color mediaColorToDrawing(System.Windows.Media.Color mediaColor)
  1277. {
  1278. return System.Drawing.Color.FromArgb(mediaColor.A, mediaColor.R, mediaColor.G, mediaColor.B);
  1279. }
  1280. public System.Windows.Media.Color drawingColorToMedia(System.Drawing.Color drawColor)
  1281. {
  1282. return System.Windows.Media.Color.FromArgb(drawColor.A, drawColor.R, drawColor.G, drawColor.B);
  1283. }
  1284. private void ReplaceColorRect_Click(object sender, MouseButtonEventArgs e)
  1285. {
  1286. ColorDialog colorDialog = new ColorDialog();
  1287. colorDialog.AllowFullOpen = true;
  1288. colorDialog.FullOpen = true;
  1289. if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1290. {
  1291. ReplaceColor = drawingColorToMedia(colorDialog.Color);
  1292. ReplaceColorRect.Fill = new SolidColorBrush(ReplaceColor);
  1293. }
  1294. }
  1295. private void InsertColorRect_Click(object sender, MouseButtonEventArgs e)
  1296. {
  1297. ColorDialog colorDialog = new ColorDialog();
  1298. colorDialog.AllowFullOpen = true;
  1299. colorDialog.FullOpen = true;
  1300. if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1301. {
  1302. InsertColor = drawingColorToMedia(colorDialog.Color);
  1303. InsertColorRect.Fill = new SolidColorBrush(InsertColor);
  1304. }
  1305. }
  1306. private void DeleteColorRect_Click(object sender, MouseButtonEventArgs e)
  1307. {
  1308. ColorDialog colorDialog = new ColorDialog();
  1309. colorDialog.AllowFullOpen = true;
  1310. colorDialog.FullOpen = true;
  1311. if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1312. {
  1313. DeleteColor = drawingColorToMedia(colorDialog.Color);
  1314. DeleteColorRect.Fill = new SolidColorBrush(DeleteColor);
  1315. }
  1316. }
  1317. private void OldMarkColorRect_Click(object sender, MouseButtonEventArgs e)
  1318. {
  1319. ColorDialog colorDialog = new ColorDialog();
  1320. colorDialog.AllowFullOpen = true;
  1321. colorDialog.FullOpen = true;
  1322. if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1323. {
  1324. OldMarkColor = drawingColorToMedia(colorDialog.Color);
  1325. OldColorRect.Fill = new SolidColorBrush(OldMarkColor);
  1326. }
  1327. }
  1328. private void NewMarkColorRect_Click(object sender, MouseButtonEventArgs e)
  1329. {
  1330. ColorDialog colorDialog = new ColorDialog();
  1331. colorDialog.AllowFullOpen = true;
  1332. colorDialog.FullOpen = true;
  1333. if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1334. {
  1335. NewMarkColor = drawingColorToMedia(colorDialog.Color);
  1336. NewColorRect.Fill = new SolidColorBrush(NewMarkColor);
  1337. }
  1338. }
  1339. private void CancleBtn_Click(object sender, RoutedEventArgs e)
  1340. {
  1341. this.Close();
  1342. }
  1343. private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  1344. {
  1345. string pdfFile = GetChoosePdf();
  1346. if (File.Exists(pdfFile))
  1347. {
  1348. if (OldFilePathList != null && OldFileComboBox.SelectedIndex > -1)
  1349. {
  1350. if (pdfFile == OldFilePathList[OldFileComboBox.SelectedIndex])
  1351. {
  1352. // MessageBoxEx.Show(App.MainPageLoader.GetString("FileCompare_DifferentFiles"));
  1353. return;
  1354. }
  1355. }
  1356. NewDocument = CPDFDocument.InitWithFilePath(pdfFile);
  1357. CheckDocPassword(NewDocument);
  1358. if (NewDocument != null && NewDocument.IsLocked == false)
  1359. {
  1360. int select = AddNewFileList(NewDocument.FileName, pdfFile);
  1361. NewFileComboBox.SelectedIndex = select;
  1362. InitNewDocument();
  1363. }
  1364. }
  1365. }
  1366. private void ADDFileBorder_DragOver(object sender, System.Windows.DragEventArgs e)
  1367. {
  1368. e.Effects = e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop) ? System.Windows.DragDropEffects.Copy : System.Windows.DragDropEffects.None;
  1369. e.Handled = true;
  1370. }
  1371. private void ADDFileBorder_Drop(object sender, System.Windows.DragEventArgs e)
  1372. {
  1373. // 检查拖拽的文件类型
  1374. if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
  1375. {
  1376. var files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
  1377. var pdfFiles = files.Where(f => f.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase)).ToArray();
  1378. if (pdfFiles.Length > 0)
  1379. {
  1380. if (File.Exists(pdfFiles[0]))
  1381. {
  1382. if (OldFilePathList != null && OldFileComboBox.SelectedIndex > -1)
  1383. {
  1384. if (pdfFiles[0] == OldFilePathList[OldFileComboBox.SelectedIndex])
  1385. {
  1386. // MessageBoxEx.Show(App.MainPageLoader.GetString("FileCompare_DifferentFiles"));
  1387. return;
  1388. }
  1389. }
  1390. NewDocument = CPDFDocument.InitWithFilePath(pdfFiles[0]);
  1391. CheckDocPassword(NewDocument);
  1392. if (NewDocument != null && NewDocument.IsLocked == false)
  1393. {
  1394. int select = AddNewFileList(NewDocument.FileName, pdfFiles[0]);
  1395. NewFileComboBox.SelectedIndex = select;
  1396. InitNewDocument();
  1397. }
  1398. }
  1399. }
  1400. else
  1401. {
  1402. }
  1403. }
  1404. }
  1405. private void SelectFileCancel_OnClick(object sender, RoutedEventArgs e)
  1406. {
  1407. ShowSelectFileBorder(false);
  1408. }
  1409. private void PageRangeCancel_OnClick(object sender, RoutedEventArgs e)
  1410. {
  1411. PageRangeBorder.Visibility = Visibility.Collapsed;
  1412. }
  1413. private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
  1414. {
  1415. if (sender is ComboBox cmb)
  1416. {
  1417. if(cmb.SelectedIndex < 0 || cmb.SelectedIndex > 16)
  1418. {
  1419. return;
  1420. }
  1421. MixMode = (CPDFBlendMode)cmb.SelectedIndex;
  1422. }
  1423. }
  1424. private void OldOpacityControl_OnTextInput(object sender, string e)
  1425. {
  1426. if (float.TryParse(e, out float oldOpacity))
  1427. {
  1428. OldOpacity = oldOpacity / 100.0;
  1429. }
  1430. }
  1431. private void NewOpacityControl_OnTextInput(object sender, string e)
  1432. {
  1433. if (float.TryParse(e, out float newOpacity))
  1434. {
  1435. NewOpacity = newOpacity / 100.0;
  1436. }
  1437. }
  1438. }
  1439. }