CropWindow.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKitViewer.PdfViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. namespace ComPDFKitDemo.PageEditor
  18. {
  19. /// <summary>
  20. /// CropWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class CropWindow : Window
  23. {
  24. public CPDFViewer CurrentViewer { get; set; }
  25. private Rect CropRect { get; set; }
  26. private CPDFDocument Document { get; set; }
  27. private int CropPageUI { get; set; }
  28. public CropWindow()
  29. {
  30. InitializeComponent();
  31. }
  32. public void SetDoc(CPDFDocument doc)
  33. {
  34. Document = doc;
  35. CropUI.CurrentDoc = Document;
  36. PageCountText.Text = "of / "+ Document.PageCount;
  37. CropPageUI = CurrentViewer.CurrentIndex;
  38. CropUI.CropPageUI = CropPageUI;
  39. StartPageText.Text = (CropPageUI+1).ToString();
  40. EndPageText.Text = (CropPageUI + 1).ToString();
  41. CropUI.InvalidateVisual();
  42. }
  43. private int IgnorePrecision(double value)
  44. {
  45. int check=(int)value;
  46. if (check <= 2)
  47. {
  48. return 0;
  49. }
  50. return check;
  51. }
  52. private void CropUI_CropChanged(object sender, KeyValuePair<Rect, Rect> e)
  53. {
  54. CropRect = e.Key;
  55. LeftText.Text = Math.Max(0, IgnorePrecision(e.Key.Left- e.Value.Left)).ToString();
  56. TopText.Text = Math.Max(0, IgnorePrecision(e.Key.Top - e.Value.Top)).ToString();
  57. RightText.Text = Math.Max(0, IgnorePrecision(e.Value.Right - e.Key.Right)).ToString();
  58. BottomText.Text = Math.Max(0, IgnorePrecision(e.Value.Bottom - e.Key.Bottom)).ToString();
  59. }
  60. private void CancelButton_Click(object sender, RoutedEventArgs e)
  61. {
  62. Close();
  63. }
  64. private void SaveButton_Click(object sender, RoutedEventArgs e)
  65. {
  66. if(CropRect!=Rect.Empty&& Document!=null&& Document.PageCount>0)
  67. {
  68. int startPage = CropPageUI;
  69. int endPage = CropPageUI;
  70. if (RangeRadioBtn.IsChecked==true)
  71. {
  72. if(int.TryParse(StartPageText.Text,out startPage))
  73. {
  74. startPage -= 1;
  75. }
  76. if (int.TryParse(EndPageText.Text, out endPage))
  77. {
  78. endPage -= 1;
  79. }
  80. }
  81. else
  82. {
  83. startPage = 0;
  84. endPage = Document.PageCount - 1;
  85. }
  86. List<int> cropPageList=new List<int>();
  87. for (int i = startPage; i <= endPage && i < Document.PageCount; i++)
  88. {
  89. cropPageList.Add(i);
  90. }
  91. if(cropPageList.Count>0)
  92. {
  93. CurrentViewer?.CropPage((CPDFDisplayBox)CropTypeBox.SelectedIndex, CropRect, cropPageList);
  94. }
  95. }
  96. Close();
  97. }
  98. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  99. {
  100. RadioButton checkRadioBtn=sender as RadioButton;
  101. if(checkRadioBtn!=null && checkRadioBtn.Tag!=null)
  102. {
  103. if(checkRadioBtn.Tag.ToString()== "AllRadioBtn")
  104. {
  105. StartPageText.IsEnabled = false;
  106. EndPageText.IsEnabled = false;
  107. return;
  108. }
  109. if (checkRadioBtn.Tag.ToString() == "RangeRadioBtn")
  110. {
  111. StartPageText.IsEnabled = true;
  112. EndPageText.IsEnabled = true;
  113. return;
  114. }
  115. }
  116. }
  117. private void RefreshButton_Click(object sender, RoutedEventArgs e)
  118. {
  119. int left, top, right, bottom;
  120. int.TryParse(LeftText.Text, out left);
  121. int.TryParse(TopText.Text, out top);
  122. int.TryParse(RightText.Text, out right);
  123. int.TryParse(BottomText.Text, out bottom);
  124. if (Document != null && Document.PageCount > CropPageUI && CropPageUI >= 0)
  125. {
  126. CPDFPage cropPage = Document.PageAtIndex(CropPageUI);
  127. Rect mediaRect = cropPage.GetBoundsForBox(CPDFDisplayBox.MediaBox);
  128. left = (int)(left + mediaRect.Left);
  129. top = (int)(top + mediaRect.Top);
  130. right = (int)(mediaRect.Right - right);
  131. bottom = (int)(mediaRect.Bottom - bottom);
  132. if (right > left && bottom > top)
  133. {
  134. Rect cropData = new Rect(left, top, right - left, bottom - top);
  135. if (mediaRect.Contains(cropData))
  136. {
  137. CropRect = cropData;
  138. CropUI.UpdateCropRect(cropData);
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }