CompareContentResultControl.xaml.cs 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. using System.Windows.Threading;
  19. using ComPDFKit.Compare;
  20. using ComPDFKit.Controls.Common;
  21. using ComPDFKit.Controls.Helper;
  22. using ComPDFKit.Controls.PDFControl;
  23. using ComPDFKit.Controls.Properties;
  24. using ComPDFKit.Import;
  25. using ComPDFKit.PDFDocument;
  26. using ComPDFKit.PDFPage;
  27. using ComPDFKitViewer;
  28. using Microsoft.WindowsAPICodePack.Dialogs;
  29. namespace ComPDFKit.Controls.Comparison
  30. {
  31. public class RichTextBoxHelper : DependencyObject
  32. {
  33. public static FlowDocument GetDocumentBind(DependencyObject obj)
  34. {
  35. return (FlowDocument)obj.GetValue(DocumentBindProperty);
  36. }
  37. public static void SetDocumentBind(DependencyObject obj, FlowDocument value)
  38. {
  39. obj.SetValue(DocumentBindProperty, value);
  40. }
  41. public static readonly DependencyProperty DocumentBindProperty =
  42. DependencyProperty.RegisterAttached("DocumentBind",
  43. typeof(TextBindProperty),
  44. typeof(RichTextBoxHelper),
  45. new FrameworkPropertyMetadata
  46. {
  47. BindsTwoWayByDefault = true,
  48. PropertyChangedCallback = (obj, e) =>
  49. {
  50. RichTextBox richTextBox = obj as RichTextBox;
  51. TextBindProperty bindItem = e.NewValue as TextBindProperty;
  52. if (richTextBox != null && bindItem != null)
  53. {
  54. richTextBox.Document = GetFlowDocument(bindItem);
  55. }
  56. }
  57. });
  58. public static FlowDocument GetFlowDocument(TextBindProperty bindItem)
  59. {
  60. FlowDocument Document = new FlowDocument();
  61. Paragraph textPara = new Paragraph();
  62. TextBlock addBlock = new TextBlock();
  63. textPara.Inlines.Add(addBlock);
  64. Document.Blocks.Add(textPara);
  65. if (bindItem.ResultType == 2)
  66. {
  67. if (bindItem.ObjType == 1)
  68. {
  69. Run textRun = new Run(LanguageHelper.CompareManager.GetString("Content_Insert"));
  70. if (bindItem.OldPageIndex == -1 || bindItem.NewPageIndex == -1)
  71. {
  72. textRun = new Run(LanguageHelper.CompareManager.GetString("Title_Insert"));
  73. }
  74. textRun.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#42464D"));
  75. addBlock.Inlines.Add(textRun);
  76. }
  77. if (bindItem.ObjType == 2)
  78. {
  79. Run textRun = new Run(LanguageHelper.CompareManager.GetString("Insert_Image"));
  80. if (bindItem.OldPageIndex == -1 || bindItem.NewPageIndex == -1)
  81. {
  82. textRun = new Run(LanguageHelper.CompareManager.GetString("Title_Insert"));
  83. }
  84. textRun.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#42464D"));
  85. addBlock.Inlines.Add(textRun);
  86. }
  87. }
  88. if (bindItem.ResultType == 1)
  89. {
  90. if (bindItem.ObjType == 1)
  91. {
  92. Run textRun = new Run(LanguageHelper.CompareManager.GetString("Content_Deleted"));
  93. if (bindItem.OldPageIndex == -1 || bindItem.NewPageIndex == -1)
  94. {
  95. textRun = new Run(LanguageHelper.CompareManager.GetString("Title_Delete"));
  96. }
  97. textRun.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#42464D"));
  98. addBlock.Inlines.Add(textRun);
  99. }
  100. if (bindItem.ObjType == 2)
  101. {
  102. Run textRun = new Run("FileCompare_DeleteImage");
  103. if (bindItem.OldPageIndex == -1 || bindItem.NewPageIndex == -1)
  104. {
  105. textRun = new Run(LanguageHelper.CompareManager.GetString("Title_Delete"));
  106. }
  107. textRun.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#42464D"));
  108. addBlock.Inlines.Add(textRun);
  109. }
  110. }
  111. if (bindItem.ResultType == 3)
  112. {
  113. if (bindItem.ObjType == 1)
  114. {
  115. Run textRun = new Run(LanguageHelper.CompareManager.GetString("Content_Replaced"));
  116. textRun.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#42464D"));
  117. addBlock.Inlines.Add(textRun);
  118. }
  119. if (bindItem.ObjType == 2)
  120. {
  121. Run textRun = new Run("FileCompare_ReplaceImage");
  122. textRun.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#42464D"));
  123. addBlock.Inlines.Add(textRun);
  124. }
  125. }
  126. if (bindItem.ResultType == 4)
  127. {
  128. if (bindItem.ObjType == 1)
  129. {
  130. //插入的文本
  131. Run textRun = new Run("FileCompare_ModifyText");
  132. textRun.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#42464D"));
  133. addBlock.Inlines.Add(textRun);
  134. }
  135. if (bindItem.ObjType == 2)
  136. {
  137. //插入的文本
  138. Run textRun = new Run("FileCompare_ModifyImage");
  139. textRun.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#42464D"));
  140. addBlock.Inlines.Add(textRun);
  141. }
  142. }
  143. return Document;
  144. }
  145. }
  146. public class BoolToVisibleConvert : IValueConverter
  147. {
  148. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  149. {
  150. if (value == null)
  151. {
  152. return Visibility.Visible;
  153. }
  154. else
  155. {
  156. if ((bool)value)
  157. {
  158. return Visibility.Visible;
  159. }
  160. else
  161. {
  162. return Visibility.Collapsed;
  163. }
  164. }
  165. }
  166. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  167. {
  168. return null;
  169. }
  170. }
  171. public class GroupHeaderConverter : IValueConverter
  172. {
  173. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  174. {
  175. try
  176. {
  177. if (value != null && value is string name)
  178. {
  179. return name;
  180. }
  181. return null;
  182. }
  183. catch
  184. {
  185. return null;
  186. }
  187. }
  188. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  189. {
  190. return null;
  191. }
  192. }
  193. public class TextBindProperty
  194. {
  195. /// <summary>
  196. /// 1 text 2 image
  197. /// </summary>
  198. public int ObjType { get; set; }
  199. /// <summary>
  200. /// 0 NULL 1 Delete 2 Insert 3 Replace 4 Change
  201. /// </summary>
  202. public int ResultType { get; set; }
  203. public CPDFCompareResult RawResult { get; set; }
  204. public int OldPageIndex { get; set; }
  205. public int NewPageIndex { get; set; }
  206. }
  207. public class CompareBindItem : INotifyPropertyChanged
  208. {
  209. public string ShowPageIndex { get; set; }
  210. public TextBindProperty BindProperty { get; set; }
  211. public Brush BindColorProperty { get; set; }
  212. public int BindIndexProperty { get; set; }
  213. public CompareBindItem()
  214. {
  215. BindProperty = new TextBindProperty();
  216. }
  217. public event PropertyChangedEventHandler PropertyChanged;
  218. protected void OnPropertyChanged([CallerMemberName] string name = null)
  219. {
  220. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  221. }
  222. }
  223. public partial class CompareContentResultControl : UserControl
  224. {
  225. public List<CPDFCompareResults> CompareResultList { get; private set; } = new List<CPDFCompareResults>();
  226. private double[] zoomLevel = { 1.00f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
  227. private ObservableCollection<CompareBindItem> BindList = new ObservableCollection<CompareBindItem>();
  228. private string filePath { get; set; } = string.Empty;
  229. private bool IsOldSave { get; set; } = true;
  230. private string OldFileName { get; set; } = string.Empty;
  231. private bool IsNewSave { get; set; } = false;
  232. private string NewFileName { get; set; } = string.Empty;
  233. private bool IsCombineSave { get; set; } = false;
  234. private string CombineFileName { get; set; } = string.Empty;
  235. private string oldName = "";
  236. private string newName = "";
  237. private bool IsCanScrollChanged = true;
  238. private CPDFDocument CombineDoc { get; set; }
  239. private CPDFDocument OldDoc { get; set; }
  240. private CPDFDocument NewDoc { get; set; }
  241. private ObservableCollection<ListBoxItem> bindNewPageList = new ObservableCollection<ListBoxItem>();
  242. private ObservableCollection<ListBoxItem> bindOldPageList = new ObservableCollection<ListBoxItem>();
  243. private List<int> visibleOldPageIndexes = new List<int>();
  244. private List<int> visibleNewPageIndexes = new List<int>();
  245. private DispatcherTimer oldTimer = new DispatcherTimer();
  246. private DispatcherTimer newTimer = new DispatcherTimer();
  247. public PDFViewControl pdfViewerCtrl { get; set; }
  248. private delegate void OnThumbnailGeneratedEventHandler(int pageIndex, byte[] thumb, int w, int h);
  249. private OnThumbnailGeneratedEventHandler OnLeftThumbnailGenerated;
  250. private OnThumbnailGeneratedEventHandler OnRightThumbnailGenerated;
  251. public event EventHandler ExitCompareEvent;
  252. public CompareContentResultControl()
  253. {
  254. InitializeComponent();
  255. ICollectionView groupView = CollectionViewSource.GetDefaultView(BindList);
  256. groupView.GroupDescriptions.Add(new PropertyGroupDescription(nameof(CompareBindItem.ShowPageIndex)));
  257. }
  258. private void Timer_Tick(object sender, EventArgs e)
  259. {
  260. if (LeftViewer == null || LeftViewer.GetCPDFViewer().GetDocument() == null)
  261. return;
  262. ItemsInViewHitTest(LeftViewer, PageOldEditListBox, bindOldPageList, visibleOldPageIndexes);
  263. oldTimer.Stop();
  264. }
  265. private void NewTimer_Tick(object sender, EventArgs e)
  266. {
  267. if (RightViewer == null || RightViewer.GetCPDFViewer().GetDocument() == null)
  268. return;
  269. ItemsInViewHitTest(RightViewer, PageNewEditListBox, bindNewPageList, visibleNewPageIndexes);
  270. newTimer.Stop();
  271. }
  272. public void LoadComparePdf(CPDFDocument combineDoc, CPDFDocument oldDoc, CPDFDocument newDoc)
  273. {
  274. CombineDoc = combineDoc;
  275. OldDoc = oldDoc;
  276. NewDoc = newDoc;
  277. OldFileTxb.Text = oldName + ".pdf";
  278. NewFileTxb.Text = newName + ".pdf";
  279. LeftViewer.InitDocument(oldDoc);
  280. LeftViewer.SetToolType(ComPDFKit.Tool.ToolType.Viewer);
  281. CPDFSaclingControl.InitWithPDFViewer(LeftViewer);
  282. RightViewer.InitDocument(newDoc);
  283. RightViewer.SetToolType(ComPDFKit.Tool.ToolType.Viewer);
  284. CPDFSaclingControl1.InitWithPDFViewer(RightViewer);
  285. OnLeftThumbnailGenerated -= _leftviewer_OnThumbnailGenerated;
  286. OnLeftThumbnailGenerated += _leftviewer_OnThumbnailGenerated;
  287. OnRightThumbnailGenerated -= _rightviewer_OnThumbnailGenerated;
  288. OnRightThumbnailGenerated += _rightviewer_OnThumbnailGenerated;
  289. LeftViewer.PDFViewTool.ScrollChangedHandler -= LeftViewerTool_ScrollChangedHandler;
  290. LeftViewer.PDFViewTool.ScrollChangedHandler += LeftViewerTool_ScrollChangedHandler;
  291. RightViewer.PDFViewTool.ScrollChangedHandler -= RightViewerTool_ScrollChangedHandler;
  292. RightViewer.PDFViewTool.ScrollChangedHandler += RightViewerTool_ScrollChangedHandler;
  293. PopulateOldThumbnailList();
  294. PopulateNewThumbnailList();
  295. oldTimer.Interval = TimeSpan.FromSeconds(0.3);
  296. oldTimer.Tick += Timer_Tick;
  297. newTimer.Interval = TimeSpan.FromSeconds(0.3);
  298. newTimer.Tick += NewTimer_Tick;
  299. oldTimer.Start();
  300. newTimer.Start();
  301. }
  302. public bool synchronizedScrolling = true;
  303. private void LeftViewerTool_ScrollChangedHandler(object sender, ScrollChangedEventArgs e)
  304. {
  305. if (SynchronizedScrollingCKBox != null && SynchronizedScrollingCKBox.IsChecked.Value&& synchronizedScrolling)
  306. {
  307. if (RightViewer != null)
  308. {
  309. RightViewer.ScrollToVerticalOffset(LeftViewer.GetVerticalOffset());
  310. }
  311. }
  312. synchronizedScrolling = true;
  313. }
  314. private void RightViewerTool_ScrollChangedHandler(object sender, ScrollChangedEventArgs e)
  315. {
  316. if (SynchronizedScrollingCKBox != null && SynchronizedScrollingCKBox.IsChecked.Value&& synchronizedScrolling)
  317. {
  318. if (LeftViewer != null)
  319. {
  320. LeftViewer.ScrollToVerticalOffset(RightViewer.GetVerticalOffset());
  321. }
  322. }
  323. }
  324. private void _rightviewer_OnThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
  325. {
  326. try
  327. {
  328. if (PageNewEditListBox.Items.IsEmpty)
  329. {
  330. return;
  331. }
  332. ScrollViewer sv = GetScrollHost(PageNewEditListBox);
  333. //ListBoxItem item = PageEditListBox.Items[pageIndex] as ListBoxItem;
  334. ListBoxItem listboxitem = PageNewEditListBox.ItemContainerGenerator.ContainerFromIndex(pageIndex) as ListBoxItem;
  335. if (CommonHelper.ViewportHelper.IsInViewport(sv, listboxitem))
  336. {
  337. Debug.WriteLine("Got thumbnail for page {0}. It is visible, so adding thumb", pageIndex);
  338. PixelFormat fmt = PixelFormats.Bgra32;
  339. BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
  340. Image image = GetImageElement(PageNewEditListBox.Items[pageIndex] as ListBoxItem);
  341. image.Source = bps;
  342. }
  343. else
  344. {
  345. Debug.WriteLine("Got thumbnail for page {0}. It is NOT visible, so ignoring thumb", pageIndex);
  346. }
  347. }
  348. catch (Exception ex)
  349. {
  350. Debug.WriteLine(ex.ToString());
  351. }
  352. }
  353. private void _leftviewer_OnThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
  354. {
  355. try
  356. {
  357. if (PageOldEditListBox.Items.IsEmpty)
  358. {
  359. return;
  360. }
  361. ScrollViewer sv = GetScrollHost(PageOldEditListBox);
  362. //ListBoxItem item = PageEditListBox.Items[pageIndex] as ListBoxItem;
  363. ListBoxItem listboxitem = PageOldEditListBox.ItemContainerGenerator.ContainerFromIndex(pageIndex) as ListBoxItem;
  364. if (CommonHelper.ViewportHelper.IsInViewport(sv, listboxitem))
  365. {
  366. Debug.WriteLine("Got thumbnail for page {0}. It is visible, so adding thumb", pageIndex);
  367. PixelFormat fmt = PixelFormats.Bgra32;
  368. BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
  369. Image image = GetImageElement(PageOldEditListBox.Items[pageIndex] as ListBoxItem);
  370. image.Source = bps;
  371. }
  372. else
  373. {
  374. Debug.WriteLine("Got thumbnail for page {0}. It is NOT visible, so ignoring thumb", pageIndex);
  375. }
  376. }
  377. catch (Exception ex)
  378. {
  379. Debug.WriteLine(ex.ToString());
  380. }
  381. }
  382. private async void ItemsInViewHitTest(PDFViewControl pdfViewer, ListBox PageEditListBox, ObservableCollection<ListBoxItem> bindPageList, List<int> visiblePageIndexes)
  383. {
  384. if (pdfViewer == null || pdfViewer.GetCPDFViewer().GetDocument() == null)
  385. return;
  386. try
  387. {
  388. ScrollViewer sv = GetScrollHost(PageEditListBox);
  389. if (sv == null) return;
  390. if (VisualTreeHelper.GetParent(this) == null)
  391. return;
  392. //List<int> pagesOnScreen = new List<int>();
  393. //pageThumbnailsToRequest.Clear();
  394. var range = GetRoughViewportRange(pdfViewer.GetCPDFViewer().GetDocument(), PageEditListBox, (PageEditListBox.Items[0] as ListBoxItem).DesiredSize, new Thickness(6, 10, 6, 10));
  395. for (int i = 0; i < PageEditListBox.Items.Count; ++i)
  396. {
  397. //if (i>=pdfViewer.Document.PageCount)
  398. //{
  399. // break;
  400. //}
  401. ListBoxItem item = PageEditListBox.Items[i] as ListBoxItem;
  402. // ListBoxItem listboxitem = PageEditListBox.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
  403. Image img = GetImageElement(item);
  404. if (i >= (range.Item1 - 1) && i <= range.Item2 || CommonHelper.ViewportHelper.IsInViewport(sv, item))//更改判断方式 因为BOTA缩略图不准确
  405. {
  406. if (img.Source == null && !visiblePageIndexes.Contains(i))
  407. {
  408. visiblePageIndexes.Add(i);
  409. await Task.Delay(1);//有刷不出图的情况 增减页面时
  410. await GetThumbnail(pdfViewer, i, (int)img.Width, (int)img.Height);
  411. Debug.WriteLine("Page {0} is visible, asking for thumb", (i + 1));
  412. }
  413. else if (img.Source == null)
  414. {
  415. // await pdfViewer.GetThumbnail(i, (int)img.Width, (int)img.Height);
  416. Debug.WriteLine("Page {0} is visible, asking for thumb", (i + 1));
  417. }
  418. }
  419. else
  420. {
  421. if (visiblePageIndexes.Contains(i))
  422. {
  423. Image image = GetImageElement(PageEditListBox.Items[i] as ListBoxItem);
  424. if (image.Source != null)
  425. {
  426. image.Source = null;
  427. //(image.Parent as Border).BorderBrush = Brushes.Transparent;
  428. Debug.WriteLine("Page {0} is out of range, removed thumb", (i + 1));
  429. }
  430. else
  431. {
  432. Debug.WriteLine("Page {0} is out of range, but had no thumb", (i + 1));
  433. }
  434. visiblePageIndexes.Remove(i);
  435. }
  436. }
  437. }
  438. }
  439. catch (Exception ex)
  440. {
  441. Debug.WriteLine(ex.ToString());
  442. }
  443. }
  444. public async Task GetThumbnail(PDFViewControl pdfViewer,int pageIndex, int imageWidth, int imageHeight)
  445. {
  446. if (imageWidth <= 0 || imageHeight <= 0)
  447. {
  448. return;
  449. }
  450. CPDFDocument pdfdoc = pdfViewer?.GetCPDFViewer()?.GetDocument();
  451. if (pdfdoc == null)
  452. {
  453. return;
  454. }
  455. CPDFPage page = pdfdoc.PageAtIndex(pageIndex);
  456. if (page == null)
  457. {
  458. return;
  459. }
  460. byte[] bmpData = new byte[imageWidth * imageHeight * 4];
  461. await Task.Run(() => page.RenderPageBitmap(0, 0, imageWidth, imageHeight, 0xFFFFFFFF, bmpData, 1, true));
  462. if (pdfViewer == LeftViewer && OnLeftThumbnailGenerated != null)
  463. {
  464. OnLeftThumbnailGenerated(pageIndex, bmpData, imageWidth, imageHeight);
  465. }
  466. else if (pdfViewer == RightViewer && OnRightThumbnailGenerated != null)
  467. {
  468. OnRightThumbnailGenerated(pageIndex, bmpData, imageWidth, imageHeight);
  469. }
  470. }
  471. public void SetCompareResult(List<CPDFCompareResults> resultList)
  472. {
  473. CompareResultList = resultList;
  474. BindList.Clear();
  475. int ResultIndex = 0;
  476. int ResultPage = 1;
  477. foreach (CPDFCompareResults result in CompareResultList)
  478. {
  479. if (result.TextResults != null && result.TextResults.Count > 0)
  480. {
  481. for (int i = result.TextResults.Count - 1; i >= 0; i--)
  482. {
  483. CPDFCompareResult item = result.TextResults[i];
  484. if ((item.OldRect.width() > 0 && item.OldRect.height() > 0) || (item.NewRect.width() > 0 && item.NewRect.height() > 0))
  485. {
  486. CompareBindItem bindItem = new CompareBindItem();
  487. string page = LanguageHelper.CompareManager.GetString("Text_Page");
  488. if (item.OldPageIndex == -1)
  489. {
  490. bindItem.ShowPageIndex = "-" + " VS " + page + " " + (item.NewPageIndex + 1);
  491. }
  492. if (item.NewPageIndex == -1)
  493. {
  494. bindItem.ShowPageIndex = page + " " + (item.OldPageIndex + 1) + " VS " + "-";
  495. }
  496. if (item.NewPageIndex != -1 && item.OldPageIndex != -1)
  497. {
  498. bindItem.ShowPageIndex = page + " " + (item.OldPageIndex + 1) + " VS " + page + " " + (item.NewPageIndex + 1);
  499. }
  500. bindItem.BindProperty.ResultType = (int)item.Type;
  501. bindItem.BindProperty.ObjType = 1;
  502. bindItem.BindProperty.RawResult = item;
  503. bindItem.BindProperty.OldPageIndex = item.OldPageIndex;
  504. bindItem.BindProperty.NewPageIndex = item.NewPageIndex;
  505. bindItem.BindColorProperty = GetBrushFlowDocument(item);
  506. BindList.Add(bindItem);
  507. }
  508. }
  509. }
  510. if (result.ImageResults != null && result.ImageResults.Count > 0)
  511. {
  512. for (int i = 0; i < result.ImageResults.Count; i++)
  513. {
  514. CPDFCompareResult item = result.ImageResults[i];
  515. if ((item.OldRect.width() > 0 && item.OldRect.height() > 0) || (item.NewRect.width() > 0 && item.NewRect.height() > 0))
  516. {
  517. CompareBindItem bindItem = new CompareBindItem();
  518. string page = LanguageHelper.CompareManager.GetString("Text_Page");
  519. if (item.OldPageIndex == -1)
  520. {
  521. bindItem.ShowPageIndex = "-" + " VS " + page + " " + (item.NewPageIndex + 1);
  522. }
  523. if (item.NewPageIndex == -1)
  524. {
  525. bindItem.ShowPageIndex = page + " " + (item.OldPageIndex + 1) + " VS " + "-";
  526. }
  527. if (item.NewPageIndex != -1 && item.OldPageIndex != -1)
  528. {
  529. bindItem.ShowPageIndex = page + " " + (item.OldPageIndex + 1) + " VS " + page + " " + (item.NewPageIndex + 1);
  530. }
  531. bindItem.BindProperty.ResultType = (int)item.Type;
  532. bindItem.BindProperty.ObjType = 2;
  533. bindItem.BindProperty.RawResult = item;
  534. bindItem.BindProperty.OldPageIndex = item.OldPageIndex;
  535. bindItem.BindProperty.NewPageIndex = item.NewPageIndex;
  536. bindItem.BindColorProperty = GetBrushFlowDocument(item);
  537. BindList.Add(bindItem);
  538. }
  539. }
  540. }
  541. }
  542. if (BindList.Count > 0)
  543. {
  544. List<string> pageList=BindList.AsEnumerable().Select(x=>x.ShowPageIndex).Distinct().ToList();
  545. if(pageList!=null && pageList.Count > 0)
  546. {
  547. foreach (string page in pageList)
  548. {
  549. int orderIndex = 1;
  550. List<CompareBindItem> orderList= BindList.AsEnumerable().Where(x => x.ShowPageIndex == page).ToList();
  551. foreach (CompareBindItem order in orderList)
  552. {
  553. order.BindIndexProperty=orderIndex++;
  554. }
  555. }
  556. }
  557. ResultList.ItemsSource = BindList;
  558. TotalResultText.Text = string.Format("{0}", BindList.Count);
  559. }
  560. else
  561. {
  562. NoCompareGrid.Visibility = Visibility.Visible;
  563. }
  564. }
  565. public Brush GetBrushFlowDocument(CPDFCompareResult bindItem)
  566. {
  567. if ((int)bindItem.Type == 2)
  568. {
  569. return InsertColorRect.Fill;
  570. }
  571. if ((int)bindItem.Type == 1)
  572. {
  573. return DeleteColorRect.Fill;
  574. }
  575. if ((int)bindItem.Type == 3)
  576. {
  577. return ReplaceColorRect.Fill;
  578. }
  579. if ((int)bindItem.Type == 4)
  580. {
  581. return ReplaceColorRect.Fill;
  582. }
  583. return new SolidColorBrush(Colors.Red);
  584. }
  585. private double CheckZoomLevel(double zoom, bool IsGrowth)
  586. {
  587. double standardZoom = 100;
  588. if (zoom <= 0.01)
  589. {
  590. return 0.01;
  591. }
  592. if (zoom >= 10)
  593. {
  594. return 10;
  595. }
  596. zoom *= 100;
  597. for (int i = 0; i < zoomLevel.Length - 1; i++)
  598. {
  599. if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
  600. {
  601. standardZoom = zoomLevel[i + 1];
  602. break;
  603. }
  604. if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
  605. {
  606. standardZoom = zoomLevel[i];
  607. break;
  608. }
  609. }
  610. return standardZoom / 100;
  611. }
  612. private void SearchResultList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  613. {
  614. synchronizedScrolling = false;
  615. if (ResultList.SelectedIndex == -1)
  616. {
  617. //synchronizedScrolling = true;
  618. return;
  619. }
  620. CompareBindItem bindItem = ResultList.SelectedItem as CompareBindItem;
  621. if (bindItem != null && LeftViewer != null && RightViewer != null)
  622. {
  623. CPDFCompareResult rawResult = bindItem.BindProperty.RawResult;
  624. int boundOffset = 3;
  625. SolidColorBrush fillBrush = Brushes.Transparent;
  626. switch (rawResult.Type)
  627. {
  628. case CPDFCompareResultType.CPDFCompareResultTypeDelete:
  629. fillBrush = Brushes.Red;
  630. break;
  631. case CPDFCompareResultType.CPDFCompareResultTypeInsert:
  632. fillBrush = Brushes.Blue;
  633. break;
  634. case CPDFCompareResultType.CPDFCompareResultTypeReplace:
  635. fillBrush = new SolidColorBrush(Color.FromRgb(0xF3, 0x90, 0x24));
  636. break;
  637. case CPDFCompareResultType.CPDFCompareResultTypeChange:
  638. fillBrush = new SolidColorBrush(Color.FromRgb(0xF3, 0x90, 0x24));
  639. break;
  640. default:
  641. break;
  642. }
  643. if (rawResult.OldPageIndex >= 0)
  644. {
  645. CPDFViewer pdfViewer = LeftViewer.GetCPDFViewer();
  646. if (pdfViewer != null)
  647. {
  648. pdfViewer.GoToPage(rawResult.OldPageIndex, new Point(rawResult.OldRect.left, rawResult.OldRect.top));
  649. }
  650. if (rawResult.Type != CPDFCompareResultType.CPDFCompareResultTypeInsert)
  651. {
  652. LeftViewer.PDFViewTool.SetPDFCompareView(null, new Pen(Brushes.Red, 2), rawResult.OldPageIndex, new List<Rect>()
  653. {
  654. new Rect(rawResult.OldRect.left-boundOffset,
  655. rawResult.OldRect.top-boundOffset,
  656. rawResult.OldRect.width()+boundOffset*2,
  657. rawResult.OldRect.height()+boundOffset*2)
  658. });
  659. }
  660. else
  661. {
  662. LeftViewer.PDFViewTool.ClearPDFCompareView();
  663. }
  664. }
  665. if (rawResult.NewPageIndex >= 0)
  666. {
  667. CPDFViewer pdfViewer = RightViewer.GetCPDFViewer();
  668. if (pdfViewer != null)
  669. {
  670. pdfViewer.GoToPage(rawResult.NewPageIndex, new Point(rawResult.NewRect.left, rawResult.NewRect.top));
  671. }
  672. if (rawResult.Type != CPDFCompareResultType.CPDFCompareResultTypeDelete)
  673. {
  674. RightViewer.PDFViewTool.SetPDFCompareView(null, new Pen(Brushes.Red, 2), rawResult.NewPageIndex, new List<Rect>()
  675. {
  676. new Rect(rawResult.NewRect.left-boundOffset,
  677. rawResult.NewRect.top-boundOffset,
  678. rawResult.NewRect.width()+boundOffset*2,
  679. rawResult.NewRect.height()+boundOffset*2)
  680. });
  681. }
  682. else
  683. {
  684. RightViewer.PDFViewTool.ClearPDFCompareView();
  685. }
  686. }
  687. }
  688. //synchronizedScrolling = true;
  689. }
  690. private void SaveBtn_Click(object sender, RoutedEventArgs e)
  691. {
  692. SavePopGrid.Visibility = Visibility.Visible;
  693. }
  694. private void ConfirmSaveBtn_Click(object sender, RoutedEventArgs e)
  695. {
  696. string oldSaveName = string.Empty;
  697. string newSaveName = string.Empty;
  698. string combineSaveName = string.Empty;
  699. filePath = CmbFilePath.Text;
  700. IsOldSave = OldCheckBox.IsChecked == true;
  701. OldFileName = filePath + "\\old.pdf";
  702. IsNewSave = NewCheckBox.IsChecked == true;
  703. NewFileName = filePath + "\\new.pdf";
  704. IsCombineSave = CombineCheckBox.IsChecked == true;
  705. CombineFileName = filePath + "\\combine.pdf";
  706. if (IsOldSave)
  707. {
  708. oldSaveName = CommonHelper.GetFileNameAddSuffix(filePath, oldName + "_compare", ".pdf");
  709. }
  710. if (IsNewSave)
  711. {
  712. newSaveName = oldSaveName = CommonHelper.GetFileNameAddSuffix(filePath, newName + "_compare", ".pdf");
  713. }
  714. if (IsCombineSave)
  715. {
  716. combineSaveName = CommonHelper.GetFileNameAddSuffix(filePath, "MergedCompareFile", ".pdf");
  717. }
  718. if (oldSaveName != string.Empty || newSaveName != string.Empty || combineSaveName != string.Empty)
  719. {
  720. string openPath = "";
  721. if (oldSaveName != string.Empty && OldDoc != null && IsOldSave)
  722. {
  723. OldDoc.WriteToFilePath(oldSaveName);
  724. openPath = oldSaveName;
  725. }
  726. if (newSaveName != string.Empty && NewDoc != null && IsNewSave)
  727. {
  728. NewDoc.WriteToFilePath(newSaveName);
  729. openPath = newSaveName;
  730. }
  731. if (combineSaveName != string.Empty && CombineDoc != null && IsCombineSave)
  732. {
  733. CombineDoc.WriteToFilePath(combineSaveName);
  734. openPath = combineSaveName;
  735. }
  736. if(openPath!=string.Empty)
  737. {
  738. Process.Start(@"explorer.exe", "/select,\"" + openPath + "\"");
  739. }
  740. }
  741. }
  742. private void CloseBtn_Click(object sender, RoutedEventArgs e)
  743. {
  744. CloseConfirmGrid.Visibility = Visibility.Visible;
  745. }
  746. private void CloseLeave()
  747. {
  748. ExitCompareEvent?.Invoke(null,null);
  749. }
  750. private void CompareBtn_Click(object sender, RoutedEventArgs e)
  751. {
  752. ThumbnailBtn.IsChecked = false;
  753. CompareBtn.IsChecked = true;
  754. ThumbnailBtnPath.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#000000"));
  755. CompareBtnPath.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFF"));
  756. }
  757. private void ThumbnailBtn_Click(object sender, RoutedEventArgs e)
  758. {
  759. ThumbnailBtn.IsChecked = true;
  760. CompareBtn.IsChecked = false;
  761. ThumbnailBtnPath.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFF"));
  762. CompareBtnPath.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#000000"));
  763. }
  764. private void ListBoxItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  765. {
  766. }
  767. private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  768. {
  769. }
  770. private void PageEditListBox_SizeChanged(object sender, SizeChangedEventArgs e)
  771. {
  772. PageOldEditListBox.ScrollIntoView(PageOldEditListBox.SelectedItem);
  773. PageNewEditListBox.ScrollIntoView(PageNewEditListBox.SelectedItem);
  774. }
  775. private void PageEditListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  776. {
  777. var list = sender as ListBox;
  778. if (list != null)
  779. {
  780. if (SynchronizedScrollingCKBox.IsChecked.Value)
  781. {
  782. if (list.Name == "PageOldEditListBox")
  783. {
  784. PageNewEditListBox.SelectedIndex = PageOldEditListBox.SelectedIndex;
  785. e.Handled = false;
  786. }
  787. if (list.Name == "PageNewEditListBox")
  788. {
  789. PageOldEditListBox.SelectedIndex = PageNewEditListBox.SelectedIndex;
  790. e.Handled = false;
  791. }
  792. if (PageNewEditListBox.SelectedIndex < LeftViewer.GetCPDFViewer().GetDocument().PageCount)
  793. {
  794. LeftViewer.GetCPDFViewer().GoToPage(PageNewEditListBox.SelectedIndex);
  795. }
  796. else
  797. {
  798. LeftViewer.GetCPDFViewer().GoToPage(LeftViewer.GetCPDFViewer().GetDocument().PageCount - 1);
  799. }
  800. if (PageOldEditListBox.SelectedIndex < RightViewer.GetCPDFViewer().GetDocument().PageCount)
  801. {
  802. RightViewer.GetCPDFViewer().GoToPage(PageNewEditListBox.SelectedIndex);
  803. }
  804. else
  805. {
  806. RightViewer.GetCPDFViewer().GoToPage(RightViewer.GetCPDFViewer().GetDocument().PageCount - 1);
  807. }
  808. }
  809. else
  810. {
  811. if (list.Name == "PageNewEditListBox")
  812. {
  813. if (PageNewEditListBox.SelectedIndex < RightViewer.GetCPDFViewer().GetDocument().PageCount)
  814. {
  815. if (PageNewEditListBox.SelectedIndex != -1)
  816. {
  817. RightViewer.GetCPDFViewer().GoToPage(PageNewEditListBox.SelectedIndex);
  818. PageOldEditListBox.SelectedIndex = -1;
  819. }
  820. }
  821. else
  822. {
  823. PageNewEditListBox.SelectedIndex = -1;
  824. LeftViewer.GetCPDFViewer().GoToPage(LeftViewer.GetCPDFViewer().GetDocument().PageCount - 1);
  825. }
  826. }
  827. if (list.Name == "PageOldEditListBox")
  828. {
  829. if (PageOldEditListBox.SelectedIndex < LeftViewer.GetCPDFViewer().GetDocument().PageCount)
  830. {
  831. if (PageOldEditListBox.SelectedIndex != -1)
  832. {
  833. LeftViewer.GetCPDFViewer().GoToPage(PageOldEditListBox.SelectedIndex);
  834. PageNewEditListBox.SelectedIndex = -1;
  835. }
  836. }
  837. else
  838. {
  839. PageOldEditListBox.SelectedIndex = -1;
  840. RightViewer.GetCPDFViewer().GoToPage(RightViewer.GetCPDFViewer().GetDocument().PageCount - 1);
  841. }
  842. }
  843. }
  844. }
  845. }
  846. private void PageEditListBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
  847. {
  848. ItemsInViewHitTest(LeftViewer, PageOldEditListBox, bindOldPageList, visibleOldPageIndexes);
  849. ItemsInViewHitTest(RightViewer, PageNewEditListBox, bindNewPageList, visibleNewPageIndexes);
  850. }
  851. private ScrollViewer GetScrollHost(ListBox listBox)
  852. {
  853. if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
  854. {
  855. int s = VisualTreeHelper.GetChildrenCount(listBox);
  856. Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
  857. if (border != null)
  858. {
  859. return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
  860. }
  861. }
  862. return null;
  863. }
  864. // return the range of items in the current view
  865. private Tuple<int, int, int> GetRoughViewportRange(CPDFDocument doc, ListBox view, Size itemSize, Thickness itemMargin)
  866. {
  867. var scrollViewer = GetScrollHost(view);
  868. if (doc == null || scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
  869. return new Tuple<int, int, int>(0, 0, 0);
  870. try
  871. {
  872. var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
  873. var currentWidth = scrollViewer.ActualWidth;
  874. //计算当前窗口大小能显示的行数和列数
  875. var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
  876. var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
  877. var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((doc.PageCount + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
  878. preItemCount = preItemCount * columnCount;
  879. var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((doc.PageCount + columnCount - 1) / columnCount));
  880. preEnd = preEnd * columnCount + columnCount - 1;
  881. var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
  882. return new Tuple<int, int, int>(
  883. Math.Max(preItemCount, 0),
  884. Math.Min(view.Items.Count, preEnd),
  885. middle);
  886. }
  887. catch (Exception ex)
  888. {
  889. }
  890. return new Tuple<int, int, int>(0, 0, 0);
  891. }
  892. private Image GetImageElement(ListBoxItem item)
  893. {
  894. Viewbox viewBox = (item.Content as StackPanel).Children[0] as Viewbox;
  895. Image img = ((viewBox.Child as Border).Child as Grid).Children[0] as Image;
  896. return img;
  897. }
  898. private int[] thumbnailSize = { 100, 150, 200, 300, 500 };
  899. public void PopulateOldThumbnailList()
  900. {
  901. visibleOldPageIndexes.Clear();
  902. bindOldPageList.Clear();
  903. // GC.Collect();
  904. int thumbnailWidth = thumbnailSize[0];
  905. bool pageRatio = OldDoc.PageCount >= NewDoc.PageCount;
  906. int pageCount = pageRatio ? OldDoc.PageCount : NewDoc.PageCount;
  907. Brush borderBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#A0A2AE"));
  908. Brush fillBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFBB00"));
  909. Brush fontBrush = Brushes.Black;
  910. Brush transparentBrush = new SolidColorBrush(Colors.Transparent);
  911. for (int i = 0; i < pageCount; i++)
  912. {
  913. CSize pageSize = new CSize();
  914. if (i < OldDoc.PageCount)
  915. {
  916. pageSize = OldDoc.GetPageSize(i);
  917. }
  918. else
  919. {
  920. borderBrush = transparentBrush;
  921. fillBrush = transparentBrush;
  922. fontBrush = transparentBrush;
  923. pageSize = NewDoc.GetPageSize(i);
  924. }
  925. if (pageSize.height == 0 || pageSize.width == 0)
  926. continue;
  927. int imageWidth = pageSize.width > pageSize.height ? thumbnailWidth * 2 : (int)(pageSize.width / pageSize.height * thumbnailWidth * 2);
  928. int imageHeight = pageSize.height > pageSize.width ? thumbnailWidth * 2 : (int)(pageSize.height / pageSize.width * thumbnailWidth * 2);
  929. Image img = new Image()
  930. {
  931. //Margin = new Thickness(0, 0, 5, 0),
  932. Width = imageWidth,
  933. Height = imageHeight,
  934. Stretch = Stretch.Uniform,
  935. };
  936. Grid grid = new Grid();
  937. grid.Children.Add(img);
  938. List<Point> points = new List<Point>()
  939. {
  940. new Point(16.75,1.25),
  941. new Point(3.25,1.25 ),
  942. new Point(3.25,19.4013878),
  943. new Point(10,14.902),
  944. new Point(16.75,19.4013878),
  945. };
  946. Polygon bookmark = new Polygon()
  947. {
  948. Points = new PointCollection(points),
  949. Fill = fillBrush,
  950. Margin = new Thickness(0, 16, 16, 0),
  951. VerticalAlignment = VerticalAlignment.Top,
  952. HorizontalAlignment = HorizontalAlignment.Right,
  953. Visibility = Visibility.Collapsed,
  954. };
  955. grid.Children.Add(bookmark);
  956. Border border = new Border()
  957. {
  958. BorderThickness = new Thickness(2),
  959. BorderBrush = borderBrush,
  960. Child = grid,
  961. };
  962. Viewbox viewBox = new Viewbox()
  963. {
  964. Margin = new Thickness(0, 8, 0, 0),
  965. Stretch = System.Windows.Media.Stretch.Uniform,
  966. Width = thumbnailWidth,
  967. Height = thumbnailWidth,
  968. Child = border,
  969. };
  970. TextBlock text = new TextBlock()
  971. {
  972. HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
  973. Text = (i + 1).ToString(),
  974. Foreground = fontBrush,
  975. Margin = new Thickness(0, 8, 0, 8),
  976. };
  977. StackPanel panel = new StackPanel();
  978. panel.Children.Add(viewBox);
  979. panel.Children.Add(text);
  980. ListBoxItem item = new ListBoxItem();
  981. item.Content = panel;
  982. item.Tag = i;
  983. item.Margin = new Thickness(6, 10, 6, 10);
  984. bindOldPageList.Add(item);
  985. }
  986. PageOldEditListBox.ItemsSource = bindOldPageList;
  987. }
  988. public void PopulateNewThumbnailList()
  989. {
  990. visibleNewPageIndexes.Clear();
  991. bindNewPageList.Clear();
  992. // GC.Collect();
  993. bool pageRatio = NewDoc.PageCount >= OldDoc.PageCount;
  994. int pageCount = pageRatio ? NewDoc.PageCount : OldDoc.PageCount;
  995. int thumbnailWidth = thumbnailSize[0];
  996. Brush borderBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#A0A2AE"));
  997. Brush fillBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFBB00"));
  998. Brush fontBrush = Brushes.Black;
  999. Brush transparentBrush = new SolidColorBrush(Colors.Transparent);
  1000. for (int i = 0; i < pageCount; i++)
  1001. {
  1002. CSize pageSize = new CSize();
  1003. if (i < NewDoc.PageCount)
  1004. {
  1005. pageSize = NewDoc.GetPageSize(i);
  1006. }
  1007. else
  1008. {
  1009. borderBrush = transparentBrush;
  1010. fillBrush = transparentBrush;
  1011. fontBrush = transparentBrush;
  1012. pageSize = OldDoc.GetPageSize(i);
  1013. }
  1014. if (pageSize.height == 0 || pageSize.width == 0)
  1015. continue;
  1016. int imageWidth = pageSize.width > pageSize.height ? thumbnailWidth * 2 : (int)(pageSize.width / pageSize.height * thumbnailWidth * 2);
  1017. int imageHeight = pageSize.height > pageSize.width ? thumbnailWidth * 2 : (int)(pageSize.height / pageSize.width * thumbnailWidth * 2);
  1018. Image img = new Image()
  1019. {
  1020. //Margin = new Thickness(0, 0, 5, 0),
  1021. Width = imageWidth,
  1022. Height = imageHeight,
  1023. Stretch = Stretch.Uniform,
  1024. };
  1025. Grid grid = new Grid();
  1026. grid.Children.Add(img);
  1027. List<Point> points = new List<Point>()
  1028. {
  1029. new Point(16.75,1.25),
  1030. new Point(3.25,1.25 ),
  1031. new Point(3.25,19.4013878),
  1032. new Point(10,14.902),
  1033. new Point(16.75,19.4013878),
  1034. };
  1035. Polygon bookmark = new Polygon()
  1036. {
  1037. Points = new PointCollection(points),
  1038. Fill = fillBrush,
  1039. Margin = new Thickness(0, 16, 16, 0),
  1040. VerticalAlignment = VerticalAlignment.Top,
  1041. HorizontalAlignment = HorizontalAlignment.Right,
  1042. Visibility = Visibility.Collapsed,
  1043. };
  1044. grid.Children.Add(bookmark);
  1045. Border border = new Border()
  1046. {
  1047. BorderThickness = new Thickness(2),
  1048. BorderBrush = borderBrush,
  1049. Child = grid,
  1050. };
  1051. Viewbox viewBox = new Viewbox()
  1052. {
  1053. Margin = new Thickness(0, 8, 0, 0),
  1054. Stretch = System.Windows.Media.Stretch.Uniform,
  1055. Width = thumbnailWidth,
  1056. Height = thumbnailWidth,
  1057. Child = border,
  1058. };
  1059. TextBlock text = new TextBlock()
  1060. {
  1061. HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
  1062. Text = (i + 1).ToString(),
  1063. Foreground = fontBrush,
  1064. Margin = new Thickness(0, 8, 0, 8),
  1065. };
  1066. StackPanel panel = new StackPanel();
  1067. panel.Children.Add(viewBox);
  1068. panel.Children.Add(text);
  1069. ListBoxItem item = new ListBoxItem();
  1070. item.Content = panel;
  1071. item.Tag = i;
  1072. item.Margin = new Thickness(6, 10, 6, 10);
  1073. bindNewPageList.Add(item);
  1074. }
  1075. PageNewEditListBox.ItemsSource = bindNewPageList;
  1076. }
  1077. public void SetCompareColor(Brush dbrush, Brush rbrush, Brush Ibrush)
  1078. {
  1079. DeleteColorRect.Fill = dbrush;
  1080. ReplaceColorRect.Fill = rbrush;
  1081. InsertColorRect.Fill = Ibrush;
  1082. }
  1083. public void SetCompareName(string oldname, string newname)
  1084. {
  1085. oldName = oldname;
  1086. newName = newname;
  1087. }
  1088. private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
  1089. {
  1090. double currentOffset = EditListBoxSV.VerticalOffset;
  1091. // Manually handle scrolling
  1092. // Scroll up
  1093. if (e.Delta > 0)
  1094. {
  1095. EditListBoxSV.ScrollToVerticalOffset(currentOffset - 20);
  1096. }
  1097. // Scroll down
  1098. else if (e.Delta < 0)
  1099. {
  1100. EditListBoxSV.ScrollToVerticalOffset(currentOffset + 20);
  1101. }
  1102. // Mark the event as handled, to prevent it from bubbling
  1103. e.Handled = true;
  1104. }
  1105. private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
  1106. {
  1107. }
  1108. private void CancelBtn_Click(object sender, RoutedEventArgs e)
  1109. {
  1110. SavePopGrid.Visibility = Visibility.Collapsed;
  1111. }
  1112. private void ConfirmExitBtn_Click(object sender, RoutedEventArgs e)
  1113. {
  1114. CloseConfirmGrid.Visibility = Visibility.Collapsed;
  1115. CloseLeave();
  1116. }
  1117. private void BrowseFilePathButton_Click(object sender, RoutedEventArgs e)
  1118. {
  1119. CommonOpenFileDialog commonFileDialog = new CommonOpenFileDialog(LanguageHelper.CompressManager.GetString("Main_OpenFolderNoteWarning"));
  1120. commonFileDialog.IsFolderPicker = true;
  1121. if (commonFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
  1122. {
  1123. CmbFilePath.Text = commonFileDialog.FileName;
  1124. }
  1125. }
  1126. private void CancelCloseBtn_Click(object sender, RoutedEventArgs e)
  1127. {
  1128. CloseConfirmGrid.Visibility = Visibility.Collapsed;
  1129. }
  1130. }
  1131. }