ComparisonSettingDialog.xaml.cs 60 KB

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