CPDFPageExtractWindow.xaml.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using Compdfkit_Tools.Helper;
  2. using Compdfkit_Tools.PDFControlUI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows;
  6. using MessageBox = System.Windows.MessageBox;
  7. namespace Compdfkit_Tools.PDFControl
  8. {
  9. public partial class CPDFPageExtractWindow : Window
  10. {
  11. private string pageRangeString = string.Empty;
  12. private List<int> pageRangeList = new List<int>();
  13. public ExtractEventClass extractEventClass = new ExtractEventClass();
  14. public delegate void DialogCloseEventHandler(object sender, ExtractDialogCloseEventArgs e);
  15. public event DialogCloseEventHandler DialogClosed;
  16. public CPDFPageExtractWindow()
  17. {
  18. InitializeComponent();
  19. }
  20. public void InitPageExtractWindow(string customPageRange, int maxPage)
  21. {
  22. if (customPageRange == string.Empty)
  23. {
  24. CPDFPageExtractUI.PageRange = PageRange.AllPages;
  25. extractEventClass.PageMode = 1;
  26. }
  27. else
  28. {
  29. CPDFPageExtractUI.PageRange = PageRange.CustomPages;
  30. extractEventClass.PageMode = 4;
  31. pageRangeString = customPageRange;
  32. CommonHelper.GetPagesInRange(ref pageRangeList, pageRangeString, CPDFPageExtractUI.MaxIndex, new char[] { ',' }, new char[] { '-' });
  33. for (int i = 0; i < pageRangeList.Count; i++)
  34. {
  35. pageRangeList[i]++;
  36. }
  37. }
  38. CPDFPageExtractUI.MaxIndex = maxPage;
  39. CPDFPageExtractUI.CurrentPageRange = customPageRange;
  40. }
  41. private void CPDFPageExtractUI_Loaded(object sender, RoutedEventArgs e)
  42. {
  43. CPDFPageExtractUI.PageRangeChanged -= CPDFPageExtractUI_PageRangeChanged;
  44. CPDFPageExtractUI.CustomPageRangeChanged -= CPDFPageExtractUI_CustomPageRangeChanged;
  45. CPDFPageExtractUI.SeparateChanged -= CPDFPageExtractUI_SeparateChanged;
  46. CPDFPageExtractUI.DeleteChanged -= CPDFPageExtractUI_DeleteChanged;
  47. CPDFPageExtractUI.ExtractEvent -= CPDFPageExtractUI_ExtractEvent;
  48. CPDFPageExtractUI.CancelEvent -= CPDFPageExtractUI_CancelEvent;
  49. CPDFPageExtractUI.PageRangeChanged += CPDFPageExtractUI_PageRangeChanged;
  50. CPDFPageExtractUI.CustomPageRangeChanged += CPDFPageExtractUI_CustomPageRangeChanged;
  51. CPDFPageExtractUI.SeparateChanged += CPDFPageExtractUI_SeparateChanged;
  52. CPDFPageExtractUI.DeleteChanged += CPDFPageExtractUI_DeleteChanged;
  53. CPDFPageExtractUI.ExtractEvent += CPDFPageExtractUI_ExtractEvent;
  54. CPDFPageExtractUI.CancelEvent += CPDFPageExtractUI_CancelEvent;
  55. }
  56. private void CPDFPageExtractUI_CancelEvent(object sender, EventArgs e)
  57. {
  58. CloseWindow(null);
  59. }
  60. private void CPDFPageExtractUI_ExtractEvent(object sender, EventArgs e)
  61. {
  62. if (extractEventClass.PageMode != 4)
  63. {
  64. if (extractEventClass.PageMode == 1 && extractEventClass.DeleteAfterExtract)
  65. {
  66. MessageBox.Show(" Please keep at least one page.", "Alert", MessageBoxButton.OK, MessageBoxImage.Warning);
  67. return;
  68. }
  69. if (extractEventClass.PageMode == 3 && CPDFPageExtractUI.MaxIndex == 1)
  70. {
  71. MessageBox.Show(" No page will be extracted.", "Alert", MessageBoxButton.OK, MessageBoxImage.Warning);
  72. return;
  73. }
  74. CloseWindow(extractEventClass);
  75. }
  76. else if(extractEventClass.PageMode == 4 && CommonHelper.GetPagesInRange(ref pageRangeList, pageRangeString, CPDFPageExtractUI.MaxIndex, new char[] { ',' }, new char[] { '-' }))
  77. {
  78. if (pageRangeList.Count == CPDFPageExtractUI.MaxIndex && extractEventClass.DeleteAfterExtract)
  79. {
  80. MessageBox.Show(" Please keep at least one page.", "Alert", MessageBoxButton.OK, MessageBoxImage.Warning);
  81. return;
  82. }
  83. extractEventClass.PageName = pageRangeString;
  84. for (int i = 0; i < pageRangeList.Count; i++)
  85. {
  86. pageRangeList[i]++;
  87. }
  88. extractEventClass.PageParm = pageRangeList;
  89. CloseWindow(extractEventClass);
  90. }
  91. else
  92. {
  93. MessageBox.Show("Please check page range.", "Alert", MessageBoxButton.OK, MessageBoxImage.Warning);
  94. }
  95. }
  96. private void CPDFPageExtractUI_CustomPageRangeChanged(object sender, string e)
  97. {
  98. pageRangeString = e;
  99. }
  100. private void CPDFPageExtractUI_DeleteChanged(object sender, bool e)
  101. {
  102. extractEventClass.DeleteAfterExtract = e;
  103. }
  104. private void CPDFPageExtractUI_SeparateChanged(object sender, bool e)
  105. {
  106. extractEventClass.ExtractToSingleFile = e;
  107. }
  108. private void CPDFPageExtractUI_PageRangeChanged(object sender, PageRange e)
  109. {
  110. extractEventClass.PageMode = (int)e;
  111. }
  112. private void CPDFPageExtractUI_Unloaded(object sender, RoutedEventArgs e)
  113. {
  114. CPDFPageExtractUI.PageRangeChanged -= CPDFPageExtractUI_PageRangeChanged;
  115. CPDFPageExtractUI.CustomPageRangeChanged -= CPDFPageExtractUI_CustomPageRangeChanged;
  116. CPDFPageExtractUI.SeparateChanged -= CPDFPageExtractUI_SeparateChanged;
  117. CPDFPageExtractUI.DeleteChanged -= CPDFPageExtractUI_DeleteChanged;
  118. CPDFPageExtractUI.ExtractEvent -= CPDFPageExtractUI_ExtractEvent;
  119. CPDFPageExtractUI.CancelEvent -= CPDFPageExtractUI_CancelEvent;
  120. }
  121. private void CloseWindow(ExtractEventClass dialogResult)
  122. {
  123. DialogClosed?.Invoke(this, new ExtractDialogCloseEventArgs(dialogResult));
  124. Close();
  125. }
  126. }
  127. public class ExtractDialogCloseEventArgs : EventArgs
  128. {
  129. public ExtractEventClass DialogResult { get; set; }
  130. public ExtractDialogCloseEventArgs(ExtractEventClass dialogResult)
  131. {
  132. DialogResult = dialogResult;
  133. }
  134. }
  135. }